summaryrefslogtreecommitdiff
path: root/bash-scripts
diff options
context:
space:
mode:
authorluxagraf <sng@luxagraf.net>2023-06-15 15:58:59 -0500
committerluxagraf <sng@luxagraf.net>2023-06-15 15:58:59 -0500
commitab987e10f154f5536bb8fd936ae0966e909fa969 (patch)
tree9de5076f38b71ececb1bc94f8d9d19170898d603 /bash-scripts
added all my scriptssynced/master
Diffstat (limited to 'bash-scripts')
-rwxr-xr-xbash-scripts/fuz19
-rwxr-xr-xbash-scripts/pwd-prompt.bash54
2 files changed, 73 insertions, 0 deletions
diff --git a/bash-scripts/fuz b/bash-scripts/fuz
new file mode 100755
index 0000000..429bf7c
--- /dev/null
+++ b/bash-scripts/fuz
@@ -0,0 +1,19 @@
+#!/usr/bin/env bash
+set -e
+
+main() {
+ previous_file="$1"
+ file_to_edit=`select_file $previous_file`
+
+ if [ -n "$file_to_edit" ] ; then
+ "$EDITOR" "$file_to_edit"
+ main "$file_to_edit"
+ fi
+}
+
+select_file() {
+ given_file="$1"
+ fzf --preview="cat {}" --preview-window=right:70%:wrap --query="$given_file"
+}
+
+main ""
diff --git a/bash-scripts/pwd-prompt.bash b/bash-scripts/pwd-prompt.bash
new file mode 100755
index 0000000..a3fce41
--- /dev/null
+++ b/bash-scripts/pwd-prompt.bash
@@ -0,0 +1,54 @@
+#!/bin/bash
+
+begin="" # The unshortened beginning of the path.
+shortbegin="" # The shortened beginning of the path.
+current="" # The section of the path we're currently working on.
+end="${2:-$(pwd)}/" # The unmodified rest of the path.
+
+if [[ "$end" =~ "$HOME" ]]; then
+ INHOME=1
+ end="${end#$HOME}" #strip /home/username from start of string
+ begin="$HOME" #start expansion from the right spot
+else
+ INHOME=0
+fi
+
+end="${end#/}" # Strip the first /
+shortenedpath="$end" # The whole path, to check the length.
+maxlength="${1:-0}"
+
+shopt -q nullglob && NGV="-s" || NGV="-u" # Store the value for later.
+shopt -s nullglob # Without this, anything that doesn't exist in the filesystem turns into */*/*/...
+
+while [[ "$end" ]] && (( ${#shortenedpath} > maxlength ))
+do
+ current="${end%%/*}" # everything before the first /
+ end="${end#*/}" # everything after the first /
+
+ shortcur="$current"
+ shortcurstar="$current" # No star if we don't shorten it.
+
+ for ((i=${#current}-2; i>=0; i--)); do
+ subcurrent="${current:0:i}"
+ matching=("$begin/$subcurrent"*) # Array of all files that start with $subcurrent.
+ (( ${#matching[*]} != 1 )) && break # Stop shortening if more than one file matches.
+ shortcur="$subcurrent"
+ shortcurstar="$subcurrent*"
+ done
+
+ #advance
+ begin="$begin/$current"
+ shortbegin="$shortbegin/$shortcurstar"
+ shortenedpath="$shortbegin/$end"
+done
+
+shortenedpath="${shortenedpath%/}" # strip trailing /
+shortenedpath="${shortenedpath#/}" # strip leading /
+
+if [ $INHOME -eq 1 ]; then
+ echo "~/$shortenedpath" #make sure it starts with ~/
+else
+ echo "/$shortenedpath" # Make sure it starts with /
+fi
+
+shopt "$NGV" nullglob # Reset nullglob in case this is being used as a function.