Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
66bb544c0a | ||
|
|
d90c1d2e9a | ||
|
|
0beec60a8a | ||
|
|
7669da5ae8 | ||
|
|
1267a71250 | ||
|
|
2aa90c04ff | ||
|
|
f6d1c2dc72 | ||
|
|
9df0845530 | ||
|
|
3b3c29e22d | ||
|
|
a866952f86 | ||
|
|
5d7bdd8041 | ||
|
|
635dfe00db | ||
|
|
064e8e9597 | ||
|
|
785915d833 |
@@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
// This is a [Semantic Version](https://semver.org/).
|
// This is a [Semantic Version](https://semver.org/).
|
||||||
// In a future version of Zig it will be used for package deduplication.
|
// In a future version of Zig it will be used for package deduplication.
|
||||||
.version = "0.7.2",
|
.version = "0.8.0",
|
||||||
|
|
||||||
// Together with name, this represents a globally unique package
|
// Together with name, this represents a globally unique package
|
||||||
// identifier. This field is generated by the Zig toolchain when the
|
// identifier. This field is generated by the Zig toolchain when the
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const utils = @import("./utils.zig");
|
||||||
|
|
||||||
pub const Reset = "\x1b[0m";
|
pub const Reset = "\x1b[0m";
|
||||||
pub const Bold = "\x1b[1m";
|
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 ascii_art_items = ascii_art_content_list.items;
|
||||||
const sys_info_items = sys_info_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 ascii_art_len: usize = ascii_art_items.len;
|
||||||
const sys_info_len: usize = sys_info_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;
|
var i: usize = 0;
|
||||||
while (i < max_len) : (i += 1) {
|
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) {
|
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 {
|
} else {
|
||||||
try stdout.print("{s:<40}", .{""});
|
try stdout.print("{s:<45}", .{""});
|
||||||
}
|
}
|
||||||
try bw.flush();
|
try bw.flush();
|
||||||
|
}
|
||||||
|
|
||||||
if (i < sys_info_len) {
|
if (i < sys_info_len) {
|
||||||
try stdout.print("{s}\n", .{sys_info_items[i]});
|
try stdout.print("{s}\n", .{sys_info_items[i]});
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ pub fn getDefaultFormattedUptimeInfo(allocator: std.mem.Allocator) !Result {
|
|||||||
|
|
||||||
pub fn getFormattedUptimeInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
pub fn getFormattedUptimeInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
||||||
const uptime = try detection.system.getSystemUptime();
|
const uptime = try detection.system.getSystemUptime();
|
||||||
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {} days, {} hours, {} minutes ", .{ key_color, key, ascii.Reset, uptime.days, uptime.hours, uptime.minutes }) };
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {} days, {} hours, {} minutes", .{ key_color, key, ascii.Reset, uptime.days, uptime.hours, uptime.minutes }) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedPackagesInfo(allocator: std.mem.Allocator) !Result {
|
pub fn getDefaultFormattedPackagesInfo(allocator: std.mem.Allocator) !Result {
|
||||||
|
|||||||
@@ -144,9 +144,16 @@ pub fn getGpuInfo(allocator: std.mem.Allocator) !std.ArrayList(GpuInfo) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const gpu_name = try allocator.dupe(u8, std.mem.span(name));
|
const gpu_name = try allocator.dupe(u8, std.mem.span(name));
|
||||||
defer allocator.free(gpu_name);
|
|
||||||
|
|
||||||
const parsed_gpu_name = try parseGpuName(allocator, 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{
|
try gpu_info_list.append(GpuInfo{
|
||||||
.gpu_name = parsed_gpu_name,
|
.gpu_name = parsed_gpu_name,
|
||||||
@@ -167,7 +174,7 @@ pub fn getGpuInfo(allocator: std.mem.Allocator) !std.ArrayList(GpuInfo) {
|
|||||||
return gpu_info_list;
|
return gpu_info_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parseGpuName(allocator: std.mem.Allocator, name: []u8) ![]u8 {
|
fn parseGpuName(allocator: std.mem.Allocator, name: []u8) !?[]u8 {
|
||||||
// NOTE: for references: https://github.com/pciutils/pciutils/blob/master/pci.ids
|
// NOTE: for references: https://github.com/pciutils/pciutils/blob/master/pci.ids
|
||||||
|
|
||||||
if (std.mem.startsWith(u8, name, "Advanced Micro Devices, Inc. [AMD/ATI]")) {
|
if (std.mem.startsWith(u8, name, "Advanced Micro Devices, Inc. [AMD/ATI]")) {
|
||||||
@@ -190,7 +197,7 @@ fn parseGpuName(allocator: std.mem.Allocator, name: []u8) ![]u8 {
|
|||||||
return parsed_gpu_name;
|
return parsed_gpu_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
return name;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getRamInfo(allocator: std.mem.Allocator) !RamInfo {
|
pub fn getRamInfo(allocator: std.mem.Allocator) !RamInfo {
|
||||||
|
|||||||
@@ -70,8 +70,8 @@ pub fn getKernelInfo(allocator: std.mem.Allocator) !KernelInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return KernelInfo{
|
return KernelInfo{
|
||||||
.kernel_name = try allocator.dupe(u8, &uts.sysname),
|
.kernel_name = try allocator.dupe(u8, std.mem.sliceTo(&uts.sysname, 0)),
|
||||||
.kernel_release = try allocator.dupe(u8, &uts.release),
|
.kernel_release = try allocator.dupe(u8, std.mem.sliceTo(&uts.release, 0)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
77
src/utils.zig
Normal file
77
src/utils.zig
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
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.getStdIn();
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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