add more command line options, general cleanup

This commit is contained in:
yosh 2024-01-22 22:28:34 -05:00
parent 82c934123d
commit 2d22802ee2
2 changed files with 37 additions and 18 deletions

View File

@ -6,12 +6,15 @@
- An implementation of POSIX Utilities (`coreutils`, `bsdutils`, `busybox`, ...)
- An implementation of `file(1)` that supports `-b` and `-i` for mime-types (yours should)
- `ed` (mentioning this separately since many unixes/distributions have it separately)
- [`clipnotify`](https://github.com/cdown/clipnotify)
- A newline-delimited menu program (`dmenu`, `rofi`, `bemenu`, `fzfmenu`, ...)
- (Optional, but recommended) `notify-send`
## using
put `shclip-{daemon,menu}` in your `$PATH` and start `shclip-daemon` in a way where it can read `$DISPLAY`. everything should Just Work, hopefully. to use the menu, run `shclip-menu`
to see the command-line opticons for `shclip-daemon`, run `shclip-daemon -h`
`shclip` reads a config file at `$XDG_CONFIG_HOME/shclip/config` and reads filters from `$XDG_CONFIG_HOME/shclip/filter.sh`. if `config` doesn't exist, then the defaults as specified in the file are used. if `filter.sh` doesn't exist, then filtering won't happen whether it's enabled or not.
if filtering is enabled, every text clip will be sent to the stdin of `filter.sh` and be replaced by its stdout. A simple replacement filter is provided as an example

View File

@ -22,10 +22,13 @@ SHCLIP_TEMP_DIR="${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}}/shclip-$USER-$DISPLAY"
errecho() { printf '%s\n' "$*" >&2; }
exists() { command -v "$@" >/dev/null 2>&1; }
notify() {
errecho "$*"
exists notify-send && notify-send "$*"
:
}
fail() {
errecho "error: $BN: $*"
exists notify-send && notify-send "error: $BN: $*"
notify "error: $BN: $*"
exit 1
}
@ -41,12 +44,10 @@ cleanup() {
toggle() {
if [ -z "$SHCLIP_ENABLED" ]; then
SHCLIP_ENABLED=1
errecho "shclip enabled!"
exists notify-send && notify-send "shclip enabled!"
notify "shclip enabled!"
else
SHCLIP_ENABLED=""
errecho "shclip disabled!"
exists notify-send && notify-send "shclip disabled!"
notify "shclip disabled!"
fi
kill "$clipnotify_pid"
}
@ -54,12 +55,10 @@ toggle() {
filter_toggle() {
if [ -z "$SHCLIP_FILTER_ENABLED" ]; then
SHCLIP_FILTER_ENABLED=1
errecho "shclip filter enabled!"
exists notify-send && notify-send "shclip filter enabled!"
notify "shclip filter enabled!"
else
SHCLIP_FILTER_ENABLED=""
errecho "shclip filter disabled!"
exists notify-send && notify-send "shclip filter disabled!"
notify "shclip filter disabled!"
fi
kill "$clipnotify_pid"
}
@ -124,8 +123,7 @@ updateclip() {
# check if clip exceeds memory limit
sz="$(du -k "$BUFFER")"
if [ "${sz%%${TAB}*}" -gt "$SHCLIP_MEMORY_LIMIT" ]; then
errecho 'clip exceeds memory threshold! not caching...'
exists notify-send && notify-send 'clip exceeds memory threshold! not caching...'
notify 'clip exceeds memory threshold! not caching...'
rm -f "$BUFFER"
return 0
fi
@ -141,7 +139,7 @@ updateclip() {
# apply text filters
[ -n "$SHCLIP_FILTER_ENABLED" ] && [ -f "$SHCLIP_CONFIG_DIR/filter.sh" ] && \
sh "$SHCLIP_CONFIG_DIR/filter.sh" < "$BUFFER" | diff "$BUFFER" - | patch "$BUFFER"
xclip -sel clipboard "$BUFFER"
xclip -sel clipboard "$BUFFER"
[ -n "$SHCLIP_SYNC_CLIPBOARD" ] && xclip -sel primary "$BUFFER"
;;
*) ;;
@ -166,16 +164,34 @@ updateclip() {
[ -z "${DISPLAY:-}" ] && fail 'Cannot find $DISPLAY. Is your X server running?'
# parse options
while getopts :pnf OPT; do
while getopts :hn:f:s:p:c:m OPT; do
case $OPT in
n) SHCLIP_ENABLED="" ;;
f) SHCLIP_FILTER_ENABLED=1 ;;
h) cat >&2 <<-EOF
please refer to README for general usage:
https://git.unix.dog/yosh/shclip
options that exist (overrides config file):
-h show this help
-n <on|off> enable/disable shclip on startup
-f <on|off> enable/disable filtering
-s <on|off> enable/disable syncing clipboard to primary
-p <on|off> enable/disable caching kde passwords
-c <num> set max clips to <num> clips
-m <num> set memory limit to <num> kibibytes
EOF
;;
n) [ "$OPTARG" = "on" ] && SHCLIP_ENABLED=1 || SHCLIP_ENABLED="" ;;
f) [ "$OPTARG" = "on" ] && SHCLIP_FILTER_ENABLED=1 || SHCLIP_FILTER_ENABLED="" ;;
s) [ "$OPTARG" = "on" ] && SHCLIP_SYNC_CLIPBOARD=1 || SHCLIP_SYNC_CLIPBOARD="" ;;
p) [ "$OPTARG" = "on" ] && SHCLIP_CACHE_PASSWORDS=1 || SHCLIP_CACHE_PASSWORDS="" ;;
c) SHCLIP_MAX_CLIPS=$OPTARG ;;
m) SHCLIP_MEMORY_LIMIT=$OPTARG ;;
*) fail "$BN: unknown option: -$OPTARG" ;;
esac
done
setup
while
while
if [ -z "$SHCLIP_ENABLED" ]; then
:
else