2020-05-29 17:54:38 +02:00
|
|
|
{ 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)
|
2021-11-29 06:05:49 +01:00
|
|
|
" -modules ${valueToString cfg.modules}";
|
2020-05-29 17:54:38 +02:00
|
|
|
|
2021-11-29 06:05:49 +01:00
|
|
|
modulesRightArgument = optionalString (cfg.modulesRight != null)
|
|
|
|
" -modules-right ${valueToString cfg.modulesRight}";
|
|
|
|
|
|
|
|
evalMode = cfg.modulesRight != null;
|
|
|
|
|
|
|
|
evalArgument = optionalString (evalMode) " -eval";
|
|
|
|
|
|
|
|
newlineArgument = optionalString cfg.newline " -newline";
|
2020-05-29 17:54:38 +02:00
|
|
|
|
|
|
|
pathAliasesArgument = optionalString (cfg.pathAliases != null)
|
2021-11-29 06:05:49 +01:00
|
|
|
" -path-aliases ${valueToString cfg.pathAliases}";
|
2020-05-29 17:54:38 +02:00
|
|
|
|
|
|
|
otherSettingPairArgument = name: value:
|
2021-11-29 06:05:49 +01:00
|
|
|
if value == true then " -${name}" else " -${name} ${valueToString value}";
|
2020-05-29 17:54:38 +02:00
|
|
|
|
|
|
|
otherSettingsArgument = optionalString (cfg.settings != { })
|
2021-11-29 06:05:49 +01:00
|
|
|
(concatStringsSep ""
|
2020-05-29 17:54:38 +02:00
|
|
|
(mapAttrsToList otherSettingPairArgument cfg.settings));
|
|
|
|
|
|
|
|
commandLineArguments = ''
|
2021-11-29 06:05:49 +01:00
|
|
|
${evalArgument}${modulesArgument}${modulesRightArgument}${newlineArgument}${pathAliasesArgument}${otherSettingsArgument}
|
2020-05-29 17:54:38 +02:00
|
|
|
'';
|
|
|
|
|
|
|
|
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" ];
|
|
|
|
};
|
|
|
|
|
2021-11-29 06:05:49 +01:00
|
|
|
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" ];
|
|
|
|
};
|
|
|
|
|
2020-05-29 17:54:38 +02:00
|
|
|
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.
|
|
|
|
'';
|
2021-10-09 11:14:08 +02:00
|
|
|
example = literalExpression ''
|
2020-05-29 17:54:38 +02:00
|
|
|
{ "\\~/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"/>.
|
|
|
|
'';
|
2021-10-09 11:14:08 +02:00
|
|
|
example = literalExpression ''
|
2020-05-29 17:54:38 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-11-01 18:27:47 +01:00
|
|
|
config = {
|
|
|
|
programs.bash.initExtra =
|
|
|
|
mkIf (cfg.enable && config.programs.bash.enable) ''
|
|
|
|
function _update_ps1() {
|
|
|
|
local old_exit_status=$?
|
2021-11-29 06:05:49 +01:00
|
|
|
${
|
|
|
|
if evalMode then "eval " else "PS1="
|
|
|
|
}"$(${pkgs.powerline-go}/bin/powerline-go -error $old_exit_status -shell bash${commandLineArguments})"
|
2020-11-01 18:27:47 +01:00
|
|
|
${cfg.extraUpdatePS1}
|
|
|
|
return $old_exit_status
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ "$TERM" != "linux" ]; then
|
|
|
|
PROMPT_COMMAND="_update_ps1;$PROMPT_COMMAND"
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
|
|
|
programs.zsh.initExtra = mkIf (cfg.enable && config.programs.zsh.enable) ''
|
|
|
|
function powerline_precmd() {
|
2021-11-29 06:05:49 +01:00
|
|
|
${
|
|
|
|
if evalMode then "eval " else "PS1="
|
|
|
|
}"$(${pkgs.powerline-go}/bin/powerline-go -error $? -shell zsh${commandLineArguments})"
|
2020-05-29 17:54:38 +02:00
|
|
|
${cfg.extraUpdatePS1}
|
2020-11-01 18:27:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function install_powerline_precmd() {
|
|
|
|
for s in "$\{precmd_functions[@]}"; do
|
|
|
|
if [ "$s" = "powerline_precmd" ]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
precmd_functions+=(powerline_precmd)
|
2020-05-29 17:54:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if [ "$TERM" != "linux" ]; then
|
2020-11-01 18:27:47 +01:00
|
|
|
install_powerline_precmd
|
2020-05-29 17:54:38 +02:00
|
|
|
fi
|
|
|
|
'';
|
2021-08-03 05:16:00 +02:00
|
|
|
|
|
|
|
# https://github.com/justjanne/powerline-go#fish
|
2021-10-29 04:09:20 +02:00
|
|
|
programs.fish.interactiveShellInit =
|
2021-08-03 05:16:00 +02:00
|
|
|
mkIf (cfg.enable && config.programs.fish.enable) ''
|
|
|
|
function fish_prompt
|
2021-11-29 06:05:49 +01:00
|
|
|
eval ${pkgs.powerline-go}/bin/powerline-go -error $status -jobs (count (jobs -p))${commandLineArguments}
|
2021-08-03 05:16:00 +02:00
|
|
|
${cfg.extraUpdatePS1}
|
|
|
|
end
|
|
|
|
'';
|
2020-05-29 17:54:38 +02:00
|
|
|
};
|
|
|
|
}
|