pistol: refactor

This pull requests refactors the pistol configuration options, since
the current options are deficient in a few ways.

Closes #3486
This commit is contained in:
Matthew Toohey 2022-12-10 15:52:30 -05:00 committed by Robert Helgesson
parent d744c2bb9b
commit b7eb400d41
No known key found for this signature in database
GPG Key ID: 36BDAA14C2797E89
6 changed files with 137 additions and 36 deletions

View File

@ -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
<filename>$XDG_CONFIG_HOME/pistol/pistol.conf</filename>.
'';
};
@ -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;
})
]);

View File

@ -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}'
'';
}

View File

@ -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.
'')
];
}

View File

@ -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;
}

View File

@ -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.
''];
}

View File

@ -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.
''];
}