aboutsummaryrefslogtreecommitdiffstats
path: root/cdlog.sh
blob: fd1b22080608ca4146857489384bd39b51142e73 (plain)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# License at bottom.

# Globals
declare -A cdlog_alias            # cd aliases

# Configuration variables
cdlog_lru=${cdlog_lru-}

# Update after FIFO change.
cdlog.update()
{
  # Update the one-letter nicknames for most recent four dirs.
  x=$c1
  y=$c2
  z=$c3
  w=$c4

  # Persist to disk.
  cat > "$cdlog_dirs" <<!
$PWD
$c1
$c2
$c3
$c4
$c5
$c6
$c7
$c8
$c9
!
}

# Set state from args
cdlog.args()
{
  c1=$1; c2=$2; c3=$3
  c4=$4; c5=$5; c6=$6
  c7=$7; c8=$8; c9=$9

  cdlog.update
}

# Utility for nth positional param
cdlog.get_param()
{
  if shift $1 ; then
    echo "$1"
  fi
}

# Initialize state, allocating new ~/.cdlog.N.dirs storage,
# with LRU replacement.
cdlog.init()
{
  local i
  local oldest
  local context

  c9=; c8=; c7=
  c6=; c5=; c4=
  c3=; c2=

  cdlog_dirs=
  context=

  for i in {1..10}; do
    if ! [ -f ~/.cdlog.$i.dirs ] ; then
      context=$i
      break
    fi
  done

  if ! [ $context ] ; then
    oldest=1
    for i in {2..10}; do
      if [ ~/.cdlog.$i.dirs -ot ~/.cdlog.$oldest.dirs ] ; then
        oldest=$i
      fi
    done
    context=$oldest
  fi

  cdlog_dirs=~/.cdlog.$context.dirs
  rm -f $cdlog_dirs

  set -- ~/.cdlog.*.dirs
  if [ $# -gt 0 -a $1 != ~/.cdlog.'*'.dirs ] ; then
    printf "Use 'cdr' or 'cdlog.recover' to switch to prior cdlog context.\n"
  fi
}

cdlog.recover()
{
  local -a d
  local dir
  local i
  local sel

  set -- ~/.cdlog.*.dirs

  if [ $# -gt 0 -a $1 != ~/.cdlog.'*'.dirs ] ; then
    printf "These cdlog contexts exist:\n"
    i=0
    for dir in "$@"; do
      printf "[%d]: %s\n" $((++i)) "$(date -r "$dir")"
      sed -e '/^$/d' "$dir"
    done

    while true; do
      if [ $i -eq 1 ] ; then
        printf "Use 1 to select context, c1 to clone,\n"
      else
        printf "Use 1-%s to select context, c1-c%s to clone,\n" $i $i
      fi
      printf "or Enter to keep current context: "
      read sel
      case $sel in
      ( [1-9] )
        sel=$(cdlog.get_param $sel "$@")
        if [ -n "$sel" ] ; then
          cdlog_dirs=$sel
          break
        fi
        ;;
      ( c[1-9] )
        sel=$(cdlog.get_param ${sel#c} "$@")
        if [ -n "$sel" ] ; then
          cp "$sel" "$cdlog_dirs"
          break
        fi
        ;;
      ( "" )
        break
        ;;
      esac
    done
  fi

  if [ -f "$cdlog_dirs" ] ; then
    mapfile -t d < "$cdlog_dirs"
    command cd "${d[0]}"
    unset d[0]
    cdlog.args "${d[@]}"
  fi
}

# Change to directory: this is aliased to cd command.
cdlog.chdir()
{
  local cur=$PWD
  local def

  if [ $# -eq 2 -a "$1" = -P ] ; then
    case $2 in
    ( */* )
      ;;
    ( * )
      def=${cdlog_alias[$2]}
      if [ -n "$def" ] ; then
          set -- -P "$def"
      fi
      ;;
    esac
  fi

  if command cd "$@" && [ "$PWD" != "$cur" ]; then
    # only if we successfully change to a different
    # directory do the following

    if [ $cdlog_lru ] ; then
      local nx=
      local pv=$cur
      local i
      local d=("" "$c1" "$c2" "$c3" "$c4" "$c5" "$c6" "$c7" "$c8" "$c9")
      unset d[0]

      for ((i = 1; i <= 9; i++)); do
        nx=${d[$i]}
        d[$i]=$pv
        if [ "$nx" = "$PWD" ] ; then
          break
        fi
        pv=$nx
      done

      cdlog.args "${d[@]}"
    else
      c9=$c8; c8=$c7; c7=$c6
      c6=$c5; c5=$c4; c4=$c3
      c3=$c2; c2=$c1

      c1=$cur

      cdlog.update
    fi
  fi
}

# Rotate through specified directories.
cdlog.rot()
{
  local cur=$PWD
  local p=$cur
  local n=$cur
  local d=("" "$c1" "$c2" "$c3" "$c4" "$c5" "$c6" "$c7" "$c8" "$c9")
  local first=y

  unset d[0]

  [ $# -gt 0 ] || set 1

  while [ $# -gt 0 ] ; do
    n=$1; shift
    case $n in
    ( [1-9] )
      if [ $first ] ; then
        command cd "${d[$n]}" || return
        printf "%s\n" "$PWD"
      else
        d[$p]=${d[$n]}
      fi
      first=
      p=$n
      ;;
    ( * )
      printf "cdlog.rot: bad argument: %s\n" "$n"
      return 1
      ;;
    esac
  done

  d[$p]=$cur

  cdlog.args "${d[@]}"
}


# Change to most recent diretory in cdlog and remove it
# from the log.
cdlog.pop()
{
  local d=("" "$c1" "$c2" "$c3" "$c4" "$c5" "$c6" "$c7" "$c8" "$c9")
  local n=1
  local i

  unset d[0]

  if [ $# -gt 0 ] ; then
    n=$1; shift
  fi

  case $n in
  ( [1-9] )
    if command cd "${d[$n]}" ; then
      for ((i = n; i <= 8; i++)); do
        d[$i]=${d[$((i + 1))]}
      done
      d[9]=
      cdlog.args "${d[@]}"
      printf "%s\n" "$PWD"
    fi
    ;;
  ( * )
    printf "cdlog.pop: bad argument: %s\n" "$n"
    return 1
    ;;
  esac
}

# Print four recent cdlog entries.
cdlog()
{
  printf "1: %s\n" "$c1"
  printf "2: %s\n" "$c2"
  printf "3: %s\n" "$c3"
  printf "4: %s\n" "$c4"

  if [ "${1-}" = "-l" ] ; then
    printf "5: %s\n" "$c5"
    printf "6: %s\n" "$c6"
    printf "7: %s\n" "$c7"
    printf "8: %s\n" "$c8"
    printf "9: %s\n" "$c9"
  fi
}

# Menu-based cd/cs.
cdlog.mcd()
{
  local sel=
  local swap=
  local res=1
  local i
  local d=("" "$c1" "$c2" "$c3" "$c4" "$c5" "$c6" "$c7" "$c8" "$c9")

  if [ $# -gt 0 ] && [ "$1" = -s ] ; then
    swap=y
  fi

  for i in {1..9}; do
    printf "%s: %s\n" $i "${d[$i]}"
  done

  read -p "? " sel

  for i in {1..10}; do
    printf "\e[A\e[K"
  done

  case "$sel" in
  ( [1-9] )
    if [ $swap ] ; then
      cdlog.rot $sel
    else
      if cdlog.chdir -P "${d[$sel]}" ; then
        printf "%s\n" "$PWD"
      fi
    fi
    res=$?
    ;;
  esac

  return $res
}

cdlog.alias()
{
  local ali=
  local def=

  if [ $# -ne 2 ] ; then
    printf "cdlog.alias: two arguments required\n"
    return 1
  fi

  cdlog_alias[$1]=$2
  return 0
}

# Tab completion for cd aliases

cdlog.complete()
{
  COMPREPLY=()
  local cur=${COMP_WORDS[COMP_CWORD]}
  local alias
  local dir

  COMPREPLY+=( $(compgen -d -- "$cur") )

  for alias in "${!cdlog_alias[@]}"; do
    case "$alias" in
    ( "$cur"* )
      COMPREPLY+=( "$alias" )
      ;;
    esac
  done

  IFS=':' set -- $CDPATH
  for dir in "$@"; do
    COMPREPLY+=( $(compgen -d -- "$dir/$cur") )
  done

  return  0
}

# Aliases.
alias cd='cdlog.chdir -P'
alias pd='cdlog.pop'
alias cs='cdlog.rot'
alias cl='cdlog'
alias cll='cdlog -l'
alias mcd='cdlog.mcd'
alias mcs='cdlog.mcd -s'
alias cdr='cdlog.recover'
alias cdalias='cdlog.alias'

# Better completion for $x[Tab]
shopt -s direxpand

# Initialize cdlog the first time.

c1=${c1-}

if [ -z "$c1" -o -z "$cdlog_dirs" ] ; then
  cdlog.init
fi

# Register completion
complete -F cdlog.complete cd

# Copyright 2024
# Kaz Kylheku <kaz@kylheku.com>
# Vancouver, Canada
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following condition is met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form need not reproduce any copyright notice.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.