-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaste
More file actions
executable file
·79 lines (66 loc) · 2.14 KB
/
paste
File metadata and controls
executable file
·79 lines (66 loc) · 2.14 KB
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
#!/usr/bin/env bash
# Check if the terminal supports colors
if tput setaf 1 >/dev/null 2>&1; then
COLOR_INFO="$(tput setaf 4)" # Bright Blue for general info
COLOR_WARN="$(tput setaf 3)" # Yellow for warnings
COLOR_SUCCESS="$(tput setaf 2)" # Green for success
COLOR_RESET="$(tput sgr0)"
else
COLOR_INFO=""
COLOR_WARN=""
COLOR_SUCCESS=""
COLOR_RESET=""
fi
# Function to print messages with appropriate colors
echoinfo() {
printf "%s%s%s\n" "$COLOR_INFO" "$1" "$COLOR_RESET" >&2
}
echowarn() {
printf "%s%s%s\n" "$COLOR_WARN" "$1" "$COLOR_RESET" >&2
}
echosuccess() {
printf "%s%s%s\n" "$COLOR_SUCCESS" "$1" "$COLOR_RESET" >&2
}
# Display help message
show_help() {
printf "Usage: %s [OPTION]\n" "$0"
printf "Capture input (interactively or from a pipe) and echo it back to stdout.\n"
printf "The input is also saved to /tmp/last_paste for future reference.\n\n"
printf "Options:\n"
printf " -h, --help Show this help message and exit\n\n"
printf "Examples:\n"
printf " %s # Capture input interactively (type or paste text)\n" "$0"
printf " echo 'Hello' | %s # Capture input from a pipe\n" "$0"
printf " %s < file # Capture input from a file\n" "$0"
printf "\nNotes:\n"
printf " - If 'bat' is installed, it will be used for input handling.\n"
printf " - If no input is provided, the script will exit with a warning.\n"
printf " - The output is saved to /tmp/last_paste.\n"
exit 0
}
# Check for help option
case "$1" in
-h | --help) show_help ;;
esac
# Determine the appropriate cat command
if command -v bat >/dev/null 2>&1; then
CAT_COMMAND="bat"
else
CAT_COMMAND="cat"
fi
# Capture input
if [ -t 0 ]; then # Interactive mode
echoinfo "📋 Enter input and press Enter (Ctrl+D to finish):"
fi
input_content=$($CAT_COMMAND)
if [ -z "$input_content" ]; then
echowarn "⚠️ No input received!"
exit 1
fi
# Save input to file and echo it back
output_file="/tmp/last_paste"
printf "%s\n" "$input_content" >"$output_file"
echosuccess "✅ Echoing the input to stdout!"
printf "%s\n" "$input_content"
# Set up LPASTE environment variable and function
export LPASTE_FILE="$output_file"