Compare commits
28 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 |
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"
|
||||||
103
.github/workflows/build.yml
vendored
103
.github/workflows/build.yml
vendored
@@ -2,11 +2,12 @@ name: Build
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [ "main" ]
|
branches: ["main"]
|
||||||
paths:
|
paths:
|
||||||
- 'src/**'
|
- ".github/workflows/build.yml"
|
||||||
- 'build.zig'
|
- "src/**"
|
||||||
- 'build.zig.zon'
|
- "build.zig"
|
||||||
|
- "build.zig.zon"
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
@@ -22,7 +23,7 @@ jobs:
|
|||||||
sudo apt-get install -y libpci-dev
|
sudo apt-get install -y libpci-dev
|
||||||
|
|
||||||
# macOS x86_64
|
# macOS x86_64
|
||||||
- os: macos-13
|
- os: macos-15
|
||||||
target: x86_64-darwin
|
target: x86_64-darwin
|
||||||
runner_arch: x64
|
runner_arch: x64
|
||||||
setup_deps: ""
|
setup_deps: ""
|
||||||
@@ -36,64 +37,64 @@ jobs:
|
|||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Setup Zig
|
- name: Setup Zig
|
||||||
uses: mlugg/setup-zig@v2
|
uses: mlugg/setup-zig@v2
|
||||||
with:
|
with:
|
||||||
version: 0.15.1
|
version: 0.15.2
|
||||||
|
|
||||||
- name: Setup platform dependencies
|
- name: Setup platform dependencies
|
||||||
if: matrix.setup_deps != ''
|
if: matrix.setup_deps != ''
|
||||||
run: ${{ matrix.setup_deps }}
|
run: ${{ matrix.setup_deps }}
|
||||||
|
|
||||||
- name: Cache Zig
|
- name: Cache Zig
|
||||||
uses: actions/cache@v4
|
uses: actions/cache@v4
|
||||||
with:
|
with:
|
||||||
path: |
|
path: |
|
||||||
~/.cache/zig
|
~/.cache/zig
|
||||||
zig-cache
|
zig-cache
|
||||||
key: ${{ runner.os }}-${{ matrix.target }}-zig-${{ hashFiles('build.zig', 'build.zig.zon') }}
|
key: ${{ runner.os }}-${{ matrix.target }}-zig-${{ hashFiles('build.zig', 'build.zig.zon') }}
|
||||||
restore-keys: |
|
restore-keys: |
|
||||||
${{ runner.os }}-${{ matrix.target }}-zig-
|
${{ runner.os }}-${{ matrix.target }}-zig-
|
||||||
|
|
||||||
- name: Build for target
|
- name: Build for target
|
||||||
run: |
|
run: |
|
||||||
zig build -Doptimize=ReleaseSafe
|
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
|
- name: Verify binary
|
||||||
run: |
|
run: |
|
||||||
ls -la zig-out/bin/
|
ls -la zig-out/bin/
|
||||||
file zig-out/bin/zigfetch-${{ matrix.target }}
|
file zig-out/bin/zigfetch-${{ matrix.target }}
|
||||||
|
|
||||||
- name: Upload artifacts
|
- name: Upload artifacts
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: zigfetch-${{ matrix.target }}
|
name: zigfetch-${{ matrix.target }}
|
||||||
path: zig-out/bin/zigfetch-${{ matrix.target }}
|
path: zig-out/bin/zigfetch-${{ matrix.target }}
|
||||||
retention-days: 3
|
retention-days: 3
|
||||||
|
|
||||||
test:
|
test:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Setup Zig
|
- name: Setup Zig
|
||||||
uses: mlugg/setup-zig@v2
|
uses: mlugg/setup-zig@v2
|
||||||
with:
|
with:
|
||||||
version: 0.15.1
|
version: 0.15.1
|
||||||
|
|
||||||
- name: Setup dependencies
|
- name: Setup dependencies
|
||||||
run: |
|
run: |
|
||||||
sudo apt-get update
|
sudo apt-get update
|
||||||
sudo apt-get install -y libpci-dev
|
sudo apt-get install -y libpci-dev
|
||||||
|
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: |
|
run: |
|
||||||
zig test src/ascii.zig
|
zig test src/ascii.zig
|
||||||
zig test src/utils.zig
|
zig test src/utils.zig
|
||||||
|
|||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
*zig-*
|
*zig-*
|
||||||
|
result
|
||||||
|
|||||||
98
README.md
98
README.md
@@ -13,13 +13,13 @@
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
- [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
|
||||||
|
|
||||||
@@ -27,17 +27,17 @@ Zigfetch is a minimal [neofetch](https://github.com/dylanaraps/neofetch)/[fastfe
|
|||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
- \>= [zig v0.15.2](https://ziglang.org/)
|
- \>= [zig v0.15.2](https://ziglang.org/)
|
||||||
|
|
||||||
### Linux only
|
### Linux only
|
||||||
|
|
||||||
- [libpci](https://github.com/pciutils/pciutils)
|
- [libpci](https://github.com/pciutils/pciutils)
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
### Build from source
|
### Build from source
|
||||||
|
|
||||||
```console
|
```bash
|
||||||
# Clone the repo
|
# Clone the repo
|
||||||
$ git clone https://github.com/utox39/zigfetch.git
|
$ git clone https://github.com/utox39/zigfetch.git
|
||||||
|
|
||||||
@@ -53,14 +53,20 @@ $ mv ./zig-out/zigfetch ~/bin/
|
|||||||
|
|
||||||
### Via [Homebrew](https://brew.sh/)
|
### Via [Homebrew](https://brew.sh/)
|
||||||
|
|
||||||
```console
|
```bash
|
||||||
$ brew install utox39/tap/zigfetch
|
brew install utox39/tap/zigfetch
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Other package managers
|
||||||
|
|
||||||
|
[](https://repology.org/project/zigfetch/versions)
|
||||||
|
|
||||||
|
- nixpkg: Thanks to [@heisfer](https://github.com/heisfer)
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```console
|
```bash
|
||||||
$ zigfetch
|
zigfetch
|
||||||
```
|
```
|
||||||
|
|
||||||
### Configuration
|
### Configuration
|
||||||
@@ -68,51 +74,51 @@ $ 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
|
```bash
|
||||||
$ mkdir -p ~/.config/zigfetch
|
mkdir -p ~/.config/zigfetch
|
||||||
```
|
```
|
||||||
|
|
||||||
- Create the config file
|
- Create the config file
|
||||||
|
|
||||||
```console
|
```bash
|
||||||
$ 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
|
```bash
|
||||||
$ cp /path/to/zigfetch/config.json ~/.config/zigfetch/config.json
|
cp /path/to/zigfetch/config.json ~/.config/zigfetch/config.json
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Modules
|
#### Modules
|
||||||
|
|
||||||
Available modules:
|
Available modules:
|
||||||
|
|
||||||
- Os
|
- Os
|
||||||
- Kernel
|
- Kernel
|
||||||
- Uptime
|
- Uptime
|
||||||
- Packages
|
- Packages
|
||||||
- Shell
|
- Shell
|
||||||
- Cpu
|
- Cpu
|
||||||
- Gpu
|
- Gpu
|
||||||
- Ram
|
- Ram
|
||||||
- Swap
|
- Swap
|
||||||
- Disk
|
- Disk
|
||||||
- Net
|
- Net
|
||||||
- WM (Window Manager)
|
- WM (Window Manager)
|
||||||
- Terminal
|
- Terminal
|
||||||
- Locale
|
- Locale
|
||||||
- Custom
|
- Custom
|
||||||
|
|
||||||
| Module type | Linux | macOS | Windows |
|
| Module type | Linux | macOS | Windows |
|
||||||
| :---------: | :-------------: | :----------------------: | :-----: |
|
| :---------: | :-------------: | :----------------------: | :-----: |
|
||||||
| os | Yes | Yes | WIP |
|
| os | Yes | Yes | WIP |
|
||||||
| kernel | Yes | Yes | WIP |
|
| kernel | Yes | Yes | WIP |
|
||||||
| uptime | 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 |
|
| shell | Yes (bash, zsh) | Yes (bash, zsh) | WIP |
|
||||||
| cpu | Yes | Yes | WIP |
|
| cpu | Yes | Yes | WIP |
|
||||||
| gpu | Yes | Yes (Apple Silicon only) | WIP |
|
| gpu | Yes | Yes (Apple Silicon only) | WIP |
|
||||||
@@ -124,6 +130,8 @@ Available modules:
|
|||||||
| terminal | Yes | Yes | WIP |
|
| terminal | Yes | Yes | WIP |
|
||||||
| locale | Yes | Yes | WIP |
|
| locale | Yes | Yes | WIP |
|
||||||
|
|
||||||
|
\*(flatpak, nix, dpkg, pacman, xbps)
|
||||||
|
|
||||||
```json
|
```json
|
||||||
"modules": [
|
"modules": [
|
||||||
{
|
{
|
||||||
@@ -168,11 +176,11 @@ To change the Username and Hostname color (HEX colors only):
|
|||||||
|
|
||||||
## Roadtrip
|
## Roadtrip
|
||||||
|
|
||||||
- [ ] Add ASCII art for each operating system and Linux distro
|
- [ ] Add ASCII art for each operating system and Linux distro
|
||||||
- [x] Add GPU info for Linux
|
- [x] Add GPU info for Linux
|
||||||
- [ ] Add packages info for Linux
|
- [x] Add packages info for Linux
|
||||||
- [x] Add user customization
|
- [x] Add user customization
|
||||||
- [ ] Add support for Windows
|
- [ ] Add support for Windows
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
|
|||||||
@@ -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.24.2",
|
.version = "0.25.0",
|
||||||
|
|
||||||
// Together with name, this represents a globally unique package
|
// Together with name, this represents a globally unique package
|
||||||
// identifier. This field is generated by the Zig toolchain when the
|
// identifier. This field is generated by the Zig toolchain when the
|
||||||
|
|||||||
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 {
|
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);
|
||||||
const packages_info = try detection.packages.getPackagesInfo(allocator);
|
defer allocator.free(packages_info);
|
||||||
defer allocator.free(packages_info);
|
return Result{ .string = 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) {
|
|
||||||
return Result{ .string = try std.fmt.allocPrint(allocator, "{s}{s}:{s} WIP", .{ key_color, key, ascii.Reset }) };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getDefaultFormattedShellInfo(allocator: std.mem.Allocator) !Result {
|
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 network = @import("./network.zig");
|
||||||
pub const system = @import("./system.zig");
|
pub const system = @import("./system.zig");
|
||||||
pub const user = @import("./user.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;
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
const std = @import("std");
|
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.array_list.Managed(u8).init(allocator);
|
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);
|
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;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -125,3 +125,17 @@ pub fn readFile(allocator: std.mem.Allocator, file: std.fs.File, size: usize) ![
|
|||||||
|
|
||||||
return allocator.dupe(u8, data);
|
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