From b7eb400d41244110c539303ef4b2859552405284 Mon Sep 17 00:00:00 2001 From: Matthew Toohey Date: Sat, 10 Dec 2022 15:52:30 -0500 Subject: [PATCH] pistol: refactor This pull requests refactors the pistol configuration options, since the current options are deficient in a few ways. Closes #3486 --- modules/programs/pistol.nix | 70 ++++++++++++++----- .../modules/programs/pistol/associations.nix | 38 ++++++++++ tests/modules/programs/pistol/config.nix | 26 +++---- tests/modules/programs/pistol/default.nix | 7 +- .../programs/pistol/double-association.nix | 18 +++++ .../programs/pistol/missing-association.nix | 14 ++++ 6 files changed, 137 insertions(+), 36 deletions(-) create mode 100644 tests/modules/programs/pistol/associations.nix create mode 100644 tests/modules/programs/pistol/double-association.nix create mode 100644 tests/modules/programs/pistol/missing-association.nix diff --git a/modules/programs/pistol.nix b/modules/programs/pistol.nix index 8d5d6d71c..4d6a1d295 100644 --- a/modules/programs/pistol.nix +++ b/modules/programs/pistol.nix @@ -3,31 +3,55 @@ with lib; let - cfg = config.programs.pistol; - configFile = - concatStringsSep "\n" (mapAttrsToList (k: v: "${k} ${v}") cfg.config); + configFile = concatStringsSep "\n" (map ({ fpath, mime, command }: + if fpath == "" then "${mime} ${command}" else "fpath ${fpath} ${command}") + cfg.associations); + association = types.submodule { + options = { + command = mkOption { + type = types.str; + description = "Preview command for files matched by this association."; + }; + + fpath = mkOption { + type = types.str; + default = ""; + description = "File path regex that this association should match."; + }; + + mime = mkOption { + type = types.str; + default = ""; + description = "Mime type regex that this association should match."; + }; + }; + }; in { + imports = [ + (mkRemovedOptionModule [ "programs" "pistol" "config" ] + "Pistol is now configured with programs.pistol.associations.") + ]; + meta.maintainers = [ hm.maintainers.mtoohey ]; options.programs.pistol = { - enable = mkEnableOption '' - Pistol, a general purpose file previewer designed for terminal file - managers''; + enable = mkEnableOption "file previewer for terminal file managers"; - config = mkOption { - type = with types; attrsOf str; - default = { }; + associations = mkOption { + type = types.listOf association; + default = [ ]; example = literalExpression '' - { - "text/*" = "bat --paging=never --color=always %pistol-filename%"; - "inode/directory" = "ls -l --color %pistol-filename%"; - } + [ + { mime = "application/json"; command = "bat %pistol-filename%"; } + { mime = "application/*"; command = "hexyl %pistol-filename%"; } + { fpath = ".*.md$"; command = "sh: bat --paging=never --color=always %pistol-filename% | head -8"; } + ] ''; description = '' - Pistol configuration written to + Associations written to the Pistol configuration at $XDG_CONFIG_HOME/pistol/pistol.conf. ''; }; @@ -35,14 +59,26 @@ in { }; config = mkIf cfg.enable (mkMerge [ - { home.packages = [ pkgs.pistol ]; } + { + assertions = [{ + assertion = all ({ fpath, mime, ... }: + (fpath != "" && mime == "") || (fpath == "" && mime != "")) + cfg.associations; + message = '' + Each entry in programs.pistol.associations must contain exactly one + of fpath or mime. + ''; + }]; - (mkIf (cfg.config != { } && pkgs.stdenv.hostPlatform.isDarwin) { + home.packages = [ pkgs.pistol ]; + } + + (mkIf (cfg.associations != [ ] && pkgs.stdenv.hostPlatform.isDarwin) { home.file."Library/Application Support/pistol/pistol.conf".text = configFile; }) - (mkIf (cfg.config != { } && !pkgs.stdenv.hostPlatform.isDarwin) { + (mkIf (cfg.associations != [ ] && !pkgs.stdenv.hostPlatform.isDarwin) { xdg.configFile."pistol/pistol.conf".text = configFile; }) ]); diff --git a/tests/modules/programs/pistol/associations.nix b/tests/modules/programs/pistol/associations.nix new file mode 100644 index 000000000..764060ba9 --- /dev/null +++ b/tests/modules/programs/pistol/associations.nix @@ -0,0 +1,38 @@ +{ pkgs, ... }: + +{ + programs.pistol = { + enable = true; + associations = [ + { + mime = "application/json"; + command = "bat %pistol-filename%"; + } + { + mime = "application/*"; + command = "hexyl %pistol-filename%"; + } + { + fpath = ".*.md$"; + command = + "sh: bat --paging=never --color=always %pistol-filename% | head -8"; + } + ]; + }; + + test.stubs.pistol = { }; + + nmt.script = let + expected = builtins.toFile "config-expected" '' + application/json bat %pistol-filename% + application/* hexyl %pistol-filename% + fpath .*.md$ sh: bat --paging=never --color=always %pistol-filename% | head -8''; + path = if pkgs.stdenv.hostPlatform.isDarwin then + "home-files/Library/Application Support/pistol/pistol.conf" + else + "home-files/.config/pistol/pistol.conf"; + in '' + assertFileExists '${path}' + assertFileContent '${path}' '${expected}' + ''; +} diff --git a/tests/modules/programs/pistol/config.nix b/tests/modules/programs/pistol/config.nix index 324304d3c..1999befa6 100644 --- a/tests/modules/programs/pistol/config.nix +++ b/tests/modules/programs/pistol/config.nix @@ -1,12 +1,4 @@ -{ pkgs, ... }: - -let - - expected = builtins.toFile "settings-expected" '' - application/json bat --paging=never --color=always --style=auto --wrap=character --terminal-width=%pistol-extra0% --line-range=1:%pistol-extra1% %pistol-filename% - text/* bat --paging=never --color=always --style=auto --wrap=character --terminal-width=%pistol-extra0% --line-range=1:%pistol-extra1% %pistol-filename%''; - -in { +{ programs.pistol = { enable = true; config = { @@ -19,13 +11,11 @@ in { test.stubs.pistol = { }; - nmt.script = let - path = if pkgs.stdenv.hostPlatform.isDarwin then - "home-files/Library/Application Support/pistol/pistol.conf" - else - "home-files/.config/pistol/pistol.conf"; - in '' - assertFileExists '${path}' - assertFileContent '${path}' '${expected}' - ''; + test.asserts.assertions.expected = [ + (let offendingFile = toString ./config.nix; + in '' + The option definition `programs.pistol.config' in `${offendingFile}' no longer has any effect; please remove it. + Pistol is now configured with programs.pistol.associations. + '') + ]; } diff --git a/tests/modules/programs/pistol/default.nix b/tests/modules/programs/pistol/default.nix index ac147c378..1efcbc6a7 100644 --- a/tests/modules/programs/pistol/default.nix +++ b/tests/modules/programs/pistol/default.nix @@ -1 +1,6 @@ -{ pistol-config = ./config.nix; } +{ + pistol-associations = ./associations.nix; + pistol-config = ./config.nix; + pistol-double-association = ./double-association.nix; + pistol-missing-association = ./missing-association.nix; +} diff --git a/tests/modules/programs/pistol/double-association.nix b/tests/modules/programs/pistol/double-association.nix new file mode 100644 index 000000000..103f4d057 --- /dev/null +++ b/tests/modules/programs/pistol/double-association.nix @@ -0,0 +1,18 @@ +{ + programs.pistol = { + enable = true; + # contains both fpath and mime + associations = [{ + fpath = ".*.md$"; + mime = "application/json"; + command = "bat %pistol-filename%"; + }]; + }; + + test.stubs.pistol = { }; + + test.asserts.assertions.expected = ['' + Each entry in programs.pistol.associations must contain exactly one + of fpath or mime. + '']; +} diff --git a/tests/modules/programs/pistol/missing-association.nix b/tests/modules/programs/pistol/missing-association.nix new file mode 100644 index 000000000..6ea0a4486 --- /dev/null +++ b/tests/modules/programs/pistol/missing-association.nix @@ -0,0 +1,14 @@ +{ + programs.pistol = { + enable = true; + # contains no fpath or mime value + associations = [{ command = "bat %pistol-filename%"; }]; + }; + + test.stubs.pistol = { }; + + test.asserts.assertions.expected = ['' + Each entry in programs.pistol.associations must contain exactly one + of fpath or mime. + '']; +}