output.v

1.2 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 json12import src.fmt1314pub fn ok(message string) {15	println('  ${fmt.amber(fmt.glyph_ok)} ${message}')16}1718pub fn note(message string) {19	println('  ${fmt.dim(message)}')20}2122pub fn warn(message string) {23	println('  ${fmt.amber('!')} ${message}')24}2526// fail prints a refusal and returns the exit code to hand back.27pub fn fail(message string) int {28	eprintln('  ${fmt.red(fmt.glyph_fail)} ${message}')29	return 130}3132pub fn fail_json(message string) int {33	eprintln(json.encode({34		'error': message35	}))36	return 137}3839pub fn usage_error(message string) int {40	eprintln('  ${fmt.red(fmt.glyph_fail)} ${message}')41	return 242}4344// refuse reports an error in whichever shape the command was asked for.45pub fn refuse(as_json bool, message string) int {46	if as_json {47		return fail_json(message)48	}49	return fail(message)50}5152pub fn emit_json(value string) {53	println(value)54}