Compare commits
41 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ef8b6bcebd | ||
|
|
acc4563d2d | ||
|
|
ea966cd474 | ||
|
|
3214725e58 | ||
|
|
f4a8c962e6 | ||
|
|
f7641c24a3 | ||
|
|
20e2ef80d2 | ||
|
|
3cfae25a75 | ||
|
|
d841a6a726 | ||
|
|
7e7d964fd4 | ||
|
|
a9091f1c93 | ||
|
|
d9ac4e58fc | ||
|
|
9c4e032c04 | ||
|
|
5730f14897 | ||
|
|
f57fc5d9ab | ||
|
|
e7f7205a44 | ||
|
|
8768e85b80 | ||
|
|
27995745e4 | ||
|
|
092171ef7f | ||
|
|
84a5edba4c | ||
|
|
212e3c3bc8 | ||
|
|
9020345b00 | ||
|
|
b7ba91d664 | ||
|
|
1c97ed3685 | ||
|
|
316238f9ea | ||
|
|
7ad5181381 | ||
|
|
02406a7644 | ||
|
|
30970649c2 | ||
|
|
981d5ee579 | ||
|
|
8aa0f4972e | ||
|
|
da70f9ee06 | ||
|
|
b6628c3135 | ||
|
|
c66d127c4d | ||
|
|
f3c9b6e9a6 | ||
|
|
d55af958e7 | ||
|
|
3bcfff5bdc | ||
|
|
42edc5c24b | ||
|
|
537a324275 | ||
|
|
224a7d29d8 | ||
|
|
eb477e5154 | ||
|
|
2d1c7506d8 |
@@ -1,5 +1,12 @@
|
||||
# Zigfetch
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
---
|
||||
|
||||
- [Description](#description)
|
||||
- [Requirements](#requirements)
|
||||
- [Installation](#installation)
|
||||
@@ -14,7 +21,7 @@ Zigfetch is a minimal [neofetch](https://github.com/dylanaraps/neofetch)/[fastfe
|
||||
|
||||
## Requirements
|
||||
|
||||
- \>= [zig v0.14.0](https://ziglang.org/)
|
||||
- \>= [zig v0.15.1](https://ziglang.org/)
|
||||
|
||||
### Linux only
|
||||
|
||||
|
||||
@@ -17,9 +17,11 @@ pub fn build(b: *std.Build) void {
|
||||
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "zigfetch",
|
||||
.root_module = b.createModule(.{
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
}),
|
||||
});
|
||||
|
||||
if (target.result.os.tag == .macos) {
|
||||
@@ -61,9 +63,11 @@ pub fn build(b: *std.Build) void {
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
|
||||
const exe_unit_tests = b.addTest(.{
|
||||
.root_module = b.createModule(.{
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
}),
|
||||
});
|
||||
|
||||
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
|
||||
|
||||
@@ -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.8.1",
|
||||
.version = "0.21.0",
|
||||
|
||||
// Together with name, this represents a globally unique package
|
||||
// identifier. This field is generated by the Zig toolchain when the
|
||||
@@ -28,7 +28,7 @@
|
||||
|
||||
// Tracks the earliest Zig version that the package considers to be a
|
||||
// supported use case.
|
||||
.minimum_zig_version = "0.14.0",
|
||||
.minimum_zig_version = "0.15.1",
|
||||
|
||||
// This field is optional.
|
||||
// Each dependency must either provide a `url` and `hash`, or a `path`.
|
||||
|
||||
@@ -65,22 +65,27 @@ test "parse ffffff" {
|
||||
try std.testing.expect((result.r == 255) and (result.g == 255) and (result.b == 255));
|
||||
}
|
||||
|
||||
pub fn printAscii(allocator: std.mem.Allocator, sys_info_list: std.ArrayList([]u8)) !void {
|
||||
const stdout_file = std.io.getStdOut().writer();
|
||||
var bw = std.io.bufferedWriter(stdout_file);
|
||||
const stdout = bw.writer();
|
||||
pub fn printAsciiAndModules(allocator: std.mem.Allocator, ascii_art_path: ?[]u8, sys_info_list: std.array_list.Managed([]u8)) !void {
|
||||
var stdout_buffer: [2048]u8 = undefined;
|
||||
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
|
||||
const stdout = &stdout_writer.interface;
|
||||
|
||||
// const ascii_art_path = "./assets/ascii/guy_fawks.txt";
|
||||
// var file = try std.fs.cwd().openFile(ascii_art_path, .{});
|
||||
// defer file.close();
|
||||
// const ascii_art_data = try file.readToEndAlloc(allocator, std.math.maxInt(usize));
|
||||
// defer allocator.free(ascii_art_data);
|
||||
var ascii_art_data: []const u8 = undefined;
|
||||
if (ascii_art_path) |ascii| {
|
||||
var ascii_file = try std.fs.cwd().openFile(ascii, .{});
|
||||
defer ascii_file.close();
|
||||
ascii_art_data = try ascii_file.readToEndAlloc(allocator, std.math.maxInt(usize));
|
||||
} else {
|
||||
ascii_art_data = @embedFile("./assets/ascii/guy_fawks.txt");
|
||||
}
|
||||
|
||||
const ascii_art_data = @embedFile("./assets/ascii/guy_fawks.txt");
|
||||
defer if (ascii_art_path != null) {
|
||||
allocator.free(ascii_art_data);
|
||||
};
|
||||
|
||||
var lines = std.mem.splitScalar(u8, ascii_art_data, '\n');
|
||||
|
||||
var ascii_art_content_list = std.ArrayList([]const u8).init(allocator);
|
||||
var ascii_art_content_list = std.array_list.Managed([]const u8).init(allocator);
|
||||
defer ascii_art_content_list.deinit();
|
||||
|
||||
while (lines.next()) |line| {
|
||||
@@ -93,10 +98,12 @@ pub fn printAscii(allocator: std.mem.Allocator, sys_info_list: std.ArrayList([]u
|
||||
const terminal_size = try utils.getTerminalSize();
|
||||
const terminal_width: usize = @intCast(terminal_size.width);
|
||||
|
||||
const left_alignment: usize = 45;
|
||||
const spacing: usize = 5;
|
||||
|
||||
const longest_ascii_art_row_len: usize = try utils.getLongestAsciiArtRowLen(ascii_art_items);
|
||||
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 can_print_ascii_art: bool = terminal_width > longest_ascii_art_row_len + longest_sys_info_string_len + spacing;
|
||||
|
||||
const ascii_art_len: usize = ascii_art_items.len;
|
||||
const sys_info_len: usize = sys_info_items.len;
|
||||
@@ -106,14 +113,20 @@ pub fn printAscii(allocator: std.mem.Allocator, sys_info_list: std.ArrayList([]u
|
||||
|
||||
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
|
||||
// Print the ascii art if the width of the terminal is greater than the spacing (5) + the longest ascii art row length + the longest sys info string length
|
||||
if (can_print_ascii_art) {
|
||||
const alignment_buffer = try allocator.alloc(u8, if (i < ascii_art_len) longest_ascii_art_row_len - (try utils.countCodepoints(ascii_art_items[i])) + spacing else longest_ascii_art_row_len + spacing);
|
||||
@memset(alignment_buffer, ' ');
|
||||
|
||||
if (i < ascii_art_len) {
|
||||
try stdout.print("{s:<45}", .{ascii_art_items[i]});
|
||||
try stdout.print("{s}{s}", .{ ascii_art_items[i], alignment_buffer });
|
||||
} else {
|
||||
try stdout.print("{s:<45}", .{""});
|
||||
try stdout.print("{s}", .{alignment_buffer});
|
||||
}
|
||||
try bw.flush();
|
||||
|
||||
allocator.free(alignment_buffer);
|
||||
|
||||
try stdout.flush();
|
||||
}
|
||||
|
||||
if (i < sys_info_len) {
|
||||
@@ -133,7 +146,7 @@ pub fn printAscii(allocator: std.mem.Allocator, sys_info_list: std.ArrayList([]u
|
||||
} else {
|
||||
try stdout.print("\n", .{});
|
||||
}
|
||||
try bw.flush();
|
||||
try stdout.flush();
|
||||
}
|
||||
|
||||
for (sys_info_list.items) |item| {
|
||||
|
||||
@@ -9,6 +9,7 @@ pub const Module = struct {
|
||||
};
|
||||
|
||||
pub const Config = struct {
|
||||
ascii_abs_path: ?[]u8 = null,
|
||||
modules: []Module,
|
||||
};
|
||||
|
||||
@@ -26,10 +27,15 @@ pub const ModuleType = enum {
|
||||
net,
|
||||
terminal,
|
||||
locale,
|
||||
custom,
|
||||
};
|
||||
|
||||
pub fn getModulesTypes(allocator: std.mem.Allocator, config: ?std.json.Parsed(Config)) !std.ArrayList(ModuleType) {
|
||||
var modules_list = std.ArrayList(ModuleType).init(allocator);
|
||||
pub fn getAsciiPath(config: ?std.json.Parsed(Config)) ?[]u8 {
|
||||
return config.?.value.ascii_abs_path;
|
||||
}
|
||||
|
||||
pub fn getModulesTypes(allocator: std.mem.Allocator, config: ?std.json.Parsed(Config)) !std.array_list.Managed(ModuleType) {
|
||||
var modules_list = std.array_list.Managed(ModuleType).init(allocator);
|
||||
|
||||
if (config) |c| {
|
||||
for (c.value.modules) |module| {
|
||||
|
||||
@@ -5,7 +5,7 @@ const detection = @import("detection.zig").os_module;
|
||||
|
||||
const Result = union(enum) {
|
||||
string: []u8,
|
||||
string_arraylist: std.ArrayList([]u8),
|
||||
string_arraylist: std.array_list.Managed([]u8),
|
||||
};
|
||||
|
||||
pub const formatters = [_]*const fn (allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) anyerror!Result{
|
||||
@@ -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{
|
||||
@@ -129,7 +130,7 @@ pub fn getFormattedGpuInfo(allocator: std.mem.Allocator, key: []const u8, key_co
|
||||
defer allocator.free(gpu_info.gpu_name);
|
||||
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{ key_color, key, ascii.Reset, gpu_info.gpu_name, gpu_info.gpu_cores, gpu_info.gpu_freq }) };
|
||||
} else if (builtin.os.tag == .linux) {
|
||||
var formatted_gpu_info_list = std.ArrayList([]u8).init(allocator);
|
||||
var formatted_gpu_info_list = std.array_list.Managed([]u8).init(allocator);
|
||||
|
||||
const gpu_info_list = try detection.hardware.getGpuInfo(allocator);
|
||||
|
||||
@@ -195,7 +196,7 @@ pub fn getDefaultFormattedNetInfo(allocator: std.mem.Allocator) !Result {
|
||||
}
|
||||
|
||||
pub fn getFormattedNetInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
||||
var formatted_net_info_list = std.ArrayList([]u8).init(allocator);
|
||||
var formatted_net_info_list = std.array_list.Managed([]u8).init(allocator);
|
||||
|
||||
var net_info_list = try detection.network.getNetInfo(allocator);
|
||||
for (net_info_list.items) |n| {
|
||||
@@ -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 }) };
|
||||
}
|
||||
|
||||
@@ -113,8 +113,8 @@ pub fn getCpuInfo(allocator: std.mem.Allocator) !CpuInfo {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn getGpuInfo(allocator: std.mem.Allocator) !std.ArrayList(GpuInfo) {
|
||||
var gpu_info_list = std.ArrayList(GpuInfo).init(allocator);
|
||||
pub fn getGpuInfo(allocator: std.mem.Allocator) !std.array_list.Managed(GpuInfo) {
|
||||
var gpu_info_list = std.array_list.Managed(GpuInfo).init(allocator);
|
||||
|
||||
const display_controller = 0x03;
|
||||
|
||||
|
||||
@@ -11,8 +11,8 @@ pub const NetInfo = struct {
|
||||
ipv4_addr: []u8,
|
||||
};
|
||||
|
||||
pub fn getNetInfo(allocator: std.mem.Allocator) !std.ArrayList(NetInfo) {
|
||||
var net_info_list = std.ArrayList(NetInfo).init(allocator);
|
||||
pub fn getNetInfo(allocator: std.mem.Allocator) !std.array_list.Managed(NetInfo) {
|
||||
var net_info_list = std.array_list.Managed(NetInfo).init(allocator);
|
||||
|
||||
var ifap: ?*c_ifaddrs.ifaddrs = null;
|
||||
if (c_ifaddrs.getifaddrs(&ifap) != 0) {
|
||||
|
||||
@@ -10,19 +10,29 @@ pub fn getShell(allocator: std.mem.Allocator) ![]u8 {
|
||||
return allocator.dupe(u8, "Unknown");
|
||||
} else return err;
|
||||
|
||||
var child = std.process.Child.init(&[_][]const u8{ shell, "--version" }, allocator);
|
||||
defer allocator.free(shell);
|
||||
|
||||
child.stdout_behavior = .Pipe;
|
||||
child.stderr_behavior = .Pipe;
|
||||
const result = try std.process.Child.run(.{ .allocator = allocator, .argv = &[_][]const u8{ shell, "--version" } });
|
||||
const result_stdout = result.stdout;
|
||||
|
||||
try child.spawn();
|
||||
if (std.mem.indexOf(u8, shell, "bash") != null) {
|
||||
const bash_version = parseBashVersion(result_stdout);
|
||||
defer allocator.free(result_stdout);
|
||||
return try std.fmt.allocPrint(allocator, "{s} {s}", .{ "bash", bash_version.? });
|
||||
}
|
||||
|
||||
const output = try child.stdout.?.reader().readAllAlloc(allocator, 1024 * 1024);
|
||||
return result_stdout;
|
||||
}
|
||||
|
||||
_ = try child.wait();
|
||||
fn parseBashVersion(shell_version_output: []u8) ?[]u8 {
|
||||
const end_index = std.mem.indexOf(u8, shell_version_output, "(");
|
||||
if (end_index == null) return null;
|
||||
|
||||
return output;
|
||||
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 {
|
||||
|
||||
@@ -236,7 +236,7 @@ pub fn getGpuInfo(allocator: std.mem.Allocator) !GpuInfo {
|
||||
|
||||
if (c_iokit.CFDictionaryGetValueIfPresent(@as(c_iokit.CFDictionaryRef, @ptrCast(properties_ptr)), model_key, &name_ref) == c_iokit.TRUE) {
|
||||
if (c_iokit.CFGetTypeID(name_ref) == c_iokit.CFStringGetTypeID()) {
|
||||
const accel_name = utils.cfStringToZigString(allocator, @as(c_iokit.CFStringRef, @ptrCast(name_ref))) catch {
|
||||
const accel_name = utils.cfTypeRefToZigString(allocator, name_ref) catch {
|
||||
return gpu_info;
|
||||
};
|
||||
|
||||
|
||||
@@ -11,8 +11,8 @@ pub const NetInfo = struct {
|
||||
ipv4_addr: []u8,
|
||||
};
|
||||
|
||||
pub fn getNetInfo(allocator: std.mem.Allocator) !std.ArrayList(NetInfo) {
|
||||
var net_info_list = std.ArrayList(NetInfo).init(allocator);
|
||||
pub fn getNetInfo(allocator: std.mem.Allocator) !std.array_list.Managed(NetInfo) {
|
||||
var net_info_list = std.array_list.Managed(NetInfo).init(allocator);
|
||||
|
||||
var ifap: ?*c_ifaddrs.ifaddrs = null;
|
||||
if (c_ifaddrs.getifaddrs(&ifap) != 0) {
|
||||
|
||||
@@ -2,34 +2,25 @@ const std = @import("std");
|
||||
const utils = @import("utils.zig");
|
||||
|
||||
pub fn getPackagesInfo(allocator: std.mem.Allocator) ![]const u8 {
|
||||
var packages_info = std.ArrayList(u8).init(allocator);
|
||||
var packages_info = std.array_list.Managed(u8).init(allocator);
|
||||
defer packages_info.deinit();
|
||||
|
||||
const homebrew_packages = countHomebrewPackages() catch |err| if (err == error.FileNotFound) 0 else return err;
|
||||
const homebrew_casks = countHomebrewCasks() catch |err| if (err == error.FileNotFound) 0 else return err;
|
||||
const macports_packages = countMacportPackages() catch |err| if (err == error.FileNotFound) 0 else return err;
|
||||
|
||||
var buffer: [10]u8 = undefined;
|
||||
var fbs = std.io.fixedBufferStream(&buffer);
|
||||
var buffer: [32]u8 = undefined;
|
||||
|
||||
if (homebrew_packages > 0) {
|
||||
try std.fmt.formatInt(homebrew_packages, 10, .lower, .{}, fbs.writer());
|
||||
try packages_info.appendSlice(" brew: ");
|
||||
try packages_info.appendSlice(fbs.getWritten());
|
||||
try packages_info.appendSlice(try std.fmt.bufPrint(&buffer, " brew: {d}", .{homebrew_packages}));
|
||||
}
|
||||
|
||||
if (homebrew_casks > 0) {
|
||||
fbs.reset();
|
||||
try std.fmt.formatInt(homebrew_casks, 10, .lower, .{}, fbs.writer());
|
||||
try packages_info.appendSlice(" brew-cask: ");
|
||||
try packages_info.appendSlice(fbs.getWritten());
|
||||
try packages_info.appendSlice(try std.fmt.bufPrint(&buffer, " brew-cask: {d}", .{homebrew_casks}));
|
||||
}
|
||||
|
||||
if (macports_packages > 0) {
|
||||
fbs.reset();
|
||||
try std.fmt.formatInt(macports_packages, 10, .lower, .{}, fbs.writer());
|
||||
try packages_info.appendSlice(" macports: ");
|
||||
try packages_info.appendSlice(fbs.getWritten());
|
||||
try packages_info.appendSlice(try std.fmt.bufPrint(&buffer, " macports: {d}", .{macports_packages}));
|
||||
}
|
||||
|
||||
return try allocator.dupe(u8, packages_info.items);
|
||||
|
||||
@@ -13,19 +13,29 @@ pub fn getShell(allocator: std.mem.Allocator) ![]u8 {
|
||||
return allocator.dupe(u8, "Unknown");
|
||||
} else return err;
|
||||
|
||||
var child = std.process.Child.init(&[_][]const u8{ shell, "--version" }, allocator);
|
||||
defer allocator.free(shell);
|
||||
|
||||
child.stdout_behavior = .Pipe;
|
||||
child.stderr_behavior = .Pipe;
|
||||
const result = try std.process.Child.run(.{ .allocator = allocator, .argv = &[_][]const u8{ shell, "--version" } });
|
||||
const result_stdout = result.stdout;
|
||||
|
||||
try child.spawn();
|
||||
if (std.mem.indexOf(u8, shell, "bash") != null) {
|
||||
const bash_version = parseBashVersion(result_stdout);
|
||||
defer allocator.free(result_stdout);
|
||||
return try std.fmt.allocPrint(allocator, "{s} {s}", .{ "bash", bash_version.? });
|
||||
}
|
||||
|
||||
const output = try child.stdout.?.reader().readAllAlloc(allocator, 1024 * 1024);
|
||||
return result_stdout;
|
||||
}
|
||||
|
||||
_ = try child.wait();
|
||||
fn parseBashVersion(shell_version_output: []u8) ?[]u8 {
|
||||
const end_index = std.mem.indexOf(u8, shell_version_output, "(");
|
||||
if (end_index == null) return null;
|
||||
|
||||
return output;
|
||||
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 {
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
const std = @import("std");
|
||||
const c_iokit = @cImport(@cInclude("IOKit/IOKitLib.h"));
|
||||
|
||||
/// Converts a CFString to a Zig string.
|
||||
pub fn cfStringToZigString(allocator: std.mem.Allocator, cf_string: c_iokit.CFStringRef) ![]u8 {
|
||||
/// Converts a CFTypeRef casted to CFStringRef to a Zig string.
|
||||
pub fn cfTypeRefToZigString(allocator: std.mem.Allocator, cf_type_ref: c_iokit.CFTypeRef) ![]u8 {
|
||||
const cf_string: c_iokit.CFStringRef = @ptrFromInt(@intFromPtr(cf_type_ref));
|
||||
|
||||
const length = c_iokit.CFStringGetLength(cf_string);
|
||||
const max_size = c_iokit.CFStringGetMaximumSizeForEncoding(length, c_iokit.kCFStringEncodingUTF8) + 1;
|
||||
const max_size_usize = @as(usize, @intCast(max_size));
|
||||
|
||||
20
src/main.zig
20
src/main.zig
@@ -10,11 +10,11 @@ pub fn main() !void {
|
||||
const allocator = gpa.allocator();
|
||||
defer _ = gpa.deinit();
|
||||
|
||||
var sys_info_list = std.ArrayList([]u8).init(allocator);
|
||||
defer sys_info_list.deinit();
|
||||
var modules_list = std.array_list.Managed([]u8).init(allocator);
|
||||
defer modules_list.deinit();
|
||||
|
||||
errdefer {
|
||||
for (sys_info_list.items) |info| {
|
||||
for (modules_list.items) |info| {
|
||||
allocator.free(info);
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,7 @@ pub fn main() !void {
|
||||
|
||||
const username = try detection.user.getUsername(allocator);
|
||||
const hostname = try detection.system.getHostname(allocator);
|
||||
try sys_info_list.append(try std.fmt.allocPrint(allocator, "{s}{s}{s}@{s}{s}{s}", .{
|
||||
try modules_list.append(try std.fmt.allocPrint(allocator, "{s}{s}{s}@{s}{s}{s}", .{
|
||||
ascii.Yellow,
|
||||
username,
|
||||
ascii.Reset,
|
||||
@@ -40,16 +40,16 @@ pub fn main() !void {
|
||||
|
||||
const separtor_buffer = try allocator.alloc(u8, username.len + hostname.len + 1);
|
||||
@memset(separtor_buffer, '-');
|
||||
try sys_info_list.append(separtor_buffer);
|
||||
try modules_list.append(separtor_buffer);
|
||||
|
||||
if (modules_types.items.len == 0) {
|
||||
inline for (0..formatters.default_formatters.len) |i| {
|
||||
const result = try formatters.default_formatters[i](allocator);
|
||||
switch (result) {
|
||||
.string => |r| try sys_info_list.append(r),
|
||||
.string => |r| try modules_list.append(r),
|
||||
.string_arraylist => |r| {
|
||||
defer r.deinit();
|
||||
try sys_info_list.appendSlice(r.items);
|
||||
try modules_list.appendSlice(r.items);
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -61,14 +61,14 @@ pub fn main() !void {
|
||||
|
||||
const result = try formatters.formatters[@intFromEnum(module_type)](allocator, module.key, key_color);
|
||||
switch (result) {
|
||||
.string => |r| try sys_info_list.append(r),
|
||||
.string => |r| try modules_list.append(r),
|
||||
.string_arraylist => |r| {
|
||||
defer r.deinit();
|
||||
try sys_info_list.appendSlice(r.items);
|
||||
try modules_list.appendSlice(r.items);
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try ascii.printAscii(allocator, sys_info_list);
|
||||
try ascii.printAsciiAndModules(allocator, config.getAsciiPath(conf), modules_list);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ pub const TermSize = struct {
|
||||
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();
|
||||
const stdout = std.fs.File.stdout();
|
||||
|
||||
switch (builtin.os.tag) {
|
||||
.windows => {
|
||||
@@ -62,6 +62,8 @@ pub fn getLongestSysInfoStringLen(strings: []const []const u8) usize {
|
||||
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);
|
||||
@@ -75,3 +77,40 @@ test "getLongestSysInfoStringLen" {
|
||||
|
||||
try std.testing.expectEqual(strings[3].len, getLongestSysInfoStringLen(strings[0..]));
|
||||
}
|
||||
|
||||
pub fn countCodepoints(str: []const u8) !usize {
|
||||
var count: usize = 0;
|
||||
var i: usize = 0;
|
||||
|
||||
while (i < str.len) {
|
||||
const byte_len = try std.unicode.utf8ByteSequenceLength(str[i]);
|
||||
if ((i + byte_len > str.len) or (!std.unicode.utf8ValidateSlice(str[i .. i + byte_len]))) return error.InvalidUtf8;
|
||||
|
||||
count += 1;
|
||||
i += byte_len;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
test "countCodepoints" {
|
||||
const str = " ████████████████";
|
||||
|
||||
try std.testing.expectEqual(28, try countCodepoints(str));
|
||||
}
|
||||
|
||||
pub fn getLongestAsciiArtRowLen(ascii_art: []const []const u8) !usize {
|
||||
var longest_len: usize = 0;
|
||||
|
||||
for (ascii_art) |ascii_row| {
|
||||
longest_len = @max(longest_len, try countCodepoints(ascii_row));
|
||||
}
|
||||
|
||||
return longest_len;
|
||||
}
|
||||
|
||||
test "getLongestAsciiArtRowLen" {
|
||||
const rows = [_][]const u8{ " ████████████████", "██░░░░██████████░░ ░░██████████░░░░██" };
|
||||
|
||||
try std.testing.expectEqual(40, try getLongestAsciiArtRowLen(rows[0..]));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user