-
Notifications
You must be signed in to change notification settings - Fork 0
/
history-cleaner
executable file
·219 lines (184 loc) · 5.27 KB
/
history-cleaner
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
210
211
212
213
214
215
216
217
218
219
#!/bin/sh
# shellcheck shell=dash
# POSIX-compliant shell script to clean up raw shell history files
USAGE="Usage: $0 [--mode <mode>] <command>
Available modes: remove (default), trash
Available commands: cleanup, list, delete, concat
- cleanup: removes all raw history files that have only one line
- list: lists all raw history files that have less than the specified number of lines
- delete: deletes all raw history files that have less than the specified number of lines
- concat: concatenates all raw history files that have less than the specified number of lines into a new file and opens it in the editor, then removes or moves the old files"
# Default value for the trash directory
TRASH_DIR=${TRASH_DIR:="$HOME/.local/share/Trash/files"}
# Error messages
ERROR_MISSING_VAR='Required environment variable is not set: HISTORY_DIR.'
ERROR_UNSUPPORTED_ACTION='Error: unsupported action'
ERROR_UNSUPPORTED_MODE='Error: unsupported mode'
# logging functions
log() {
printf '%s\n' "$1" >&2
}
error() {
printf '%s\n' "$1"
exit 1
}
# Ask a question and expect a yes or no answer
# Usage: ask_question "Question" "y" && echo "Yes" || echo "No"
ask_question() {
question="$1"
printf "%s [y/N] " "$question"
read -r response
case $response in
[yY] | [yY][eE][sS]) return 0 ;;
*) return 1 ;;
esac
}
# Get the maximum number of lines for a file to be considered 'short'
get_max_lines(){
printf "Enter the maximum number of lines for a file to be considered 'short': " >&2
read -r max_lines
echo "$max_lines"
}
# Find all history files in the raw history directory
find_history_files() {
find "${HISTORY_DIR}/raw" -type f -name '*.sh' -print
}
# Filter the files whose number of lines is lower of equal that the given threshold
having_max_lines() {
max_lines="$1"
while read -r file; do
lines=$(wc -l < "$file" | awk '{print $1}')
if [ "$lines" -le "$max_lines" ]; then
printf '%s\n' "$file"
fi
done
}
# Remove or move files based on the remove_mode global variable
remove_or_move_files() {
while read -r file; do
if $remove_mode; then
rm "$file"
else
mv "$file" "$TRASH_DIR"
fi
done
}
# List all history files that have less than max_lines lines
list_short_files() {
max_lines="$1"
find_history_files | (
while read -r file; do
lines=$(wc -l <"$file")
if [ "$lines" -le "$max_lines" ]; then
log "File: $file"
cat "$file"
echo ""
fi
done
)
}
# Remove all history files that have less than max_lines lines
delete_short_files() {
max_lines="$1"
find_history_files | having_max_lines "$max_lines" | remove_or_move_files
}
# Remove all history files that have only one line
cleanup_empty_files() {
delete_short_files 1
}
# Concatenate all history files that have less than max_lines lines into a new file
# and open it in the editor, then remove or move the old files.
concat_short_files() {
max_lines="$1"
new_file_name="${2:-history_short_files_$(date +"%Y%m%d_%H%M%S").sh}"
new_file="${HISTORY_DIR}/raw/$new_file_name"
# Collect the list of files to be concatenated
files_to_concat=$(find_history_files | having_max_lines "$max_lines")
# Concatenate the files
printf '%s\n' "$files_to_concat" | while read -r file; do
cat "$file" >>"$new_file"
echo "" >>"$new_file"
done
# Open the new file in the editor
"${EDITOR:-vi}" "$new_file"
# Confirm before removing files
if ! ask_question "Do you want to remove the old files?"; then
exit 0
fi
# Remove or move the old files
echo "$files_to_concat" | remove_or_move_files
}
# Argument parsing function
parse_arguments() {
# parse command line options using getopt
if ! options=$(getopt -o h --long help,mode: -n 'history-cleaner' -- "$@"); then
error "$ERROR_PARSING"
fi
# set positional parameters
eval set -- "$options"
# Set default values for options
mode='remove'
remove_mode=true
# process options
while true; do
case $1 in
--mode)
mode="$2"
shift 2
;;
-h | --help)
log "$USAGE"
exit 0
;;
--)
shift
break
;;
*)
error "$ERROR_PARSING"
;;
esac
done
case "$mode" in
"remove") remove_mode=true ;;
"trash") remove_mode=false ;;
*) error "$ERROR_UNSUPPORTED_MODE" ;;
esac
# Get arguments
if [ $# -lt 1 ]; then
error 'Error: action is required'
fi
# set action
action=$1
# check if action is supported
if [ "$action" != 'cleanup' ] && [ "$action" != 'list' ] && [ "$action" != 'delete' ] && [ "$action" != 'concat' ]; then
error "$ERROR_UNSUPPORTED_ACTION"
fi
}
# main function
main() {
action="$1"
remove_mode="$2"
case $action in
"cleanup") cleanup_empty_files;;
"list") list_short_files "$(get_max_lines)";;
"delete") delete_short_files "$(get_max_lines)";;
"concat") concat_short_files "$(get_max_lines)";;
*)
echo "Usage: $0 <command> [-m remove|trash]" >&2
echo "Available commands: cleanup, list, delete, concat" >&2
exit 1
;;
esac
}
# shellcheck disable=3028
if [ "$0" = "${BASH_SOURCE:-$0}" ]; then
# Call the argument parsing function
parse_arguments "$@"
# Check if required environment variables are set
if [ -z "$HISTORY_DIR" ]; then
error "$ERROR_MISSING_VAR"
fi
# call main function
main "$action" "$remove_mode"
fi