summarylogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulian Daube2020-12-14 14:11:44 +0100
committerJulian Daube2020-12-14 14:11:44 +0100
commit0c73d19248c98589396fb063fc0b95495211fc3f (patch)
treea7451ac62059522e3a240723b2ceca4664a11c9b
parent74ceab4b1ec9d7c53d136e56c516ea458a3502e4 (diff)
downloadaur-0c73d19248c98589396fb063fc0b95495211fc3f.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 :)
-rw-r--r--.SRCINFO4
-rw-r--r--PKGBUILD3
-rw-r--r--swaycwd56
3 files changed, 59 insertions, 4 deletions
diff --git a/.SRCINFO b/.SRCINFO
index 921b9064c985..17be746eef9a 100644
--- a/.SRCINFO
+++ b/.SRCINFO
@@ -1,6 +1,6 @@
pkgbase = swaycwd
pkgdesc = alternative to xcwd for swayvm
- pkgver = 1.0
+ pkgver = 1.1
pkgrel = 1
url = https://webcache.googleusercontent.com/search?q=cache:fXlJ80wnYwgJ:https://www.reddit.com/r/swaywm/comments/ayedi1/opening_terminals_at_the_same_directory/+&cd=1&hl=de&ct=clnk&gl=de&client=firefox-b-d
arch = any
@@ -9,7 +9,7 @@ pkgbase = swaycwd
depends = jq
depends = procps
source = swaycwd
- md5sums = fad6fddc15abd75e7ca7760553dd2a28
+ md5sums = b26f324308016ded12e36d54540d6bd2
pkgname = swaycwd
diff --git a/PKGBUILD b/PKGBUILD
index bd722820fcff..8c5821ab5858 100644
--- a/PKGBUILD
+++ b/PKGBUILD
@@ -5,7 +5,7 @@
pkgname=swaycwd
pkgdesc="alternative to xcwd for swayvm"
pkgrel=1
-pkgver=1.0
+pkgver=1.1
url="https://webcache.googleusercontent.com/search?q=cache:fXlJ80wnYwgJ:https://www.reddit.com/r/swaywm/comments/ayedi1/opening_terminals_at_the_same_directory/+&cd=1&hl=de&ct=clnk&gl=de&client=firefox-b-d"
arch=("any")
@@ -17,3 +17,4 @@ package() {
install -m 755 "$srcdir/swaycwd" "$pkgdir/usr/bin/swaycwd"
}
md5sums=('fad6fddc15abd75e7ca7760553dd2a28')
+md5sums=('b26f324308016ded12e36d54540d6bd2')
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