summaryrefslogtreecommitdiff
path: root/pkgs/little-things-script/default.nix
blob: dc094e586ead86e2b3aa4596804c7c3605c2734e (about) (plain) (blame)
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
{
  lib,
  stdenv,
  little-things,
  installShellFiles,
  fzf,
}:

stdenv.mkDerivation (finalAttrs: {
  pname = "little-things-script";
  version = "0-unstable-2026-01-12";

  buildInputs = [ little-things ];
  src = little-things;

  buildPhase = ''
    mkdir $out $out/bin
    echo '#!/usr/bin/env bash
    ltb="${little-things}/bin"
    if [ "$#" -lt 1 ]; then
      "$0" __comp__ | ${fzf}/bin/fzf -m | sed "s,^,little-things ,"
      exit 127;
    else
      cmd="$1"
      shift
    fi
    if [ "$cmd" == "__comp__" ]; then
      ls -1 "$ltb"
    elif [ -x "$ltb/$cmd" ]; then
      exec "$ltb/$cmd" "$@"
    else
      echo "$cmd is not executable or not a file" >&2
      exit 127;
    fi' >$out/bin/little-things
    chmod +x $out/bin/little-things
  '';

  nativeBuildInputs = [ installShellFiles ];

  postInstall = ''
    compfuns() {
    echo '_generate_little-things_completions() {
  local idx=$1; shift
  local words=( "$@" )
  local current_word=''${words[idx]}

  local array=()
  while IFS= read -r line; do
    array+=("$line")
  done < <(little-things __comp__)
  for elem in "''${array[@]}"; do
    if [[ $elem == "$current_word"* ]]; then echo "$elem"; fi
  done
}

_complete_little-things_bash() {
  local raw=($(_generate_little-things_completions "$COMP_CWORD" "''${COMP_WORDS[@]}"))
  COMPREPLY=( "''${raw[@]}" )
}

_complete_little-things_zsh() {
  local -a raw
  raw=($(_generate_little-things_completions "$CURRENT" "''${words[@]}"))
  compadd -- $raw
}

if [ -n "''${ZSH_VERSION:-}" ]; then
  autoload -Uz compinit
  compinit
  compdef _complete_little-things_zsh little-things
elif [ -n "''${BASH_VERSION:-}" ]; then
  complete -F _complete_little-things_bash little-things
fi'
    }
    installShellCompletion --cmd little-things \
      --bash <(compfuns) \
      --zsh <(compfuns)
  '';

  meta = little-things.meta;

})