{ pkgs, lib, ... }:

let

  # TODO: deduplicate, move to some lib
  _tmp_func_name = "_tmp_func_Oaw5aifeeniezeiquonaipheNahthae9caik4Rai"; # pwgen ftw
  mkArgsAlias = alias: ''${_tmp_func_name}() { unset -f ${_tmp_func_name}; ${alias}; }; ${_tmp_func_name}'';
  mkArgsAliasX = alias: ''${_tmp_func_name}() { unset -f ${_tmp_func_name}; (set -x; ${alias}; ); }; ${_tmp_func_name}'';

in

{
  home.packages = with pkgs; [
    bat
    bat-extras.batman
  ];

  # bat - TODO: condition these on programs.bat.enable ?
  home.shellAliases.cat = "bat --style=plain --paging=never";
  home.shellAliases.cata = mkArgsAlias ''printf "echo + "$@"; bat --style plain --paging never %q\n" "$@" | xargs -d'\n' ${lib.getExe pkgs.mprocs}'';
  home.shellAliases.bata = mkArgsAlias ''printf "echo + "$@"; bat --style plain %q\n" "$@" | xargs -d'\n' ${lib.getExe pkgs.mprocs}'';
  home.shellAliases.man = "batman";
  # home.shellAliases.man = "PAGER=${pkgs.most}/bin/most man";
  # home.shellAliases.zbat = mkArgsAliasX ''atool "$1" --cat | bat --file-name "$1"'';

  # TODO: implement programs.bat.enableAliases
  programs.bat = {
    enable = true;
    #config.style = "plain";
    config.theme = "railscast";
    themes.railscast.file = "railscast-textmate.tmtheme";
    themes.railscast.src = pkgs.fetchFromGitHub {
        owner = "jeromedalbert";
        repo = "sublime-text-railscasttextmate-theme";
        rev = "0a4861f35e72f9ad4cc1cac42730ea563be2ffd3"; # 11 years old
        hash = "sha256-1Ml5vqP9r6oCfLhaNGPQJXS8e7utxQQycIm3Hk9wE3w=";
    };
    config.map-syntax = [
      "poetry.toml:TOML"
      "flake.lock:JSON"
    ];
    # only in unstable as of 22.11:
    #extraPackages = with pkgs.bat-extras; [ batdiff batman batgrep batwatch ];
  };

  # TODO: generate bat syntax cache: 'bat cache --build'
  # TODO: JQ_COLORS: https://jqlang.org/manual/#colors
}
