diff options
| author | Anton Bobov <anton@bobov.name> | 2024-03-19 22:43:29 +0500 |
|---|---|---|
| committer | Anton Bobov <anton@bobov.name> | 2024-03-19 22:43:29 +0500 |
| commit | 68d72de4c4b69fcac17c04609ff76960214d7bc0 (patch) | |
| tree | 1b9fa68e5d1d44d069a71905c8e47fdf8d890d0b | |
| parent | e2917305c959106ab01018b4c768505fb2f655ac (diff) | |
Add clipboard script
| -rwxr-xr-x | cb | 139 |
1 files changed, 139 insertions, 0 deletions
@@ -0,0 +1,139 @@ +#!/bin/bash + +# from: https://gist.github.com/RichardBronosky/56d8f614fab2bacdd8b048fb58d0c0c7 + +LINUX_copy() { + cat | xclip -selection clipboard +} + +LINUX_paste() { + xclip -selection clipboard -o +} + +WSL_copy() { + cat | /mnt/c/Windows/System32/clip.exe +} + +WSL_paste() { + /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe Get-Clipboard | sed 's/\r//' +} + +CYGWIN_copy() { + cat >/dev/clipboard +} + +CYGWIN_paste() { + cat /dev/clipboard +} + +MAC_copy() { + cat | pbcopy +} + +MAC_paste() { + pbpaste +} + +stdin_is_a_pipe() { + [[ -p /dev/stdin ]] +} + +stdin_is_a_tty() { + [[ -t 0 ]] +} + +stdin_is_pipe_like() { + stdin_is_a_pipe || ! stdin_is_a_tty +} + +stdout_is_pipe_like() { + ! stdout_is_a_tty # meaning # it must be a pipe or redirection +} + +stdout_is_a_tty() { + [[ -t 1 ]] +} + +requested_open_ended() { + [[ ${args[0]:-} == "-" ]] +} + +requested_test_suite() { + [[ ${args[0]:-} == "--test" ]] +} + +enable_tee_like_chaining() { + # see `man tee` + if stdout_is_pipe_like; then + ${os}_paste + elif requested_open_ended; then + ${os}_paste + echo + fi +} + +prevent_prompt_from_being_on_the_same_line() { + if stdout_is_a_tty; then # we don't have to be strict about not altering the output + echo + fi +} + +detect_os() { + if [[ -f /proc/version ]] && grep -iq Microsoft /proc/version; then + printf WSL + else + case "$(uname -s)" in + Linux*) printf LINUX ;; + Darwin*) printf MAC ;; + CYGWIN*) printf CYGWIN ;; + esac + fi +} + +test_suite() { + # setup + printf '1234' | ${BASH_SOURCE[0]} + + printf "\n1. output to TTY\n" + ${BASH_SOURCE[0]} + printf "1234 should be above.\n" + + printf "\n2. output to pipe\n" + ${BASH_SOURCE[0]} | cat + echo + printf "1234 should be above.\n" + + printf "\n3. input from pipe and output to pipe\n" + printf '1234' | ${BASH_SOURCE[0]} | cat + echo + printf "1234 should be above.\n" +} + +function debug() { + stdin_is_a_pipe && echo "stdin_is_a_pipe: 1" >>/tmp/ono || echo "stdin_is_a_pipe: 0" >>/tmp/ono + stdin_is_a_tty && echo "stdin_is_a_tty: 1" >>/tmp/ono || echo "stdin_is_a_tty: 0" >>/tmp/ono + stdin_is_pipe_like && echo "stdin_is_pipe_like: 1" >>/tmp/ono || echo "stdin_is_pipe_like: 0" >>/tmp/ono + stdout_is_pipe_like && echo "stdout_is_pipe_like: 1" >>/tmp/ono || echo "stdout_is_pipe_like: 0" >>/tmp/ono + stdout_is_a_tty && echo "stdout_is_a_tty: 1" >>/tmp/ono || echo "stdout_is_a_tty: 0" >>/tmp/ono + echo >>/tmp/ono +} + +main() { + os="$(detect_os)" + if stdin_is_pipe_like; then + "${os}_copy" + enable_tee_like_chaining + else # stdin is not pipe-like + "${os}_paste" + prevent_prompt_from_being_on_the_same_line + fi +} + +args=("$@") +if requested_test_suite; then + export DEBUG=1 + test_suite +else + [[ ${DEBUG:-} == 1 ]] && debug + main +fi |
