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.storage910const log_marker = '<!-- Koh maintains this file. Do not edit. Read before starting work. -->'11const log_shown = 201213// refresh_log_md rewrites LOG.md after a change to history has landed.14// The history is what matters, so a LOG.md that cannot be written is a warning15// on stderr, never a failed command: a save that exits 1 after landing would16// send an agent to save again.17pub fn refresh_log_md(store &storage.Store) {18	write_log_md(store) or { eprintln('  ! LOG.md was not updated: ${err.msg()}') }19}2021// write_log_md refreshes LOG.md from the line. It runs after every save,22// whatever made it — save, checkpoint, undo, load, revert, pluck or a steal.23fn write_log_md(store &storage.Store) ! {24	path := os.join_path(store.root, 'LOG.md')25	if os.is_file(path) {26		existing := os.read_file(path)!27		if !existing.starts_with(log_marker) {28			// Someone else's log file; leave it alone.29			return30		}31	}32	saves := line(store)!33	shown := if saves.len < log_shown { saves.len } else { log_shown }34	mut text := '${log_marker}\n\n# Koh Log\n\n'35	for save in saves[..shown] {36		text += '✦ [${save.id}]  ${save.message} · ${clock(save.created_at)}\n'37	}38	if saves.len > shown {39		text += '\n… ${saves.len - shown} older saves\n'40	}41	text += '\n*full history: koh view*\n'42	os.write_file(path, text)!43}4445fn clock(unix_seconds i64) string {46	return time.unix(unix_seconds).local().format_ss()47}