From fe0f355277b220f304b2e58695349add20741e91 Mon Sep 17 00:00:00 2001 From: kj-sh604 Date: Wed, 17 Jul 2024 22:27:14 -0400 Subject: feat: new POSIX and fish functions --- .config/fish/functions/create_fish_dotenv.fish | 17 ++++++++++++ .config/shell/.bashrc | 13 +++++++++ .config/shell/.zshrc | 32 +++++++++++++++------- .config/shell/posix-functions/POSIX_open.sh | 6 ++++ .../shell/posix-functions/create_POSIX_dotenv.sh | 16 +++++++++++ 5 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 .config/fish/functions/create_fish_dotenv.fish create mode 100644 .config/shell/posix-functions/POSIX_open.sh create mode 100644 .config/shell/posix-functions/create_POSIX_dotenv.sh (limited to '.config') diff --git a/.config/fish/functions/create_fish_dotenv.fish b/.config/fish/functions/create_fish_dotenv.fish new file mode 100644 index 0000000..0574b53 --- /dev/null +++ b/.config/fish/functions/create_fish_dotenv.fish @@ -0,0 +1,17 @@ +function create_fish_dotenv + if test -f .env + echo "# Auto-generated .env.fish file" > .env.fish + cat .env | while read line + if not string match -qr '^\s*#' -q $line + if not string match -qr '^\s*$' -q $line + set key (string split -m 1 '=' $line)[1] + set value (string split -m 1 '=' $line)[2] + echo "set -x $key $value" >> .env.fish + end + end + end + echo ".env.fish file created successfully." + else + echo ".env file not found in current directory." + end +end diff --git a/.config/shell/.bashrc b/.config/shell/.bashrc index 10d0a4b..286afd9 100644 --- a/.config/shell/.bashrc +++ b/.config/shell/.bashrc @@ -71,4 +71,17 @@ done unset use_color sh +source_if_exists() { + [ -f "$1" ] && source "$1" +} + +ensure_directory_and_file() { + [ ! -d "$1" ] && mkdir -p "$1" + [ ! -f "$2" ] && touch "$2" +} + +# Source personal POSIX functions +source_if_exists ~/.config/shell/posix-functions/create_POSIX_dotenv.sh +source_if_exists ~/.config/shell/posix-functions/POSIX_open.sh + export HISTFILE="$XDG_STATE_HOME"/shell/history diff --git a/.config/shell/.zshrc b/.config/shell/.zshrc index a9d3e89..47a0922 100644 --- a/.config/shell/.zshrc +++ b/.config/shell/.zshrc @@ -1,13 +1,25 @@ -# source zsh extensions (order is important) -source ~/.config/shell/git-prompt.zsh -source ~/.config/shell/git-prompts/kj_sh604.zsh -source ~/.config/shell/zsh-autosuggestions/zsh-autosuggestions.zsh -source ~/.config/shell/zsh-fast-syntax-highlighting/fast-syntax-highlighting.plugin.zsh -source ~/.config/shell/zsh-history-substring-search/zsh-history-substring-search.zsh -# source ~/.config/shell/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh - -[ ! -d ~/.local/state/shell ] && mkdir -p ~/.local/state/shell -[ ! -f ~/.local/state/shell/zsh_history ] && touch ~/.local/state/shell/zsh_history +source_if_exists() { + [ -f "$1" ] && source "$1" +} + +ensure_directory_and_file() { + [ ! -d "$1" ] && mkdir -p "$1" + [ ! -f "$2" ] && touch "$2" +} + + +source_if_exists ~/.config/shell/git-prompt.zsh +source_if_exists ~/.config/shell/git-prompts/kj_sh604.zsh +source_if_exists ~/.config/shell/zsh-autosuggestions/zsh-autosuggestions.zsh +source_if_exists ~/.config/shell/zsh-fast-syntax-highlighting/fast-syntax-highlighting.plugin.zsh +source_if_exists ~/.config/shell/zsh-history-substring-search/zsh-history-substring-search.zsh +# source_if_exists ~/.config/shell/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh + +# Source personal POSIX functions +source_if_exists ~/.config/shell/posix-functions/create_POSIX_dotenv.sh +source_if_exists ~/.config/shell/posix-functions/POSIX_open.sh + +ensure_directory_and_file ~/.local/state/shell ~/.local/state/shell/zsh_history # configure history settings HISTFILE=~/.local/state/shell/zsh_history diff --git a/.config/shell/posix-functions/POSIX_open.sh b/.config/shell/posix-functions/POSIX_open.sh new file mode 100644 index 0000000..b53e93f --- /dev/null +++ b/.config/shell/posix-functions/POSIX_open.sh @@ -0,0 +1,6 @@ +open() { + command -v xdg-open > /dev/null && xdg-open "$@" && return + [ "$(uname)" = "Darwin" ] && open "$@" && return + [ "$(uname)" = "Haiku" ] && open "$@" && return + echo "error: could not detect the open command for your system." && return 1 +} diff --git a/.config/shell/posix-functions/create_POSIX_dotenv.sh b/.config/shell/posix-functions/create_POSIX_dotenv.sh new file mode 100644 index 0000000..9e5121d --- /dev/null +++ b/.config/shell/posix-functions/create_POSIX_dotenv.sh @@ -0,0 +1,16 @@ +create_POSIX_dotenv() { + if [ -f .env ]; then + echo "# auto-generated .env.sh file" > .env.sh + while IFS= read -r line; do + if [ "${line#\#}" != "$line" ] || [ -z "$line" ]; then + continue + fi + key=$(echo "$line" | cut -d '=' -f 1) + value=$(echo "$line" | cut -d '=' -f 2-) + echo "export $key=\"$value\"" >> .env.sh + done < .env + echo ".env.sh file created successfully." + else + echo ".env file not found in current directory." + fi +} -- cgit v1.2.3