Skip to content

Instantly share code, notes, and snippets.

@rouge8
Created December 30, 2020 19:40
Show Gist options
  • Save rouge8/d6cfdb9e0b75699dfba6ae0a591df6e2 to your computer and use it in GitHub Desktop.
Save rouge8/d6cfdb9e0b75699dfba6ae0a591df6e2 to your computer and use it in GitHub Desktop.
git status prompt in zig vs bash
#!/bin/bash
# Format 'git status' output for use in my prompt
set -eu
untracked=""
modified=""
for line in $(command git status --porcelain=v1 2> /dev/null | command cut -c-2); do
if [[ $line == ?? ]]; then
untracked='?'
else
modified='+'
fi
if [[ -n $untracked ]] && [[ -n $modified ]]; then
break
fi
done
prompt_status="$modified$untracked"
if [[ -n $prompt_status ]]; then
echo $prompt_status
fi
const std = @import("std");
pub fn main() !void {
const argv = [_][]const u8{
"git", "status", "--porcelain",
};
const stdout = std.io.getStdOut().writer();
var result = try std.ChildProcess.exec(.{
.allocator = std.heap.c_allocator,
.argv = &argv,
});
var modified = false;
var untracked = false;
var lines = std.mem.split(result.stdout, "\n");
while (lines.next()) |line| {
if (std.mem.startsWith(u8, line, "??")) {
untracked = true;
} else {
modified = true;
}
if (modified and untracked) {
break;
}
}
if (modified) {
try stdout.print("+", .{});
}
if (untracked) {
try stdout.print("?", .{});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment