summarylogtreecommitdiffstats
path: root/swaycwd
diff options
context:
space:
mode:
authorJulian Daube2020-12-14 14:11:44 +0100
committerJulian Daube2020-12-14 14:11:44 +0100
commit0c73d19248c98589396fb063fc0b95495211fc3f (patch)
treea7451ac62059522e3a240723b2ceca4664a11c9b /swaycwd
parent74ceab4b1ec9d7c53d136e56c516ea458a3502e4 (diff)
downloadaur-swaycwd.tar.gz
fix fallback directory detection
firefox (probably due to sandboxing) results in /proc-based cwds. This can be annoying. So swaycwd will now detect if the cwd still is in /proc and return the fallback dir. The Fallbackdir (previously was hardcoded to /home/julian) can now be passed via a command line arg. And since commandline args are parsed now, we can also print a helptext as well :)
Diffstat (limited to 'swaycwd')
-rw-r--r--swaycwd56
1 files changed, 55 insertions, 1 deletions
diff --git a/swaycwd b/swaycwd
index 6b75d53fca29..5585b6b12925 100644
--- a/swaycwd
+++ b/swaycwd
@@ -1,6 +1,60 @@
#!/usr/bin/env bash
+fallback="$HOME"
+
+# taken from https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
+! PARSED=$(getopt --options="h?f:" --longoptions="fallback:,help" --name "$0" -- "$@")
+if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
+ # e.g. return value is 1
+ # then getopt has complained about wrong arguments to stdout
+ exit 2
+fi
+
+# helptext
+usage="$(basename $0) [-h] [-f=/fallback/directory]
+
+Get Current Working Directory for program under cursor in sway.
+
+Options:
+ -h, --help Print this help text
+ -f, --fallback Set directory to print when error occured (defaults to $HOME)"
+
+# parse args
+eval set -- "$PARSED"
+
+while true; do
+ case "$1" in
+ -f|--fallback)
+ fallback="$2"
+ shift 2
+ ;;
+ -h|--help)
+ echo "$usage"
+ exit 0
+ ;;
+ --)
+ break
+ ;;
+ *)
+ echo "argparse error"
+ exit 1
+ ;;
+ esac
+done
+
+# get parent pid of program under cursor
pid=$(swaymsg -t get_tree | jq '.. | select(.type?) | select(.type=="con") | select(.focused==true).pid')
ppid=$(pgrep --newest --parent ${pid})
-readlink /proc/${ppid}/cwd || echo $HOME
+# get cwd from proc dir
+location=$(readlink /proc/${ppid}/cwd)
+
+if [ "$?" -ne 0 ]; then
+ # just print out fallback dir on error
+ echo $fallback
+ exit 0
+fi
+
+# find out if the path points to /proc dir
+# use fallback in that case
+echo $location | grep -q /proc && echo $fallback || echo $location