1234567891011121314151617181920: ${KOH_COLOR_DIR:=73}21: ${KOH_COLOR_VERSION:=214}22: ${KOH_COLOR_META:=245}23: ${KOH_COLOR_ALERT:=203}24: ${KOH_SHOW_VERSION:=true}25: ${KOH_SHOW_SAVES:=true}26: ${KOH_SHOW_SIZE:=true}27: ${KOH_PROMPT_CHAR:=→}28: ${KOH_DEFAULT_PROMPT:=true}2930_koh_bin() {31 if [[ -n $KOH_BIN ]]; then print -r -- $KOH_BIN32 else print koh33 fi34}353637_koh_root() {38 local dir=$PWD39 while true; do40 [[ -f $dir/.koh ]] && { print -r -- $dir; return 0; }41 [[ -d $dir/.koh || $dir == / ]] && return 142 dir=${dir:h}43 done44}454647_koh_version() {48 local root=$1 line49 if [[ -f $root/VERSION ]]; then50 read -r line <$root/VERSION && print -r -- ${line// /}51 elif [[ -f $root/v.mod ]]; then52 [[ "$(<$root/v.mod)" =~ "version: *'([^']+)'" ]] && print -r -- $match[1]53 elif [[ -f $root/package.json ]]; then54 [[ "$(<$root/package.json)" =~ '"version" *: *"([^"]+)"' ]] && print -r -- $match[1]55 elif [[ -f $root/Cargo.toml ]]; then56 [[ "$(<$root/Cargo.toml)" =~ $'(^|\n)version *= *"([^"]+)"' ]] && print -r -- $match[2]57 elif [[ -f $root/build.zig.zon ]]; then58 [[ "$(<$root/build.zig.zon)" =~ '\.version *= *"([^"]+)"' ]] && print -r -- $match[1]59 elif [[ -f $root/pyproject.toml ]]; then60 [[ "$(<$root/pyproject.toml)" =~ $'(^|\n)version *= *"([^"]+)"' ]] && print -r -- $match[2]61 fi62}6364_koh_size() {65 local -F value=$166 local unit=B67 for next in KB MB GB; do68 (( value < 1024 )) && break69 (( value /= 1024 ))70 unit=$next71 done72 [[ $unit == B ]] && print -r -- "$1 B" || printf '%.1f %s' $value $unit73}747576_koh_segment() {77 local colour=$1 root json78 root=$(_koh_root) || return79 json=$(cd $root && command $(_koh_bin) status --json 2>/dev/null) || return80 local saves size dirty flagged version81 [[ $json =~ '"saves":([0-9]+)' ]] && saves=$match[1]82 [[ $json =~ '"size_bytes":([0-9]+)' ]] && size=$(_koh_size $match[1])83 [[ $json == *'"dirty":true'* ]] && dirty=184 [[ $json == *'"head_flagged":true'* ]] && flagged=185 [[ $KOH_SHOW_VERSION == true ]] && version=$(_koh_version $root)8687 local c_dir= c_version= c_meta= c_alert= reset=88 if [[ -n $colour ]]; then89 c_dir="%F{$KOH_COLOR_DIR}" c_version="%F{$KOH_COLOR_VERSION}"90 c_meta="%F{$KOH_COLOR_META}" c_alert="%F{$KOH_COLOR_ALERT}" reset=%f91 fi92 local out="${c_dir}${root:t}${reset}"93 [[ -n $version ]] && out+="${c_meta} · ${reset}${c_version}v${version#v}${reset}"94 [[ $KOH_SHOW_SAVES == true && -n $saves ]] && out+="${c_meta} · $saves save$( (( saves == 1 )) || print s)${reset}"95 [[ $KOH_SHOW_SIZE == true && -n $size ]] && out+="${c_meta} · $size${reset}"96 [[ -n $dirty ]] && out+=" ${c_alert}!${reset}"97 [[ -n $flagged ]] && out+=" ${c_alert}⚑${reset}"98 print -rn -- $out99}100101koh_prompt() { _koh_segment colour; }102koh_prompt_info() { _koh_segment; }103104if [[ $KOH_DEFAULT_PROMPT == true ]]; then105 setopt prompt_subst106 _koh_precmd() {107 local last=$?108 local segment=$(koh_prompt)109 if [[ -z $segment ]]; then110 segment="%F{$KOH_COLOR_DIR}%2~%f"111 fi112 local char="%F{$KOH_COLOR_META}${KOH_PROMPT_CHAR}%f"113 (( last != 0 )) && char="%F{$KOH_COLOR_ALERT}${KOH_PROMPT_CHAR}%f"114 PROMPT="${segment}"$'\n'"${char} "115 }116 autoload -Uz add-zsh-hook117 add-zsh-hook precmd _koh_precmd118fi119120121_koh3() {122 local -a commands=(123 'save:snapshot the working tree'124 'checkpoint:save, even with no changes'125 'status:what changed since the last save'126 'view:the history, saves and checkpoints'127 'diff:unified diff'128 'undo:step back n saves'129 'revert:discard unsaved changes'130 'load:restore any save'131 'remote:choose S3 or a folder, once per machine'132 'offer:send new saves to the remote'133 'steal:bring in saves from the remote'134 'flag:mark a save unsafe'135 'pluck:apply one save, or its inverse'136 'peek:restore a save into a temp folder'137 'init:create .koh here'138 'doctor:integrity check and cleanup'139 'strip:remove a path from history'140 'build:build from koh.build'141 'update:update Koh itself'142 'migrate:convert a Koh 2 repo here'143 'man:the manual, built in'144 )145 _koh3_ids() {146 local -a ids147 ids=(${(f)"$(command $(_koh_bin) view --json 2>/dev/null |148 grep -o '"id":"[0-9a-f]*","parent":"[0-9a-f]*","message":"[^"]*"' |149 sed -E 's/"id":"([0-9a-f]*)".*"message":"([^"]*)"/\1:\2/' | head -30)"})150 _describe 'save' ids151 }152 if (( CURRENT == 2 )); then153 _describe 'koh command' commands154 return155 fi156 case $words[2] in157 save|checkpoint) _arguments '--agent[model name]:agent:' '--session[session name]:session:' '--json' ;;158 view) _arguments '--checkpoints[start on the checkpoints tab]' '--since[filter by time]:expr:' '--session[session name]:session:' '--json' ;;159 diff|load|peek) _koh3_ids ;;160 pluck) _arguments '--reverse' '--dry-run' '*:save:_koh3_ids' ;;161 flag) _arguments '--reason[why]:reason:' '--clear' '--json' '*:save:_koh3_ids' ;;162 undo) _arguments '--session' '--yes' ;;163 revert) _arguments '--yes' ;;164 remote) _arguments '1:kind:(folder s3)' '--forget' '--json' ;;165 steal) _arguments '--list' '--mine' '--theirs' '--json' ;;166 strip) _arguments '--save[one save]:save:_koh3_ids' '*:path:_files' ;;167 doctor) _arguments '--backup[copy .koh to]:path:_files' ;;168 build) _arguments '*--platform[target platform]:platform:(macos-aarch64 macos-x86_64 linux-aarch64 linux-x86_64)' '--all' ;;169 update) _arguments '--check' '--yes' '--json' ;;170 migrate) _arguments '--lane[Koh 2 lane]:lane:(dev main)' '*--with[a lost version, from a backup]:file:_files' '--yes[skip the question]' '--json' ;;171 man) _describe 'command' commands ;;172 esac173}174if (( $+functions[compdef] )); then175 compdef _koh3 ${$(_koh_bin):t}176fi