Skip to content

Commit 4272679

Browse files
committed
Create script for copying file contents
1 parent 54a4fa3 commit 4272679

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
# Scripts
22

33
These are misc scripts I've made over the years to help with different things.
4-
5-
It's migrated from [lindhe/dotfiles](https://github.com/lindhe/dotfiles) and I couldn't be bothered getting the history too.
6-
So the old stuff is in the history over there.

terminal/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Terminal
2+
3+
Terminal helper programs.
4+
5+
* `copy.sh`: Copy the contents of a file to the clipboard.

terminal/copy.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env bash
2+
# Copy the contents of a file to the clipboard.
3+
4+
set -euo pipefail
5+
6+
stderr() {
7+
echo "${@}" 1>&2
8+
}
9+
10+
fail() {
11+
stderr "${1}"
12+
stderr ""
13+
stderr "Exiting …"
14+
exit "${2:-1}"
15+
}
16+
17+
if [[ $# -ne 1 ]]; then
18+
stderr ""
19+
stderr "USAGE:"
20+
stderr " ${0} <file>"
21+
stderr ""
22+
exit 0
23+
fi
24+
25+
if [[ "$(uname -r)" =~ .*microsoft.* ]]; then
26+
declare -r IS_WSL=true
27+
else
28+
declare -r IS_WSL=false
29+
fi
30+
31+
if [[ "${IS_WSL}" == "false" ]]; then
32+
missing_dependencies=false
33+
declare -r dependencies=(
34+
xclip
35+
)
36+
for dep in "${dependencies[@]}"; do
37+
if ! command -v "${dep}" &> /dev/null; then
38+
stderr "❌ ERROR: Missing dependency ${dep}"
39+
missing_dependencies=true
40+
fi
41+
done
42+
if ${missing_dependencies}; then
43+
fail 'Please install the missing dependencies!'
44+
fi
45+
fi
46+
47+
if [[ "${IS_WSL}" == "true" ]]; then
48+
/mnt/c/Windows/System32/clip.exe < "${1}"
49+
else
50+
xclip -selection clipboard < "${1}"
51+
fi

0 commit comments

Comments
 (0)