log_md.v

1.6 KB · raw

1//! LOG.md: the one-screen history Koh keeps at the repo root for people (and2//! agents) who want the last few saves without running a command.34module history56import os7import time8import src.fmt9import src.storage1011const log_marker = '<!-- Koh maintains this file. Do not edit. Read before starting work. -->'12const log_shown = 201314// refresh_log_md rewrites LOG.md after a change to history has landed.15// The history is what matters, so a LOG.md that cannot be written is a warning16// on stderr, never a failed command: a save that exits 1 after landing would17// send an agent to save again.18pub fn refresh_log_md(store &storage.Store) {19	write_log_md(store) or { eprintln('  ! LOG.md was not updated: ${err.msg()}') }20}2122// write_log_md refreshes LOG.md from the line. It runs after every save,23// whatever made it — save, checkpoint, undo, load, revert, pluck or a steal.24fn write_log_md(store &storage.Store) ! {25	path := os.join_path(store.root, 'LOG.md')26	if os.is_file(path) {27		existing := os.read_file(path)!28		if !existing.starts_with(log_marker) {29			// Someone else's log file; leave it alone.30			return31		}32	}33	saves := line(store)!34	shown := if saves.len < log_shown { saves.len } else { log_shown }35	mut text := '${log_marker}\n\n# Koh Log\n\n'36	for save in saves[..shown] {37		text += '✦ [${save.id}]  ${fmt.safe_text(save.message)} · ${clock(save.created_at)}\n'38	}39	if saves.len > shown {40		text += '\n… ${saves.len - shown} older saves\n'41	}42	text += '\n*full history: koh view*\n'43	os.write_file(path, text)!44}4546fn clock(unix_seconds i64) string {47	return time.unix(unix_seconds).local().format_ss()48}