aboutsummaryrefslogtreecommitdiffstats
path: root/cdlog.sh
blob: e26f3741f7ce0ad727a5cec7f39ddad783bbc7a2 (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
# License at bottom.

# Initialize cdlog the first time.

c1=${c1-}

if [ -z "$c1" ] ; then
  c9=; c8=; c7=
  c6=; c5=; c4=
  c3=; c2=
fi

# One-letter nicknames for most recent four dirs.
cdlog.nicks()
{
  x=$c1
  y=$c2
  z=$c3
  w=$c4
}

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

  cdlog.nicks
}

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

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

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

    c1=$cur

    cdlog.nicks
  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()
{
  if [ -n "$c1" ] && command cd "$c1"; then
    c1=$c2; c2=$c3; c3=$c4
    c4=$c5; c5=$c6; c6=$c7
    c7=$c8; c8=$c9;

    c9=

    cdlog.nicks

    printf "%s\n" "$PWD"
  fi
}

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

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

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

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

  read -p "? " sel

  printf "\e[10A\e[J"

  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
}

# Aliases.
alias cd='cdlog.chdir -P'
alias pd='cdlog.pop'
alias cs='cdlog.rot'
alias cl='cdlog'
alias mcd='cdlog.mcd'
alias mcs='cdlog.mcd -s'

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

# 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.