diff options
| author | Suleyman Farajli <suleyman@farajli.net> | 2025-11-25 18:47:27 +0400 |
|---|---|---|
| committer | Suleyman Farajli <suleyman@farajli.net> | 2025-11-25 18:47:27 +0400 |
| commit | d947956270b092df10637bb3531441caca698b86 (patch) | |
| tree | 8c32170ef044687b11be79398140a36430e2ff0a /scripts/gui/sclip | |
| parent | c388ade6b6d955138698731af02dfbe5c676439a (diff) | |
feat: new api for scripts
Diffstat (limited to 'scripts/gui/sclip')
| -rwxr-xr-x | scripts/gui/sclip | 69 |
1 files changed, 35 insertions, 34 deletions
diff --git a/scripts/gui/sclip b/scripts/gui/sclip index 4854aa1..56ea57f 100755 --- a/scripts/gui/sclip +++ b/scripts/gui/sclip @@ -1,71 +1,72 @@ #!/bin/sh -. slib +. "lib_common.sh" +. "lib_handle.sh" help() { cat << EOF -${0}: print the clipboard content +${progname}: print the clipboard content options: - -h Print this message and exit -s Do not send notifications -c [text] Copy text to clipboard -f [file] Copy content of file into clipboard -i Copy standart input into clipboard + -h Print this message and exit EOF - exit 0 } -check_program "xclip" +clipboard_handle check_program + +if [ "${#}" -eq 0 ]; then + clipboard_handle get-clipboard + exit $? +fi -i_flg=0 +read_stdin=0 silent=0 while getopts "f:c:sih" option; do case "${option}" in f) filepath="${OPTARG}" ;; - c) input="${OPTARG}" ;; - s) silent=1 ;; - - i) i_flg=1 ;; - + i) read_stdin=1 ;; h) help ;; *) invalid_use -h ;; esac done - shift $((OPTIND - 1)) [ "${#}" -ne 0 ] && invalid_use if [ -n "${filepath}" ]; then - mime_type=$(file --mime-type -b "${filepath}") - - if [ "${silent}" -eq 1 ]; then - run "xclip -selection clipboard -t ${mime_type} -i ${filepath}" - fi + mime_type=$(file_handle get-mime_type "$filepath") || { + err "Failed to detect mime type"; exit 1;} - run --success-notify "${filepath} copied" \ - --failure-notify "${filepath} failed to copy" \ - "xclip -selection clipboard -t ${mime_type} -i ${filepath}" -fi + clipboard_handle file "${mime_type}" "${filepath}" + status=$? -[ "${i_flg}" -eq 1 ] && input=$(cat) +else + [ -z "$input" ] && [ "$read_stdin" -eq 1 ] && input=$(cat) + [ -z "$input" ] && err "Empty input" -if [ -n "${input}" ]; then - # Don't use `run` since pipes are used - printf '%s' "${input}" | xclip -selection clipboard + printf '%s' "${input}" | clipboard_handle text + status=$? +fi - if [ $? -eq 0 ]; then - [ "${silent}" -eq 0 ] && send_notification "${argv0}:" "'${input}' copied" - exit 0 +if [ "${silent}" -eq 0 ]; then + if [ "${status}" -eq 0 ]; then + if [ -n "${filepath}" ]; then + notify_handle "Copied file: ${filepath}" + else + notify_handle "Copied text" + fi + else + if [ -n "${filepath}" ]; then + notify_handle "Failed to copy file: ${filepath}" + else + notify_handle "Failed to copy text" + fi fi - - [ "${silent}" -eq 0 ] && send_notification "${argv0}:" "'${input}' failed to copy" - - exit 1 fi - -run "xclip -o -selection clipboard" |
