log_md.v

1.2 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// write_log_md refreshes LOG.md from the line. It runs after every save,14// whatever made it — save, checkpoint, undo, load, revert, pluck or a steal.15pub fn write_log_md(store &storage.Store) ! {16	path := os.join_path(store.root, 'LOG.md')17	if os.is_file(path) {18		existing := os.read_file(path)!19		if !existing.starts_with(log_marker) {20			// Someone else's log file; leave it alone.21			return22		}23	}24	saves := line(store)!25	shown := if saves.len < log_shown { saves.len } else { log_shown }26	mut text := '${log_marker}\n\n# Koh Log\n\n'27	for save in saves[..shown] {28		text += '✦ [${save.id}]  ${save.message} · ${clock(save.created_at)}\n'29	}30	if saves.len > shown {31		text += '\n… ${saves.len - shown} older saves\n'32	}33	text += '\n*full history: koh log*\n'34	os.write_file(path, text)!35}3637fn clock(unix_seconds i64) string {38	return time.unix(unix_seconds).local().format_ss()39}