koh3.plugin.zsh

7.1 KB · raw

1# koh3.plugin.zsh — Koh 3 in your zsh prompt, plus tab completion.2#3#   kiln · v2.1.0 · 6 saves · 4.2 MB !4#   →5#6# The segment reads `koh status --json`, never the .koh file itself, so it7# works with any schema and needs no sqlite3. `!` means unsaved changes and8# `⚑` a flagged head save. Koh 2 repos (a .koh folder) are left to Koh 2's9# plugin.10#11# Install: source this file from ~/.zshrc. To keep your own prompt, set12# KOH_DEFAULT_PROMPT=false first and put $(koh_prompt) where you want it.13# koh_prompt_info gives the same text without colour, for Starship or p10k:14#15#   [custom.koh]16#   command = "koh_prompt_info"17#   when = "test -f .koh"18#   shell = ["zsh"]1920: ${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}3536# The folder holding a Koh 3 .koh file, searching upward like koh does.37_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}4546# The project's own version, from whichever manifest it keeps.47_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}7475# _koh_segment <colour?> — the segment text, or nothing outside a Koh 3 repo.76_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_precmd118fi119120# Tab completion: the commands of `koh`'s usage, and save IDs where one is taken.121_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:check repository health'139    'ignore:ignore or unignore files and folders'140    'strip:remove a path from history'141    'build:build from koh.build'142    'update:update Koh itself'143    'migrate:convert a Koh 2 repo here'144    'man:the manual, built in'145  )146  _koh3_ids() {147    local -a ids148    ids=(${(f)"$(command $(_koh_bin) view --json 2>/dev/null |149      grep -o '"id":"[0-9a-f]*","parent":"[0-9a-f]*","message":"[^"]*"' |150      sed -E 's/"id":"([0-9a-f]*)".*"message":"([^"]*)"/\1:\2/' | head -30)"})151    _describe 'save' ids152  }153  if (( CURRENT == 2 )); then154    _describe 'koh command' commands155    return156  fi157  case $words[2] in158    save|checkpoint) _arguments '--agent[model name]:agent:' '--session[session name]:session:' '--json' ;;159    view)            _arguments '--checkpoints[start on the checkpoints tab]' '--since[filter by time]:expr:' '--session[session name]:session:' '--json' ;;160    diff|load|peek)  _koh3_ids ;;161    pluck)           _arguments '--reverse' '--dry-run' '*:save:_koh3_ids' ;;162    flag)            _arguments '--reason[why]:reason:' '--clear' '--json' '*:save:_koh3_ids' ;;163    undo)            _arguments '--session' '--yes' ;;164    revert)          _arguments '--yes' ;;165    remote)          _arguments '1:kind:(folder s3)' '--forget' '--yes' '--json' ;;166    offer)           _arguments '--yes' '--builds' '--notes:notes:' '--json' ;;167    steal)           _arguments '--yes' '--list' '--mine' '--theirs' '--json' ;;168    ignore)          _arguments '--global[edit ~/.kohignore for every repo]' '--yes[skip the question]' '--json' '*:path:_files' ;;169    strip)           _arguments '--yes[skip the yes/no question; paths still typed]' '--save[one save]:save:_koh3_ids' '*:path:_files' ;;170    doctor)          _arguments '--offline[skip remote content checks]' '--fast[skip the slow remote audit]' '--json[print the report as JSON]' '--yes[remove old peek folders]' '--backup[copy .koh to]:path:_files' ;;171    build)           _arguments '*--platform[target platform]:platform:(macos-aarch64 macos-x86_64 linux-aarch64 linux-x86_64)' '--all' ;;172    update)          _arguments '--check' '--yes' '--json' ;;173    migrate)         _arguments '--lane[Koh 2 lane]:lane:(dev main)' '*--with[a lost version, from a backup]:file:_files' '--yes[skip the question]' '--json' ;;174    man)             _describe 'command' commands ;;175  esac176}177if (( $+functions[compdef] )); then178  compdef _koh3 ${$(_koh_bin):t}179fi