Compare commits
59 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3bcfff5bdc | ||
|
|
42edc5c24b | ||
|
|
537a324275 | ||
|
|
224a7d29d8 | ||
|
|
eb477e5154 | ||
|
|
2d1c7506d8 | ||
|
|
428d882591 | ||
|
|
a1c62a56ae | ||
|
|
5b423d1cef | ||
|
|
6574945e56 | ||
|
|
66bb544c0a | ||
|
|
d90c1d2e9a | ||
|
|
0beec60a8a | ||
|
|
7669da5ae8 | ||
|
|
1267a71250 | ||
|
|
2aa90c04ff | ||
|
|
f6d1c2dc72 | ||
|
|
9df0845530 | ||
|
|
3b3c29e22d | ||
|
|
a866952f86 | ||
|
|
5d7bdd8041 | ||
|
|
635dfe00db | ||
|
|
064e8e9597 | ||
|
|
785915d833 | ||
|
|
e492443a2c | ||
|
|
f1bebb08e9 | ||
|
|
f3af8712d2 | ||
|
|
67a2e19ac4 | ||
|
|
2de5047eb7 | ||
|
|
e88586c47a | ||
|
|
28f94d87ed | ||
|
|
0d7f606b7d | ||
|
|
ec0e3ffd18 | ||
|
|
f73b1be023 | ||
|
|
3a5a268bbe | ||
|
|
bdf4a3f7d7 | ||
|
|
68cc541575 | ||
|
|
e28a3b96e5 | ||
|
|
01ad39c0b5 | ||
|
|
a18fd7ade8 | ||
|
|
3e14691e14 | ||
|
|
4eab0e4487 | ||
|
|
3b737ed4d7 | ||
|
|
073d512b67 | ||
|
|
707f94bd8b | ||
|
|
de214a4a89 | ||
|
|
5b63b30a4f | ||
|
|
9af26702cc | ||
|
|
c660fc903f | ||
|
|
90abbcb1fd | ||
|
|
a52dd145da | ||
|
|
5faf60e53d | ||
|
|
7993cab4df | ||
|
|
b4ec1b6d2d | ||
|
|
cd40fe34d2 | ||
|
|
5e71eaf3f6 | ||
|
|
7ac43a2694 | ||
|
|
d9c68cb9cd | ||
|
|
1865be9030 |
37
README.md
37
README.md
@@ -1,12 +1,12 @@
|
|||||||
# Zigfetch
|
# Zigfetch
|
||||||
|
|
||||||
- [Description](#description)
|
- [Description](#description)
|
||||||
- [Requirements](#requirements)
|
- [Requirements](#requirements)
|
||||||
- [Installation](#installation)
|
- [Installation](#installation)
|
||||||
- [Usage](#usage)
|
- [Usage](#usage)
|
||||||
- [Configuration](#configuration)
|
- [Configuration](#configuration)
|
||||||
- [Roadtrip](#roadtrip)
|
- [Roadtrip](#roadtrip)
|
||||||
- [Contributing](#contributing)
|
- [Contributing](#contributing)
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
@@ -14,7 +14,11 @@ Zigfetch is a minimal [neofetch](https://github.com/dylanaraps/neofetch)/[fastfe
|
|||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
- [zig v0.14.0](https://ziglang.org/)
|
- \>= [zig v0.14.0](https://ziglang.org/)
|
||||||
|
|
||||||
|
### Linux only
|
||||||
|
|
||||||
|
- [libpci](https://github.com/pciutils/pciutils)
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -43,20 +47,20 @@ $ zigfetch
|
|||||||
> [!IMPORTANT]
|
> [!IMPORTANT]
|
||||||
> Currently, Zig does not have a built-in library for JSON validation via JSON schema, so it is very important to follow the pattern shown in the default configuration file ([config.json](https://github.com/utox39/zigfetch/blob/main/config.json)) to avoid errors
|
> Currently, Zig does not have a built-in library for JSON validation via JSON schema, so it is very important to follow the pattern shown in the default configuration file ([config.json](https://github.com/utox39/zigfetch/blob/main/config.json)) to avoid errors
|
||||||
|
|
||||||
- Create the config folder
|
- Create the config folder
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ mkdir -p ~/.config/zigfetch
|
$ mkdir -p ~/.config/zigfetch
|
||||||
```
|
```
|
||||||
|
|
||||||
- Create the config file
|
- Create the config file
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ cd ~/.config/zigfetch
|
$ cd ~/.config/zigfetch
|
||||||
$ touch config.json
|
$ touch config.json
|
||||||
```
|
```
|
||||||
|
|
||||||
- Or copy the default config (preferred way)
|
- Or copy the default config (preferred way)
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ cp /path/to/zigfetch/config.json ~/.config/zigfetch/config.json
|
$ cp /path/to/zigfetch/config.json ~/.config/zigfetch/config.json
|
||||||
@@ -64,11 +68,12 @@ $ cp /path/to/zigfetch/config.json ~/.config/zigfetch/config.json
|
|||||||
|
|
||||||
## Roadtrip
|
## Roadtrip
|
||||||
|
|
||||||
- [ ] Add ASCII art for each operating system and Linux distro
|
- [ ] 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
|
- [ ] Add packages info for Linux
|
||||||
- [x] Add user customization
|
- [x] Add user customization
|
||||||
- [ ] Add support for Windows
|
- [ ] Add support for Windows
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
If you would like to contribute to this project just create a pull request which I will try to review as soon as possible.
|
If you would like to contribute to this project just create a pull request which I will try to review as soon as possible.
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ pub fn build(b: *std.Build) void {
|
|||||||
|
|
||||||
if (target.result.os.tag == .linux) {
|
if (target.result.os.tag == .linux) {
|
||||||
exe.linkLibC();
|
exe.linkLibC();
|
||||||
|
exe.linkSystemLibrary("pci");
|
||||||
}
|
}
|
||||||
|
|
||||||
// This declares intent for the executable to be installed into the
|
// This declares intent for the executable to be installed into the
|
||||||
|
|||||||
@@ -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.5.0",
|
.version = "0.9.1",
|
||||||
|
|
||||||
// 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,27 +90,42 @@ 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) {
|
||||||
if (i < ascii_art_len) {
|
// Print the ascii art if the width of the terminal is greater than the left alignment (45) + the longest sys info string length
|
||||||
try stdout.print("{s:<40} \t", .{ascii_art_items[i]});
|
if (can_print_ascii_art) {
|
||||||
} else {
|
if (i < ascii_art_len) {
|
||||||
try stdout.print("{s:<40}", .{""});
|
try stdout.print("{s:<45}", .{ascii_art_items[i]});
|
||||||
|
} else {
|
||||||
|
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]});
|
||||||
} else if (i == sys_info_len + 1) {
|
} else if (i == sys_info_len + 1) {
|
||||||
|
// Print the first row of colors
|
||||||
for (0..8) |j| {
|
for (0..8) |j| {
|
||||||
try stdout.print("\x1b[48;5;{d}m \x1b[0m", .{j});
|
try stdout.print("\x1b[48;5;{d}m \x1b[0m", .{j});
|
||||||
}
|
}
|
||||||
try stdout.print("\n", .{});
|
try stdout.print("\n", .{});
|
||||||
} else if (i == sys_info_len + 2) {
|
} else if (i == sys_info_len + 2) {
|
||||||
|
// Print the second row of colors
|
||||||
for (8..16) |j| {
|
for (8..16) |j| {
|
||||||
try stdout.print("\x1b[48;5;{d}m \x1b[0m", .{j});
|
try stdout.print("\x1b[48;5;{d}m \x1b[0m", .{j});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,9 +23,9 @@ pub const ModuleType = enum {
|
|||||||
ram,
|
ram,
|
||||||
swap,
|
swap,
|
||||||
disk,
|
disk,
|
||||||
|
net,
|
||||||
terminal,
|
terminal,
|
||||||
locale,
|
locale,
|
||||||
net, // It must always be the last element of this array
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn getModulesTypes(allocator: std.mem.Allocator, config: ?std.json.Parsed(Config)) !std.ArrayList(ModuleType) {
|
pub fn getModulesTypes(allocator: std.mem.Allocator, config: ?std.json.Parsed(Config)) !std.ArrayList(ModuleType) {
|
||||||
|
|||||||
@@ -3,7 +3,12 @@ const std = @import("std");
|
|||||||
const ascii = @import("ascii.zig");
|
const ascii = @import("ascii.zig");
|
||||||
const detection = @import("detection.zig").os_module;
|
const detection = @import("detection.zig").os_module;
|
||||||
|
|
||||||
pub const formatters = [_]*const fn (allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) anyerror![]u8{
|
const Result = union(enum) {
|
||||||
|
string: []u8,
|
||||||
|
string_arraylist: std.ArrayList([]u8),
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const formatters = [_]*const fn (allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) anyerror!Result{
|
||||||
&getFormattedOsInfo,
|
&getFormattedOsInfo,
|
||||||
&getFormattedKernelInfo,
|
&getFormattedKernelInfo,
|
||||||
&getFormattedUptimeInfo,
|
&getFormattedUptimeInfo,
|
||||||
@@ -14,11 +19,12 @@ pub const formatters = [_]*const fn (allocator: std.mem.Allocator, key: []const
|
|||||||
&getFormattedRamInfo,
|
&getFormattedRamInfo,
|
||||||
&getFormattedSwapInfo,
|
&getFormattedSwapInfo,
|
||||||
&getFormattedDiskInfo,
|
&getFormattedDiskInfo,
|
||||||
|
&getFormattedNetInfo,
|
||||||
&getFormattedTerminalNameInfo,
|
&getFormattedTerminalNameInfo,
|
||||||
&getFormattedLocaleInfo,
|
&getFormattedLocaleInfo,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const default_formatters = [_]*const fn (allocator: std.mem.Allocator) anyerror![]u8{
|
pub const default_formatters = [_]*const fn (allocator: std.mem.Allocator) anyerror!Result{
|
||||||
&getDefaultFormattedOsInfo,
|
&getDefaultFormattedOsInfo,
|
||||||
&getDefaultFormattedKernelInfo,
|
&getDefaultFormattedKernelInfo,
|
||||||
&getDefaultFormattedUptimeInfo,
|
&getDefaultFormattedUptimeInfo,
|
||||||
@@ -29,145 +35,166 @@ pub const default_formatters = [_]*const fn (allocator: std.mem.Allocator) anyer
|
|||||||
&getDefaultFormattedRamInfo,
|
&getDefaultFormattedRamInfo,
|
||||||
&getDefaultFormattedSwapInfo,
|
&getDefaultFormattedSwapInfo,
|
||||||
&getDefaultFormattedDiskInfo,
|
&getDefaultFormattedDiskInfo,
|
||||||
|
&getDefaultFormattedNetInfo,
|
||||||
&getDefaultFormattedTerminalNameInfo,
|
&getDefaultFormattedTerminalNameInfo,
|
||||||
&getDefaultFormattedLocaleInfo,
|
&getDefaultFormattedLocaleInfo,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn getDefaultFormattedKernelInfo(allocator: std.mem.Allocator) ![]u8 {
|
pub fn getDefaultFormattedKernelInfo(allocator: std.mem.Allocator) !Result {
|
||||||
return try getFormattedKernelInfo(allocator, "Kernel", ascii.Yellow);
|
return try getFormattedKernelInfo(allocator, "Kernel", ascii.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedKernelInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 {
|
pub fn getFormattedKernelInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
||||||
const kernel_info = try detection.system.getKernelInfo(allocator);
|
const kernel_info = try detection.system.getKernelInfo(allocator);
|
||||||
defer allocator.free(kernel_info.kernel_name);
|
defer allocator.free(kernel_info.kernel_name);
|
||||||
defer allocator.free(kernel_info.kernel_release);
|
defer allocator.free(kernel_info.kernel_release);
|
||||||
|
|
||||||
return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} {s}", .{ key_color, key, ascii.Reset, kernel_info.kernel_name, kernel_info.kernel_release });
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} {s}", .{ key_color, key, ascii.Reset, kernel_info.kernel_name, kernel_info.kernel_release }) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedOsInfo(allocator: std.mem.Allocator) ![]u8 {
|
pub fn getDefaultFormattedOsInfo(allocator: std.mem.Allocator) !Result {
|
||||||
return try getFormattedOsInfo(allocator, "OS", ascii.Yellow);
|
return try getFormattedOsInfo(allocator, "OS", ascii.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedOsInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 {
|
pub fn getFormattedOsInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
||||||
const os_info = try detection.system.getOsInfo(allocator);
|
const os_info = try detection.system.getOsInfo(allocator);
|
||||||
defer allocator.free(os_info);
|
defer allocator.free(os_info);
|
||||||
return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, os_info });
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, os_info }) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedLocaleInfo(allocator: std.mem.Allocator) ![]u8 {
|
pub fn getDefaultFormattedLocaleInfo(allocator: std.mem.Allocator) !Result {
|
||||||
return try getFormattedLocaleInfo(allocator, "Locale", ascii.Yellow);
|
return try getFormattedLocaleInfo(allocator, "Locale", ascii.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedLocaleInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 {
|
pub fn getFormattedLocaleInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
||||||
const locale = try detection.system.getLocale(allocator);
|
const locale = try detection.system.getLocale(allocator);
|
||||||
defer allocator.free(locale);
|
defer allocator.free(locale);
|
||||||
return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, locale });
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, locale }) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedUptimeInfo(allocator: std.mem.Allocator) ![]u8 {
|
pub fn getDefaultFormattedUptimeInfo(allocator: std.mem.Allocator) !Result {
|
||||||
return try getFormattedUptimeInfo(allocator, "Uptime", ascii.Yellow);
|
return try getFormattedUptimeInfo(allocator, "Uptime", ascii.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedUptimeInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 {
|
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 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) ![]u8 {
|
pub fn getDefaultFormattedPackagesInfo(allocator: std.mem.Allocator) !Result {
|
||||||
return try getFormattedPackagesInfo(allocator, "Packages", ascii.Yellow);
|
return try getFormattedPackagesInfo(allocator, "Packages", ascii.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedPackagesInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 {
|
pub fn getFormattedPackagesInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
||||||
if (builtin.os.tag == .macos) {
|
if (builtin.os.tag == .macos) {
|
||||||
const packages_info = try detection.packages.getPackagesInfo(allocator);
|
const packages_info = try detection.packages.getPackagesInfo(allocator);
|
||||||
defer allocator.free(packages_info);
|
defer allocator.free(packages_info);
|
||||||
return try std.fmt.allocPrint(allocator, "{s}{s}:{s}{s}", .{ key_color, key, ascii.Reset, packages_info });
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s}{s}", .{ key_color, key, ascii.Reset, packages_info }) };
|
||||||
} else if (builtin.os.tag == .linux) {
|
} else if (builtin.os.tag == .linux) {
|
||||||
return try std.fmt.allocPrint(allocator, "{s}{s}:{s} WIP", .{ key_color, key, ascii.Reset });
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} WIP", .{ key_color, key, ascii.Reset }) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedShellInfo(allocator: std.mem.Allocator) ![]u8 {
|
pub fn getDefaultFormattedShellInfo(allocator: std.mem.Allocator) !Result {
|
||||||
return try getFormattedShellInfo(allocator, "Shell", ascii.Yellow);
|
return try getFormattedShellInfo(allocator, "Shell", ascii.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedShellInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 {
|
pub fn getFormattedShellInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
||||||
const shell = try detection.user.getShell(allocator);
|
const shell = try detection.user.getShell(allocator);
|
||||||
defer allocator.free(shell);
|
defer allocator.free(shell);
|
||||||
return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, shell[0..(shell.len - 1)] });
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, shell[0..(shell.len - 1)] }) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedCpuInfo(allocator: std.mem.Allocator) ![]u8 {
|
pub fn getDefaultFormattedCpuInfo(allocator: std.mem.Allocator) !Result {
|
||||||
return try getFormattedCpuInfo(allocator, "Cpu", ascii.Yellow);
|
return try getFormattedCpuInfo(allocator, "Cpu", ascii.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedCpuInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 {
|
pub fn getFormattedCpuInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
||||||
const cpu_info = try detection.hardware.getCpuInfo(allocator);
|
const cpu_info = try detection.hardware.getCpuInfo(allocator);
|
||||||
defer allocator.free(cpu_info.cpu_name);
|
defer allocator.free(cpu_info.cpu_name);
|
||||||
return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{ key_color, key, ascii.Reset, cpu_info.cpu_name, cpu_info.cpu_cores, cpu_info.cpu_max_freq });
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{ key_color, key, ascii.Reset, cpu_info.cpu_name, cpu_info.cpu_cores, cpu_info.cpu_max_freq }) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedGpuInfo(allocator: std.mem.Allocator) ![]u8 {
|
pub fn getDefaultFormattedGpuInfo(allocator: std.mem.Allocator) !Result {
|
||||||
return try getFormattedGpuInfo(allocator, "Gpu", ascii.Yellow);
|
if (builtin.os.tag == .macos) {
|
||||||
|
return try getFormattedGpuInfo(allocator, "Gpu", ascii.Yellow);
|
||||||
|
} else if (builtin.os.tag == .linux) {
|
||||||
|
return try getFormattedGpuInfo(allocator, "Gpu", ascii.Yellow);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedGpuInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 {
|
pub fn getFormattedGpuInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
||||||
if (builtin.os.tag == .macos) {
|
if (builtin.os.tag == .macos) {
|
||||||
const gpu_info = try detection.hardware.getGpuInfo(allocator);
|
const gpu_info = try detection.hardware.getGpuInfo(allocator);
|
||||||
defer allocator.free(gpu_info.gpu_name);
|
defer allocator.free(gpu_info.gpu_name);
|
||||||
return 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 });
|
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) {
|
} else if (builtin.os.tag == .linux) {
|
||||||
return try std.fmt.allocPrint(allocator, "{s}{s}:{s} WIP", .{ key_color, key, ascii.Reset });
|
var formatted_gpu_info_list = std.ArrayList([]u8).init(allocator);
|
||||||
|
|
||||||
|
const gpu_info_list = try detection.hardware.getGpuInfo(allocator);
|
||||||
|
|
||||||
|
for (gpu_info_list.items) |g| {
|
||||||
|
var formatted_gpu_info: []u8 = undefined;
|
||||||
|
if ((g.gpu_cores == 0) or (g.gpu_freq == 0.0)) {
|
||||||
|
formatted_gpu_info = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, g.gpu_name });
|
||||||
|
} else {
|
||||||
|
formatted_gpu_info = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s} ({}) @ {d:.2} GHz", .{ key_color, key, ascii.Reset, g.gpu_name, g.gpu_cores, g.gpu_freq });
|
||||||
|
}
|
||||||
|
try formatted_gpu_info_list.append(formatted_gpu_info);
|
||||||
|
allocator.free(g.gpu_name);
|
||||||
|
}
|
||||||
|
gpu_info_list.deinit();
|
||||||
|
|
||||||
|
return Result{ .string_arraylist = formatted_gpu_info_list };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedRamInfo(allocator: std.mem.Allocator) ![]u8 {
|
pub fn getDefaultFormattedRamInfo(allocator: std.mem.Allocator) !Result {
|
||||||
return try getFormattedRamInfo(allocator, "Ram", ascii.Yellow);
|
return try getFormattedRamInfo(allocator, "Ram", ascii.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedRamInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 {
|
pub fn getFormattedRamInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
||||||
const ram_info = if (builtin.os.tag == .macos) try detection.hardware.getRamInfo() else if (builtin.os.tag == .linux) try detection.hardware.getRamInfo(allocator);
|
const ram_info = if (builtin.os.tag == .macos) try detection.hardware.getRamInfo() else if (builtin.os.tag == .linux) try detection.hardware.getRamInfo(allocator);
|
||||||
return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {d:.2} / {d:.2} GiB ({}%)", .{ key_color, key, ascii.Reset, ram_info.ram_usage, ram_info.ram_size, ram_info.ram_usage_percentage });
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {d:.2} / {d:.2} GiB ({}%)", .{ key_color, key, ascii.Reset, ram_info.ram_usage, ram_info.ram_size, ram_info.ram_usage_percentage }) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedSwapInfo(allocator: std.mem.Allocator) ![]u8 {
|
pub fn getDefaultFormattedSwapInfo(allocator: std.mem.Allocator) !Result {
|
||||||
return try getFormattedSwapInfo(allocator, "Swap", ascii.Yellow);
|
return try getFormattedSwapInfo(allocator, "Swap", ascii.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedSwapInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 {
|
pub fn getFormattedSwapInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
||||||
const swap_info = if (builtin.os.tag == .macos) try detection.hardware.getSwapInfo() else if (builtin.os.tag == .linux) try detection.hardware.getSwapInfo(allocator);
|
const swap_info = if (builtin.os.tag == .macos) try detection.hardware.getSwapInfo() else if (builtin.os.tag == .linux) try detection.hardware.getSwapInfo(allocator);
|
||||||
if (swap_info) |s| {
|
if (swap_info) |s| {
|
||||||
return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {d:.2} / {d:.2} GiB ({}%)", .{ key_color, key, ascii.Reset, s.swap_usage, s.swap_usage, s.swap_usage_percentage });
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {d:.2} / {d:.2} GiB ({}%)", .{ key_color, key, ascii.Reset, s.swap_usage, s.swap_size, s.swap_usage_percentage }) };
|
||||||
} else {
|
} else {
|
||||||
return try std.fmt.allocPrint(allocator, "{s}{s}:{s} Disabled", .{ key_color, key, ascii.Reset });
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} Disabled", .{ key_color, key, ascii.Reset }) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedDiskInfo(allocator: std.mem.Allocator) ![]u8 {
|
pub fn getDefaultFormattedDiskInfo(allocator: std.mem.Allocator) !Result {
|
||||||
return try getFormattedDiskInfo(allocator, "Disk", ascii.Yellow);
|
return try getFormattedDiskInfo(allocator, "Disk", ascii.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedDiskInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 {
|
pub fn getFormattedDiskInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
||||||
const disk_info = try detection.hardware.getDiskSize("/");
|
const disk_info = try detection.hardware.getDiskSize("/");
|
||||||
return try std.fmt.allocPrint(allocator, "{s}{s} ({s}):{s} {d:.2} / {d:.2} GB ({}%)", .{ key_color, key, disk_info.disk_path, ascii.Reset, disk_info.disk_usage, disk_info.disk_size, disk_info.disk_usage_percentage });
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s} ({s}):{s} {d:.2} / {d:.2} GB ({}%)", .{ key_color, key, disk_info.disk_path, ascii.Reset, disk_info.disk_usage, disk_info.disk_size, disk_info.disk_usage_percentage }) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedTerminalNameInfo(allocator: std.mem.Allocator) ![]u8 {
|
pub fn getDefaultFormattedTerminalNameInfo(allocator: std.mem.Allocator) !Result {
|
||||||
return try getFormattedTerminalNameInfo(allocator, "Terminal", ascii.Yellow);
|
return try getFormattedTerminalNameInfo(allocator, "Terminal", ascii.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedTerminalNameInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) ![]u8 {
|
pub fn getFormattedTerminalNameInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
||||||
const terminal_name = try detection.user.getTerminalName(allocator);
|
const terminal_name = try detection.user.getTerminalName(allocator);
|
||||||
defer allocator.free(terminal_name);
|
defer allocator.free(terminal_name);
|
||||||
return try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, terminal_name });
|
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} {s}", .{ key_color, key, ascii.Reset, terminal_name }) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedNetInfo(allocator: std.mem.Allocator) ![]u8 {
|
pub fn getDefaultFormattedNetInfo(allocator: std.mem.Allocator) !Result {
|
||||||
return try getFormattedNetInfo(allocator, "Local IP", ascii.Yellow);
|
return try getFormattedNetInfo(allocator, "Local IP", ascii.Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedNetInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !std.ArrayList([]u8) {
|
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.ArrayList([]u8).init(allocator);
|
||||||
|
|
||||||
var net_info_list = try detection.network.getNetInfo(allocator);
|
var net_info_list = try detection.network.getNetInfo(allocator);
|
||||||
@@ -178,5 +205,5 @@ pub fn getFormattedNetInfo(allocator: std.mem.Allocator, key: []const u8, key_co
|
|||||||
}
|
}
|
||||||
net_info_list.deinit();
|
net_info_list.deinit();
|
||||||
|
|
||||||
return formatted_net_info_list;
|
return Result{ .string_arraylist = formatted_net_info_list };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const c_unistd = @cImport(@cInclude("unistd.h"));
|
const c_unistd = @cImport(@cInclude("unistd.h"));
|
||||||
const c_statvfs = @cImport(@cInclude("sys/statvfs.h"));
|
const c_statvfs = @cImport(@cInclude("sys/statvfs.h"));
|
||||||
|
const c_libpci = @cImport(@cInclude("pci/pci.h"));
|
||||||
|
|
||||||
/// Struct representing CPU informations
|
/// Struct representing CPU informations
|
||||||
pub const CpuInfo = struct {
|
pub const CpuInfo = struct {
|
||||||
@@ -9,6 +10,13 @@ pub const CpuInfo = struct {
|
|||||||
cpu_max_freq: f32,
|
cpu_max_freq: f32,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Struct representing GPU informations
|
||||||
|
pub const GpuInfo = struct {
|
||||||
|
gpu_name: []u8,
|
||||||
|
gpu_cores: i32,
|
||||||
|
gpu_freq: f64,
|
||||||
|
};
|
||||||
|
|
||||||
/// Struct representing RAM usage informations
|
/// Struct representing RAM usage informations
|
||||||
pub const RamInfo = struct {
|
pub const RamInfo = struct {
|
||||||
ram_size: f64,
|
ram_size: f64,
|
||||||
@@ -43,6 +51,7 @@ pub fn getCpuInfo(allocator: std.mem.Allocator) !CpuInfo {
|
|||||||
|
|
||||||
// Parsing /proc/cpuinfo
|
// Parsing /proc/cpuinfo
|
||||||
var model_name: ?[]const u8 = null;
|
var model_name: ?[]const u8 = null;
|
||||||
|
var cpu_max_freq_mhz_str: ?[]const u8 = null;
|
||||||
|
|
||||||
var lines = std.mem.splitScalar(u8, cpuinfo_data, '\n');
|
var lines = std.mem.splitScalar(u8, cpuinfo_data, '\n');
|
||||||
while (lines.next()) |line| {
|
while (lines.next()) |line| {
|
||||||
@@ -52,22 +61,50 @@ pub fn getCpuInfo(allocator: std.mem.Allocator) !CpuInfo {
|
|||||||
_ = parts.next(); // discards the key
|
_ = parts.next(); // discards the key
|
||||||
if (parts.next()) |value| {
|
if (parts.next()) |value| {
|
||||||
model_name = std.mem.trim(u8, value, " ");
|
model_name = std.mem.trim(u8, value, " ");
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
} else if (std.mem.startsWith(u8, trimmed, "cpu MHz") and cpu_max_freq_mhz_str == null) {
|
||||||
|
var parts = std.mem.splitScalar(u8, trimmed, ':');
|
||||||
|
_ = parts.next(); // discard the key
|
||||||
|
if (parts.next()) |value| {
|
||||||
|
cpu_max_freq_mhz_str = std.mem.trim(u8, value, " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((model_name != null) and (cpu_max_freq_mhz_str != null)) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reads /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
|
var cpu_max_freq: f32 = 0.0;
|
||||||
const cpuinfo_max_freq_path = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
|
|
||||||
var file2 = try std.fs.cwd().openFile(cpuinfo_max_freq_path, .{});
|
|
||||||
defer file2.close();
|
|
||||||
const cpuinfo_max_freq_data = try file2.readToEndAlloc(allocator, std.math.maxInt(usize));
|
|
||||||
defer allocator.free(cpuinfo_max_freq_data);
|
|
||||||
|
|
||||||
// Parsing /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
|
const cpuinfo_max_freq_path = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
|
||||||
const trimmed = std.mem.trim(u8, cpuinfo_max_freq_data, " \n\r");
|
var cmf_exists: bool = true;
|
||||||
const cpu_max_freq_khz: f32 = try std.fmt.parseFloat(f32, trimmed);
|
|
||||||
const cpu_max_freq: f32 = cpu_max_freq_khz / 1_000_000;
|
// Checks if /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq exists
|
||||||
|
_ = std.fs.accessAbsolute(cpuinfo_max_freq_path, .{ .mode = .read_only }) catch |err| {
|
||||||
|
if (err == std.posix.AccessError.FileNotFound) {
|
||||||
|
cmf_exists = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (cmf_exists) {
|
||||||
|
// Reads /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
|
||||||
|
var file2 = try std.fs.cwd().openFile(cpuinfo_max_freq_path, .{});
|
||||||
|
|
||||||
|
defer file2.close();
|
||||||
|
const cpuinfo_max_freq_data = try file2.readToEndAlloc(allocator, std.math.maxInt(usize));
|
||||||
|
defer allocator.free(cpuinfo_max_freq_data);
|
||||||
|
|
||||||
|
// Parsing /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
|
||||||
|
const trimmed = std.mem.trim(u8, cpuinfo_max_freq_data, " \n\r");
|
||||||
|
const cpu_max_freq_khz: f32 = try std.fmt.parseFloat(f32, trimmed);
|
||||||
|
cpu_max_freq = cpu_max_freq_khz / 1_000_000;
|
||||||
|
} else {
|
||||||
|
if (cpu_max_freq_mhz_str != null) {
|
||||||
|
const cpu_max_freq_mhz: f32 = try std.fmt.parseFloat(f32, cpu_max_freq_mhz_str.?);
|
||||||
|
cpu_max_freq = cpu_max_freq_mhz / 1_000;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return CpuInfo{
|
return CpuInfo{
|
||||||
.cpu_name = try allocator.dupe(u8, model_name orelse "Unknown"),
|
.cpu_name = try allocator.dupe(u8, model_name orelse "Unknown"),
|
||||||
@@ -76,6 +113,93 @@ 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);
|
||||||
|
|
||||||
|
const display_controller = 0x03;
|
||||||
|
|
||||||
|
const pacc = c_libpci.pci_alloc();
|
||||||
|
defer c_libpci.pci_cleanup(pacc);
|
||||||
|
c_libpci.pci_init(pacc);
|
||||||
|
c_libpci.pci_scan_bus(pacc);
|
||||||
|
|
||||||
|
var devices = pacc.*.devices;
|
||||||
|
while (devices != null) : (devices = devices.*.next) {
|
||||||
|
// NOTE: for references: https://github.com/pciutils/pciutils/blob/3ec74c71c01878f92e751f15bb8febe720c3ab40/lib/access.c#L194
|
||||||
|
const known_fields = c_libpci.pci_fill_info(devices, c_libpci.PCI_FILL_IDENT | c_libpci.PCI_FILL_CLASS);
|
||||||
|
if (known_fields <= 0) {
|
||||||
|
return error.NoLibpciFieldsFound;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((devices.*.device_class >> 8) == display_controller) {
|
||||||
|
var name_buffer: [256]u8 = undefined;
|
||||||
|
|
||||||
|
const name = c_libpci.pci_lookup_name(
|
||||||
|
pacc,
|
||||||
|
&name_buffer,
|
||||||
|
name_buffer.len,
|
||||||
|
c_libpci.PCI_LOOKUP_VENDOR | c_libpci.PCI_LOOKUP_DEVICE,
|
||||||
|
devices.*.vendor_id,
|
||||||
|
devices.*.device_id,
|
||||||
|
);
|
||||||
|
|
||||||
|
const gpu_name = try allocator.dupe(u8, std.mem.span(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,
|
||||||
|
.gpu_cores = 0,
|
||||||
|
.gpu_freq = 0.0,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (gpu_info_list.items.len == 0) {
|
||||||
|
try gpu_info_list.append(GpuInfo{
|
||||||
|
.gpu_name = try allocator.dupe(u8, "Unknown"),
|
||||||
|
.gpu_cores = 0,
|
||||||
|
.gpu_freq = 0.0,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return gpu_info_list;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parseGpuName(allocator: std.mem.Allocator, name: []u8) !?[]u8 {
|
||||||
|
// NOTE: for references: https://github.com/pciutils/pciutils/blob/master/pci.ids
|
||||||
|
|
||||||
|
if (std.mem.startsWith(u8, name, "Advanced Micro Devices, Inc. [AMD/ATI]")) {
|
||||||
|
const size = std.mem.replacementSize(u8, name, "Advanced Micro Devices, Inc. [AMD/ATI]", "AMD");
|
||||||
|
const parsed_gpu_name = try allocator.alloc(u8, size);
|
||||||
|
_ = std.mem.replace(u8, name, "Advanced Micro Devices, Inc. [AMD/ATI]", "AMD", parsed_gpu_name);
|
||||||
|
|
||||||
|
return parsed_gpu_name;
|
||||||
|
} else if (std.mem.startsWith(u8, name, "Intel Corporation")) {
|
||||||
|
const size = std.mem.replacementSize(u8, name, "Intel Corporation", "Intel");
|
||||||
|
const parsed_gpu_name = try allocator.alloc(u8, size);
|
||||||
|
_ = std.mem.replace(u8, name, "Intel Corporation", "Intel", parsed_gpu_name);
|
||||||
|
|
||||||
|
return parsed_gpu_name;
|
||||||
|
} else if (std.mem.startsWith(u8, name, "NVIDIA Corporation")) {
|
||||||
|
const size = std.mem.replacementSize(u8, name, "NVIDIA Corporation", "NVIDIA");
|
||||||
|
const parsed_gpu_name = try allocator.alloc(u8, size);
|
||||||
|
_ = std.mem.replace(u8, name, "NVIDIA Corporation", "NVIDIA", parsed_gpu_name);
|
||||||
|
|
||||||
|
return parsed_gpu_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn getRamInfo(allocator: std.mem.Allocator) !RamInfo {
|
pub fn getRamInfo(allocator: std.mem.Allocator) !RamInfo {
|
||||||
// Reads /proc/meminfo
|
// Reads /proc/meminfo
|
||||||
const meminfo_path = "/proc/meminfo";
|
const meminfo_path = "/proc/meminfo";
|
||||||
@@ -203,7 +327,7 @@ pub fn getDiskSize(disk_path: []const u8) !DiskInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const total_size = stat.f_blocks * stat.f_frsize;
|
const total_size = stat.f_blocks * stat.f_frsize;
|
||||||
const free_size = stat.f_bavail * stat.f_frsize;
|
const free_size = stat.f_bfree * stat.f_frsize;
|
||||||
const used_size = total_size - free_size;
|
const used_size = total_size - free_size;
|
||||||
|
|
||||||
const used_size_percentage = (used_size * 100) / total_size;
|
const used_size_percentage = (used_size * 100) / total_size;
|
||||||
|
|||||||
@@ -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)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,9 @@ pub fn getUsername(allocator: std.mem.Allocator) ![]u8 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn getShell(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);
|
var child = std.process.Child.init(&[_][]const u8{ shell, "--version" }, allocator);
|
||||||
defer allocator.free(shell);
|
defer allocator.free(shell);
|
||||||
@@ -20,12 +22,29 @@ pub fn getShell(allocator: std.mem.Allocator) ![]u8 {
|
|||||||
|
|
||||||
_ = try child.wait();
|
_ = 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;
|
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 {
|
pub fn getTerminalName(allocator: std.mem.Allocator) ![]u8 {
|
||||||
const term_progrm = std.process.getEnvVarOwned(allocator, "TERM_PROGRAM") catch |err| if (err == error.EnvironmentVariableNotFound) {
|
const term_program = std.process.getEnvVarOwned(allocator, "TERM_PROGRAM") catch |err| if (err == error.EnvironmentVariableNotFound) {
|
||||||
return allocator.dupe(u8, "Unknown");
|
return allocator.dupe(u8, "Unknown");
|
||||||
} else return err;
|
} else return err;
|
||||||
return term_progrm;
|
return term_program;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -172,7 +172,6 @@ fn getCpuFreqAppleSilicon() !f64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: test on intel machine
|
|
||||||
pub fn getCpuFreqIntel() f64 {
|
pub fn getCpuFreqIntel() f64 {
|
||||||
var freq: f64 = 0;
|
var freq: f64 = 0;
|
||||||
var size: usize = @sizeOf(f64);
|
var size: usize = @sizeOf(f64);
|
||||||
@@ -419,7 +418,7 @@ pub fn getDiskSize(disk_path: []const u8) !DiskInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const total_size = stat.f_blocks * stat.f_frsize;
|
const total_size = stat.f_blocks * stat.f_frsize;
|
||||||
const free_size = stat.f_bavail * stat.f_frsize;
|
const free_size = stat.f_bfree * stat.f_frsize;
|
||||||
const used_size = total_size - free_size;
|
const used_size = total_size - free_size;
|
||||||
|
|
||||||
const used_size_percentage = (used_size * 100) / total_size;
|
const used_size_percentage = (used_size * 100) / total_size;
|
||||||
|
|||||||
@@ -9,7 +9,9 @@ pub fn getUsername(allocator: std.mem.Allocator) ![]u8 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn getShell(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);
|
var child = std.process.Child.init(&[_][]const u8{ shell, "--version" }, allocator);
|
||||||
defer allocator.free(shell);
|
defer allocator.free(shell);
|
||||||
@@ -23,12 +25,29 @@ pub fn getShell(allocator: std.mem.Allocator) ![]u8 {
|
|||||||
|
|
||||||
_ = try child.wait();
|
_ = 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;
|
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 {
|
pub fn getTerminalName(allocator: std.mem.Allocator) ![]u8 {
|
||||||
const term_progrm = std.process.getEnvVarOwned(allocator, "TERM_PROGRAM") catch |err| if (err == error.EnvironmentVariableNotFound) {
|
const term_program = std.process.getEnvVarOwned(allocator, "TERM_PROGRAM") catch |err| if (err == error.EnvironmentVariableNotFound) {
|
||||||
return allocator.dupe(u8, "Unknown");
|
return allocator.dupe(u8, "Unknown");
|
||||||
} else return err;
|
} else return err;
|
||||||
return term_progrm;
|
return term_program;
|
||||||
}
|
}
|
||||||
|
|||||||
29
src/main.zig
29
src/main.zig
@@ -31,7 +31,7 @@ pub fn main() !void {
|
|||||||
ascii.Yellow,
|
ascii.Yellow,
|
||||||
username,
|
username,
|
||||||
ascii.Reset,
|
ascii.Reset,
|
||||||
ascii.Magenta,
|
ascii.Yellow,
|
||||||
hostname,
|
hostname,
|
||||||
ascii.Reset,
|
ascii.Reset,
|
||||||
}));
|
}));
|
||||||
@@ -43,8 +43,15 @@ pub fn main() !void {
|
|||||||
try sys_info_list.append(separtor_buffer);
|
try sys_info_list.append(separtor_buffer);
|
||||||
|
|
||||||
if (modules_types.items.len == 0) {
|
if (modules_types.items.len == 0) {
|
||||||
inline for (0..formatters.formatters.len) |i| {
|
inline for (0..formatters.default_formatters.len) |i| {
|
||||||
try sys_info_list.append(try formatters.default_formatters[i](allocator));
|
const result = try formatters.default_formatters[i](allocator);
|
||||||
|
switch (result) {
|
||||||
|
.string => |r| try sys_info_list.append(r),
|
||||||
|
.string_arraylist => |r| {
|
||||||
|
defer r.deinit();
|
||||||
|
try sys_info_list.appendSlice(r.items);
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (conf) |c| {
|
} else if (conf) |c| {
|
||||||
for (modules_types.items, c.value.modules) |module_type, module| {
|
for (modules_types.items, c.value.modules) |module_type, module| {
|
||||||
@@ -52,16 +59,14 @@ pub fn main() !void {
|
|||||||
const rgb = try ascii.hexColorToRgb(module.key_color);
|
const rgb = try ascii.hexColorToRgb(module.key_color);
|
||||||
const key_color = try std.fmt.bufPrint(&buf, "\x1b[38;2;{d};{d};{d}m", .{ rgb.r, rgb.g, rgb.b });
|
const key_color = try std.fmt.bufPrint(&buf, "\x1b[38;2;{d};{d};{d}m", .{ rgb.r, rgb.g, rgb.b });
|
||||||
|
|
||||||
if (module_type == config.ModuleType.net) {
|
const result = try formatters.formatters[@intFromEnum(module_type)](allocator, module.key, key_color);
|
||||||
var formatted_net_info_list = try formatters.getFormattedNetInfo(allocator, module.key, key_color);
|
switch (result) {
|
||||||
defer formatted_net_info_list.deinit();
|
.string => |r| try sys_info_list.append(r),
|
||||||
|
.string_arraylist => |r| {
|
||||||
try sys_info_list.appendSlice(formatted_net_info_list.items);
|
defer r.deinit();
|
||||||
|
try sys_info_list.appendSlice(r.items);
|
||||||
continue;
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
try sys_info_list.append(try formatters.formatters[@intFromEnum(module_type)](allocator, module.key, key_color));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
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.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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