-
Notifications
You must be signed in to change notification settings - Fork 0
/
cb
executable file
·138 lines (111 loc) · 2.86 KB
/
cb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/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