Add linux kernel information to +version

This commit is contained in:
Bartosz Sokorski
2025-07-04 00:26:43 +02:00
parent 3e9cfacb7d
commit c32f7adb10
3 changed files with 51 additions and 19 deletions

View File

@ -15,8 +15,6 @@ pub const Options = struct {};
/// The `version` command is used to display information about Ghostty. Recognized as /// The `version` command is used to display information about Ghostty. Recognized as
/// either `+version` or `--version`. /// either `+version` or `--version`.
pub fn run(alloc: Allocator) !u8 { pub fn run(alloc: Allocator) !u8 {
_ = alloc;
const stdout = std.io.getStdOut().writer(); const stdout = std.io.getStdOut().writer();
const tty = std.io.getStdOut().isTty(); const tty = std.io.getStdOut().isTty();
@ -34,15 +32,20 @@ pub fn run(alloc: Allocator) !u8 {
try stdout.print(" - channel: {s}\n", .{@tagName(build_config.release_channel)}); try stdout.print(" - channel: {s}\n", .{@tagName(build_config.release_channel)});
try stdout.print("Build Config\n", .{}); try stdout.print("Build Config\n", .{});
try stdout.print(" - Zig version: {s}\n", .{builtin.zig_version_string}); try stdout.print(" - Zig version : {s}\n", .{builtin.zig_version_string});
try stdout.print(" - build mode : {}\n", .{builtin.mode}); try stdout.print(" - build mode : {}\n", .{builtin.mode});
try stdout.print(" - app runtime: {}\n", .{build_config.app_runtime}); try stdout.print(" - app runtime : {}\n", .{build_config.app_runtime});
try stdout.print(" - font engine: {}\n", .{build_config.font_backend}); try stdout.print(" - font engine : {}\n", .{build_config.font_backend});
try stdout.print(" - renderer : {}\n", .{renderer.Renderer}); try stdout.print(" - renderer : {}\n", .{renderer.Renderer});
try stdout.print(" - libxev : {s}\n", .{@tagName(xev.backend)}); try stdout.print(" - libxev : {s}\n", .{@tagName(xev.backend)});
if (comptime build_config.app_runtime == .gtk) { if (comptime build_config.app_runtime == .gtk) {
try stdout.print(" - desktop env: {s}\n", .{@tagName(internal_os.desktopEnvironment())}); if (comptime builtin.os.tag == .linux) {
try stdout.print(" - GTK version:\n", .{}); const kernel_info = internal_os.getKernelInfo(alloc);
defer if (kernel_info) |k| alloc.free(k);
try stdout.print(" - kernel version: {s}\n", .{kernel_info orelse "Kernel information unavailable"});
}
try stdout.print(" - desktop env : {s}\n", .{@tagName(internal_os.desktopEnvironment())});
try stdout.print(" - GTK version :\n", .{});
try stdout.print(" build : {}\n", .{gtk_version.comptime_version}); try stdout.print(" build : {}\n", .{gtk_version.comptime_version});
try stdout.print(" runtime : {}\n", .{gtk_version.getRuntimeVersion()}); try stdout.print(" runtime : {}\n", .{gtk_version.getRuntimeVersion()});
try stdout.print(" - libadwaita : enabled\n", .{}); try stdout.print(" - libadwaita : enabled\n", .{});

27
src/os/kernel_info.zig Normal file
View File

@ -0,0 +1,27 @@
const std = @import("std");
const builtin = @import("builtin");
pub fn getKernelInfo(alloc: std.mem.Allocator) ?[]const u8 {
if (comptime builtin.os.tag != .linux) return null;
const path = "/proc/sys/kernel/osrelease";
var file = std.fs.openFileAbsolute(path, .{}) catch return null;
defer file.close();
// 128 bytes should be enough to hold the kernel information
const kernel_info = file.readToEndAlloc(alloc, 128) catch return null;
defer alloc.free(kernel_info);
return alloc.dupe(u8, std.mem.trim(u8, kernel_info, &std.ascii.whitespace)) catch return null;
}
test "read /proc/sys/kernel/osrelease" {
if (comptime builtin.os.tag != .linux) return null;
const allocator = std.testing.allocator;
const kernel_info = try getKernelInfo(allocator);
defer allocator.free(kernel_info);
// Since we can't hardcode the info in tests, just check
// if something was read from the file
try std.testing.expect(kernel_info.len > 0);
try std.testing.expect(!std.mem.eql(u8, kernel_info, ""));
}

View File

@ -14,6 +14,7 @@ const openpkg = @import("open.zig");
const pipepkg = @import("pipe.zig"); const pipepkg = @import("pipe.zig");
const resourcesdir = @import("resourcesdir.zig"); const resourcesdir = @import("resourcesdir.zig");
const systemd = @import("systemd.zig"); const systemd = @import("systemd.zig");
const kernelInfo = @import("kernel_info.zig");
// Namespaces // Namespaces
pub const args = @import("args.zig"); pub const args = @import("args.zig");
@ -58,6 +59,7 @@ pub const pipe = pipepkg.pipe;
pub const resourcesDir = resourcesdir.resourcesDir; pub const resourcesDir = resourcesdir.resourcesDir;
pub const ResourcesDir = resourcesdir.ResourcesDir; pub const ResourcesDir = resourcesdir.ResourcesDir;
pub const ShellEscapeWriter = shell.ShellEscapeWriter; pub const ShellEscapeWriter = shell.ShellEscapeWriter;
pub const getKernelInfo = kernelInfo.getKernelInfo;
test { test {
_ = i18n; _ = i18n;