1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-26 21:19:45 +01:00

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; with lib;
let let
cfg = config.programs.pistol; cfg = config.programs.pistol;
configFile = configFile = concatStringsSep "\n" (map ({ fpath, mime, command }:
concatStringsSep "\n" (mapAttrsToList (k: v: "${k} ${v}") cfg.config); 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 { in {
imports = [
(mkRemovedOptionModule [ "programs" "pistol" "config" ]
"Pistol is now configured with programs.pistol.associations.")
];
meta.maintainers = [ hm.maintainers.mtoohey ]; meta.maintainers = [ hm.maintainers.mtoohey ];
options.programs.pistol = { options.programs.pistol = {
enable = mkEnableOption '' enable = mkEnableOption "file previewer for terminal file managers";
Pistol, a general purpose file previewer designed for terminal file
managers'';
config = mkOption { associations = mkOption {
type = with types; attrsOf str; type = types.listOf association;
default = { }; default = [ ];
example = literalExpression '' example = literalExpression ''
{ [
"text/*" = "bat --paging=never --color=always %pistol-filename%"; { mime = "application/json"; command = "bat %pistol-filename%"; }
"inode/directory" = "ls -l --color %pistol-filename%"; { mime = "application/*"; command = "hexyl %pistol-filename%"; }
} { fpath = ".*.md$"; command = "sh: bat --paging=never --color=always %pistol-filename% | head -8"; }
]
''; '';
description = '' description = ''
Pistol configuration written to Associations written to the Pistol configuration at
<filename>$XDG_CONFIG_HOME/pistol/pistol.conf</filename>. <filename>$XDG_CONFIG_HOME/pistol/pistol.conf</filename>.
''; '';
}; };
@ -35,14 +59,26 @@ in {
}; };
config = mkIf cfg.enable (mkMerge [ 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 = home.file."Library/Application Support/pistol/pistol.conf".text =
configFile; configFile;
}) })
(mkIf (cfg.config != { } && !pkgs.stdenv.hostPlatform.isDarwin) { (mkIf (cfg.associations != [ ] && !pkgs.stdenv.hostPlatform.isDarwin) {
xdg.configFile."pistol/pistol.conf".text = configFile; 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 = { programs.pistol = {
enable = true; enable = true;
config = { config = {
@ -19,13 +11,11 @@ in {
test.stubs.pistol = { }; test.stubs.pistol = { };
nmt.script = let test.asserts.assertions.expected = [
path = if pkgs.stdenv.hostPlatform.isDarwin then (let offendingFile = toString ./config.nix;
"home-files/Library/Application Support/pistol/pistol.conf" in ''
else The option definition `programs.pistol.config' in `${offendingFile}' no longer has any effect; please remove it.
"home-files/.config/pistol/pistol.conf"; Pistol is now configured with programs.pistol.associations.
in '' '')
assertFileExists '${path}' ];
assertFileContent '${path}' '${expected}'
'';
} }

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