From f46972e46676886423b8336c30bd0f1027fa87af Mon Sep 17 00:00:00 2001 From: Benoit Louy Date: Mon, 29 Nov 2021 00:05:49 -0500 Subject: [PATCH] powerline-go: add support for -modules-right Add modulesRight setting to instruct powerline-go to configure right side prompt. Use eval mode when this setting is set. --- modules/programs/powerline-go.nix | 41 +++++++++++++++---- tests/modules/programs/powerline-go/bash.nix | 5 ++- .../powerline-go/bashmodulesright.nix | 34 +++++++++++++++ .../modules/programs/powerline-go/default.nix | 2 + tests/modules/programs/powerline-go/zsh.nix | 3 ++ .../programs/powerline-go/zshmodulesright.nix | 37 +++++++++++++++++ 6 files changed, 112 insertions(+), 10 deletions(-) create mode 100644 tests/modules/programs/powerline-go/bashmodulesright.nix create mode 100644 tests/modules/programs/powerline-go/zshmodulesright.nix diff --git a/modules/programs/powerline-go.nix b/modules/programs/powerline-go.nix index aed8449af..ec4e45c27 100644 --- a/modules/programs/powerline-go.nix +++ b/modules/programs/powerline-go.nix @@ -19,22 +19,29 @@ let builtins.toString value; modulesArgument = optionalString (cfg.modules != null) - "-modules ${valueToString cfg.modules}"; + " -modules ${valueToString cfg.modules}"; - newlineArgument = optionalString cfg.newline "-newline"; + modulesRightArgument = optionalString (cfg.modulesRight != null) + " -modules-right ${valueToString cfg.modulesRight}"; + + evalMode = cfg.modulesRight != null; + + evalArgument = optionalString (evalMode) " -eval"; + + newlineArgument = optionalString cfg.newline " -newline"; pathAliasesArgument = optionalString (cfg.pathAliases != null) - "-path-aliases ${valueToString cfg.pathAliases}"; + " -path-aliases ${valueToString cfg.pathAliases}"; otherSettingPairArgument = name: value: - if value == true then "-${name}" else "-${name} ${valueToString value}"; + if value == true then " -${name}" else " -${name} ${valueToString value}"; otherSettingsArgument = optionalString (cfg.settings != { }) - (concatStringsSep " " + (concatStringsSep "" (mapAttrsToList otherSettingPairArgument cfg.settings)); commandLineArguments = '' - ${modulesArgument} ${newlineArgument} ${pathAliasesArgument} ${otherSettingsArgument} + ${evalArgument}${modulesArgument}${modulesRightArgument}${newlineArgument}${pathAliasesArgument}${otherSettingsArgument} ''; in { @@ -56,6 +63,18 @@ in { example = [ "host" "ssh" "cwd" "gitlite" "jobs" "exit" ]; }; + modulesRight = mkOption { + default = null; + type = types.nullOr (types.listOf types.str); + description = '' + List of module names to load to be displayed on the right side. + Currently not supported by bash. Specifying a value for this + option will force powerline-go to use the eval format to set + the prompt. + ''; + example = [ "host" "venv" "git" ]; + }; + newline = mkOption { default = false; type = types.bool; @@ -111,7 +130,9 @@ in { mkIf (cfg.enable && config.programs.bash.enable) '' function _update_ps1() { local old_exit_status=$? - PS1="$(${pkgs.powerline-go}/bin/powerline-go -error $old_exit_status ${commandLineArguments})" + ${ + if evalMode then "eval " else "PS1=" + }"$(${pkgs.powerline-go}/bin/powerline-go -error $old_exit_status -shell bash${commandLineArguments})" ${cfg.extraUpdatePS1} return $old_exit_status } @@ -123,7 +144,9 @@ in { programs.zsh.initExtra = mkIf (cfg.enable && config.programs.zsh.enable) '' function powerline_precmd() { - PS1="$(${pkgs.powerline-go}/bin/powerline-go -error $? -shell zsh ${commandLineArguments})" + ${ + if evalMode then "eval " else "PS1=" + }"$(${pkgs.powerline-go}/bin/powerline-go -error $? -shell zsh${commandLineArguments})" ${cfg.extraUpdatePS1} } @@ -145,7 +168,7 @@ in { programs.fish.interactiveShellInit = mkIf (cfg.enable && config.programs.fish.enable) '' function fish_prompt - eval ${pkgs.powerline-go}/bin/powerline-go -error $status -jobs (count (jobs -p)) ${commandLineArguments} + eval ${pkgs.powerline-go}/bin/powerline-go -error $status -jobs (count (jobs -p))${commandLineArguments} ${cfg.extraUpdatePS1} end ''; diff --git a/tests/modules/programs/powerline-go/bash.nix b/tests/modules/programs/powerline-go/bash.nix index 34017d884..a48e16fe0 100644 --- a/tests/modules/programs/powerline-go/bash.nix +++ b/tests/modules/programs/powerline-go/bash.nix @@ -24,7 +24,10 @@ with lib; assertFileExists home-files/.bashrc assertFileContains \ home-files/.bashrc \ - '/bin/powerline-go -error $old_exit_status -modules nix-shell -newline -path-aliases \~/project/foo=prj-foo -ignore-repos /home/me/project1,/home/me/project2' + 'PS1=' + assertFileContains \ + home-files/.bashrc \ + '/bin/powerline-go -error $old_exit_status -shell bash -modules nix-shell -newline -path-aliases \~/project/foo=prj-foo -ignore-repos /home/me/project1,/home/me/project2' ''; }; } diff --git a/tests/modules/programs/powerline-go/bashmodulesright.nix b/tests/modules/programs/powerline-go/bashmodulesright.nix new file mode 100644 index 000000000..688c0fad7 --- /dev/null +++ b/tests/modules/programs/powerline-go/bashmodulesright.nix @@ -0,0 +1,34 @@ +{ config, lib, pkgs, ... }: + +with lib; + +{ + config = { + programs = { + bash.enable = true; + + powerline-go = { + enable = true; + newline = true; + modules = [ "nix-shell" ]; + modulesRight = [ "git" ]; + pathAliases = { "\\~/project/foo" = "prj-foo"; }; + settings = { + ignore-repos = [ "/home/me/project1" "/home/me/project2" ]; + }; + }; + }; + + test.stubs.powerline-go = { }; + + nmt.script = '' + assertFileExists home-files/.bashrc + assertFileContains \ + home-files/.bashrc \ + 'eval' + assertFileContains \ + home-files/.bashrc \ + '/bin/powerline-go -error $old_exit_status -shell bash -eval -modules nix-shell -modules-right git -newline -path-aliases \~/project/foo=prj-foo -ignore-repos /home/me/project1,/home/me/project2' + ''; + }; +} diff --git a/tests/modules/programs/powerline-go/default.nix b/tests/modules/programs/powerline-go/default.nix index 49ed71d26..45ab3d2d0 100644 --- a/tests/modules/programs/powerline-go/default.nix +++ b/tests/modules/programs/powerline-go/default.nix @@ -2,4 +2,6 @@ powerline-go-bash = ./bash.nix; powerline-go-zsh = ./zsh.nix; powerline-go-fish = ./fish.nix; + powerline-go-bashmodulesright = ./bashmodulesright.nix; + powerline-go-zshmodulesright = ./zshmodulesright.nix; } diff --git a/tests/modules/programs/powerline-go/zsh.nix b/tests/modules/programs/powerline-go/zsh.nix index 161499c9a..21856e9d1 100644 --- a/tests/modules/programs/powerline-go/zsh.nix +++ b/tests/modules/programs/powerline-go/zsh.nix @@ -25,6 +25,9 @@ with lib; nmt.script = '' assertFileExists home-files/.zshrc + assertFileContains \ + home-files/.zshrc \ + 'PS1=' assertFileContains \ home-files/.zshrc \ '/bin/powerline-go -error $? -shell zsh -modules nix-shell -newline -path-aliases \~/project/foo=prj-foo -ignore-repos /home/me/project1,/home/me/project2' diff --git a/tests/modules/programs/powerline-go/zshmodulesright.nix b/tests/modules/programs/powerline-go/zshmodulesright.nix new file mode 100644 index 000000000..ffe2e1d28 --- /dev/null +++ b/tests/modules/programs/powerline-go/zshmodulesright.nix @@ -0,0 +1,37 @@ +{ config, lib, pkgs, ... }: + +with lib; + +{ + config = { + programs = { + zsh.enable = true; + + powerline-go = { + enable = true; + newline = true; + modules = [ "nix-shell" ]; + modulesRight = [ "git" ]; + pathAliases = { "\\~/project/foo" = "prj-foo"; }; + settings = { + ignore-repos = [ "/home/me/project1" "/home/me/project2" ]; + }; + }; + }; + + test.stubs = { + powerline-go = { }; + zsh = { }; + }; + + nmt.script = '' + assertFileExists home-files/.zshrc + assertFileContains \ + home-files/.zshrc \ + 'eval' + assertFileContains \ + home-files/.zshrc \ + '/bin/powerline-go -error $? -shell zsh -eval -modules nix-shell -modules-right git -newline -path-aliases \~/project/foo=prj-foo -ignore-repos /home/me/project1,/home/me/project2' + ''; + }; +}