/
dishrc
76 lines (62 loc) · 2.22 KB
/
dishrc
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
#!/bin/bash
dish_dir="$HOME/.did"
dish_history_log=dish_history
# Keep track of the last directory we were in so that commands like "cd" and "pop" get recorded in the dirctory that they were invoked.
dish_previous_wd="${PWD}"
function dish_log_command()
{
local status=$?
local command
command=$(history +1)
if [[ -z "$command" ]]; then return; fi
if pwd | grep ^"$dish_dir" >/dev/null 2>&1; then
# don't store history of the history directory itself
return
fi
local outdir
outdir="${dish_dir}${PWD}" # Assume that the command didn't change the working directory
# Now check if the command did actually change the working directory
if [[ "${PWD}" != "${dish_history_log}" ]]; then
outdir="${dish_dir}${dish_previous_wd}"
dish_previous_wd=${PWD}
fi
mkdir -p "$outdir"
local history_file="$outdir/$dish_history_log"
if [[ -s "$history_file" ]]; then
local command_id
command_id=$(echo "$command" | awk '{print $1}')
local prev_id
prev_id=$(tail -n 1 "$history_file" | awk '{print $1}')
# repeated/same command
if [[ $command_id -eq $prev_id ]]; then
return;
fi
fi
echo "$command $(date +%Y%m%d_%H:%M:%S) $status" >> "$history_file"
}
# what i did in this directory
function dish()
{
local outdir
outdir="$dish_dir$(pwd)"
[ ! -d "$outdir" ] && mkdir -p "$outdir" # This takes care of a corner case where did is the first command run in a new directory
local history_file="$outdir/$dish_history_log"
if [[ -s "$history_file" ]]; then
cat "$history_file"
fi
}
# Global did
function dishg()
{
find "$dish_dir" -name "$dish_history_log" -print0|xargs -0 cat
}
# If the user hasn't specified the PROMPT_COMMAND then set it up
# otherwise we'll assume they know what they are doing
# and will add dish_log_command manually to their custom PROMPT_COMMAND
#
# disabling this at will potentially block users from accessing the functionality,
# and they won't know why! An experienced bash practicioner should know
# how to deal with this, if it's an issue!
#if [[ -z ${PROMPT_COMMAND} ]]; then
export PROMPT_COMMAND=dish_log_command
#fi