modules/terminal: implement termux colorschemes (colors.properties)

This commit is contained in:
Socks Candy 2023-11-26 17:16:07 +10:00 committed by Alexander Sosedkin
parent 6a0167241b
commit f05bfc132d

View file

@ -1,36 +1,47 @@
# Copyright (c) 2019-2022, see AUTHORS. Licensed under MIT License, see LICENSE.
{ config, lib, ... }:
with lib;
let
# Copyright (c) 2019-2024, see AUTHORS. Licensed under MIT License, see LICENSE.
{ config
, lib
, pkgs
, ...
}:
with lib; let
cfg = config.terminal;
in
{
###### interface
options = {
terminal.font = mkOption {
default = null;
type = types.nullOr types.path;
example = lib.literalExpression
''"''${pkgs.terminus_font_ttf}/share/fonts/truetype/TerminusTTF.ttf"'';
description = ''
Font used for the terminal.
'';
terminal = {
font = mkOption {
default = null;
type = types.nullOr types.path;
example =
lib.literalExpression
''"''${pkgs.terminus_font_ttf}/share/fonts/truetype/TerminusTTF.ttf"'';
description = ''
Font used for the terminal.
'';
};
colors = mkOption {
default = { };
type = types.lazyAttrsOf types.str;
example = lib.literalExpression ''
{
background = "#000000";
foreground = "#FFFFFF";
cursor = "#FFFFFF";
}
'';
description = ''
Colorscheme used for the terminal.
'';
};
};
};
###### implementation
config = {
build.activation =
let
fontPath =
@ -40,9 +51,20 @@ in
configDir = "${config.user.home}/.termux";
fontTarget = "${configDir}/font.ttf";
fontBackup = "${configDir}/font.ttf.bak";
inherit (lib.generators) toKeyValue;
colors = pkgs.writeTextFile {
name = "colors.properties";
text = toKeyValue { } cfg.colors;
};
colorsTarget = "${configDir}/colors.properties";
colorsBackup = "${configDir}/colors.properties.bak";
colorsPath = "${config.build.installationDir}/${colors}";
in
if (cfg.font != null) then
{
(
if (cfg.font != null)
then {
linkFont = ''
$DRY_RUN_CMD mkdir $VERBOSE_ARG -p "${configDir}"
if [ -e "${fontTarget}" ] && ! [ -L "${fontTarget}" ]; then
@ -52,8 +74,7 @@ in
$DRY_RUN_CMD ln $VERBOSE_ARG -sf "${fontPath}" "${fontTarget}"
'';
}
else
{
else {
unlinkFont = ''
if [ -e "${fontTarget}" ] && [ -L "${fontTarget}" ]; then
$DRY_RUN_CMD rm $VERBOSE_ARG "${fontTarget}"
@ -68,6 +89,36 @@ in
fi
fi
'';
};
}
)
// (
if (cfg.colors != { })
then {
linkColors = ''
$DRY_RUN_CMD mkdir $VERBOSE_ARG -p "${configDir}"
if [ -e "${colorsTarget}" ] && ! [ -L "${colorsTarget}" ]; then
$DRY_RUN_CMD mv $VERBOSE_ARG "${colorsTarget}" "${colorsBackup}"
$DRY_RUN_CMD echo "${colorsTarget} has been moved to ${colorsBackup}"
fi
$DRY_RUN_CMD ln $VERBOSE_ARG -sf "${colorsPath}" "${colorsTarget}"
'';
}
else {
unlinkColors = ''
if [ -e "${colorsTarget}" ] && [ -L "${colorsTarget}" ]; then
$DRY_RUN_CMD rm $VERBOSE_ARG "${colorsTarget}"
if [ -e "${colorsBackup}" ]; then
$DRY_RUN_CMD mv $VERBOSE_ARG "${colorsBackup}" "${colorsTarget}"
$DRY_RUN_CMD echo "${colorsTarget} has been restored from backup"
else
if $DRY_RUN_CMD rm $VERBOSE_ARG -d "${configDir}" 2>/dev/null
then
$DRY_RUN_CMD echo "removed empty ${configDir}"
fi
fi
fi
'';
}
);
};
}