Compare commits
45 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fa1f013640 | |||
| c14e14fc6c | |||
| 2854d2a922 | |||
|
|
8bd839a567 | ||
|
|
7c7b214c1d | ||
|
|
41af324564 | ||
|
|
366cd7b497 | ||
|
|
d85ae15fe6 | ||
|
|
19edc21b89 | ||
|
|
d50ccb06b1 | ||
|
|
c8fb8d6a3e | ||
|
|
df0f21bc48 | ||
|
|
8258df5aae | ||
|
|
937746ad50 | ||
|
|
6ca8dcf334 | ||
|
|
87d31a9112 | ||
|
|
c867ea69c6 | ||
|
|
29d5bb1d98 | ||
|
|
a88aab97ff | ||
|
|
2de0d6ec09 | ||
|
|
6f4e4eb044 | ||
|
|
a64db7a7d3 | ||
|
|
e2cbda50c1 | ||
|
|
51de84d210 | ||
|
|
b9dc58fabe | ||
|
|
2dfcadf413 | ||
|
|
2ea3adef12 | ||
|
|
b4b92abcb5 | ||
|
|
3847bc247a | ||
|
|
784d52eac4 | ||
|
|
f78406448a | ||
|
|
50b592b125 | ||
|
|
7faff1e70d | ||
|
|
c17b97608c | ||
|
|
1e1ad7be17 | ||
|
|
429c0f89b9 | ||
|
|
ad0617a7e5 | ||
|
|
df75b0fdb5 | ||
|
|
89f68fe674 | ||
|
|
89dfeca508 | ||
|
|
a72ede5259 | ||
|
|
7254bda609 | ||
|
|
d8ddd4a18e | ||
|
|
948554d022 | ||
|
|
a9bb9cdeaf |
48
.gitea/workflows/release.yml
Normal file
48
.gitea/workflows/release.yml
Normal file
@@ -0,0 +1,48 @@
|
||||
name: Release
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- "v*"
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y libpci-dev zip
|
||||
|
||||
- name: Install Zig
|
||||
uses: mlugg/setup-zig@v2
|
||||
with:
|
||||
version: 0.15.2
|
||||
|
||||
- name: Build
|
||||
run: zig build -Doptimize=ReleaseSafe
|
||||
|
||||
- name: Package and release
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.RELEASE_TOKEN }}
|
||||
run: |
|
||||
cd zig-out/bin
|
||||
zip ../../zigfetch-x86_64-linux.zip zigfetch
|
||||
cd ../..
|
||||
|
||||
RELEASE_ID=$(curl -s -X POST "https://gitea.bitua.io/api/v1/repos/bitua/zigfetch/releases" \
|
||||
-H "Authorization: token ${GITEA_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"tag_name\": \"${GITHUB_REF_NAME}\", \"name\": \"${GITHUB_REF_NAME}\"}" | jq -r .id)
|
||||
|
||||
echo "Release ID: $RELEASE_ID"
|
||||
|
||||
curl -s -X POST "https://gitea.bitua.io/api/v1/repos/bitua/zigfetch/releases/${RELEASE_ID}/assets?name=zigfetch-x86_64-linux.zip" \
|
||||
-H "Authorization: token ${GITEA_TOKEN}" \
|
||||
-F "attachment=@zigfetch-x86_64-linux.zip"
|
||||
101
.github/workflows/build.yml
vendored
101
.github/workflows/build.yml
vendored
@@ -2,7 +2,12 @@ name: Build
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "main" ]
|
||||
branches: ["main"]
|
||||
paths:
|
||||
- ".github/workflows/build.yml"
|
||||
- "src/**"
|
||||
- "build.zig"
|
||||
- "build.zig.zon"
|
||||
|
||||
jobs:
|
||||
build:
|
||||
@@ -18,7 +23,7 @@ jobs:
|
||||
sudo apt-get install -y libpci-dev
|
||||
|
||||
# macOS x86_64
|
||||
- os: macos-13
|
||||
- os: macos-15
|
||||
target: x86_64-darwin
|
||||
runner_arch: x64
|
||||
setup_deps: ""
|
||||
@@ -32,64 +37,64 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Zig
|
||||
uses: mlugg/setup-zig@v2
|
||||
with:
|
||||
version: 0.15.1
|
||||
- name: Setup Zig
|
||||
uses: mlugg/setup-zig@v2
|
||||
with:
|
||||
version: 0.15.2
|
||||
|
||||
- name: Setup platform dependencies
|
||||
if: matrix.setup_deps != ''
|
||||
run: ${{ matrix.setup_deps }}
|
||||
- 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: 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
|
||||
- name: Build for target
|
||||
run: |
|
||||
zig build -Doptimize=ReleaseSafe
|
||||
|
||||
mv zig-out/bin/zigfetch zig-out/bin/zigfetch-${{ matrix.target }}
|
||||
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: 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
|
||||
- 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: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Zig
|
||||
uses: mlugg/setup-zig@v2
|
||||
with:
|
||||
version: 0.15.1
|
||||
- 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: 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
|
||||
- name: Run tests
|
||||
run: |
|
||||
zig test src/ascii.zig
|
||||
zig test src/utils.zig
|
||||
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
*zig-*
|
||||
*zig-*
|
||||
result
|
||||
|
||||
18
CONTRIBUTING.md
Normal file
18
CONTRIBUTING.md
Normal file
@@ -0,0 +1,18 @@
|
||||
# How to contribute to Zigfetch
|
||||
|
||||
## Did you find a bug?
|
||||
|
||||
- Do not open up a GitHub issue if the bug is a security vulnerability, refer to the [security policy](https://github.com/utox39/zigfetch/blob/main/SECURITY.md).
|
||||
- Ensure the bug was not already reported by searching on GitHub under [Issues](https://github.com/utox39/zigfetch/issues).
|
||||
- If you're unable to find an open issue addressing the problem, open a new one.
|
||||
|
||||
## Do you want to make some changes to the code or documentation?
|
||||
|
||||
1. Fork the repo.
|
||||
2. Create a new branch (because the `main` branch is protected).
|
||||
3. Commit and push the changes to the new branch.
|
||||
4. Open a pull request.
|
||||
|
||||
---
|
||||
|
||||
**Thank you for your contribution!**
|
||||
104
README.md
104
README.md
@@ -13,13 +13,13 @@
|
||||
|
||||
---
|
||||
|
||||
- [Description](#description)
|
||||
- [Requirements](#requirements)
|
||||
- [Installation](#installation)
|
||||
- [Usage](#usage)
|
||||
- [Configuration](#configuration)
|
||||
- [Roadtrip](#roadtrip)
|
||||
- [Contributing](#contributing)
|
||||
- [Description](#description)
|
||||
- [Requirements](#requirements)
|
||||
- [Installation](#installation)
|
||||
- [Usage](#usage)
|
||||
- [Configuration](#configuration)
|
||||
- [Roadtrip](#roadtrip)
|
||||
- [Contributing](#contributing)
|
||||
|
||||
## Description
|
||||
|
||||
@@ -27,15 +27,17 @@ Zigfetch is a minimal [neofetch](https://github.com/dylanaraps/neofetch)/[fastfe
|
||||
|
||||
## Requirements
|
||||
|
||||
- \>= [zig v0.15.1](https://ziglang.org/)
|
||||
- \>= [zig v0.15.2](https://ziglang.org/)
|
||||
|
||||
### Linux only
|
||||
|
||||
- [libpci](https://github.com/pciutils/pciutils)
|
||||
- [libpci](https://github.com/pciutils/pciutils)
|
||||
|
||||
## Installation
|
||||
|
||||
```console
|
||||
### Build from source
|
||||
|
||||
```bash
|
||||
# Clone the repo
|
||||
$ git clone https://github.com/utox39/zigfetch.git
|
||||
|
||||
@@ -49,10 +51,22 @@ $ zig build -Doptimize=ReleaseSafe
|
||||
$ mv ./zig-out/zigfetch ~/bin/
|
||||
```
|
||||
|
||||
### Via [Homebrew](https://brew.sh/)
|
||||
|
||||
```bash
|
||||
brew install utox39/tap/zigfetch
|
||||
```
|
||||
|
||||
### Other package managers
|
||||
|
||||
[](https://repology.org/project/zigfetch/versions)
|
||||
|
||||
- nixpkg: Thanks to [@heisfer](https://github.com/heisfer)
|
||||
|
||||
## Usage
|
||||
|
||||
```console
|
||||
$ zigfetch
|
||||
```bash
|
||||
zigfetch
|
||||
```
|
||||
|
||||
### Configuration
|
||||
@@ -60,51 +74,51 @@ $ zigfetch
|
||||
> [!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
|
||||
|
||||
- Create the config folder
|
||||
- Create the config folder
|
||||
|
||||
```console
|
||||
$ mkdir -p ~/.config/zigfetch
|
||||
```bash
|
||||
mkdir -p ~/.config/zigfetch
|
||||
```
|
||||
|
||||
- Create the config file
|
||||
- Create the config file
|
||||
|
||||
```console
|
||||
$ cd ~/.config/zigfetch
|
||||
$ touch config.json
|
||||
```bash
|
||||
cd ~/.config/zigfetch
|
||||
touch config.json
|
||||
```
|
||||
|
||||
- Or copy the default config (preferred way)
|
||||
- Or copy the default config (preferred way)
|
||||
|
||||
```console
|
||||
$ cp /path/to/zigfetch/config.json ~/.config/zigfetch/config.json
|
||||
```bash
|
||||
cp /path/to/zigfetch/config.json ~/.config/zigfetch/config.json
|
||||
```
|
||||
|
||||
#### Modules
|
||||
|
||||
Available modules:
|
||||
|
||||
- Os
|
||||
- Kernel
|
||||
- Uptime
|
||||
- Packages
|
||||
- Shell
|
||||
- Cpu
|
||||
- Gpu
|
||||
- Ram
|
||||
- Swap
|
||||
- Disk
|
||||
- Net
|
||||
- WM (Window Manager)
|
||||
- Terminal
|
||||
- Locale
|
||||
- Custom
|
||||
- Os
|
||||
- Kernel
|
||||
- Uptime
|
||||
- Packages
|
||||
- Shell
|
||||
- Cpu
|
||||
- Gpu
|
||||
- Ram
|
||||
- Swap
|
||||
- Disk
|
||||
- Net
|
||||
- WM (Window Manager)
|
||||
- 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 |
|
||||
| packages | Yes* | Yes (Homebrew, Macports) | WIP |
|
||||
| shell | Yes (bash, zsh) | Yes (bash, zsh) | WIP |
|
||||
| cpu | Yes | Yes | WIP |
|
||||
| gpu | Yes | Yes (Apple Silicon only) | WIP |
|
||||
@@ -116,6 +130,8 @@ Available modules:
|
||||
| terminal | Yes | Yes | WIP |
|
||||
| locale | Yes | Yes | WIP |
|
||||
|
||||
\*(flatpak, nix, dpkg, pacman, xbps)
|
||||
|
||||
```json
|
||||
"modules": [
|
||||
{
|
||||
@@ -160,12 +176,12 @@ To change the Username and Hostname color (HEX colors only):
|
||||
|
||||
## Roadtrip
|
||||
|
||||
- [ ] Add ASCII art for each operating system and Linux distro
|
||||
- [x] Add GPU info for Linux
|
||||
- [ ] Add packages info for Linux
|
||||
- [x] Add user customization
|
||||
- [ ] Add support for Windows
|
||||
- [ ] Add ASCII art for each operating system and Linux distro
|
||||
- [x] Add GPU info for Linux
|
||||
- [x] Add packages info for Linux
|
||||
- [x] Add user customization
|
||||
- [ ] Add support for Windows
|
||||
|
||||
## 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.
|
||||
Please see [CONTIBUTING](https://github.com/utox39/zigfetch/blob/main/CONTRIBUTING.md). Thanks!
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
// This is a [Semantic Version](https://semver.org/).
|
||||
// In a future version of Zig it will be used for package deduplication.
|
||||
.version = "0.24.0",
|
||||
.version = "0.25.0",
|
||||
|
||||
// Together with name, this represents a globally unique package
|
||||
// identifier. This field is generated by the Zig toolchain when the
|
||||
@@ -28,7 +28,7 @@
|
||||
|
||||
// Tracks the earliest Zig version that the package considers to be a
|
||||
// supported use case.
|
||||
.minimum_zig_version = "0.15.1",
|
||||
.minimum_zig_version = "0.15.2",
|
||||
|
||||
// This field is optional.
|
||||
// Each dependency must either provide a `url` and `hash`, or a `path`.
|
||||
|
||||
20
default.nix
Normal file
20
default.nix
Normal file
@@ -0,0 +1,20 @@
|
||||
{ pkgs ? import <nixpkgs> {} }:
|
||||
|
||||
pkgs.stdenv.mkDerivation {
|
||||
pname = "zigfetch";
|
||||
version = "0.25.0";
|
||||
|
||||
src = ./.;
|
||||
|
||||
nativeBuildInputs = with pkgs; [
|
||||
zig.hook
|
||||
];
|
||||
|
||||
buildInputs = with pkgs; [
|
||||
pciutils
|
||||
];
|
||||
|
||||
zigBuildFlags = [
|
||||
"-Doptimize=ReleaseSafe"
|
||||
];
|
||||
}
|
||||
13
shell.nix
Normal file
13
shell.nix
Normal file
@@ -0,0 +1,13 @@
|
||||
{ pkgs ? import <nixpkgs> {} }:
|
||||
|
||||
let
|
||||
unstable = import (builtins.fetchTarball {
|
||||
url = "https://github.com/NixOS/nixpkgs/archive/nixos-unstable.tar.gz";
|
||||
}) {};
|
||||
in
|
||||
pkgs.mkShell {
|
||||
buildInputs = [
|
||||
unstable.zig
|
||||
pkgs.pciutils
|
||||
];
|
||||
}
|
||||
@@ -100,13 +100,9 @@ pub fn getDefaultFormattedPackagesInfo(allocator: std.mem.Allocator) !Result {
|
||||
}
|
||||
|
||||
pub fn getFormattedPackagesInfo(allocator: std.mem.Allocator, key: []const u8, key_color: []const u8) !Result {
|
||||
if (builtin.os.tag == .macos) {
|
||||
const packages_info = try detection.packages.getPackagesInfo(allocator);
|
||||
defer allocator.free(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) {
|
||||
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} WIP", .{ key_color, key, ascii.Reset }) };
|
||||
}
|
||||
const packages_info = try detection.packages.getPackagesInfo(allocator);
|
||||
defer allocator.free(packages_info);
|
||||
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s}{s}", .{ key_color, key, ascii.Reset, packages_info }) };
|
||||
}
|
||||
|
||||
pub fn getDefaultFormattedShellInfo(allocator: std.mem.Allocator) !Result {
|
||||
|
||||
@@ -2,3 +2,4 @@ pub const hardware = @import("./hardware.zig");
|
||||
pub const network = @import("./network.zig");
|
||||
pub const system = @import("./system.zig");
|
||||
pub const user = @import("./user.zig");
|
||||
pub const packages = @import("./packages.zig");
|
||||
|
||||
242
src/linux/packages.zig
Normal file
242
src/linux/packages.zig
Normal file
@@ -0,0 +1,242 @@
|
||||
const std = @import("std");
|
||||
const utils = @import("../utils.zig");
|
||||
|
||||
pub fn getPackagesInfo(allocator: std.mem.Allocator) ![]const u8 {
|
||||
var packages_info = std.array_list.Managed(u8).init(allocator);
|
||||
defer packages_info.deinit();
|
||||
|
||||
const flatpak_packages = countFlatpakPackages() catch |err| if (err == error.FileNotFound) 0 else return err;
|
||||
const nix_packages = countNixPackages(allocator) catch 0;
|
||||
const dpkg_packages = countDpkgPackages(allocator) catch |err| if (err == error.FileNotFound) 0 else return err;
|
||||
const pacman_packages = countPacmanPackages() catch |err| if (err == error.FileNotFound) 0 else return err;
|
||||
const xbps_packages = countXbpsPackages(allocator) catch |err| if (err == error.FileNotFound) 0 else return err;
|
||||
|
||||
var buffer: [32]u8 = undefined;
|
||||
|
||||
if (nix_packages > 0) {
|
||||
try packages_info.appendSlice(try std.fmt.bufPrint(&buffer, " Nix: {d}", .{nix_packages}));
|
||||
}
|
||||
|
||||
if (flatpak_packages > 0) {
|
||||
try packages_info.appendSlice(try std.fmt.bufPrint(&buffer, " Flatpak: {d}", .{flatpak_packages}));
|
||||
}
|
||||
|
||||
if (dpkg_packages > 0) {
|
||||
try packages_info.appendSlice(try std.fmt.bufPrint(&buffer, " Dpkg: {d}", .{dpkg_packages}));
|
||||
}
|
||||
|
||||
if (pacman_packages > 0) {
|
||||
try packages_info.appendSlice(try std.fmt.bufPrint(&buffer, " Pacman: {d}", .{pacman_packages}));
|
||||
}
|
||||
|
||||
if (xbps_packages > 0) {
|
||||
try packages_info.appendSlice(try std.fmt.bufPrint(&buffer, " Xbps: {d}", .{xbps_packages}));
|
||||
}
|
||||
|
||||
return try allocator.dupe(u8, packages_info.items);
|
||||
}
|
||||
|
||||
fn countFlatpakPackages() !usize {
|
||||
const flatpak_apps = try countFlatpakApps();
|
||||
const flatpak_runtimes = try countFlatpakRuntimes();
|
||||
|
||||
return flatpak_apps + flatpak_runtimes;
|
||||
}
|
||||
|
||||
fn countFlatpakApps() !usize {
|
||||
var dir = try std.fs.openDirAbsolute("/var/lib/flatpak/app/", .{ .iterate = true });
|
||||
defer dir.close();
|
||||
|
||||
var iter = dir.iterate();
|
||||
|
||||
var count: usize = 0;
|
||||
|
||||
while (try iter.next()) |e| {
|
||||
if (e.kind != .directory) continue;
|
||||
|
||||
var sub_dir = try dir.openDir(e.name, .{});
|
||||
defer sub_dir.close();
|
||||
|
||||
var current = sub_dir.openDir("current", .{}) catch continue;
|
||||
defer current.close();
|
||||
|
||||
// If `current` was opened successfully, increment the count
|
||||
count += 1;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
fn countFlatpakRuntimes() !usize {
|
||||
var dir = try std.fs.openDirAbsolute("/var/lib/flatpak/runtime/", .{ .iterate = true });
|
||||
defer dir.close();
|
||||
|
||||
var iter = dir.iterate();
|
||||
|
||||
var counter: usize = 0;
|
||||
|
||||
while (try iter.next()) |e| {
|
||||
if (std.mem.endsWith(u8, e.name, ".Locale") or std.mem.endsWith(u8, e.name, ".Debug")) continue;
|
||||
|
||||
var arch_dir = try dir.openDir(e.name, .{ .iterate = true });
|
||||
defer arch_dir.close();
|
||||
var arch_iter = arch_dir.iterate();
|
||||
while (try arch_iter.next()) |arch_e| {
|
||||
if (arch_e.kind != .directory) continue;
|
||||
|
||||
var sub_dir = try arch_dir.openDir(arch_e.name, .{ .iterate = true });
|
||||
defer sub_dir.close();
|
||||
var sub_iter = sub_dir.iterate();
|
||||
while (try sub_iter.next()) |_| {
|
||||
counter += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return counter;
|
||||
}
|
||||
|
||||
fn countNixPackages(allocator: std.mem.Allocator) !usize {
|
||||
// `/run/current-system` is a sym-link, so we need to obtein the real path
|
||||
const real_path = try std.fs.realpathAlloc(allocator, "/run/current-system");
|
||||
defer allocator.free(real_path);
|
||||
|
||||
var hash: [32]u8 = undefined;
|
||||
std.crypto.hash.Blake3.hash(real_path, &hash, .{});
|
||||
const hash_hex = try std.fmt.allocPrint(allocator, "{x}", .{hash});
|
||||
defer allocator.free(hash_hex);
|
||||
|
||||
var count: usize = 0;
|
||||
|
||||
// Inspired by https://github.com/fastfetch-cli/fastfetch/blob/608382109cda6623e53f318e8aced54cf8e5a042/src/detection/packages/packages_nix.c#L81
|
||||
count = checkNixCache(allocator, hash_hex) catch |err| switch (err) {
|
||||
error.FileNotFound, error.InvalidCache => {
|
||||
// nix-store --query --requisites /run/current-system | wc -l
|
||||
const result = try std.process.Child.run(.{ .allocator = allocator, .argv = &[_][]const u8{
|
||||
"sh",
|
||||
"-c",
|
||||
"nix-store --query --requisites /run/current-system | wc -l",
|
||||
} });
|
||||
|
||||
const result_trimmed = std.mem.trim(u8, result.stdout, "\n");
|
||||
defer allocator.free(result.stdout);
|
||||
defer allocator.free(result.stderr);
|
||||
|
||||
count = try std.fmt.parseInt(usize, result_trimmed, 10);
|
||||
|
||||
try writeNixCache(allocator, hash_hex, count);
|
||||
|
||||
return count;
|
||||
},
|
||||
else => return err,
|
||||
};
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
fn getNixCachePath(allocator: std.mem.Allocator) ![]const u8 {
|
||||
var cache_dir_path = std.process.getEnvVarOwned(allocator, "XDG_CACHE_HOME") catch try allocator.dupe(u8, "");
|
||||
if (cache_dir_path.len == 0) {
|
||||
allocator.free(cache_dir_path);
|
||||
const home = try std.process.getEnvVarOwned(allocator, "HOME");
|
||||
defer allocator.free(home);
|
||||
cache_dir_path = try std.fs.path.join(allocator, &.{ home, ".cache", "zigfetch", "nix" });
|
||||
} else {
|
||||
cache_dir_path = try std.fs.path.join(allocator, &.{ cache_dir_path, "zigfetch", "nix" });
|
||||
}
|
||||
|
||||
return cache_dir_path;
|
||||
}
|
||||
|
||||
fn writeNixCache(allocator: std.mem.Allocator, hash: []const u8, count: usize) !void {
|
||||
const cache_dir_path = try getNixCachePath(allocator);
|
||||
defer allocator.free(cache_dir_path);
|
||||
|
||||
try std.fs.cwd().makePath(cache_dir_path);
|
||||
var cache_dir = try std.fs.cwd().openDir(cache_dir_path, .{});
|
||||
defer cache_dir.close();
|
||||
var cache_file = try cache_dir.createFile("nix_cache", .{ .truncate = true });
|
||||
defer cache_file.close();
|
||||
|
||||
const cache_content = try std.fmt.allocPrint(allocator, "{s}\n{d}", .{ hash, count });
|
||||
defer allocator.free(cache_content);
|
||||
try cache_file.writeAll(cache_content);
|
||||
}
|
||||
|
||||
fn checkNixCache(allocator: std.mem.Allocator, hash: []const u8) !usize {
|
||||
const cache_dir_path = try getNixCachePath(allocator);
|
||||
defer allocator.free(cache_dir_path);
|
||||
var cache_dir = try std.fs.cwd().openDir(cache_dir_path, .{});
|
||||
defer cache_dir.close();
|
||||
|
||||
var cache_file = try cache_dir.openFile("nix_cache", .{ .mode = .read_only });
|
||||
const cache_size = (try cache_file.stat()).size;
|
||||
const cache_content = try utils.readFile(allocator, cache_file, cache_size);
|
||||
defer allocator.free(cache_content);
|
||||
|
||||
var cache_iter = std.mem.splitScalar(u8, cache_content, '\n');
|
||||
|
||||
const hash_needle = cache_iter.next().?;
|
||||
if (!std.mem.eql(u8, hash, hash_needle)) {
|
||||
return error.InvalidCache;
|
||||
}
|
||||
|
||||
// The next element in the split is the package count
|
||||
return std.fmt.parseInt(usize, cache_iter.next().?, 10);
|
||||
}
|
||||
|
||||
fn countDpkgPackages(allocator: std.mem.Allocator) !usize {
|
||||
const dpkg_status_path = "/var/lib/dpkg/status";
|
||||
const dpkg_file = try std.fs.cwd().openFile(dpkg_status_path, .{ .mode = .read_only });
|
||||
defer dpkg_file.close();
|
||||
const file_size = (try dpkg_file.stat()).size;
|
||||
|
||||
const content = try utils.readFile(allocator, dpkg_file, file_size);
|
||||
defer allocator.free(content);
|
||||
|
||||
var count: usize = 0;
|
||||
var iter = std.mem.splitSequence(u8, content, "\n\n");
|
||||
// TODO: find a way to avoid this loop
|
||||
while (iter.next()) |_| {
|
||||
count += 1;
|
||||
}
|
||||
|
||||
// Subtruct 1 to remove an useless line
|
||||
return count - 1;
|
||||
}
|
||||
|
||||
fn countPacmanPackages() !usize {
|
||||
// Subtruct 1 to remove `ALPM_DB_VERSION` from the count
|
||||
return try utils.countEntries("/var/lib/pacman/local") - 1;
|
||||
}
|
||||
|
||||
fn countXbpsPackages(allocator: std.mem.Allocator) !usize {
|
||||
var dir = try std.fs.openDirAbsolute("/var/db/xbps/", .{ .iterate = true });
|
||||
defer dir.close();
|
||||
|
||||
var count: usize = 0;
|
||||
|
||||
var dir_iter = dir.iterate();
|
||||
|
||||
while (try dir_iter.next()) |e| {
|
||||
if ((e.kind == .file) and std.mem.startsWith(u8, e.name, "pkgdb-")) {
|
||||
const pkgdb_file = try dir.openFile(e.name, .{ .mode = .read_only });
|
||||
defer pkgdb_file.close();
|
||||
const file_size = (try pkgdb_file.stat()).size;
|
||||
|
||||
const content = try utils.readFile(allocator, pkgdb_file, file_size);
|
||||
defer allocator.free(content);
|
||||
|
||||
var file_iter = std.mem.splitSequence(u8, content, "<string>installed</string>");
|
||||
// TODO: find a way to avoid this loop
|
||||
while (file_iter.next()) |_| {
|
||||
count += 1;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Subtruct 1 to remove an useless line
|
||||
return count - 1;
|
||||
}
|
||||
@@ -121,10 +121,8 @@ pub fn getWindowManagerInfo(allocator: std.mem.Allocator) ![]const u8 {
|
||||
defer file.close();
|
||||
|
||||
// NOTE: https://stackoverflow.com/questions/23534263/what-is-the-maximum-allowed-limit-on-the-length-of-a-process-name
|
||||
var file_buf: [16]u8 = undefined;
|
||||
var reader = std.fs.File.Reader.init(file, &file_buf);
|
||||
const read = try reader.read(&file_buf);
|
||||
const proc_name = file_buf[0..read];
|
||||
const proc_name = try utils.readFile(allocator, file, 16);
|
||||
defer allocator.free(proc_name);
|
||||
|
||||
const proc_name_trimmed = std.mem.trim(u8, proc_name, "\n");
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const std = @import("std");
|
||||
const utils = @import("utils.zig");
|
||||
const utils = @import("../utils.zig");
|
||||
|
||||
pub fn getPackagesInfo(allocator: std.mem.Allocator) ![]const u8 {
|
||||
var packages_info = std.array_list.Managed(u8).init(allocator);
|
||||
|
||||
@@ -23,17 +23,3 @@ pub fn cfTypeRefToZigString(allocator: std.mem.Allocator, cf_type_ref: c_iokit.C
|
||||
|
||||
return allocator.realloc(buffer, actual_len);
|
||||
}
|
||||
|
||||
pub fn countEntries(dir_path: []const u8) !usize {
|
||||
var dir = try std.fs.openDirAbsolute(dir_path, .{ .iterate = true });
|
||||
defer dir.close();
|
||||
|
||||
var count: usize = 0;
|
||||
var iter = dir.iterate();
|
||||
|
||||
while (try iter.next()) |_| {
|
||||
count += 1;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
@@ -119,10 +119,23 @@ pub fn readFile(allocator: std.mem.Allocator, file: std.fs.File, size: usize) ![
|
||||
var file_buf = try allocator.alloc(u8, size);
|
||||
defer allocator.free(file_buf);
|
||||
|
||||
var reader = std.fs.File.Reader.init(file, file_buf);
|
||||
const read = try reader.read(file_buf);
|
||||
const read = try file.read(file_buf);
|
||||
|
||||
const data = file_buf[0..read];
|
||||
|
||||
return allocator.dupe(u8, data);
|
||||
}
|
||||
|
||||
pub fn countEntries(dir_path: []const u8) !usize {
|
||||
var dir = try std.fs.openDirAbsolute(dir_path, .{ .iterate = true });
|
||||
defer dir.close();
|
||||
|
||||
var count: usize = 0;
|
||||
var iter = dir.iterate();
|
||||
|
||||
while (try iter.next()) |_| {
|
||||
count += 1;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user