tenseleyflow/ndotfiles / 5ec9dd9

Browse files

some fish functions

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
5ec9dd9a6717b68363732da8a6210ce52fe1e560
Parents
dc10159
Tree
ed41af3

4 changed files

StatusFile+-
A fish/functions/glog.fish 11 0
A fish/functions/sshf.fish 24 0
A fish/functions/sysrs.fish 8 0
A fish/functions/sysru.fish 8 0
fish/functions/glog.fishadded
@@ -0,0 +1,11 @@
1
+## browse commits, preview diff, enter for full patch
2
+#
3
+function glog
4
+    command git rev-parse --git-dir >/dev/null ^/dev/null; or begin; echo "not a git repo"; return 1; end
5
+    set -l sel (git log --graph --date=short --pretty=format:'%C(auto)%h %ad %d %s %C(blue)%an%Creset' \
6
+        | fzf --ansi --no-sort --reverse --tiebreak=index \
7
+              --preview 'git show --color=always (echo {} | awk "{print \$1}")')
8
+    test -n "$sel"; or return
9
+    set -l sha (echo $sel | awk '{print $1}')
10
+    git show --stat --patch --color=always $sha | less -R
11
+end
fish/functions/sshf.fishadded
@@ -0,0 +1,24 @@
1
+## pick a host from ~/.ssh/config and tailscale peers
2
+#
3
+function sshf
4
+    set -l cfghosts (awk '/^Host[[:space:]]+/ {for (i=2;i<=NF;i++) if ($i !~ /[*?]/) print $i}' ~/.ssh/config 2>/dev/null)
5
+    for f in ~/.ssh/config.d/*.conf
6
+        test -r $f; and set cfghosts $cfghosts (awk '/^Host[[:space:]]+/ {for (i=2;i<=NF;i++) if ($i !~ /[*?]/) print $i}' $f)
7
+    end
8
+    set -l lines
9
+    for h in $cfghosts; set lines $lines $h; end
10
+    if type -q tailscale; and type -q jq
11
+        set -l ts (tailscale status --json | jq -r '.Peer[] | "\(.HostName)\t\(.TailscaleIPs[0])"')
12
+        set lines $lines $ts
13
+    end
14
+    test (count $lines) -gt 0; or begin; echo "no hosts found"; return 1; end
15
+    set -l sel (printf '%s\n' $lines | column -t -s \t | fzf --prompt="ssh> " --preview 'echo {}')
16
+    test -n "$sel"; or return
17
+    set -l last (echo $sel | awk '{print $NF}')
18
+    if string match -qr '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' -- $last
19
+        set host $last
20
+    else
21
+        set host (echo $sel | awk '{print $1}')
22
+    end
23
+    ssh $host
24
+end
fish/functions/sysrs.fishadded
@@ -0,0 +1,8 @@
1
+## restart a system service
2
+#
3
+function sysrs
4
+    set -l unit (systemctl list-units --type=service --no-legend --no-pager \
5
+        | awk '{print $1}' | fzf --preview 'systemctl status {1} --no-pager' --preview-window=right,70%)
6
+    test -n "$unit"; or return
7
+    sudo systemctl restart $unit
8
+end
fish/functions/sysru.fishadded
@@ -0,0 +1,8 @@
1
+## restart a user service
2
+#
3
+function sysru
4
+    set -l unit (systemctl --user list-units --type=service --no-legend --no-pager \
5
+        | awk '{print $1}' | fzf --preview 'systemctl --user status {1} --no-pager' --preview-window=right,70%)
6
+    test -n "$unit"; or return
7
+    systemctl --user restart $unit
8
+end