|
@@ -0,0 +1,163 @@
|
|
|
+use str
|
|
|
+use re
|
|
|
+
|
|
|
+fn nix-shell-prompt-string {
|
|
|
+ put '['
|
|
|
+ styled nix yellow
|
|
|
+ if (has-env NIX_SHELL_NAME) {
|
|
|
+ put ':'
|
|
|
+ styled $E:NIX_SHELL_NAME green
|
|
|
+ }
|
|
|
+ put ']'
|
|
|
+}
|
|
|
+
|
|
|
+fn k8s-prompt-string {
|
|
|
+ var k8s-config = (kubectl config view -o json | from-json)
|
|
|
+ var k8s-context = $k8s-config[current-context]
|
|
|
+ var k8s-namespace = (each { |ctx| if (eq $ctx[name] $k8s-context) { put $ctx[context][namespace] } } $k8s-config[contexts])
|
|
|
+ put '['
|
|
|
+ styled $k8s-context magenta
|
|
|
+ put '|'
|
|
|
+ styled $k8s-namespace blue
|
|
|
+ put ']'
|
|
|
+}
|
|
|
+
|
|
|
+fn aws-prompt-string {
|
|
|
+ put '['
|
|
|
+ styled $E:AWS_PROFILE yellow
|
|
|
+ put ']'
|
|
|
+}
|
|
|
+
|
|
|
+fn parse-git-branch {
|
|
|
+ try {
|
|
|
+ git symbolic-ref -q HEAD 2> /dev/null
|
|
|
+ } catch e {
|
|
|
+ try {
|
|
|
+ git rev-parse --short HEAD 2> /dev/null
|
|
|
+ } catch e {
|
|
|
+ echo ""
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+fn parse-git-state {
|
|
|
+ var git-revision = ''
|
|
|
+ var git-branch = ''
|
|
|
+ var git-changed = (num 0)
|
|
|
+ var git-staged = (num 0)
|
|
|
+ var git-conflicts = (num 0)
|
|
|
+ var git-untracked = (num 0)
|
|
|
+ var git-ahead = (num 0)
|
|
|
+ var git-behind = (num 0)
|
|
|
+
|
|
|
+ git status --porcelain=v2 -b 2>/dev/null | each {|line|
|
|
|
+ if (eq '#' $line[0]) {
|
|
|
+ try {
|
|
|
+ var match = (re:find '# branch.ab \+(\d+) -(\d+)' $line)
|
|
|
+ set git-ahead = (num $match[groups][1][text])
|
|
|
+ set git-behind = (num $match[groups][2][text])
|
|
|
+ } catch e {
|
|
|
+ # noop
|
|
|
+ }
|
|
|
+
|
|
|
+ if (str:has-prefix $line '# branch.head') {
|
|
|
+ set git-branch = $line[14..]
|
|
|
+ }
|
|
|
+
|
|
|
+ if (str:has-prefix $line '# branch.oid') {
|
|
|
+ set git-revision = $line[13..20]
|
|
|
+ }
|
|
|
+
|
|
|
+ } elif (eq u $line[0]) {
|
|
|
+ set git-conflicts = (+ $git-conflicts 1)
|
|
|
+ } elif (eq '?' $line[0]) {
|
|
|
+ set git-untracked = (+ $git-untracked 1)
|
|
|
+ } else {
|
|
|
+ if (not-eq '.' $line[2]) {
|
|
|
+ set git-staged = (+ $git-staged 1)
|
|
|
+ }
|
|
|
+ if (not-eq '.' $line[3]) {
|
|
|
+ set git-changed = (+ $git-changed 1)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var git-ref = $git-branch
|
|
|
+ var git-ref-prefix = ''
|
|
|
+ if (eq $git-branch '(detached)') {
|
|
|
+ try {
|
|
|
+ var tag @rest = (git tag --points-at HEAD)
|
|
|
+ set git-ref-prefix = '#'
|
|
|
+ set git-ref = $tag
|
|
|
+ } catch e {
|
|
|
+ set git-ref-prefix = '@'
|
|
|
+ set git-ref = $git-revision
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ put '('
|
|
|
+ put $git-ref-prefix
|
|
|
+ styled $git-ref magenta bold
|
|
|
+ if (> $git-ahead 0) {
|
|
|
+ put '^'$git-ahead
|
|
|
+ }
|
|
|
+ if (> $git-behind 0) {
|
|
|
+ put 'v'$git-behind
|
|
|
+ }
|
|
|
+ put '|'
|
|
|
+ if (> $git-staged 0) {
|
|
|
+ styled +$git-staged green
|
|
|
+ }
|
|
|
+ if (> $git-conflicts 0) {
|
|
|
+ styled x$git-conflicts red
|
|
|
+ }
|
|
|
+ if (> $git-changed 0) {
|
|
|
+ styled '*'$git-changed red
|
|
|
+ }
|
|
|
+ if (> $git-untracked 0) {
|
|
|
+ put '..'
|
|
|
+ }
|
|
|
+ if (== 0 $git-changed $git-staged $git-conflicts $git-untracked) {
|
|
|
+ styled ok green bold
|
|
|
+ }
|
|
|
+ put ')'
|
|
|
+}
|
|
|
+
|
|
|
+fn git-prompt-string {
|
|
|
+ try {
|
|
|
+ parse-git-state
|
|
|
+ } catch e {
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+set edit:prompt = {
|
|
|
+ put "\n"
|
|
|
+ styled '['(date +%H:%M)']' bold bright-cyan
|
|
|
+ put ' '
|
|
|
+ styled (tilde-abbr $pwd) yellow
|
|
|
+ put ' '
|
|
|
+ git-prompt-string
|
|
|
+ put "\n% "
|
|
|
+}
|
|
|
+
|
|
|
+set edit:rprompt = {
|
|
|
+ put '['
|
|
|
+ styled elv green
|
|
|
+ put ']'
|
|
|
+ if (has-env IN_NIX_SHELL) {
|
|
|
+ nix-shell-prompt-string
|
|
|
+ }
|
|
|
+ if (has-env IN_K8S) {
|
|
|
+ k8s-prompt-string
|
|
|
+ }
|
|
|
+ if (has-env AWS_PROFILE) {
|
|
|
+ aws-prompt-string
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+# when stale replace clock with --:--
|
|
|
+set edit:prompt-stale-transform = {|x|
|
|
|
+ put $x[0] # newline
|
|
|
+ styled '[--:--]' bright-cyan
|
|
|
+ put $x[2..]
|
|
|
+}
|