output.v

1.5 KB · raw

1//! Output for the command line: the ✦ / ✗ habit, and the `--json` shapes2//! agents read.3//!4//! Colour comes from `src.fmt`, so turning it off turns it off everywhere.5//! Every command that prints data has a JSON form with stable field names, and6//! every error is `{"error": "..."}` on stderr. Exit codes are fixed: 07//! success, 1 refusal or error, 2 bad usage.89module cli1011import json212import os13import src.fmt1415pub fn ok(message string) {16	done_working()17	println('  ${fmt.amber(fmt.glyph_ok)} ${message}')18}1920pub fn note(message string) {21	done_working()22	println('  ${fmt.dim(message)}')23}2425pub fn warn(message string) {26	done_working()27	println('  ${fmt.amber('!')} ${message}')28}2930// fail prints a refusal and returns the exit code to hand back.31pub fn fail(message string) int {32	done_working()33	eprintln('  ${fmt.red(fmt.glyph_fail)} ${message}')34	return 135}3637pub fn fail_json(message string) int {38	eprintln(json2.encode({39		'error': message40	}))41	return 142}4344// usage_error reports a mistake in the command line itself. It can happen45// before the options are parsed, so `--json` is looked for on the raw line.46pub fn usage_error(message string) int {47	if '--json' in os.args {48		fail_json(message)49	} else {50		eprintln('  ${fmt.red(fmt.glyph_fail)} ${message}')51	}52	return 253}5455// refuse reports an error in whichever shape the command was asked for.56pub fn refuse(as_json bool, message string) int {57	if as_json {58		return fail_json(message)59	}60	return fail(message)61}6263pub fn emit_json(value string) {64	done_working()65	println(value)66}