Compare commits
83 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0aec3d8e5b | ||
|
|
dd2f50c79c | ||
|
|
16bb74c47b | ||
|
|
20f9c6e1d5 | ||
|
|
b09443db4d | ||
|
|
dd3b79b32d | ||
|
|
22376139f2 | ||
|
|
a835074ee6 | ||
|
|
3d16102f37 | ||
|
|
24a5e98089 | ||
|
|
f03f7e163c | ||
|
|
69d02a4bb7 | ||
|
|
6a86a81fcc | ||
|
|
a125053c70 | ||
|
|
d9ec3a7de7 | ||
|
|
5f6460f46e | ||
|
|
aab5212e6e | ||
|
|
abff59b7d6 | ||
|
|
1dc0559b8e | ||
|
|
2b32119bc7 | ||
|
|
837b7cd5c4 | ||
|
|
6317269c4f | ||
|
|
989b5b2305 | ||
|
|
c32318dac2 | ||
|
|
c64f2f384d | ||
|
|
c82622cf8e | ||
|
|
9babb021f1 | ||
|
|
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 | ||
|
|
428d882591 | ||
|
|
a1c62a56ae | ||
|
|
5b423d1cef | ||
|
|
6574945e56 | ||
|
|
66bb544c0a | ||
|
|
d90c1d2e9a | ||
|
|
0beec60a8a | ||
|
|
7669da5ae8 | ||
|
|
1267a71250 | ||
|
|
2aa90c04ff | ||
|
|
f6d1c2dc72 | ||
|
|
9df0845530 | ||
|
|
3b3c29e22d | ||
|
|
a866952f86 | ||
|
|
5d7bdd8041 |
95
.github/workflows/build.yml
vendored
Normal file
95
.github/workflows/build.yml
vendored
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
name: Build
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ "main" ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
# Linux x86_64
|
||||||
|
- os: ubuntu-latest
|
||||||
|
target: x86_64-linux
|
||||||
|
runner_arch: x64
|
||||||
|
setup_deps: |
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install -y libpci-dev
|
||||||
|
|
||||||
|
# macOS x86_64
|
||||||
|
- os: macos-13
|
||||||
|
target: x86_64-darwin
|
||||||
|
runner_arch: x64
|
||||||
|
setup_deps: ""
|
||||||
|
|
||||||
|
# macOS aarch64 (Apple Silicon)
|
||||||
|
- os: macos-latest
|
||||||
|
target: aarch64-darwin
|
||||||
|
runner_arch: arm64
|
||||||
|
setup_deps: ""
|
||||||
|
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup Zig
|
||||||
|
uses: mlugg/setup-zig@v2
|
||||||
|
with:
|
||||||
|
version: 0.15.1
|
||||||
|
|
||||||
|
- name: Setup platform dependencies
|
||||||
|
if: matrix.setup_deps != ''
|
||||||
|
run: ${{ matrix.setup_deps }}
|
||||||
|
|
||||||
|
- name: Cache Zig
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
~/.cache/zig
|
||||||
|
zig-cache
|
||||||
|
key: ${{ runner.os }}-${{ matrix.target }}-zig-${{ hashFiles('build.zig', 'build.zig.zon') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-${{ matrix.target }}-zig-
|
||||||
|
|
||||||
|
- name: Build for target
|
||||||
|
run: |
|
||||||
|
zig build -Doptimize=ReleaseSafe
|
||||||
|
|
||||||
|
mv zig-out/bin/zigfetch zig-out/bin/zigfetch-${{ matrix.target }}
|
||||||
|
|
||||||
|
- name: Verify binary
|
||||||
|
run: |
|
||||||
|
ls -la zig-out/bin/
|
||||||
|
file zig-out/bin/zigfetch-${{ matrix.target }}
|
||||||
|
|
||||||
|
- name: Upload artifacts
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: zigfetch-${{ matrix.target }}
|
||||||
|
path: zig-out/bin/zigfetch-${{ matrix.target }}
|
||||||
|
retention-days: 3
|
||||||
|
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup Zig
|
||||||
|
uses: mlugg/setup-zig@v2
|
||||||
|
with:
|
||||||
|
version: 0.15.1
|
||||||
|
|
||||||
|
- name: Setup dependencies
|
||||||
|
run: |
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install -y libpci-dev
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
run: |
|
||||||
|
zig test src/ascii.zig
|
||||||
|
zig test src/utils.zig
|
||||||
94
README.md
94
README.md
@@ -1,5 +1,18 @@
|
|||||||
# Zigfetch
|
# Zigfetch
|
||||||
|
|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|
|
||||||
|
| Default config | Custom config |
|
||||||
|
| :-----------------------------------------------------: | :---------------------------------------------------: |
|
||||||
|
|  |  |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
- [Description](#description)
|
- [Description](#description)
|
||||||
- [Requirements](#requirements)
|
- [Requirements](#requirements)
|
||||||
- [Installation](#installation)
|
- [Installation](#installation)
|
||||||
@@ -14,7 +27,7 @@ Zigfetch is a minimal [neofetch](https://github.com/dylanaraps/neofetch)/[fastfe
|
|||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
- \>= [zig v0.14.0](https://ziglang.org/)
|
- \>= [zig v0.15.1](https://ziglang.org/)
|
||||||
|
|
||||||
### Linux only
|
### Linux only
|
||||||
|
|
||||||
@@ -66,10 +79,87 @@ $ touch config.json
|
|||||||
$ cp /path/to/zigfetch/config.json ~/.config/zigfetch/config.json
|
$ cp /path/to/zigfetch/config.json ~/.config/zigfetch/config.json
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Modules
|
||||||
|
|
||||||
|
Available modules:
|
||||||
|
|
||||||
|
- Os
|
||||||
|
- Kernel
|
||||||
|
- Uptime
|
||||||
|
- Packages
|
||||||
|
- Shell
|
||||||
|
- Cpu
|
||||||
|
- Gpu
|
||||||
|
- Ram
|
||||||
|
- Swap
|
||||||
|
- Disk
|
||||||
|
- Net
|
||||||
|
- Terminal
|
||||||
|
- Locale
|
||||||
|
- Custom
|
||||||
|
|
||||||
|
| Module type | Linux | macOS | Windows |
|
||||||
|
| :---------: | :-------------: | :----------------------: | :-----: |
|
||||||
|
| os | Yes | Yes | WIP |
|
||||||
|
| kernel | Yes | Yes | WIP |
|
||||||
|
| uptime | Yes | Yes | WIP |
|
||||||
|
| packages | WIP | Yes (Homebrew, Macports) | WIP |
|
||||||
|
| shell | Yes (bash, zsh) | Yes (bash, zsh) | WIP |
|
||||||
|
| cpu | Yes | Yes | WIP |
|
||||||
|
| gpu | Yes | Yes (Apple Silicon only) | WIP |
|
||||||
|
| ram | Yes | Yes | WIP |
|
||||||
|
| swap | Yes | Yes | WIP |
|
||||||
|
| disk | Yes | Yes | WIP |
|
||||||
|
| net | Yes | Yes | WIP |
|
||||||
|
| terminal | Yes | Yes | WIP |
|
||||||
|
| locale | Yes | Yes | WIP |
|
||||||
|
|
||||||
|
```json
|
||||||
|
"modules": [
|
||||||
|
{
|
||||||
|
"type": "os",
|
||||||
|
"key": "OS",
|
||||||
|
"key_color": "#5E81AC"
|
||||||
|
},
|
||||||
|
...
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Custom module
|
||||||
|
|
||||||
|
```json
|
||||||
|
"modules": [
|
||||||
|
{
|
||||||
|
"type": "custom",
|
||||||
|
"key": "-----------",
|
||||||
|
"key_color": "#5E81AC"
|
||||||
|
},
|
||||||
|
...
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Custom ASCII art
|
||||||
|
|
||||||
|
To use an ASCII art of your choice:
|
||||||
|
|
||||||
|
```json
|
||||||
|
"ascii_abs_path": "absolute_path/to/your/ascii_art.txt"
|
||||||
|
```
|
||||||
|
|
||||||
|
Don't use the `~` character.
|
||||||
|
|
||||||
|
#### Username and Hostname color
|
||||||
|
|
||||||
|
To change the Username and Hostname color (HEX colors only):
|
||||||
|
|
||||||
|
```json
|
||||||
|
"username_hostname_color": "#5E81AC"
|
||||||
|
```
|
||||||
|
|
||||||
## 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
|
||||||
|
|||||||
BIN
assets/screenshot-custom-config.png
Normal file
BIN
assets/screenshot-custom-config.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 209 KiB |
BIN
assets/screenshot-default-config.png
Normal file
BIN
assets/screenshot-default-config.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 200 KiB |
16
build.zig
16
build.zig
@@ -17,9 +17,11 @@ pub fn build(b: *std.Build) void {
|
|||||||
|
|
||||||
const exe = b.addExecutable(.{
|
const exe = b.addExecutable(.{
|
||||||
.name = "zigfetch",
|
.name = "zigfetch",
|
||||||
.root_source_file = b.path("src/main.zig"),
|
.root_module = b.createModule(.{
|
||||||
.target = target,
|
.root_source_file = b.path("src/main.zig"),
|
||||||
.optimize = optimize,
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (target.result.os.tag == .macos) {
|
if (target.result.os.tag == .macos) {
|
||||||
@@ -61,9 +63,11 @@ pub fn build(b: *std.Build) void {
|
|||||||
run_step.dependOn(&run_cmd.step);
|
run_step.dependOn(&run_cmd.step);
|
||||||
|
|
||||||
const exe_unit_tests = b.addTest(.{
|
const exe_unit_tests = b.addTest(.{
|
||||||
.root_source_file = b.path("src/main.zig"),
|
.root_module = b.createModule(.{
|
||||||
.target = target,
|
.root_source_file = b.path("src/main.zig"),
|
||||||
.optimize = optimize,
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
|
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
|
||||||
|
|||||||
@@ -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.3",
|
.version = "0.22.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
|
||||||
@@ -28,7 +28,7 @@
|
|||||||
|
|
||||||
// Tracks the earliest Zig version that the package considers to be a
|
// Tracks the earliest Zig version that the package considers to be a
|
||||||
// supported use case.
|
// supported use case.
|
||||||
.minimum_zig_version = "0.14.0",
|
.minimum_zig_version = "0.15.1",
|
||||||
|
|
||||||
// This field is optional.
|
// This field is optional.
|
||||||
// Each dependency must either provide a `url` and `hash`, or a `path`.
|
// Each dependency must either provide a `url` and `hash`, or a `path`.
|
||||||
|
|||||||
@@ -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";
|
||||||
@@ -64,22 +65,27 @@ test "parse ffffff" {
|
|||||||
try std.testing.expect((result.r == 255) and (result.g == 255) and (result.b == 255));
|
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 {
|
pub fn printAsciiAndModules(allocator: std.mem.Allocator, ascii_art_path: ?[]u8, sys_info_list: std.array_list.Managed([]u8)) !void {
|
||||||
const stdout_file = std.io.getStdOut().writer();
|
var stdout_buffer: [2048]u8 = undefined;
|
||||||
var bw = std.io.bufferedWriter(stdout_file);
|
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
|
||||||
const stdout = bw.writer();
|
const stdout = &stdout_writer.interface;
|
||||||
|
|
||||||
// const ascii_art_path = "./assets/ascii/guy_fawks.txt";
|
var ascii_art_data: []const u8 = undefined;
|
||||||
// var file = try std.fs.cwd().openFile(ascii_art_path, .{});
|
if (ascii_art_path) |ascii| {
|
||||||
// defer file.close();
|
var ascii_file = try std.fs.cwd().openFile(ascii, .{});
|
||||||
// const ascii_art_data = try file.readToEndAlloc(allocator, std.math.maxInt(usize));
|
defer ascii_file.close();
|
||||||
// defer allocator.free(ascii_art_data);
|
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 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();
|
defer ascii_art_content_list.deinit();
|
||||||
|
|
||||||
while (lines.next()) |line| {
|
while (lines.next()) |line| {
|
||||||
@@ -89,18 +95,39 @@ 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 = utils.getTerminalSize() catch utils.TermSize{ .height = 50, .width = 50 };
|
||||||
|
const terminal_width: usize = @intCast(terminal_size.width);
|
||||||
|
|
||||||
|
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 > longest_ascii_art_row_len + longest_sys_info_string_len + spacing;
|
||||||
|
|
||||||
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 spacing (5) + the longest ascii art row length + the longest sys info string length
|
||||||
try stdout.print("{s:<40} \t", .{ascii_art_items[i]});
|
if (can_print_ascii_art) {
|
||||||
} else {
|
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);
|
||||||
try stdout.print("{s:<40}", .{""});
|
@memset(alignment_buffer, ' ');
|
||||||
|
|
||||||
|
if (i < ascii_art_len) {
|
||||||
|
try stdout.print("{s}{s}", .{ ascii_art_items[i], alignment_buffer });
|
||||||
|
} else {
|
||||||
|
try stdout.print("{s}", .{alignment_buffer});
|
||||||
|
}
|
||||||
|
|
||||||
|
allocator.free(alignment_buffer);
|
||||||
|
|
||||||
|
try stdout.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]});
|
||||||
@@ -119,7 +146,7 @@ pub fn printAscii(allocator: std.mem.Allocator, sys_info_list: std.ArrayList([]u
|
|||||||
} else {
|
} else {
|
||||||
try stdout.print("\n", .{});
|
try stdout.print("\n", .{});
|
||||||
}
|
}
|
||||||
try bw.flush();
|
try stdout.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (sys_info_list.items) |item| {
|
for (sys_info_list.items) |item| {
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const builtin = @import("builtin");
|
|
||||||
const ascii = @import("ascii.zig");
|
const ascii = @import("ascii.zig");
|
||||||
|
|
||||||
pub const Module = struct {
|
pub const Module = struct {
|
||||||
@@ -9,6 +8,8 @@ pub const Module = struct {
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub const Config = struct {
|
pub const Config = struct {
|
||||||
|
ascii_abs_path: ?[]u8 = null,
|
||||||
|
username_hostname_color: ?[]u8 = null,
|
||||||
modules: []Module,
|
modules: []Module,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -26,10 +27,23 @@ pub const ModuleType = enum {
|
|||||||
net,
|
net,
|
||||||
terminal,
|
terminal,
|
||||||
locale,
|
locale,
|
||||||
|
custom,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn getModulesTypes(allocator: std.mem.Allocator, config: ?std.json.Parsed(Config)) !std.ArrayList(ModuleType) {
|
pub fn getAsciiPath(config: ?std.json.Parsed(Config)) ?[]u8 {
|
||||||
var modules_list = std.ArrayList(ModuleType).init(allocator);
|
if (config) |c| {
|
||||||
|
return c.value.ascii_abs_path;
|
||||||
|
} else return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn getUsernameHostnameColor(config: ?std.json.Parsed(Config)) ?[]u8 {
|
||||||
|
if (config) |c| {
|
||||||
|
return c.value.username_hostname_color;
|
||||||
|
} else return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
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| {
|
if (config) |c| {
|
||||||
for (c.value.modules) |module| {
|
for (c.value.modules) |module| {
|
||||||
@@ -37,6 +51,7 @@ pub fn getModulesTypes(allocator: std.mem.Allocator, config: ?std.json.Parsed(Co
|
|||||||
if (module_enum) |m| {
|
if (module_enum) |m| {
|
||||||
try modules_list.append(m);
|
try modules_list.append(m);
|
||||||
} else {
|
} else {
|
||||||
|
modules_list.deinit();
|
||||||
return error.InvalidModule;
|
return error.InvalidModule;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ const detection = @import("detection.zig").os_module;
|
|||||||
|
|
||||||
const Result = union(enum) {
|
const Result = union(enum) {
|
||||||
string: []u8,
|
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{
|
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,
|
&getFormattedNetInfo,
|
||||||
&getFormattedTerminalNameInfo,
|
&getFormattedTerminalNameInfo,
|
||||||
&getFormattedLocaleInfo,
|
&getFormattedLocaleInfo,
|
||||||
|
&getFormattedCustom,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const default_formatters = [_]*const fn (allocator: std.mem.Allocator) anyerror!Result{
|
pub const default_formatters = [_]*const fn (allocator: std.mem.Allocator) anyerror!Result{
|
||||||
@@ -40,6 +41,17 @@ pub const default_formatters = [_]*const fn (allocator: std.mem.Allocator) anyer
|
|||||||
&getDefaultFormattedLocaleInfo,
|
&getDefaultFormattedLocaleInfo,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub fn getFormattedUsernameHostname(allocator: std.mem.Allocator, color: []const u8, username: []const u8, hostname: []const u8) ![]u8 {
|
||||||
|
return try std.fmt.allocPrint(allocator, "{s}{s}{s}@{s}{s}{s}", .{
|
||||||
|
color,
|
||||||
|
username,
|
||||||
|
ascii.Reset,
|
||||||
|
color,
|
||||||
|
hostname,
|
||||||
|
ascii.Reset,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedKernelInfo(allocator: std.mem.Allocator) !Result {
|
pub fn getDefaultFormattedKernelInfo(allocator: std.mem.Allocator) !Result {
|
||||||
return try getFormattedKernelInfo(allocator, "Kernel", ascii.Yellow);
|
return try getFormattedKernelInfo(allocator, "Kernel", ascii.Yellow);
|
||||||
}
|
}
|
||||||
@@ -78,7 +90,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 {
|
||||||
@@ -129,7 +141,7 @@ pub fn getFormattedGpuInfo(allocator: std.mem.Allocator, key: []const u8, key_co
|
|||||||
defer allocator.free(gpu_info.gpu_name);
|
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 }) };
|
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) {
|
||||||
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);
|
const gpu_info_list = try detection.hardware.getGpuInfo(allocator);
|
||||||
|
|
||||||
@@ -195,7 +207,7 @@ pub fn getDefaultFormattedNetInfo(allocator: std.mem.Allocator) !Result {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn getFormattedNetInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !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);
|
var net_info_list = try detection.network.getNetInfo(allocator);
|
||||||
for (net_info_list.items) |n| {
|
for (net_info_list.items) |n| {
|
||||||
@@ -207,3 +219,7 @@ pub fn getFormattedNetInfo(allocator: std.mem.Allocator, key: []const u8, key_co
|
|||||||
|
|
||||||
return Result{ .string_arraylist = formatted_net_info_list };
|
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) {
|
pub fn getGpuInfo(allocator: std.mem.Allocator) !std.array_list.Managed(GpuInfo) {
|
||||||
var gpu_info_list = std.ArrayList(GpuInfo).init(allocator);
|
var gpu_info_list = std.array_list.Managed(GpuInfo).init(allocator);
|
||||||
|
|
||||||
const display_controller = 0x03;
|
const display_controller = 0x03;
|
||||||
|
|
||||||
@@ -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 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{
|
try gpu_info_list.append(GpuInfo{
|
||||||
.gpu_name = parsed_gpu_name,
|
.gpu_name = parsed_gpu_name,
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ pub const NetInfo = struct {
|
|||||||
ipv4_addr: []u8,
|
ipv4_addr: []u8,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn getNetInfo(allocator: std.mem.Allocator) !std.ArrayList(NetInfo) {
|
pub fn getNetInfo(allocator: std.mem.Allocator) !std.array_list.Managed(NetInfo) {
|
||||||
var net_info_list = std.ArrayList(NetInfo).init(allocator);
|
var net_info_list = std.array_list.Managed(NetInfo).init(allocator);
|
||||||
|
|
||||||
var ifap: ?*c_ifaddrs.ifaddrs = null;
|
var ifap: ?*c_ifaddrs.ifaddrs = null;
|
||||||
if (c_ifaddrs.getifaddrs(&ifap) != 0) {
|
if (c_ifaddrs.getifaddrs(&ifap) != 0) {
|
||||||
|
|||||||
@@ -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,21 +6,33 @@ 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);
|
|
||||||
defer allocator.free(shell);
|
defer allocator.free(shell);
|
||||||
|
|
||||||
child.stdout_behavior = .Pipe;
|
const result = try std.process.Child.run(.{ .allocator = allocator, .argv = &[_][]const u8{ shell, "--version" } });
|
||||||
child.stderr_behavior = .Pipe;
|
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 {
|
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.CFDictionaryGetValueIfPresent(@as(c_iokit.CFDictionaryRef, @ptrCast(properties_ptr)), model_key, &name_ref) == c_iokit.TRUE) {
|
||||||
if (c_iokit.CFGetTypeID(name_ref) == c_iokit.CFStringGetTypeID()) {
|
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;
|
return gpu_info;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ pub const NetInfo = struct {
|
|||||||
ipv4_addr: []u8,
|
ipv4_addr: []u8,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn getNetInfo(allocator: std.mem.Allocator) !std.ArrayList(NetInfo) {
|
pub fn getNetInfo(allocator: std.mem.Allocator) !std.array_list.Managed(NetInfo) {
|
||||||
var net_info_list = std.ArrayList(NetInfo).init(allocator);
|
var net_info_list = std.array_list.Managed(NetInfo).init(allocator);
|
||||||
|
|
||||||
var ifap: ?*c_ifaddrs.ifaddrs = null;
|
var ifap: ?*c_ifaddrs.ifaddrs = null;
|
||||||
if (c_ifaddrs.getifaddrs(&ifap) != 0) {
|
if (c_ifaddrs.getifaddrs(&ifap) != 0) {
|
||||||
|
|||||||
@@ -2,34 +2,25 @@ const std = @import("std");
|
|||||||
const utils = @import("utils.zig");
|
const utils = @import("utils.zig");
|
||||||
|
|
||||||
pub fn getPackagesInfo(allocator: std.mem.Allocator) ![]const u8 {
|
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();
|
defer packages_info.deinit();
|
||||||
|
|
||||||
const homebrew_packages = countHomebrewPackages() catch |err| if (err == error.FileNotFound) 0 else return err;
|
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 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;
|
const macports_packages = countMacportPackages() catch |err| if (err == error.FileNotFound) 0 else return err;
|
||||||
|
|
||||||
var buffer: [10]u8 = undefined;
|
var buffer: [32]u8 = undefined;
|
||||||
var fbs = std.io.fixedBufferStream(&buffer);
|
|
||||||
|
|
||||||
if (homebrew_packages > 0) {
|
if (homebrew_packages > 0) {
|
||||||
try std.fmt.formatInt(homebrew_packages, 10, .lower, .{}, fbs.writer());
|
try packages_info.appendSlice(try std.fmt.bufPrint(&buffer, " brew: {d}", .{homebrew_packages}));
|
||||||
try packages_info.appendSlice(" brew: ");
|
|
||||||
try packages_info.appendSlice(fbs.getWritten());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (homebrew_casks > 0) {
|
if (homebrew_casks > 0) {
|
||||||
fbs.reset();
|
try packages_info.appendSlice(try std.fmt.bufPrint(&buffer, " brew-cask: {d}", .{homebrew_casks}));
|
||||||
try std.fmt.formatInt(homebrew_casks, 10, .lower, .{}, fbs.writer());
|
|
||||||
try packages_info.appendSlice(" brew-cask: ");
|
|
||||||
try packages_info.appendSlice(fbs.getWritten());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (macports_packages > 0) {
|
if (macports_packages > 0) {
|
||||||
fbs.reset();
|
try packages_info.appendSlice(try std.fmt.bufPrint(&buffer, " macports: {d}", .{macports_packages}));
|
||||||
try std.fmt.formatInt(macports_packages, 10, .lower, .{}, fbs.writer());
|
|
||||||
try packages_info.appendSlice(" macports: ");
|
|
||||||
try packages_info.appendSlice(fbs.getWritten());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return try allocator.dupe(u8, packages_info.items);
|
return try allocator.dupe(u8, packages_info.items);
|
||||||
|
|||||||
@@ -9,21 +9,33 @@ 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);
|
|
||||||
defer allocator.free(shell);
|
defer allocator.free(shell);
|
||||||
|
|
||||||
child.stdout_behavior = .Pipe;
|
const result = try std.process.Child.run(.{ .allocator = allocator, .argv = &[_][]const u8{ shell, "--version" } });
|
||||||
child.stderr_behavior = .Pipe;
|
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 {
|
pub fn getTerminalName(allocator: std.mem.Allocator) ![]u8 {
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const c_iokit = @cImport(@cInclude("IOKit/IOKitLib.h"));
|
const c_iokit = @cImport(@cInclude("IOKit/IOKitLib.h"));
|
||||||
|
|
||||||
/// Converts a CFString to a Zig string.
|
/// Converts a CFTypeRef casted to CFStringRef to a Zig string.
|
||||||
pub fn cfStringToZigString(allocator: std.mem.Allocator, cf_string: c_iokit.CFStringRef) ![]u8 {
|
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 length = c_iokit.CFStringGetLength(cf_string);
|
||||||
const max_size = c_iokit.CFStringGetMaximumSizeForEncoding(length, c_iokit.kCFStringEncodingUTF8) + 1;
|
const max_size = c_iokit.CFStringGetMaximumSizeForEncoding(length, c_iokit.kCFStringEncodingUTF8) + 1;
|
||||||
const max_size_usize = @as(usize, @intCast(max_size));
|
const max_size_usize = @as(usize, @intCast(max_size));
|
||||||
|
|||||||
35
src/main.zig
35
src/main.zig
@@ -10,11 +10,11 @@ pub fn main() !void {
|
|||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
defer _ = gpa.deinit();
|
defer _ = gpa.deinit();
|
||||||
|
|
||||||
var sys_info_list = std.ArrayList([]u8).init(allocator);
|
var modules_list = std.array_list.Managed([]u8).init(allocator);
|
||||||
defer sys_info_list.deinit();
|
defer modules_list.deinit();
|
||||||
|
|
||||||
errdefer {
|
errdefer {
|
||||||
for (sys_info_list.items) |info| {
|
for (modules_list.items) |info| {
|
||||||
allocator.free(info);
|
allocator.free(info);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -27,29 +27,30 @@ pub fn main() !void {
|
|||||||
|
|
||||||
const username = try detection.user.getUsername(allocator);
|
const username = try detection.user.getUsername(allocator);
|
||||||
const hostname = try detection.system.getHostname(allocator);
|
const hostname = try detection.system.getHostname(allocator);
|
||||||
try sys_info_list.append(try std.fmt.allocPrint(allocator, "{s}{s}{s}@{s}{s}{s}", .{
|
|
||||||
ascii.Yellow,
|
const username_hostname_color = if (config.getUsernameHostnameColor(conf)) |color| blk: {
|
||||||
username,
|
var buf: [32]u8 = undefined;
|
||||||
ascii.Reset,
|
const rgb = try ascii.hexColorToRgb(color);
|
||||||
ascii.Yellow,
|
const formatted_color = try std.fmt.bufPrint(&buf, "\x1b[38;2;{d};{d};{d}m", .{ rgb.r, rgb.g, rgb.b });
|
||||||
hostname,
|
break :blk formatted_color;
|
||||||
ascii.Reset,
|
} else ascii.Yellow;
|
||||||
}));
|
|
||||||
|
try modules_list.append(try formatters.getFormattedUsernameHostname(allocator, username_hostname_color, username, hostname));
|
||||||
allocator.free(hostname);
|
allocator.free(hostname);
|
||||||
allocator.free(username);
|
allocator.free(username);
|
||||||
|
|
||||||
const separtor_buffer = try allocator.alloc(u8, username.len + hostname.len + 1);
|
const separtor_buffer = try allocator.alloc(u8, username.len + hostname.len + 1);
|
||||||
@memset(separtor_buffer, '-');
|
@memset(separtor_buffer, '-');
|
||||||
try sys_info_list.append(separtor_buffer);
|
try modules_list.append(separtor_buffer);
|
||||||
|
|
||||||
if (modules_types.items.len == 0) {
|
if (modules_types.items.len == 0) {
|
||||||
inline for (0..formatters.default_formatters.len) |i| {
|
inline for (0..formatters.default_formatters.len) |i| {
|
||||||
const result = try formatters.default_formatters[i](allocator);
|
const result = try formatters.default_formatters[i](allocator);
|
||||||
switch (result) {
|
switch (result) {
|
||||||
.string => |r| try sys_info_list.append(r),
|
.string => |r| try modules_list.append(r),
|
||||||
.string_arraylist => |r| {
|
.string_arraylist => |r| {
|
||||||
defer r.deinit();
|
defer r.deinit();
|
||||||
try sys_info_list.appendSlice(r.items);
|
try modules_list.appendSlice(r.items);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -61,14 +62,14 @@ pub fn main() !void {
|
|||||||
|
|
||||||
const result = try formatters.formatters[@intFromEnum(module_type)](allocator, module.key, key_color);
|
const result = try formatters.formatters[@intFromEnum(module_type)](allocator, module.key, key_color);
|
||||||
switch (result) {
|
switch (result) {
|
||||||
.string => |r| try sys_info_list.append(r),
|
.string => |r| try modules_list.append(r),
|
||||||
.string_arraylist => |r| {
|
.string_arraylist => |r| {
|
||||||
defer r.deinit();
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
116
src/utils.zig
Normal file
116
src/utils.zig
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
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.fs.File.stdout();
|
||||||
|
|
||||||
|
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 = getTerminalSize() catch TermSize{ .height = 50, .width = 50 };
|
||||||
|
|
||||||
|
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..]));
|
||||||
|
}
|
||||||
|
|
||||||
|
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