//! LOG.md: the one-screen history Koh keeps at the repo root for people (and //! agents) who want the last few saves without running a command. module history import os import time import src.storage const log_marker = '' const log_shown = 20 // refresh_log_md rewrites LOG.md after a change to history has landed. // The history is what matters, so a LOG.md that cannot be written is a warning // on stderr, never a failed command: a save that exits 1 after landing would // send an agent to save again. pub fn refresh_log_md(store &storage.Store) { write_log_md(store) or { eprintln(' ! LOG.md was not updated: ${err.msg()}') } } // write_log_md refreshes LOG.md from the line. It runs after every save, // whatever made it — save, checkpoint, undo, load, revert, pluck or a steal. fn write_log_md(store &storage.Store) ! { path := os.join_path(store.root, 'LOG.md') if os.is_file(path) { existing := os.read_file(path)! if !existing.starts_with(log_marker) { // Someone else's log file; leave it alone. return } } saves := line(store)! shown := if saves.len < log_shown { saves.len } else { log_shown } mut text := '${log_marker}\n\n# Koh Log\n\n' for save in saves[..shown] { text += '✦ [${save.id}] ${save.message} · ${clock(save.created_at)}\n' } if saves.len > shown { text += '\n… ${saves.len - shown} older saves\n' } text += '\n*full history: koh view*\n' os.write_file(path, text)! } fn clock(unix_seconds i64) string { return time.unix(unix_seconds).local().format_ss() }