浏览代码

nixpkgs/elvish: initial commit

Thomas Dy 2 年之前
父节点
当前提交
349a0d6b31

+ 25 - 0
.config/nixpkgs/elvish/flake.lock

@@ -0,0 +1,25 @@
+{
+  "nodes": {
+    "nixpkgs": {
+      "locked": {
+        "lastModified": 1662096612,
+        "narHash": "sha256-R+Q8l5JuyJryRPdiIaYpO5O3A55rT+/pItBrKcy7LM4=",
+        "owner": "NixOS",
+        "repo": "nixpkgs",
+        "rev": "21de2b973f9fee595a7a1ac4693efff791245c34",
+        "type": "github"
+      },
+      "original": {
+        "id": "nixpkgs",
+        "type": "indirect"
+      }
+    },
+    "root": {
+      "inputs": {
+        "nixpkgs": "nixpkgs"
+      }
+    }
+  },
+  "root": "root",
+  "version": 7
+}

+ 54 - 0
.config/nixpkgs/elvish/flake.nix

@@ -0,0 +1,54 @@
+{
+  description = "Elvish configuration";
+
+  outputs = { self, nixpkgs }:
+    {
+      packages.x86_64-linux.default =
+        with import nixpkgs {
+          system = "x86_64-linux";
+        };
+        let
+          carapace-bin = buildGoModule rec {
+            pname = "carapace-bin";
+            version = "0.15.0";
+
+            src = fetchFromGitHub {
+              owner = "rsteube";
+              repo = "carapace-bin";
+              rev = "v${version}";
+              sha256 = "0lnsrma6f6s25j21gyn56bsh3yfagb50xsw10sgsw6iczc8ri5fx";
+            };
+
+            ldflags = [ "-s" "-w" ];
+
+            tags = [ "release" ];
+
+            preBuild = ''
+              cd cmd/carapace
+              go generate ./...
+            '';
+
+            vendorSha256 = "sha256-OrbVqCgsVX5b5knN6IdlJBWeGfg2fh09a2xe5+2EGEs=";
+          };
+          config = runCommand "elvish-config" {
+            nativeBuildInputs = [ makeWrapper ];
+          } ''
+            mkdir -p $out/elvish
+
+            cp -r ${./lib} $out/elvish/lib
+
+            cat <<EOF > $out/elvish/rc.elv
+            use config
+            eval (carapace _carapace|slurp)
+            EOF
+          '';
+        in
+        writeScriptBin "elvish" ''
+          #!/bin/sh
+          export XDG_DATA_DIRS="$XDG_DATA_DIRS:${config}"
+          export PATH="$PATH:${carapace-bin}/bin"
+          keyctl new_session >/dev/null
+          exec ${elvish}/bin/elvish -rc "${config}/elvish/rc.elv" "$@"
+        '';
+    };
+}

+ 45 - 0
.config/nixpkgs/elvish/lib/config.elv

@@ -0,0 +1,45 @@
+use prompt
+use nix-shell
+
+# import local.elv if present, for example, from ~/.config/elvish/lib/local.elv
+try {
+  use local
+} catch e {
+}
+
+edit:add-var ls~ { |@a|
+  e:ls --color=auto --group-directories-first -F --quoting-style literal $@a
+}
+
+if (has-external yadm) {
+  set-env YADM_OVERRIDE_REPO ~/.dotfiles
+} else {
+  edit:add-var yadm~ { |@a| git --git-dir=~/.dotfiles/ --work-tree=~ $@a }
+}
+
+if (has-external difft) {
+  set-env GIT_EXTERNAL_DIFF difft
+}
+
+if (has-external delta) {
+  set-env GIT_PAGER delta
+}
+
+# some additional bindings
+use readline-binding
+set edit:completion:binding[Ctrl-W] = $edit:close-mode~
+set edit:completion:binding[Tab] = $edit:completion:accept~
+
+var max-len = 30
+
+# print time if command takes longer than 5 seconds
+set edit:after-command = [{|m|
+  if (> $m[duration] 5) {
+    var cmd = $m[src][code]
+    var len = (count $cmd)
+    if (> $len $max-len) {
+      set cmd = $cmd[..$max-len]…
+    }
+    printf "%s took %.3fs\n" $cmd $m[duration]
+  }
+}]

+ 27 - 0
.config/nixpkgs/elvish/lib/nix-shell.elv

@@ -0,0 +1,27 @@
+use path
+
+edit:add-var nix~ {|@a|
+  var is-shell = (and ^
+    (> (count $a) 0) ^
+    (eq $a[0] "develop") ^
+    (not (has-value $a "--command" )))
+
+  if $is-shell {
+    set a = $a[1..]
+    e:nix develop $@a --command elvish
+  } else {
+    e:nix $@a
+  }
+}
+
+edit:add-var nix-shell~ {|@a|
+  var is-shell = (not (or ^
+    (has-value $a "--command") ^
+    (has-value $a "--run")))
+
+  if $is-shell {
+    e:nix-shell $@a --command elvish
+  } else {
+    e:nix-shell $@a
+  }
+}

+ 163 - 0
.config/nixpkgs/elvish/lib/prompt.elv

@@ -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..]
+}