aboutsummaryrefslogtreecommitdiffstats
path: root/zuwu.zsh
blob: cacfd5a7240761d173876fa2919694d636665c30 (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
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
# zuwu: cute lil zsh microframework thingy
# ~ memdmp

export _ZUWU_INSTALLED=1;

# Completion Handling
if ! grep '# The following lines were added by compinstall' ~/.zshrc >/dev/null 2>/dev/null && ! grep 'compinit' ~/.zshrc; then
  <<EOF >> ~/.zshrc
# Note: Do not remove the 'The following lines were...' comment!
# The following lines were added by compinstall
zstyle ':completion:*' completer _complete _ignored _approximate
zstyle ':completion:*' format 'Completing %d'
zstyle ':completion:*' group-name ''
zstyle ':completion:*' list-colors ''
zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s
zstyle ':completion:*' matcher-list 'm:{[:lower:]}={[:upper:]} m:{[:lower:][:upper:]}={[:upper:][:lower:]} r:|[._-]=** r:|=** l:|=*' '' '' ''
zstyle ':compinstall' filename "\$HOME/.zshrc"

autoload -Uz compinit
compinit
# End of lines added by compinstall
EOF
fi
# History Size Determination
if ! grep 'HISTSIZE' ~/.zshrc >/dev/null 2>/dev/null; then
  <<EOF >>~/.zshrc
# Lines configured by zsh-newuser-install
HISTFILE="~/.histfile"
HISTSIZE="10000"
SAVEHIST="10000"
# End of lines configured by zsh-newuser-install
EOF
fi

# -
noop() {}

# Ensure ~/.local/bin is in the PATH - note that this will "match" if something like `.*$HOME/.local/bin.*`, i don't care
if ! grep "$HOME/.local/bin" <<< "$PATH"; then
  echo -n 'if ! grep "$HOME/.local/bin" <<< "$PATH"; then
  export PATH="$PATH:$HOME/.local/bin"
fi
' >> "$HOME/.zshenv"
  export PATH="$PATH:$HOME/.local/bin"
fi
# Ensure ~/.local/bin exists
if ! [[ -d "$HOME/.local/bin" ]]; then
  mkdir "$HOME/.local/bin" || echo "Failed to create '$HOME/.local/bin' for your sanity's sake. For your own sanity, please make this directory."
fi

# Load some shit that you will need
autoload -U up-line-or-beginning-search
autoload -U down-line-or-beginning-search

# Keybinds and all that jazz
reloadopt() {
  if [[ -f "$HOME/.zshconf" ]]; then
    if ! [[ -d "$HOME/.config" ]]; then
      mkdir "$HOME/.config"
    fi
    mv "$HOME/.zshconf" "$HOME/.config/zuwu.conf"
  fi
  if [[ -f "$HOME/.config/zuwu.conf" ]]; then
    source "$HOME/.config/zuwu.conf"
  fi
  
  __bindkey() {
    if [[ "$1" != "" ]]; then
      bindkey "$1" "$2"
    fi
  }

  noop "${WILL_SHARE_HISTORY:=false}"
  noop "${KEYBIND_FORWARD_WORD:="^[[1;5C"}"
  noop "${KEYBIND_BACKWARD_WORD:="^[[1;5D"}"
  noop "${DO_AUTOCD:=true}"
  noop "${DO_ERR_BEEP:=false}"
  noop "${DO_EXTENDED_BLOB:=true}"
  noop "${LINE_OR_BEGIN_SEARCH_KEY:=true}"
  noop "${DELETE_CHAR:=true}"
  noop "${INSEND_MOVE_KEY:=true}"
  noop "${BINDKEY_KIND:=e}"

  __opt() {
    if "$1"; then
      setopt "$2"
    else
      unsetopt "$2"
    fi
  }

  if [[ "$BINDKEY_KIND" == "e" ]] || [[ "$BINDKEY_KIND" == "v" ]]; then
    bindkey -"$BINDKEY_KIND"
  fi
  
  __opt "$WILL_SHARE_HISTORY" SHARE_HISTORY
  __bindkey "$KEYBIND_FORWARD_WORD" forward-word
  __bindkey "$KEYBIND_BACKWARD_WORD" backward-word
  __opt "$DO_AUTOCD" autocd
  __opt "$DO_ERR_BEEP" beep
  __opt "$DO_EXTENDED_BLOB" extendedglob
  if "$LINE_OR_BEGIN_SEARCH_KEY"; then
    _bkey() {
      zle -N "$1"
      bindkey "$2" "$1"
    }
    _bkey "up-line-or-beginning-search" "^[[A" # Up
    _bkey "down-line-or-beginning-search" "^[[B" # Down
  else
    bindkey -r "^[[A"
    bindkey -r "^[[B"
  fi
  if "$DELETE_CHAR"; then
    bindkey "^[[3~" delete-char
    bindkey "^[[3;2~" delete-wo0rd
  else
    bindkey -r "^[[3~"
    bindkey -r "^[[3;2~"
  fi
  if "$INSEND_MOVE_KEY"; then
    bindkey "^[[H" beginning-of-line
    bindkey "^[[F" end-of-line
  else
    bindkey -r "^[[H"
    bindkey -r "^[[F"
  fi
}
reloadopt

# Fix $HOME path being entirely inlined in zsh completion shit (or any other string at the beginnig thereof)
if grep "\"$HOME" "$HOME/.zshrc"; then
  sed -i "s|\"$HOME|\"\$HOME|g" "$HOME/.zshrc"
  zstyle :compinstall filename "$HOME/.zshrc"
fi

# Load zgen, if present
if [[ -d "$HOME/.zgen" ]]; then
  source "$HOME/.zgen/zgen.zsh"
  ZGEN=true
fi

# Utilities
_each_share_dir() {
  for sdir in /usr/share /usr/local/share "$HOME/.local/share"; do
    for arg in "$@"; do
      echo "$sdir/$arg"
    done
  done
}

# Plugins
# > ZSH Autosuggestions
for f in "$(_each_share_dir zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh zsh-autosuggestions/zsh-autosuggestions.zsh)"; do
  if [[ -f "$f" ]]; then
    source "$f"
    break
  fi
done
# > ZSH Syntax Highlighting
for f in "$(_each_share_dir zsh/plugins/fast-syntax-highlighting/fast-syntax-highlighting.plugin.zsh zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh)"; do
  if [[ -f "$f" ]]; then
    source "$f"
    break
  fi
done

# Cute Prompting
if [[ -f ~/.config/starship.toml ]]; then
  eval "$(starship init zsh)"
else
  _SYS_USER_COLOUR="%F{22}"
  _SYS_SYSTEM_COLOUR="%B%F{green}"
  if [[ "$(whoami)" == "root" ]]; then
    _SYS_USER_COLOUR="%F{red}"
  fi
  PROMPT="$(echo -n "${_SYS_USER_COLOUR}%n%f%F{7}@%f${_SYS_SYSTEM_COLOUR}%m%f%b%F{7} in %f%B%F{27}%~%f%b\n%B%F{magenta}%(?..%F{red})❯ %f%b")"
  RPROMPT="%B%F{magenta}%(?..%F{red})%?%f%b"
fi

# Custom History Handling
__sethist() {
  HISTDIR="${HISTDIR:-"$HOME/.zsh_history"}"
  HISTID="${HISTID:-"default"}"
  if [[ -f "$HISTDIR" ]]; then
    mv "$HISTDIR" /tmp/history
    mkdir "$HISTDIR"
    mv /tmp/history "$HISTDIR/$HISTID"
  fi
  HISTFILE="$HISTDIR/$HISTID"
  if [[ "$HISTFILE" == "$HISTDIR/" ]]; then
    HISTID="default"
    HISTFILE="$HISTDIR/default"
  fi;
}

__sethist

# For some reason, 30 tends to be a common "default" setting for this when not manually specified on shitty systems
if [[ "$HISTSIZE" == "30" ]]; then
  HISTSIZE="10000"
fi
if [[ "$SAVEHIST" == "30" ]]; then
  SAVEHIST="10000"
fi