blob: 35eb7de68673337b98b94c0a98d6e995289eeb23 (
plain) (
blame)
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
|
# session.bash - A session management script for Bash
# Copyright (C) 2021, 2022 Soni L.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# note: consider adding the following to your PS1:
PS1='{$([[ "${HISTFILE#~/.bash_history.}" != "$HISTFILE" ]] && printf "@ " || printf "/ ")${HISTFILE#~/.bash_history.}} '"$PS1"
if [[ "$HISTFILE" = ~/.bash_history ]]; then
HISTFILE="$HISTFILE".default
fi
# simple interface to history/"session" management
# usage: session <name>
# switches HISTFILE to ~/.bash_history.<name>
session() {
local session sessioncwd sessionflags
if [[ "$1" =~ ^[a-z][a-z0-9]*$ ]]; then
if [[ "${HISTFILE#~/.bash_history.}" != "$HISTFILE" ]]; then
session="${HISTFILE#~/.bash_history.}"
sessioncwd="$(grep -s ' # '"$session"'$' ~/.bash_session_cwd | tail -n 1)"
sessionflags="${sessioncwd% # *}"
sessionflags="${sessionflags##* # }"
if (eval "$sessionflags" && test -v NOHIST); then
history -c
fi
fi
# remove session switch from history
history -d -1 >/dev/null 2>/dev/null || true
# write/append history
if shopt -q histappend; then history -a; else history -w; fi
# clear history
history -c
# set HISTFILE
HISTFILE=~/.bash_history."$1"
# load new session's history
history -r
# see setsessioncwd()
eval "$(grep -s ' # '"$1"'$' ~/.bash_session_cwd | tail -n 1)"
else
echo "invalid session"
false
fi
}
# NOHIST handling - clear history on session exit
# FIXME: what happens if another plugin wants to hook EXIT?! (they get replaced/overridden - this could be a problem)
trap '
if [[ "${HISTFILE#~/.bash_history.}" != "$HISTFILE" ]]; then
session="${HISTFILE#~/.bash_history.}"
sessioncwd="$(grep -s '\'' # '\''"$session"'\''$'\'' ~/.bash_session_cwd | tail -n 1)"
sessionflags="${sessioncwd% # *}"
sessionflags="${sessionflags##* # }"
if (eval "$sessionflags" && test -v NOHIST); then
history -c
fi
fi
' exit
# setup completion for session(), using only builtins
complete -C 'source <(printf "%s" '\''
set -- ~/.bash_history."$2"*
flag=0
[[ -e "$1" ]] && for f in "${@#~/.bash_history.}" ; do
if [[ "$f" =~ ^[a-z][a-z0-9]*$ ]] ; then
((flag)) && printf "\n"
printf "%s" "$f"
flag=1
fi
done
'\'')' session
# sets the current cwd as the default cwd for the session
setsessioncwd() {
local session
# guard against relative HISTFILE
if [[ "${HISTFILE#~/.bash_history.}" != "$HISTFILE" ]]; then
session="${HISTFILE#~/.bash_history.}"
if [[ "$session" =~ ^[a-z][a-z0-9]*$ ]]; then
printf 'cd %q # %s\n' "$PWD" "$session" >>~/.bash_session_cwd
else
echo "invalid session"
false
fi
else
echo "invalid session"
false
fi
}
# saves the current working directory into the current history.
# does NOT save to the history *file*.
savecwd() {
history -s "$(printf 'cd %q' "$PWD")"
}
echo "session.bash Copyright (C) 2021, 2022 Soni L."
echo "SPDX-License-Identifier: AGPL-3.0-or-later."
echo "This software is made with love by a queer trans person."
|