aboutsummaryrefslogtreecommitdiff
path: root/misc/zsh/run-help-nix
blob: 534b51ec2e87edc9625a36b8b09e91d8068bc157 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
emulate -L zsh

# run-help is a zsh widget that can be bound to a key. It mainly looks up the
# man page for the currently typed in command.
#
# Although run-help works for any command without requiring special support,
# it can only deduce the right man page based solely on the name of the
# command. Programs like Nix provide better integration with run-help by
# helping zsh identify Nix subcommands and their corresponding man pages. This
# is what this function does.
#
# To actually use run-help on zsh, place the following lines in your .zshrc:
#
#     (( $+aliases[run-help] )) && unalias run-help
#     autoload -Uz run-help run-help-nix
#
# Then also assign run-help to any key of choice:
#
#     bindkey '^[h' run-help

if (( $# == 0 )); then
  man nix
  return
fi

while [[ "$#" != 0 && "$1" == -* ]]; do
  shift
done

case "$1" in
  flake)
    case "$2" in
      archive|check|clone|info|init|lock|metadata|new|prefetch|show|update)
        man "nix3-$1-$2" ;;
      *)
        man "nix3-$1" ;;
    esac ;;
  hash)
    case "$2" in
      file|path|to-base16|to-base32|to-base64|to-sri)
        man "nix3-$1-$2" ;;
      *)
        man "nix3-$1" ;;
    esac ;;
  key)
    case "$2" in
      convert-secret-to-public|generate-secret)
        man "nix3-$1-$2" ;;
      *)
        man "nix3-$1" ;;
    esac ;;
  nar)
    case "$2" in
      cat|dump-path|ls)
        man "nix3-$1-$2" ;;
      *)
        man "nix3-$1" ;;
    esac ;;
  profile)
    case "$2" in
      diff-closures|history|install|list|remove|rollback|upgrade|wipe-history)
        man "nix3-$1-$2" ;;
      *)
        man "nix3-$1" ;;
    esac ;;
  realisation)
    case "$2" in
      info)
        man "nix3-$1-$2" ;;
      *)
        man "nix3-$1" ;;
    esac ;;
  registry)
    case "$2" in
      add|list|pin|remove)
        man "nix3-$1-$2" ;;
      *)
        man "nix3-$1" ;;
    esac ;;
  store)
    case "$2" in
      add-file|add-path|cat|copy-sigs|delete|diff-closures|dump-path|gc|ls)
        ;& # fallthrough
      make-content-addressable|optimise|ping|prefetch-file|repair|sign|verify)
        man "nix3-$1-$2" ;;
      *)
        man "nix3-$1" ;;
    esac ;;
  *)
    if man -w "nix3-$1" >/dev/null 2>&1; then
      man "nix3-$1"
    else
      man nix
    fi ;;
esac

return $?