12345678module cli910import os11import src.fmt1213const manual = $embed_file('../../docs/koh3-man.txt').to_string()1415pub fn cmd_man(raw []string) int {16 args := parse('man', raw, Options{}) or { return usage_error(err.msg()) }17 mut text := manual18 if args.positional.len > 0 {19 name := args.positional_at(0)20 text = section_of(name) or {21 eprintln(' ${fmt.red(fmt.glyph_fail)} the manual has no "${name}" — try "koh man" for the whole page')22 return 223 }24 }25 page(text)26 return 027}28293031fn section_of(name string) ?string {32 lines := manual.split_into_lines()33 mut start := -134 for i, line in lines {35 if line.starts_with(' koh ') && line.contains(' ${name}') {36 start = i37 break38 }39 }40 if start < 0 {41 return none42 }43 mut end := lines.len44 for i := start + 1; i < lines.len; i++ {45 trimmed := lines[i].trim_space()46 if trimmed.starts_with('koh ') && lines[i].starts_with(' ') {47 end = i48 break49 }50 if lines[i].starts_with('OPTIONS') || lines[i].starts_with('ENVIRONMENT') {51 end = i52 break53 }54 }55 return lines[start..end].join('\n')56}5758596061fn page(text string) {62 pager := os.getenv('PAGER')63 if os.is_atty(1) != 0 && pager != '' {64 path := os.join_path(os.temp_dir(), 'koh-man-${os.getpid()}.txt')65 os.write_file(path, text) or {66 print(text)67 return68 }69 70 71 72 os.system('${pager} ${os.quoted_path(path)}')73 os.rm(path) or {}74 return75 }76 print(text)77 if !text.ends_with('\n') {78 println('')79 }80}