From 3af19028cee81d90dff06186dafd356704ff58c3 Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Sat, 28 Oct 2023 12:49:49 +0200 Subject: [PATCH] Add themeswitch script --- nixos/flake.lock | 24 +++++------ scripts/toggle-light-dark.sh | 80 ++++++++++++++++++++++++++++++++++++ stow/vim/.vimrc | 2 +- 3 files changed, 93 insertions(+), 13 deletions(-) create mode 100755 scripts/toggle-light-dark.sh diff --git a/nixos/flake.lock b/nixos/flake.lock index ba37cbe..7bced25 100644 --- a/nixos/flake.lock +++ b/nixos/flake.lock @@ -8,11 +8,11 @@ "systems": "systems" }, "locked": { - "lastModified": 1695973661, - "narHash": "sha256-BP2H4c42GThPIhERtTpV1yCtwQHYHEKdRu7pjrmQAwo=", + "lastModified": 1698410321, + "narHash": "sha256-MphuSlgpmKwtJncGMohryHiK55J1n6WzVQ/OAfmfoMc=", "owner": "numtide", "repo": "devshell", - "rev": "cd4e2fda3150dd2f689caeac07b7f47df5197c31", + "rev": "1aed986e3c81a4f6698e85a7452cbfcc4b31a36e", "type": "github" }, "original": { @@ -62,11 +62,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1697851979, - "narHash": "sha256-lJ8k4qkkwdvi+t/Xc6Fn74kUuobpu9ynPGxNZR6OwoA=", + "lastModified": 1698288402, + "narHash": "sha256-jIIjApPdm+4yt8PglX8pUOexAdEiAax/DXW3S/Mb21E=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "5550a85a087c04ddcace7f892b0bdc9d8bb080c8", + "rev": "60b9db998f71ea49e1a9c41824d09aa274be1344", "type": "github" }, "original": { @@ -93,11 +93,11 @@ }, "nixpkgs-unstable": { "locked": { - "lastModified": 1697723726, - "narHash": "sha256-SaTWPkI8a5xSHX/rrKzUe+/uVNy6zCGMXgoeMb7T9rg=", + "lastModified": 1698318101, + "narHash": "sha256-gUihHt3yPD7bVqg+k/UVHgngyaJ3DMEBchbymBMvK1E=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "7c9cc5a6e5d38010801741ac830a3f8fd667a7a0", + "rev": "63678e9f3d3afecfeafa0acead6239cdb447574c", "type": "github" }, "original": { @@ -125,11 +125,11 @@ "nixpkgs-stable": "nixpkgs-stable" }, "locked": { - "lastModified": 1697943852, - "narHash": "sha256-DaBxUPaZhQ3yLCmAATshYB7qo7NwcMvSFWz9T3bjYYY=", + "lastModified": 1698273636, + "narHash": "sha256-swsqg/ckSVJnravx7ie9NFQSKIH27owtlk0wh4+xStk=", "owner": "Mic92", "repo": "sops-nix", - "rev": "30a0ba4a20703b4bfe047fe5def1fc24978e322c", + "rev": "014e44d334a39481223a5d163530d4c4ca2e75cb", "type": "github" }, "original": { diff --git a/scripts/toggle-light-dark.sh b/scripts/toggle-light-dark.sh new file mode 100755 index 0000000..7ba6e5c --- /dev/null +++ b/scripts/toggle-light-dark.sh @@ -0,0 +1,80 @@ +#!/usr/bin/env bash +# Toggle light- or dark-mode for your applications +# Usage: toggle [-m light|dark] + +################# +### Variables ### +################# + +THEME_LIGHT='tdpeuter-light' +THEME_DARK='tdpeuter-dark' +THEME_DEFAULT="${THEME_LIGHT}" + +STATE_FILE="${HOME}/.local/state/sisyphus/theme" + +declare -A theme_next +theme_next[${THEME_LIGHT}]="${THEME_DARK}" +theme_next[${THEME_DARK}]="${THEME_LIGHT}" + +declare -A gsettings_alt +gsettings_alt[${THEME_LIGHT}]='default' +gsettings_alt[${THEME_DARK}]='prefer-dark' + +############# +### Logic ### +############# + +# Parse options +while getopts ":m:" option; do + case "${option}" in + m) + if [ "${OPTARG}" == 'light' ]; then + theme="${THEME_LIGHT}" + elif [ "${OPTARG}" == 'dark' ]; then + theme="${THEME_DARK}" + else + >&2 printf "Error: Invalid mode: '%s'.\nShould be either 'light' or 'dark'\n" "${option}" + exit 1 + fi + ;; + *) + >&2 printf "Error: Invalid option: '%s'.\n" "${option}" + exit 1 + ;; + esac +done +shift $(( OPTIND - 1 )) + +# Check if the state file exists +if ! [ -d "$(dirname ${STATE_FILE})" ]; then + mkdir -p "$(dirname ${STATE_FILE})" +fi + +# Choose next theme +previous_theme="$(cat ${STATE_FILE})" +if ! [[ -z "${previous_theme}" || "${theme}" ]]; then + theme="${theme_next[${previous_theme}]}" +fi +echo "${theme:=${THEME_DEFAULT}}" > "${STATE_FILE}" + +###################### +### Set all themes ### +###################### + +# GNOME +if [ "$(command -v gsettings)" ]; then + gsettings set org.gnome.desktop.interface color-scheme "${gsettings_alt[${theme}]}" +fi + +# Kitty +if [ "$(command -v kitty)" ]; then + kitten themes --config-file-name theme.conf "${theme}" +fi + +# Vim +# This does not reload the config, but will use it when you restart vim +# Toggle an existing window using `:colorscheme ${theme}` +if [ "$(command -v vim)" ]; then + echo "colorscheme ${theme}" > ~/.vim/theme.conf +fi + diff --git a/stow/vim/.vimrc b/stow/vim/.vimrc index 933d6ed..bbd482f 100644 --- a/stow/vim/.vimrc +++ b/stow/vim/.vimrc @@ -2,7 +2,7 @@ " ~/.vimrc " -colorscheme tdpeuter-light +source ~/.vim/theme.conf set autoindent set conceallevel=2