1234567module fmt89import os10import time1112pub const glyph_ok = '✦'13pub const glyph_fail = '✗'1415pub fn colour_enabled() bool {16 if os.getenv('NO_COLOR') != '' || os.getenv('KOH_NO_COLOR') != '' {17 return false18 }19 return os.is_atty(1) != 020}2122fn paint(text string, code string) string {23 if text == '' || !colour_enabled() {24 return text25 }26 return '\x1b[${code}m${text}\x1b[0m'27}2829pub fn bold(text string) string {30 return paint(text, '1')31}3233pub fn reverse(text string) string {34 return paint(text, '7')35}3637pub fn amber(text string) string {38 return paint(text, '38;5;214')39}4041pub fn red(text string) string {42 return paint(text, '38;5;203')43}4445pub fn green(text string) string {46 return paint(text, '38;5;114')47}4849pub fn dim(text string) string {50 return paint(text, '38;5;245')51}52535455pub fn gold(text string) string {56 return paint(text, '38;5;179')57}5859pub fn muted(text string) string {60 return paint(text, '38;5;241')61}6263pub fn blue(text string) string {64 return paint(text, '38;5;110')65}666768pub fn light(text string) string {69 return paint(text, '38;5;223')70}7172pub fn warning(text string) string {73 return paint(text, '38;5;173')74}7576777879pub fn safe_text(text string) string {80 mut safe := ''81 for c in text.runes() {82 if c < 0x20 || c == 0x7f {83 safe += '\\x${u8(c).hex()}'84 } else {85 safe += c.str()86 }87 }88 return safe89}909192pub fn strip_ansi(text string) string {93 mut out := ''94 mut skipping := false95 for c in text.runes() {96 if c == `\x1b` {97 skipping = true98 continue99 }100 if skipping {101 if (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`) {102 skipping = false103 }104 continue105 }106 out += c.str()107 }108 return out109}110111112pub fn visible_width(text string) int {113 return strip_ansi(text).runes().len114}115116117118pub fn fit(text string, width int) string {119 if visible_width(text) <= width {120 return text121 }122 mut out := []rune{}123 mut shown := 0124 mut skipping := false125 for c in text.runes() {126 if c == `\x1b` {127 skipping = true128 }129 if skipping {130 out << c131 if (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`) {132 skipping = false133 }134 continue135 }136 if shown == width {137 break138 }139 out << c140 shown++141 }142 cut := out.string()143 return if cut.contains('\x1b') { cut + '\x1b[0m' } else { cut }144}145146pub fn stamp(unix_seconds i64) string {147 return time.unix(unix_seconds).local().format()148}149150pub fn date_of(unix_seconds i64) string {151 return time.unix(unix_seconds).local().ymmdd()152}153154pub fn hour_of(unix_seconds i64) string {155 return time.unix(unix_seconds).local().hhmm()156}157158pub fn clock_of(unix_seconds i64) string {159 return time.unix(unix_seconds).local().hhmmss()160}161162pub fn human_bytes(bytes i64) string {163 mut value := f64(bytes)164 mut unit := 'B'165 for suffix in ['KiB', 'MiB', 'GiB'] {166 if value < 1024 {167 break168 }169 value /= 1024170 unit = suffix171 }172 if unit == 'B' {173 return '${bytes} B'174 }175 return '${value:.1f} ${unit}'176}177178179pub fn plural(count int, noun string) string {180 if count == 1 {181 return '1 ${noun}'182 }183 return '${count} ${noun}s'184}