Merge pull request #11 from utox39/feat/parse-shell-version

Feat/parse shell version
This commit is contained in:
utox39
2025-08-06 00:01:34 +02:00
committed by GitHub
2 changed files with 34 additions and 0 deletions

View File

@@ -22,9 +22,26 @@ pub fn getShell(allocator: std.mem.Allocator) ![]u8 {
_ = try child.wait();
if (std.mem.indexOf(u8, shell, "bash") != null) {
const bash_version = parseBashVersion(output);
defer allocator.free(output);
return try std.fmt.allocPrint(allocator, "{s} {s}", .{ "bash", bash_version.? });
}
return output;
}
fn parseBashVersion(shell_version_output: []u8) ?[]u8 {
const end_index = std.mem.indexOf(u8, shell_version_output, "(");
if (end_index == null) return null;
const version_keyword = "version ";
const version_keyword_index = std.mem.indexOf(u8, shell_version_output[0..end_index.?], version_keyword);
if (version_keyword_index == null) return null;
return shell_version_output[version_keyword_index.? + version_keyword.len .. end_index.?];
}
pub fn getTerminalName(allocator: std.mem.Allocator) ![]u8 {
const term_program = std.process.getEnvVarOwned(allocator, "TERM_PROGRAM") catch |err| if (err == error.EnvironmentVariableNotFound) {
return allocator.dupe(u8, "Unknown");

View File

@@ -25,9 +25,26 @@ pub fn getShell(allocator: std.mem.Allocator) ![]u8 {
_ = try child.wait();
if (std.mem.indexOf(u8, shell, "bash") != null) {
const bash_version = parseBashVersion(output);
defer allocator.free(output);
return try std.fmt.allocPrint(allocator, "{s} {s}", .{ "bash", bash_version.? });
}
return output;
}
fn parseBashVersion(shell_version_output: []u8) ?[]u8 {
const end_index = std.mem.indexOf(u8, shell_version_output, "(");
if (end_index == null) return null;
const version_keyword = "version ";
const version_keyword_index = std.mem.indexOf(u8, shell_version_output[0..end_index.?], version_keyword);
if (version_keyword_index == null) return null;
return shell_version_output[version_keyword_index.? + version_keyword.len .. end_index.?];
}
pub fn getTerminalName(allocator: std.mem.Allocator) ![]u8 {
const term_program = std.process.getEnvVarOwned(allocator, "TERM_PROGRAM") catch |err| if (err == error.EnvironmentVariableNotFound) {
return allocator.dupe(u8, "Unknown");