Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
da70f9ee06 | ||
|
|
b6628c3135 | ||
|
|
c66d127c4d | ||
|
|
f3c9b6e9a6 | ||
|
|
d55af958e7 | ||
|
|
3bcfff5bdc | ||
|
|
42edc5c24b | ||
|
|
537a324275 | ||
|
|
224a7d29d8 | ||
|
|
eb477e5154 | ||
|
|
2d1c7506d8 | ||
|
|
428d882591 | ||
|
|
a1c62a56ae | ||
|
|
5b423d1cef | ||
|
|
6574945e56 | ||
|
|
66bb544c0a | ||
|
|
d90c1d2e9a | ||
|
|
0beec60a8a | ||
|
|
7669da5ae8 | ||
|
|
1267a71250 | ||
|
|
2aa90c04ff | ||
|
|
f6d1c2dc72 | ||
|
|
9df0845530 | ||
|
|
3b3c29e22d | ||
|
|
a866952f86 | ||
|
|
5d7bdd8041 |
@@ -69,7 +69,7 @@ $ cp /path/to/zigfetch/config.json ~/.config/zigfetch/config.json
|
||||
## Roadtrip
|
||||
|
||||
- [ ] Add ASCII art for each operating system and Linux distro
|
||||
- [ ] Add GPU info for Linux
|
||||
- [x] Add GPU info for Linux
|
||||
- [ ] Add packages info for Linux
|
||||
- [x] Add user customization
|
||||
- [ ] Add support for Windows
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
// This is a [Semantic Version](https://semver.org/).
|
||||
// In a future version of Zig it will be used for package deduplication.
|
||||
.version = "0.7.3",
|
||||
.version = "0.10.0",
|
||||
|
||||
// Together with name, this represents a globally unique package
|
||||
// identifier. This field is generated by the Zig toolchain when the
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const std = @import("std");
|
||||
const utils = @import("./utils.zig");
|
||||
|
||||
pub const Reset = "\x1b[0m";
|
||||
pub const Bold = "\x1b[1m";
|
||||
@@ -89,18 +90,31 @@ pub fn printAscii(allocator: std.mem.Allocator, sys_info_list: std.ArrayList([]u
|
||||
const ascii_art_items = ascii_art_content_list.items;
|
||||
const sys_info_items = sys_info_list.items;
|
||||
|
||||
const terminal_size = try utils.getTerminalSize();
|
||||
const terminal_width: usize = @intCast(terminal_size.width);
|
||||
|
||||
const left_alignment: usize = 45;
|
||||
|
||||
const longest_sys_info_string_len = utils.getLongestSysInfoStringLen(sys_info_items);
|
||||
const can_print_ascii_art: bool = terminal_width > left_alignment + longest_sys_info_string_len;
|
||||
|
||||
const ascii_art_len: usize = ascii_art_items.len;
|
||||
const sys_info_len: usize = sys_info_items.len;
|
||||
const max_len: usize = if (ascii_art_len > sys_info_len) ascii_art_len else sys_info_len;
|
||||
|
||||
// NOTE: sys_info_len + 3 to be able to print the colors
|
||||
const max_len: usize = if ((ascii_art_len > sys_info_len) and can_print_ascii_art) ascii_art_len else sys_info_len + 3;
|
||||
|
||||
var i: usize = 0;
|
||||
while (i < max_len) : (i += 1) {
|
||||
// Print the ascii art if the width of the terminal is greater than the left alignment (45) + the longest sys info string length
|
||||
if (can_print_ascii_art) {
|
||||
if (i < ascii_art_len) {
|
||||
try stdout.print("{s:<40} \t", .{ascii_art_items[i]});
|
||||
try stdout.print("{s:<45}", .{ascii_art_items[i]});
|
||||
} else {
|
||||
try stdout.print("{s:<40}", .{""});
|
||||
try stdout.print("{s:<45}", .{""});
|
||||
}
|
||||
try bw.flush();
|
||||
}
|
||||
|
||||
if (i < sys_info_len) {
|
||||
try stdout.print("{s}\n", .{sys_info_items[i]});
|
||||
|
||||
@@ -26,6 +26,7 @@ pub const ModuleType = enum {
|
||||
net,
|
||||
terminal,
|
||||
locale,
|
||||
custom,
|
||||
};
|
||||
|
||||
pub fn getModulesTypes(allocator: std.mem.Allocator, config: ?std.json.Parsed(Config)) !std.ArrayList(ModuleType) {
|
||||
|
||||
@@ -22,6 +22,7 @@ pub const formatters = [_]*const fn (allocator: std.mem.Allocator, key: []const
|
||||
&getFormattedNetInfo,
|
||||
&getFormattedTerminalNameInfo,
|
||||
&getFormattedLocaleInfo,
|
||||
&getFormattedCustom,
|
||||
};
|
||||
|
||||
pub const default_formatters = [_]*const fn (allocator: std.mem.Allocator) anyerror!Result{
|
||||
@@ -207,3 +208,7 @@ pub fn getFormattedNetInfo(allocator: std.mem.Allocator, key: []const u8, key_co
|
||||
|
||||
return Result{ .string_arraylist = formatted_net_info_list };
|
||||
}
|
||||
|
||||
pub fn getFormattedCustom(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
||||
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}{s}", .{ key_color, key, ascii.Reset }) };
|
||||
}
|
||||
|
||||
@@ -145,7 +145,15 @@ pub fn getGpuInfo(allocator: std.mem.Allocator) !std.ArrayList(GpuInfo) {
|
||||
|
||||
const gpu_name = try allocator.dupe(u8, std.mem.span(name));
|
||||
|
||||
const parsed_gpu_name = try parseGpuName(allocator, gpu_name) orelse gpu_name;
|
||||
const maybe_parsed_gpu_name = try parseGpuName(allocator, gpu_name);
|
||||
var parsed_gpu_name: []u8 = undefined;
|
||||
|
||||
if (maybe_parsed_gpu_name != null) {
|
||||
allocator.free(gpu_name);
|
||||
parsed_gpu_name = maybe_parsed_gpu_name.?;
|
||||
} else {
|
||||
parsed_gpu_name = gpu_name;
|
||||
}
|
||||
|
||||
try gpu_info_list.append(GpuInfo{
|
||||
.gpu_name = parsed_gpu_name,
|
||||
|
||||
@@ -70,8 +70,8 @@ pub fn getKernelInfo(allocator: std.mem.Allocator) !KernelInfo {
|
||||
}
|
||||
|
||||
return KernelInfo{
|
||||
.kernel_name = try allocator.dupe(u8, &uts.sysname),
|
||||
.kernel_release = try allocator.dupe(u8, &uts.release),
|
||||
.kernel_name = try allocator.dupe(u8, std.mem.sliceTo(&uts.sysname, 0)),
|
||||
.kernel_release = try allocator.dupe(u8, std.mem.sliceTo(&uts.release, 0)),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,9 @@ pub fn getUsername(allocator: std.mem.Allocator) ![]u8 {
|
||||
}
|
||||
|
||||
pub fn getShell(allocator: std.mem.Allocator) ![]u8 {
|
||||
const shell = try std.process.getEnvVarOwned(allocator, "SHELL");
|
||||
const shell = std.process.getEnvVarOwned(allocator, "SHELL") catch |err| if (err == error.EnvironmentVariableNotFound) {
|
||||
return allocator.dupe(u8, "Unknown");
|
||||
} else return err;
|
||||
|
||||
var child = std.process.Child.init(&[_][]const u8{ shell, "--version" }, allocator);
|
||||
defer allocator.free(shell);
|
||||
@@ -20,9 +22,26 @@ pub fn getShell(allocator: std.mem.Allocator) ![]u8 {
|
||||
|
||||
_ = try child.wait();
|
||||
|
||||
if (std.mem.indexOf(u8, shell, "bash") != null) {
|
||||
const bash_version = parseBashVersion(output);
|
||||
defer allocator.free(output);
|
||||
return try std.fmt.allocPrint(allocator, "{s} {s}", .{ "bash", bash_version.? });
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
fn parseBashVersion(shell_version_output: []u8) ?[]u8 {
|
||||
const end_index = std.mem.indexOf(u8, shell_version_output, "(");
|
||||
if (end_index == null) return null;
|
||||
|
||||
const version_keyword = "version ";
|
||||
const version_keyword_index = std.mem.indexOf(u8, shell_version_output[0..end_index.?], version_keyword);
|
||||
if (version_keyword_index == null) return null;
|
||||
|
||||
return shell_version_output[version_keyword_index.? + version_keyword.len .. end_index.?];
|
||||
}
|
||||
|
||||
pub fn getTerminalName(allocator: std.mem.Allocator) ![]u8 {
|
||||
const term_program = std.process.getEnvVarOwned(allocator, "TERM_PROGRAM") catch |err| if (err == error.EnvironmentVariableNotFound) {
|
||||
return allocator.dupe(u8, "Unknown");
|
||||
|
||||
@@ -9,7 +9,9 @@ pub fn getUsername(allocator: std.mem.Allocator) ![]u8 {
|
||||
}
|
||||
|
||||
pub fn getShell(allocator: std.mem.Allocator) ![]u8 {
|
||||
const shell = try std.process.getEnvVarOwned(allocator, "SHELL");
|
||||
const shell = std.process.getEnvVarOwned(allocator, "SHELL") catch |err| if (err == error.EnvironmentVariableNotFound) {
|
||||
return allocator.dupe(u8, "Unknown");
|
||||
} else return err;
|
||||
|
||||
var child = std.process.Child.init(&[_][]const u8{ shell, "--version" }, allocator);
|
||||
defer allocator.free(shell);
|
||||
@@ -23,9 +25,26 @@ pub fn getShell(allocator: std.mem.Allocator) ![]u8 {
|
||||
|
||||
_ = try child.wait();
|
||||
|
||||
if (std.mem.indexOf(u8, shell, "bash") != null) {
|
||||
const bash_version = parseBashVersion(output);
|
||||
defer allocator.free(output);
|
||||
return try std.fmt.allocPrint(allocator, "{s} {s}", .{ "bash", bash_version.? });
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
fn parseBashVersion(shell_version_output: []u8) ?[]u8 {
|
||||
const end_index = std.mem.indexOf(u8, shell_version_output, "(");
|
||||
if (end_index == null) return null;
|
||||
|
||||
const version_keyword = "version ";
|
||||
const version_keyword_index = std.mem.indexOf(u8, shell_version_output[0..end_index.?], version_keyword);
|
||||
if (version_keyword_index == null) return null;
|
||||
|
||||
return shell_version_output[version_keyword_index.? + version_keyword.len .. end_index.?];
|
||||
}
|
||||
|
||||
pub fn getTerminalName(allocator: std.mem.Allocator) ![]u8 {
|
||||
const term_program = std.process.getEnvVarOwned(allocator, "TERM_PROGRAM") catch |err| if (err == error.EnvironmentVariableNotFound) {
|
||||
return allocator.dupe(u8, "Unknown");
|
||||
|
||||
79
src/utils.zig
Normal file
79
src/utils.zig
Normal file
@@ -0,0 +1,79 @@
|
||||
const std = @import("std");
|
||||
const builtin = @import("builtin");
|
||||
|
||||
pub const TermSize = struct {
|
||||
width: u16,
|
||||
height: u16,
|
||||
};
|
||||
|
||||
pub fn getTerminalSize() !TermSize {
|
||||
// https://github.com/softprops/zig-termsize (https://github.com/softprops/zig-termsize/blob/main/src/main.zig)
|
||||
|
||||
const stdout = std.io.getStdOut();
|
||||
|
||||
switch (builtin.os.tag) {
|
||||
.windows => {
|
||||
var buf: std.os.windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
|
||||
switch (std.os.windows.kernel32.GetConsoleScreenBufferInfo(stdout.handle, &buf)) {
|
||||
std.os.windows.TRUE => return TermSize{
|
||||
.width = @intCast(buf.srWindow.Right - buf.srWindow.Left + 1),
|
||||
.height = @intCast(buf.srWindow.Bottom - buf.srWindow.Top + 1),
|
||||
},
|
||||
else => return error.GetConsoleScreenBufferInfoFailed,
|
||||
}
|
||||
},
|
||||
.linux, .macos => {
|
||||
var buf: std.posix.winsize = undefined;
|
||||
switch (std.posix.errno(
|
||||
std.posix.system.ioctl(
|
||||
stdout.handle,
|
||||
std.posix.T.IOCGWINSZ,
|
||||
@intFromPtr(&buf),
|
||||
),
|
||||
)) {
|
||||
std.posix.E.SUCCESS => return TermSize{
|
||||
.width = buf.col,
|
||||
.height = buf.row,
|
||||
},
|
||||
else => return error.IoctlFailed,
|
||||
}
|
||||
},
|
||||
else => return error.UnsupportedOperatingSystem,
|
||||
}
|
||||
}
|
||||
|
||||
test "getTerminalSize" {
|
||||
const terminal_size = try getTerminalSize();
|
||||
|
||||
std.debug.print("Height: {}, Width {}\n", .{ terminal_size.height, terminal_size.width });
|
||||
|
||||
try std.testing.expect((terminal_size.height > 0) and (terminal_size.width > 0));
|
||||
}
|
||||
|
||||
pub fn getLongestSysInfoStringLen(strings: []const []const u8) usize {
|
||||
const ansi_reset = "\x1b[0m";
|
||||
var longest_len: usize = 0;
|
||||
|
||||
// Ignore the username@host and the separator
|
||||
for (strings[2..]) |s| {
|
||||
const ansi_restet_index = std.mem.indexOf(u8, s, ansi_reset);
|
||||
var start: usize = 0;
|
||||
|
||||
if (ansi_restet_index != null) {
|
||||
// `start` is the index of the last character of the ANSI reset escape sequence + 1
|
||||
start = ansi_restet_index.? + ansi_reset.len + 1;
|
||||
|
||||
if (start > s.len) continue;
|
||||
}
|
||||
|
||||
longest_len = @max(longest_len, s[start..].len);
|
||||
}
|
||||
|
||||
return longest_len;
|
||||
}
|
||||
|
||||
test "getLongestSysInfoStringLen" {
|
||||
const strings = [_][]const u8{ "", "", "test", "test-test", "test1" };
|
||||
|
||||
try std.testing.expectEqual(strings[3].len, getLongestSysInfoStringLen(strings[0..]));
|
||||
}
|
||||
Reference in New Issue
Block a user