summaryrefslogtreecommitdiff
path: root/modules/mac/programs/fish.nix
blob: 50bc8372ae76eead18d4ea47edcdc7913a7012da (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
83
84
85
{
  programs.fish = {
    enable = true;
    shellInit = "set -g fish_greeting";

    # https://github.com/arbourd/prompt
    promptInit = ''
      function fish_prompt -d "Prints left prompt"
        set -l last_status  $status
        set -l glyph        "\$"
        set -l glyph_color  (set_color normal)
        set -l pwd          (prompt_pwd)
        set -l pwd_color    (set_color blue)

        if test (id -u "$USER") -eq 0
          set glyph "#"
        end

        if test "$last_status" -ne 0
          set pwd_color (set_color red)
        end

        if command git rev-parse --git-dir > /dev/null 2>/dev/null
          if ! command git diff --cached --no-ext-diff --quiet --exit-code 2>/dev/null
            set glyph_color (set_color green)
          else if command git rev-parse --verify --quiet refs/stash > /dev/null 2>/dev/null
            set glyph_color (set_color yellow)
          end
        end

        printf "$pwd_color$pwd $glyph_color$glyph "
      end

      function fish_right_prompt -d "Prints right prompt"
        function __git_branch_name
          command git symbolic-ref --short HEAD 2>/dev/null \
            || command git describe --tags --exact-match HEAD 2>/dev/null \
            || command git rev-parse --short HEAD 2>/dev/null
        end

        function __git_changed
          ! command git diff-index --quiet HEAD 2>/dev/null \
            || count (command git ls-files --others --exclude-standard) >/dev/null
        end

        function __git_modified_files
          count (command git status --porcelain --ignore-submodules 2>/dev/null)
        end

        function __git_symbol
          command git rev-list --count --left-right @{upstream}...@ 2>/dev/null | read behind ahead

          switch "$behind $ahead"
            case " " "0 0"
            case "0 *"
              printf " ↑"
            case "* 0"
              printf " ↓"
            case "*"
              printf " ⥄"
          end
        end

        if set branch_name (__git_branch_name)
          set -l git_symbol (__git_symbol)
          set -l git_color
          set -l git_modified_files (__git_modified_files)
          set -l git_changed_files

          if __git_changed
            set git_color (set_color red)
          else
            set git_color (set_color cyan)
          end

          if test "$git_modified_files" -gt 0
            set git_changed_files (set_color yellow)"$git_modified_files changed "
          end

          printf "$git_changed_files$git_color$branch_name$git_symbol"
        end
      end
    '';
  };
}