-
Notifications
You must be signed in to change notification settings - Fork 0
/
localhistory.sh
executable file
·221 lines (212 loc) · 9.45 KB
/
localhistory.sh
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
220
221
#!/bin/bash
# Local history for Bash
# Version 1.4
# Use `lht help` for help and more information
# Copyright (c) 2023 Sqaaakoi
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# Prompt command magic
# This is NOT automatically injected; you must run this script with "--prompt-command" to inject it.
# This behaviour is intended so you can easily remove that argument from your .bashrc or similar so that you can override $PROMPT_COMMAND yourself.
# Please note that this must be injected to $PROMPT_COMMAND for this entire script to function correctly.
__localhistory_prompt_command() {
# r/w history before running commands, helps with multiple shells
history -a; history -n;
$@
__localhistory .
}
# Inject prompt command if "--prompt-command" is specified
if [ "$1" == "--prompt-command" ]; then
__PROMPT_COMMAND() {
__localhistory_prompt_command
$@
}
export PROMPT_COMMAND="__PROMPT_COMMAND $PROMPT_COMMAND"
fi
# Local history per project
# Default history file
export __LOCALHISTORY_DEFAULT="$HISTFILE"
# File name for local history log
export __LOCALHISTORY_FILENAME=".local_bash_history"
# Current status of local history being enabled
export __LOCALHISTORY_ENABLED="true"
# Set to true if the local history has just been updated
export __LOCALHISTORY_RECENTLY_UPDATED="false"
# If the local history is active
export __LOCALHISTORY_ACTIVE="false"
# Find history file
__localhistory() {
if [ "$__LOCALHISTORY_ENABLED" = "false" ]; then
if [ "$__LOCALHISTORY_RECENTLY_UPDATED" = "true" ]; then
__LOCALHISTORY_RECENTLY_UPDATED="false"
HISTFILE="$__LOCALHISTORY_DEFAULT"
return
fi
fi
if [ "$__LOCALHISTORY_ENABLED" = "true" ]; then
local __LOCALHISTORY_TRAVERSAL="$(__localhistory_realpath "$1")"
# Is there a writable bash history file here?
if [ -w "$(__localhistory_realpath "$__LOCALHISTORY_TRAVERSAL/$__LOCALHISTORY_FILENAME")" ]; then
# Only set and display if new history file is found
__localhistory_update "$__LOCALHISTORY_TRAVERSAL/$__LOCALHISTORY_FILENAME"
export __LOCALHISTORY_ACTIVE="true"
elif [ "$__LOCALHISTORY_TRAVERSAL" != "/" ]; then
# Try traversing up to look again if we aren't already in root
__localhistory "$__LOCALHISTORY_TRAVERSAL/.."
else
# Fallback to home directory
__localhistory_update "$__LOCALHISTORY_DEFAULT"
export __LOCALHISTORY_ACTIVE="false"
fi
fi
}
# Update the history file
__localhistory_update() {
# $1 is the file
if [ "$HISTFILE" != "$(__localhistory_realpath "$1")" ]; then
# Write last history file
history -a
# Clear loaded history so history can be replaced
history -c
HISTFILE="$(__localhistory_realpath "$1")"
export __LOCALHISTORY_RECENTLY_UPDATED="true"
# Read history
history -r;
else
export __LOCALHISTORY_RECENTLY_UPDATED="false"
fi
}
# Used for paths with spaces in name
__localhistory_realpath() {
realpath "$@"
}
# Local history management command
lht() {
if [ "$1" = "status" ] || [ -z "$1" ]; then
if [ "$__LOCALHISTORY_ENABLED" == "false" ]; then
echo -e "\033[96mLocal history is \033[91mdisabled\033[00m"
lht filename
lht file
return 2
else
if [ "$__LOCALHISTORY_ACTIVE" = "false" ]; then
echo -e "\033[96mLocal history is \033[36minactive\033[00m"
lht filename
lht file
return 1
else
echo -e "\033[96mLocal history is \033[92mactive\033[00m"
lht filename
lht file
return 0
fi
fi
fi
if [ "$1" = "filename" ]; then
echo -e "\033[96mLocal history filename: \033[94m$__LOCALHISTORY_FILENAME\033[94m"
return 0
fi
if [ "$1" = "file" ]; then
echo -e "\033[96mHistory file: \033[94m$HISTFILE\033[00m"
return 0
fi
if [ "$1" = "enable" ] || [ "$1" = "on" ]; then
export __LOCALHISTORY_ENABLED="true"
__localhistory .
export __LOCALHISTORY_RECENTLY_UPDATED="false"
echo -e "\033[92mEnabled\033[96m local history\033[00m"
lht file
return 0
fi
if [ "$1" = "disable" ] || [ "$1" = "off" ]; then
export __LOCALHISTORY_ENABLED="false"
export __LOCALHISTORY_ACTIVE="false"
export __LOCALHISTORY_RECENTLY_UPDATED="true"
echo -e "\033[91mDisabled\033[96m local history\033[00m"
lht file
return 0
fi
if [ "$1" = "create" ] || [ "$1" = "init" ] || [ "$1" = "mk" ]; then
touch "$__LOCALHISTORY_FILENAME"
if [ "$2" = "git" ]; then
echo -e "$__LOCALHISTORY_FILENAME" >> .gitignore
fi
if [ "$2" = "git-local" ]; then
echo -e "$__LOCALHISTORY_FILENAME" >> .git/info/exclude
fi
__localhistory .
lht file
return 0
fi
if [ "$1" = "delete" ] || [ "$1" = "rm" ]; then
if [ "$__LOCALHISTORY_ACTIVE" == "true" ]; then
if [ "$HISTFILE" == "$__LOCALHISTORY_DEFAULT" ]; then
echo -e "\033[33mLocal history file seems to be the same as the default file; not deleting\033[00m"
lht file
return 2
fi;
history -c
rm $HISTFILE
HISTFILE="$__LOCALHISTORY_DEFAULT"
__localhistory .
lht file
return 0
fi;
echo -e "\033[31mLocal history file doesn't exist\033[00m"
lht file
return 1
fi
if [ "$1" = "prompt_status" ]; then
if [ "$__LOCALHISTORY_RECENTLY_UPDATED" = "true" ]; then
echo -e "$5$HISTFILE$6"
return 0
fi
if [ "$__LOCALHISTORY_ENABLED" = "true" ]; then
if [ "$__LOCALHISTORY_ACTIVE" = "true" ]; then
echo -e "$2"
return 0
fi
echo -e "$3"
return 0
fi
echo -e "$4"
return 0
fi
if [ "$1" = "help" ]; then
echo -e "\033[96mlht: Local History Tool\033[00m"
echo -e "\033[36mVersion 1.4\033[00m"
echo -e ""
echo -e "\033[94mRequired arguments are displayed as \033[32m[required]\033[00m"
echo -e "\033[94mOptional / multi-choice arguments are displayed as \033[32m(option|alternate-option)\033[00m"
echo -e ""
echo -e "\033[36mlht\033[00m"
echo -e "\033[36mlht status\033[00m"
echo -e "\033[37m - Shows if local history is enabled, active, and the current history file\033[00m"
echo -e "\033[36mlht filename\033[00m"
echo -e "\033[37m - Shows the name of the local history file\033[00m"
echo -e "\033[36mlht file\033[00m"
echo -e "\033[37m - Shows the current history file\033[00m"
echo -e "\033[36mlht enable\033[00m"
echo -e "\033[36mlht on\033[00m"
echo -e "\033[37m - Enables local history for this session\033[00m"
echo -e "\033[36mlht disable\033[00m"
echo -e "\033[36mlht off\033[00m"
echo -e "\033[37m - Disables local history for this session\033[00m"
echo -e "\033[36mlht mk \033[32m(git|git-local)\033[00m"
echo -e "\033[36mlht init \033[32m(git|git-local)\033[00m"
echo -e "\033[36mlht create \033[32m(git|git-local)\033[00m"
echo -e "\033[37m - Creates a local history file and optionally adds it to .gitignore \033[32m(git)\033[37m or .git/info/exclude \033[32m(git-local)\033[00m"
echo -e "\033[36mlht rm\033[00m"
echo -e "\033[36mlht delete\033[00m"
echo -e "\033[37m - Safely deletes the current local history file if it exists, by checking for conflicts with delete global history file before deletion.\033[00m"
echo -e "\033[36mlht prompt_status \033[32m[active] [inactive] [disabled] [path_prefix] [path_suffix]\033[00m"
echo -e "\033[37m - Shows \033[32m[active]\033[37m if enabled and active, \033[32m[inactive]\033[37m if local history is enabled and inactive, \033[32m[disabled]\033[37m if local history is disabled, and the new history file path surrounded by \033[32m[path_prefix]\033[37m and \033[32m[path_suffix]\033[37m if active and recently updated\033[00m"
echo -e "\033[36mlht help\033[00m"
echo -e "\033[36mlht --help\033[00m"
echo -e "\033[37m - Shows this help message\033[00m"
return 0
fi
echo -e "Unknown subcommand; run \"lht help\""
return 4
}