Add support for using dark/light mode

This commit is contained in:
Jonny Barnes 2024-03-22 15:59:05 +00:00
parent 12dd8faba4
commit 7187d52ee6
Signed by: jonny
SSH key fingerprint: SHA256:CTuSlns5U7qlD9jqHvtnVmfYV3Zwl2Z7WnJ4/dqOaL8
3 changed files with 148 additions and 89 deletions

23
zsh/dark-mode-notify.zsh Executable file
View file

@ -0,0 +1,23 @@
#!/usr/bin/env zsh
function switch-dark-mode()
{
local darkMode=true;
if [[ $(defaults read -g AppleInterfaceStyle 2> /dev/null) != 'Dark' ]]; then
darkMode=false
fi
if [[ $darkMode == true ]]; then
echo "Switched to dark mode"
export MACOS_APPEARANCE="dark"
else
echo "Switched to light mode"
export MACOS_APPEARANCE="light"
fi
# Reload zshrc
pkill -usr1 zsh
}
switch-dark-mode

View file

@ -4,131 +4,150 @@
# Generate TLS certs using a local CA # Generate TLS certs using a local CA
gencert () { gencert () {
DOMAIN=$1 DOMAIN=$1
test -d /usr/local/opt/openssl@1.1/bin && PATH='/usr/local/opt/openssl@1.1/bin':$PATH test -d /usr/local/opt/openssl@1.1/bin && PATH='/usr/local/opt/openssl@1.1/bin':$PATH
test -d /opt/homebrew/opt/openssl@1.1/bin && PATH='/opt/homebrew/opt/openssl@1.1/bin':$PATH test -d /opt/homebrew/opt/openssl@1.1/bin && PATH='/opt/homebrew/opt/openssl@1.1/bin':$PATH
test -f /usr/local/etc/openssl@1.1/openssl.cnf && SSLCNF='/usr/local/etc/openssl@1.1/openssl.cnf' test -f /usr/local/etc/openssl@1.1/openssl.cnf && SSLCNF='/usr/local/etc/openssl@1.1/openssl.cnf'
test -f /opt/homebrew/etc/openssl@1.1/openssl.cnf && SSLCNF='/opt/homebrew/etc/openssl@1.1/openssl.cnf' test -f /opt/homebrew/etc/openssl@1.1/openssl.cnf && SSLCNF='/opt/homebrew/etc/openssl@1.1/openssl.cnf'
test -f /etc/ssl/openssl.cnf && SSLCNF='/etc/ssl/openssl.cnf' test -f /etc/ssl/openssl.cnf && SSLCNF='/etc/ssl/openssl.cnf'
cd $HOME/git/ca cd $HOME/git/ca
[[ ! -d $DOMAIN ]] && mkdir $DOMAIN [[ ! -d $DOMAIN ]] && mkdir $DOMAIN
cd $DOMAIN cd $DOMAIN
[[ -f key ]] && mv key key.bak [[ -f key ]] && mv key key.bak
[[ -f csr ]] && mv csr csr.bak [[ -f csr ]] && mv csr csr.bak
[[ -f crt ]] && mv crt crt.bak [[ -f crt ]] && mv crt crt.bak
openssl ecparam -name secp384r1 -genkey -noout -out key openssl ecparam -name secp384r1 -genkey -noout -out key
chmod 644 key chmod 644 key
openssl req -new -sha256 -key key -subj "/C=UK/ST=England/L=Bury/O=JMB Dev Ltd/CN=$DOMAIN" -reqexts SAN -config <(cat $SSLCNF <(printf "[SAN]\nsubjectAltName=DNS:$DOMAIN")) -out csr openssl req -new -sha256 -key key -subj "/C=UK/ST=England/L=Bury/O=JMB Dev Ltd/CN=$DOMAIN" -reqexts SAN -config <(cat $SSLCNF <(printf "[SAN]\nsubjectAltName=DNS:$DOMAIN")) -out csr
openssl x509 -req -in csr -extfile <(cat $SSLCNF <(printf "[SAN]\nsubjectAltName=DNS:$DOMAIN")) -extensions SAN -CA ../jmb-ca-ecc.pem -CAkey ../jmb-ca-ecc.key -CAcreateserial -days 90 -sha256 -out crt openssl x509 -req -in csr -extfile <(cat $SSLCNF <(printf "[SAN]\nsubjectAltName=DNS:$DOMAIN")) -extensions SAN -CA ../jmb-ca-ecc.pem -CAkey ../jmb-ca-ecc.key -CAcreateserial -days 90 -sha256 -out crt
cd $HOME/git/ca cd $HOME/git/ca
echo 'Certs generated for $DOMAIN' echo 'Certs generated for $DOMAIN'
} }
# Generate a random 7 digit number, used for CCLs ImKeys # Generate a random 7 digit number, used for CCLs ImKeys
imkey () { imkey () {
imkey=$(shuf -i 1111111-9999999 -n1) imkey=$(shuf -i 1111111-9999999 -n1)
echo $imkey echo $imkey
} }
# Delete branches selected via fzf # Delete branches selected via fzf
delete-branches () { delete-branches () {
git branch | git branch |
grep --invert-match '\*' | # Remove current branch grep --invert-match '\*' | # Remove current branch
fzf --multi --preview="git log {..}" | fzf --multi --preview="git log {..}" |
gxargs --no-run-if-empty git branch --delete gxargs --no-run-if-empty git branch --delete
} }
# Delete branches selected via fzf (force the deletion) # Delete branches selected via fzf (force the deletion)
delete-branches-force () { delete-branches-force () {
git branch | git branch |
grep --invert-match '\*' | # Remove current branch grep --invert-match '\*' | # Remove current branch
fzf --multi --preview="git log {..}" | fzf --multi --preview="git log {..}" |
gxargs --no-run-if-empty git branch --delete --force gxargs --no-run-if-empty git branch --delete --force
} }
# Open a PR # Open a PR
function pr-checkout() { function pr-checkout() {
local pr_number local pr_number
pr_number=$( pr_number=$(
gh api 'repos/:owner/:repo/pulls' --jq '.[] | "#\(.number) \(.title)"' | gh api 'repos/:owner/:repo/pulls' --jq '.[] | "#\(.number) \(.title)"' |
fzf | fzf |
sd '^\#(\d+)\s.*' '$1' sd '^\#(\d+)\s.*' '$1'
) )
if [ -n "$pr_number" ]; then if [ -n "$pr_number" ]; then
gh pr checkout "$pr_number" gh pr checkout "$pr_number"
fi fi
} }
# tail, but better # tail, but better
function watch-file() { function watch-file() {
tail -f $1 | bat --paging=never -l log tail -f $1 | bat --paging=never -l log
} }
# Get a temporary directory in a cross-platform manner # Get a temporary directory in a cross-platform manner
# See https://unix.stackexchange.com/a/685873 # See https://unix.stackexchange.com/a/685873
function get-temporary-directory() { function get-temporary-directory() {
local temporary_directory=${XDG_RUNTIME_DIR:-${TMPDIR:-${TMP:-${TEMP:-/tmp}}}} local temporary_directory=${XDG_RUNTIME_DIR:-${TMPDIR:-${TMP:-${TEMP:-/tmp}}}}
echo $temporary_directory echo $temporary_directory
} }
# Start an SSH SOCKS tunnel # Start an SSH SOCKS tunnel
function start-proxy() { function start-proxy() {
if [ -z "$SSH_PROXY_HOST" ]; then if [ -z "$SSH_PROXY_HOST" ]; then
echo "SSH_PROXY_HOST is not set or empty" echo "SSH_PROXY_HOST is not set or empty"
return return
fi fi
local socket_dir=$(get-temporary-directory) local socket_dir=$(get-temporary-directory)
# Check if the last character of socket_dir is a directory separator character # Check if the last character of socket_dir is a directory separator character
if [[ $socket_dir[-1] == / ]]; then if [[ $socket_dir[-1] == / ]]; then
# If yes, remove the directory separator character # If yes, remove the directory separator character
socket_dir="${socket_dir%?}" socket_dir="${socket_dir%?}"
fi fi
echo "Opening proxy connection" echo "Opening proxy connection"
# Below we use -S and -M to help make closing the connection more reliable # Below we use -S and -M to help make closing the connection more reliable
# See this Stack Overflow answer for more info # See this Stack Overflow answer for more info
# https://unix.stackexchange.com/a/525388 # https://unix.stackexchange.com/a/525388
# -D 1337 opens up the SOXKS tunnel on localhost:1337 # -D 1337 opens up the SOXKS tunnel on localhost:1337
# -f Tells `ssh` to fork the ssh process in to the background # -f Tells `ssh` to fork the ssh process in to the background
# -C Enables compression of data on the connections # -C Enables compression of data on the connections
# -q Uses quiet mode # -q Uses quiet mode
# -N Do not execute a remote command on this connection # -N Do not execute a remote command on this connection
# -S Set the ControlPath for this connection # -S Set the ControlPath for this connection
# -M Place the client into `master` mode # -M Place the client into `master` mode
ssh -D 1337 -f -C -q -N -S $socket_dir/ssh-proxy-control -M $SSH_PROXY_HOST ssh -D 1337 -f -C -q -N -S $socket_dir/ssh-proxy-control -M $SSH_PROXY_HOST
} }
# Stop an open proxy connection # Stop an open proxy connection
function stop-proxy() { function stop-proxy() {
if [ -z "$SSH_PROXY_HOST" ]; then if [ -z "$SSH_PROXY_HOST" ]; then
echo "SSH_PROXY_HOST is not set or empty" echo "SSH_PROXY_HOST is not set or empty"
return return
fi fi
local socket_dir=$(get-temporary-directory) local socket_dir=$(get-temporary-directory)
# Check if the last character of socket_dir is a directory separator character # Check if the last character of socket_dir is a directory separator character
if [[ $socket_dir[-1] == / ]]; then if [[ $socket_dir[-1] == / ]]; then
# If yes, remove the directory separator character # If yes, remove the directory separator character
socket_dir="${socket_dir%?}" socket_dir="${socket_dir%?}"
fi fi
echo "Closing proxy connection" echo "Closing proxy connection"
# See the comments in `start-proxy()` for more info # See the comments in `start-proxy()` for more info
ssh -S $socket_dir/ssh-proxy-control -O exit $SSH_PROXY_HOST ssh -S $socket_dir/ssh-proxy-control -O exit $SSH_PROXY_HOST
}
# Get the system appearance
function get-system-appearance() {
if ! type defaults &>/dev/null; then
echo ""
fi
local darkMode=true;
if [[ $(defaults read -g AppleInterfaceStyle 2> /dev/null) != 'Dark' ]]; then
darkMode=false
fi
if [[ $darkMode == true ]]; then
echo "dark"
else
echo "light"
fi
} }

View file

@ -41,9 +41,15 @@ fi
export PATH="/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/opt/homebrew/sbin:/usr/local/sbin:/usr/sbin:/sbin:$HOME/.local/bin:$PATH" export PATH="/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/opt/homebrew/sbin:/usr/local/sbin:/usr/sbin:/sbin:$HOME/.local/bin:$PATH"
export MANPATH="/opt/homebrew/manpages:/usr/local/man:$MANPATH" export MANPATH="/opt/homebrew/manpages:/usr/local/man:$MANPATH"
# Source my own functions
source $HOME/.zsh/functions.zsh
# Determine the running OS # Determine the running OS
source $HOME/.zsh/platform.zsh source $HOME/.zsh/platform.zsh
# Detect system appearance
export MACOS_APPEARANCE=`get-system-appearance`
# ZSH syntax highlighting # ZSH syntax highlighting
source $HOME/.zsh/zsh-syntax-highlighting.zsh source $HOME/.zsh/zsh-syntax-highlighting.zsh
@ -108,9 +114,6 @@ fi
# PHP binaries # PHP binaries
test -d $HOME/.php/bin && export PATH="$PATH:$HOME/.php/bin" test -d $HOME/.php/bin && export PATH="$PATH:$HOME/.php/bin"
# macOS Python User binaries
test -d $HOME/Library/Python/3.7/bin && export PATH="$PATH:$HOME/Library/Python/3.7/bin"
# JetBrains Toolbox scripts # JetBrains Toolbox scripts
test -d "$HOME/Library/Application Support/JetBrains/Toolbox/scripts" && export PATH="$PATH:$HOME/Library/Application Support/JetBrains/Toolbox/scripts" test -d "$HOME/Library/Application Support/JetBrains/Toolbox/scripts" && export PATH="$PATH:$HOME/Library/Application Support/JetBrains/Toolbox/scripts"
@ -120,19 +123,29 @@ if type brew &>/dev/null; then
fi fi
# Colourised output for `ls` # Colourised output for `ls`
export CLICOLOR=true # export CLICOLOR=true
export CLICOLOR_FORCE=true # export CLICOLOR_FORCE=true
export LSCOLORS='dxfxcxdxbxegedabagacad' # export LSCOLORS='dxfxcxdxbxegedabagacad'
export LS_COLORS='di=33;40:ln=35;40:so=32;40:pi=33;40:ex=31;40:bd=34;46:cd=34;43:su=0;41:sg=0;46:tw=0;42:ow=0;43:' # export LS_COLORS='di=33;40:ln=35;40:so=32;40:pi=33;40:ex=31;40:bd=34;46:cd=34;43:su=0;41:sg=0;46:tw=0;42:ow=0;43:'
# vivid Dark mode ayu
# vivid Light mode snazzy
if type vivid &>/dev/null; then if type vivid &>/dev/null; then
export LS_COLORS="$(vivid generate ayu)" local vividTheme="ayu"
if [[ $MACOS_APPEARANCE == "light" ]]; then
vividTheme="snazzy"
fi
export LS_COLORS="$(vivid generate $vividTheme)"
fi fi
# Set colour scheme got bat # Set colour scheme got bat
export BAT_THEME='OneHalfDark' # bat Dark mode OneHalfDark
# bat Light mode Coldark-Dark
# Source my own functions local batTheme="OneHalfDark"
test -e $HOME/.zsh/functions.zsh && source $HOME/.zsh/functions.zsh if [[ $MACOS_APPEARANCE == "light" ]]; then
batTheme="Coldark-Dark"
fi
export BAT_THEME=$batTheme
# Setup fzf completions # Setup fzf completions
export FZF_COMPLETION_TRIGGER='~~' export FZF_COMPLETION_TRIGGER='~~'
@ -184,3 +197,7 @@ fi
# Init starship prompt -- https://starship.rs # Init starship prompt -- https://starship.rs
eval "$(starship init zsh)" eval "$(starship init zsh)"
# Finally we can have zsh auto source this rc file on command
# attribution: https://www.reddit.com/r/commandline/comments/12g76v/
trap "source $HOME/.zshrc" USR1