From f05bfc132d7d20e02f90c2d963f47dcb8abf0ef4 Mon Sep 17 00:00:00 2001 From: Socks Candy Date: Sun, 26 Nov 2023 17:16:07 +1000 Subject: [PATCH] modules/terminal: implement termux colorschemes (colors.properties) --- modules/terminal.nix | 103 ++++++++++++++++++++++++++++++++----------- 1 file changed, 77 insertions(+), 26 deletions(-) diff --git a/modules/terminal.nix b/modules/terminal.nix index 21d1592..16d8cbb 100644 --- a/modules/terminal.nix +++ b/modules/terminal.nix @@ -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 + ''; + } + ); }; }