Skip to content

Instantly share code, notes, and snippets.

@mjhanninen
Last active April 24, 2018 10:59
Show Gist options
  • Save mjhanninen/405fc35af6f0e824a10b6fa5c020042b to your computer and use it in GitHub Desktop.
Save mjhanninen/405fc35af6f0e824a10b6fa5c020042b to your computer and use it in GitHub Desktop.
Soija's prompt
#!/usr/bin/perl
use strict;
my %fg = ();
my %bg = ();
for my $i (0 .. 255) {
$fg{$i} = "[38;5;${i}m";
$bg{$i} = "[48;5;${i}m";
}
# my $info_color = $fg{45};
# my $emph_color = $fg{214};
# my $low_details = $fg{240};
# my $warn_color = $fg{196};
# my $reset_color = "";
my $info_color = $fg{240}; # Gray 62
my $emph_color = $fg{124}; # Indian red
my $warn_color = $fg{196};
my $reset_color = "";
# ------------------------------------------------------------------------------
# Default status line
# ------------------------------------------------------------------------------
my $user = $ENV{USER};
my $hostname = `hostname`;
chomp $hostname;
# Present working directory with home part stripped away
my $pwd = $ENV{PWD};
$pwd =~ s/^$ENV{HOME}/~/;
# The user@host:path line
print "${info_color}${user}\@${hostname}:${emph_color}${pwd}";
# ------------------------------------------------------------------------------
# Git status line
# ------------------------------------------------------------------------------
my $embedded_env = $ENV{EMBEDDED_ENV};
if ($embedded_env) {
print "\n${info_color}env: ${emph_color}${embedded_env}"
}
# ------------------------------------------------------------------------------
# Git status line
# ------------------------------------------------------------------------------
sub parse_branchinfo
{
my $branchinfo = shift;
my $branch;
my $tracked;
my $ahead = 0;
my $behind = 0;
if ($branchinfo =~ m/^## (.*?)(?:\.{3}(.*?)(?: \[(.*)\])?)?$/) {
$branch = $1;
$tracked = $2;
my $diff = $3;
$ahead = $1 if $diff =~ m/ahead (\d+)/;
$behind = $1 if $diff =~ m/behind (\d+)/;
}
return ($branch, $tracked, $ahead, $behind);
}
# Git status line
my $git_root = `git rev-parse --show-toplevel 2>&1`;
if ($? == 0) {
chomp $git_root;
$git_root =~ s/^$ENV{HOME}/~/;
my $branch = undef;
my $tracked = undef;
my $ahead = undef;
my $behind = undef;
my %counts = {};
# Requires Git version 1.8
#my $pid = open(GIT, "git status --short --branch |");
my $pid = open(GIT, "git status --branch --porcelain |");
while (<GIT>) {
($branch, $tracked, $ahead, $behind) = parse_branchinfo $_ if m/^## /;
$counts{$1}++ if m/^([ ACDMR?][ ADMU?]) /;
}
close(GIT);
print "\n${info_color}git: ";
if (defined($branch)) {
print "${emph_color}${branch}${info_color}";
if (defined($tracked)) {
print " (${emph_color}${tracked}${info_color}";
if ($ahead + $behind) {
print ", ";
if ($ahead) {
print "${emph_color}${ahead} ahead${info_color}";
}
if ($ahead * $behind) {
print ", ";
}
if ($behind) {
print "${emph_color}${behind} behind${info_color}";
}
}
print ")";
}
}
else {
print "${warn_color}(no branch)";
}
my $git_repo_basename = $git_root;
$git_repo_basename =~ s#^.*/##;
print "${info_color} in ${emph_color}${git_repo_basename}";
my @rules = (
["\\?\\?", "untracked"],
["[MARC][ MD]", "staged"],
["D[ M]", "staged"],
["[ MARC][MD]", "touched"],
["DM", "touched"],
);
my @status = ();
my %aggr = ();
foreach my $r (@rules) {
my ($pat, $name) = @$r;
foreach my $k (keys(%counts)) {
if ($k =~ m/^$pat/) {
$aggr{$name} += $counts{$k};
}
}
}
my @groups = (
"staged",
"touched",
"untracked",
"other",
);
foreach my $group (@groups) {
push @status, "${aggr{$group}} ${group}" if $aggr{$group} > 0;
}
print "${info_color}";
if (@status) {
print " with ${emph_color}";
print join("${info_color}, ${emph_color}", @status) if @status;
}
}
# ------------------------------------------------------------------------------
# The prompt
# ------------------------------------------------------------------------------
# The prompt line. Note that the ANSI escape sequence is given at the end of
# the previous line in order not to confuse the Bash readline.
print "${reset_color}\n";
print "\$ ";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment