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. # Copyright (c) 2019-2024, see AUTHORS. Licensed under MIT License, see LICENSE.
{ config
{ config, lib, ... }: , lib
, pkgs
with lib; , ...
}:
let with lib; let
cfg = config.terminal; cfg = config.terminal;
in in
{ {
###### interface ###### interface
options = { options = {
terminal = {
terminal.font = mkOption { font = mkOption {
default = null; default = null;
type = types.nullOr types.path; type = types.nullOr types.path;
example = lib.literalExpression example =
lib.literalExpression
''"''${pkgs.terminus_font_ttf}/share/fonts/truetype/TerminusTTF.ttf"''; ''"''${pkgs.terminus_font_ttf}/share/fonts/truetype/TerminusTTF.ttf"'';
description = '' description = ''
Font used for the terminal. 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 ###### implementation
config = { config = {
build.activation = build.activation =
let let
fontPath = fontPath =
@ -40,9 +51,20 @@ in
configDir = "${config.user.home}/.termux"; configDir = "${config.user.home}/.termux";
fontTarget = "${configDir}/font.ttf"; fontTarget = "${configDir}/font.ttf";
fontBackup = "${configDir}/font.ttf.bak"; 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 in
if (cfg.font != null) then (
{ if (cfg.font != null)
then {
linkFont = '' linkFont = ''
$DRY_RUN_CMD mkdir $VERBOSE_ARG -p "${configDir}" $DRY_RUN_CMD mkdir $VERBOSE_ARG -p "${configDir}"
if [ -e "${fontTarget}" ] && ! [ -L "${fontTarget}" ]; then if [ -e "${fontTarget}" ] && ! [ -L "${fontTarget}" ]; then
@ -52,8 +74,7 @@ in
$DRY_RUN_CMD ln $VERBOSE_ARG -sf "${fontPath}" "${fontTarget}" $DRY_RUN_CMD ln $VERBOSE_ARG -sf "${fontPath}" "${fontTarget}"
''; '';
} }
else else {
{
unlinkFont = '' unlinkFont = ''
if [ -e "${fontTarget}" ] && [ -L "${fontTarget}" ]; then if [ -e "${fontTarget}" ] && [ -L "${fontTarget}" ]; then
$DRY_RUN_CMD rm $VERBOSE_ARG "${fontTarget}" $DRY_RUN_CMD rm $VERBOSE_ARG "${fontTarget}"
@ -68,6 +89,36 @@ in
fi fi
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
'';
}
);
}; };
} }