aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/programs/powerline-go.nix
blob: a4cd233cf70e493cdb6d7aaf73f0812ad7ba953b (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.programs.powerline-go;

  # Convert an option value to a string to be passed as argument to
  # powerline-go:
  valueToString = value:
    if builtins.isList value then
      builtins.concatStringsSep "," (builtins.map valueToString value)
    else if builtins.isAttrs value then
      valueToString
      (mapAttrsToList (key: val: "${valueToString key}=${valueToString val}")
        value)
    else
      builtins.toString value;

  modulesArgument = optionalString (cfg.modules != null)
    "-modules ${valueToString cfg.modules}";

  newlineArgument = optionalString cfg.newline "-newline";

  pathAliasesArgument = optionalString (cfg.pathAliases != null)
    "-path-aliases ${valueToString cfg.pathAliases}";

  otherSettingPairArgument = name: value:
    if value == true then "-${name}" else "-${name} ${valueToString value}";

  otherSettingsArgument = optionalString (cfg.settings != { })
    (concatStringsSep " "
      (mapAttrsToList otherSettingPairArgument cfg.settings));

  commandLineArguments = ''
    ${modulesArgument} ${newlineArgument} ${pathAliasesArgument} ${otherSettingsArgument}
  '';

in {
  meta.maintainers = [ maintainers.DamienCassou ];

  options = {
    programs.powerline-go = {
      enable = mkEnableOption
        "Powerline-go, a beautiful and useful low-latency prompt for your shell";

      modules = mkOption {
        default = null;
        type = types.nullOr (types.listOf types.str);
        description = ''
          List of module names to load. The list of all available
          modules as well as the choice of default ones are at
          <link xlink:href="https://github.com/justjanne/powerline-go"/>.
        '';
        example = [ "host" "ssh" "cwd" "gitlite" "jobs" "exit" ];
      };

      newline = mkOption {
        default = false;
        type = types.bool;
        description = ''
          Set to true if the prompt should be on a line of its own.
        '';
        example = true;
      };

      pathAliases = mkOption {
        default = null;
        type = types.nullOr (types.attrsOf types.str);
        description = ''
          Pairs of full-path and corresponding desired short name. You
          may use '~' to represent your home directory but you should
          protect it to avoid shell substitution.
        '';
        example = literalExample ''
          { "\\~/projects/home-manager" = "prj:home-manager"; }
        '';
      };

      settings = mkOption {
        default = { };
        type = with types; attrsOf (oneOf [ bool int str (listOf str) ]);
        description = ''
          This can be any key/value pair as described in
          <link xlink:href="https://github.com/justjanne/powerline-go"/>.
        '';
        example = literalExample ''
          {
            hostname-only-if-ssh = true;
            numeric-exit-codes = true;
            cwd-max-depth = 7;
            ignore-repos = [ "/home/me/big-project" "/home/me/huge-project" ];
          }
        '';
      };

      extraUpdatePS1 = mkOption {
        default = "";
        description = "Shell code to execute after the prompt is set.";
        example = ''
          PS1=$PS1"NixOS> ";
        '';
        type = types.str;
      };
    };
  };

  config = mkIf (cfg.enable && config.programs.bash.enable) {
    programs.bash.initExtra = ''
      function _update_ps1() {
        local old_exit_status=$?
        PS1="$(${pkgs.powerline-go}/bin/powerline-go -error $old_exit_status ${commandLineArguments})"
        ${cfg.extraUpdatePS1}
        return $old_exit_status
      }

      if [ "$TERM" != "linux" ]; then
        PROMPT_COMMAND="_update_ps1;$PROMPT_COMMAND"
      fi
    '';
  };
}