output.v

1.4 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	println('  ${fmt.amber(fmt.glyph_ok)} ${message}')17}1819pub fn note(message string) {20	println('  ${fmt.dim(message)}')21}2223pub fn warn(message string) {24	println('  ${fmt.amber('!')} ${message}')25}2627// fail prints a refusal and returns the exit code to hand back.28pub fn fail(message string) int {29	eprintln('  ${fmt.red(fmt.glyph_fail)} ${message}')30	return 131}3233pub fn fail_json(message string) int {34	eprintln(json2.encode({35		'error': message36	}))37	return 138}3940// usage_error reports a mistake in the command line itself. It can happen41// before the options are parsed, so `--json` is looked for on the raw line.42pub fn usage_error(message string) int {43	if '--json' in os.args {44		fail_json(message)45	} else {46		eprintln('  ${fmt.red(fmt.glyph_fail)} ${message}')47	}48	return 249}5051// refuse reports an error in whichever shape the command was asked for.52pub fn refuse(as_json bool, message string) int {53	if as_json {54		return fail_json(message)55	}56	return fail(message)57}5859pub fn emit_json(value string) {60	println(value)61}