From 99d9d843b2b1393a2561b15044e744e2753d7c2f Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Sat, 19 Aug 2023 16:50:42 -0600 Subject: [PATCH] [shell] Add several functions --- dotfiles/lib/functions/add_to_path | 7 +++++++ dotfiles/lib/functions/join_by_char | 22 ++++++++++++++++++++++ dotfiles/lib/functions/nixpkgs_source | 7 +++++++ dotfiles/lib/functions/path_from_lines | 7 +++++++ dotfiles/lib/functions/path_lines | 5 +++-- 5 files changed, 46 insertions(+), 2 deletions(-) create mode 100755 dotfiles/lib/functions/add_to_path create mode 100755 dotfiles/lib/functions/join_by_char create mode 100755 dotfiles/lib/functions/nixpkgs_source create mode 100755 dotfiles/lib/functions/path_from_lines diff --git a/dotfiles/lib/functions/add_to_path b/dotfiles/lib/functions/add_to_path new file mode 100755 index 00000000..7053bb82 --- /dev/null +++ b/dotfiles/lib/functions/add_to_path @@ -0,0 +1,7 @@ +#!/usr/bin/env sh + +function add_to_path { + path_lines "$2" | { cat -; echo "$1" } | path_from_lines +} + +add_to_path "$@" diff --git a/dotfiles/lib/functions/join_by_char b/dotfiles/lib/functions/join_by_char new file mode 100755 index 00000000..5490f0d8 --- /dev/null +++ b/dotfiles/lib/functions/join_by_char @@ -0,0 +1,22 @@ +#!/usr/bin/env sh + +function join_by_char { + delimiter="$1" + if [ -z "$delimiter" ]; then + echo "Please specify a delimiter." + return 1 + fi + + while IFS= read -r line; do + # Append the line to the result with the delimiter, but not for the first line + if [ -z "$result" ]; then + result="$line" + else + result="$result$delimiter$line" + fi + done + + echo "$result" +} + +join_by_char "$@" diff --git a/dotfiles/lib/functions/nixpkgs_source b/dotfiles/lib/functions/nixpkgs_source new file mode 100755 index 00000000..02b5e4d0 --- /dev/null +++ b/dotfiles/lib/functions/nixpkgs_source @@ -0,0 +1,7 @@ +#!/usr/bin/env sh + +function nixpkgs_source { + nix eval --expr '' --impure +} + +nixpkgs_source diff --git a/dotfiles/lib/functions/path_from_lines b/dotfiles/lib/functions/path_from_lines new file mode 100755 index 00000000..776a055c --- /dev/null +++ b/dotfiles/lib/functions/path_from_lines @@ -0,0 +1,7 @@ +#!/usr/bin/env sh + +function path_from_lines { + join_by_char ":" +} + +path_from_lines "$@" diff --git a/dotfiles/lib/functions/path_lines b/dotfiles/lib/functions/path_lines index 03321e1f..bca39da6 100755 --- a/dotfiles/lib/functions/path_lines +++ b/dotfiles/lib/functions/path_lines @@ -1,7 +1,8 @@ #!/usr/bin/env sh function path_lines { - split_by_char ":" "$PATH" + var="${1:-PATH}" + split_by_char ":" "${!var}" } -path_lines +path_lines "$@"