summaryrefslogtreecommitdiff
path: root/.local/bin
diff options
context:
space:
mode:
authorBenjamin Chausse <benjamin@chausse.xyz>2024-05-12 03:27:14 -0400
committerBenjamin Chausse <benjamin@chausse.xyz>2024-05-12 03:27:14 -0400
commite53ab0e7e1f4b04e2339b5452e13df7081f0d189 (patch)
tree0b68d696a0fb98ed86b38e6e6ec664794052ae00 /.local/bin
parentf9e97c28c63f0a47922defb4b99765ea381fb35c (diff)
POSIX profile
Diffstat (limited to '.local/bin')
-rwxr-xr-x.local/bin/shortcutgen75
1 files changed, 54 insertions, 21 deletions
diff --git a/.local/bin/shortcutgen b/.local/bin/shortcutgen
index fe5ed75..4701655 100755
--- a/.local/bin/shortcutgen
+++ b/.local/bin/shortcutgen
@@ -1,23 +1,56 @@
#!/bin/sh
-
-# raw (basic processing before converting to specific formats)
-# - remove comments
-# - remove empty lines
-# - remove spacing (keep commas between columns)
-# - remove trailing whitespace
-
-# Strip comments, remove empty lines, condense spacing, remove trailing whitespace
-raw="$(sed 's/#.*//g;s/,[[:space:]]*/,/g;/^$/d;s/[[:space:]]*$//g' <"${XDG_CONFIG_HOME:-$HOME/.config}/shortcutrc")"
-
-# lf (sourced by lfrc)
-echo "$raw" | sed 's/\(.*\),\(.*\),\(.*\)/map g\2 cd \3/' >"${XDG_CONFIG_HOME:-$HOME/.config}/lf/shortcuts"
-
-# Shell aliases (read by zshrc)
-echo "$raw" | sed 's/\(.*\),\(.*\),\(.*\)/alias g\2="cd \3"/' >"${XDG_CACHE_HOME:-$HOME/.cache}/zsh-shortcuts"
-
-# Environment variables (read by .profile)
-echo "$raw" | sed 's/^\(.*\),\(.*\),/export G\U\2=,/;s/,\(.*\)/\1/' >"${XDG_CACHE_HOME:-$HOME/.cache}/env-shortcuts"
-
-
-
+# Quit if a valid configuration source doesn't exist
+[ -f "$XDG_CONFIG_HOME/shortcutrc" ] && src="$XDG_CONFIG_HOME/shortcutrc" || exit 1
+
+# If you do not want to create a specific file, replace it's variable with /dev/null
+awk -v lf_filepath="${XDG_CONFIG_HOME:-$HOME/.config}/lf/shortcuts" \
+ -v zsh_filepath="${XDG_CACHE_HOME:-$HOME/.cache}/zsh-shortcuts" \
+ -v env_filepath="${XDG_CACHE_HOME:-$HOME/.cache}/env-shortcuts" \
+'
+BEGIN {
+ FS=","
+ OFS=","
+}
+
+# Function to remove whitespaces from a string
+function remove_whitespace(str) {
+ gsub(/[[:space:]]/, "", str)
+ return str
+}
+
+{
+ # Remove comments
+ gsub(/#.*/, "")
+
+ # Remove whitespaces from column 2
+ $2 = remove_whitespace($2)
+
+ # Remove leading and trailing whitespaces from column 3
+ gsub(/^[[:space:]]+|[[:space:]]+$/, "", $3)
+
+ # whenever a line contains only commas, replace it with an empty string
+ # so that we can check if a line is empty or not
+ gsub(/^,+$|^,+|,+$|,+,/, "", $0)
+
+ # Check if line is empty or contains only whitespace
+ if (NF) {
+
+ # Convert column 2 to lowercase
+ lc_col2 = tolower($2)
+
+ # Convert column 2 to uppercase
+ uc_col2 = toupper($2)
+
+ # Write to $XDG_CONFIG_HOME/lf/shortcuts
+ print "map g" lc_col2 " cd " $3 > ENVIRON["XDG_CONFIG_HOME"] "/lf/shortcuts"
+
+ # Write to $XDG_CACHE_HOME/zsh-shortcuts
+ # print "alias g" lc_col2 "=\"cd " $3 "\"" > ENVIRON["XDG_CACHE_HOME"] "/zsh-shortcuts"
+ print "g" lc_col2 "() {cd " $3 "}" > ENVIRON["XDG_CACHE_HOME"] "/zsh-shortcuts"
+
+ # Write to $XDG_CACHE_HOME/env-shortcuts
+ print "export G" uc_col2 "=\"" $3 "\"" > ENVIRON["XDG_CACHE_HOME"] "/env-shortcuts"
+ }
+}
+' "$src"