-
Notifications
You must be signed in to change notification settings - Fork 0
/
friendly.bash-0.2
209 lines (174 loc) · 3.81 KB
/
friendly.bash-0.2
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# This is a bash script library to help make bash scripting easier
# and more human-readable.
#
# I created this because I noticed there were certain things you
# could do in Bash, such as escape commands, which are awesome to
# use, yet not very human-readable. I like a more human-readable
# language.
#
# So this is basically a set of functions I'm using and making
# available to the public under the MIT license.
## Cursor Visibility
hideCursor() {
printf '\e[?25l'
}
showCursor() {
printf '\e[?25h'
}
## Line Wrapping
lineWrapping() {
lwoption=$@
if [ $lwoption = "enabled" ]; then
printf '\e[?7h'
elif [ $lwoption = "disabled" ]; then
printf '\e[?7l'
else
printf "lineWrapping: Invalid option - $lwoption \n \
\"enabled\" or \"disabled\" expected. \n"
fi
}
# Message and Cursor Placement
# Prints a message on a specified line
printAtLine() {
local LINE=$1
shift 1
echo -e "\033[${LINE}H$@"
}
# Prints a message starting on a specific line and column
printAtLineCol() {
local LINE=$1
local COL=$2
shift 2
printf "\e[${LINE};${COL}H$@"
}
gotoLine() {
local LINE=$1
printf "\e[${LINE}H"
}
gotoLineCol() {
local LINE=$1
local COL=$2
printf "\e[${LINE};${COL}H"
}
moveUp() {
local LINES=$1
printf "\e[${LINES}A"
}
moveDown() {
local LINES=$1
printf "\e[${LINES}B"
}
moveLeft() {
local COLS=$1
printf "\e[${COLS}D"
}
moveRight() {
local COLS=$1
printf "\e[${COLS}C"
}
# File Selector (will require fzf)
fileSelector() {
local extension="*"
local multi=""
local directory="."
# Join all arguments into a single string
local args="$@"
# Parse arguments
if [[ "$@" == *"-m"* ]] || [[ "$@" == *"--multi"* ]]; then
multi="--multi"
fi
for arg in "$@"; do
if [[ $arg != -* ]]; then
if [[ -d $arg ]]; then
directory=$arg
else
extension=$arg
fi
fi
done
if command -v fzf >/dev/null 2>&1; then
SelectedFiles=$(find "$directory" -type f -name "*$extension*" 2>/dev/null | fzf $multi --prompt 'Files:')
echo "$SelectedFiles"
else
echo "FZF isn't installed."
fi
}
## File manipulation
# Replaces specified text in a file with new text.
replaceInFile() {
local file="$1"
local search="$2"
local replace="$3"
sed -i "s/${search}/${replace}/g" "$file"
}
# Checks to see if a file exists
fileExists() {
local file_path=$1
[[ -f "$file_path" ]]
}
# Checks to see if a directory exists
directoryExists() {
local dir_path=$1
[[ -d "$dir_path" ]]
}
# Extracts a column from a .csv file
extractColumn() {
local file="$1"
local column="$2"
awk -F ',' "{print \$$column}" "$file"
}
# Filters lines of a file by a pattern
filterLines() {
local file=$1
local pattern=$2
local separator=$3
if [ "$separator" = "newline" ]; then
separator='\n' # Set separator to \n for "newline" option
fi
local filtered_lines=""
while IFS= read -r line; do
filtered_lines+="$line$separator"
done < <(grep "$pattern" "$file")
echo -n "$filtered_lines"
}
# Confirmation prompt
confirm() {
if [[ $@ ]]
then
confirmMessage="$@"
else
confirmMessage="Are you sure?"
fi
while true; do
read -p "$confirmMessage [y/N]: " choice
case "$choice" in
y|Y )
return 0;;
n|N )
return 1;;
* )
echo "Invalid input. Please enter 'y' for Yes or 'n' for No."
esac
done
}
# User Interface Functions
# Progress Bar
progressBar() {
local total_steps=$1
# Check if the total number of steps is zero
if [[ $total_steps -eq 0 ]]; then
echo "Total steps cannot be zero."
return 1
fi
# Pipe the input stream to pv to display the progress bar
pv -l -s "$total_steps" >/dev/null
}
setScrollArea() {
FIRSTLINE=$1
SECONDLINE=$2
printf "\e[${FIRSTLINE};${SECONDLINE}r"
gotoLine $1
}
releaseScrollArea() {
printf "\e[;r"
}