format.v

778 B · raw

1//! Small formatting helpers shared by the commands.23module cli45import time67pub fn stamp(unix_seconds i64) string {8	return time.unix(unix_seconds).local().format()9}1011pub fn day(unix_seconds i64) string {12	return time.unix(unix_seconds).local().ymmdd()13}1415pub fn clock(unix_seconds i64) string {16	return time.unix(unix_seconds).local().format_ss()17}1819pub fn human_bytes(bytes i64) string {20	mut value := f64(bytes)21	mut unit := 'B'22	for suffix in ['KiB', 'MiB', 'GiB'] {23		if value < 1024 {24			break25		}26		value /= 102427		unit = suffix28	}29	if unit == 'B' {30		return '${bytes} B'31	}32	return '${value:.1f} ${unit}'33}3435// plural renders "1 file" / "3 files".36pub fn plural(count int, noun string) string {37	if count == 1 {38		return '1 ${noun}'39	}40	return '${count} ${noun}s'41}