| 1 | import os |
| 2 | import term |
| 3 | import time |
| 4 | import flag |
| 5 | |
| 6 | struct Context { |
| 7 | mut: |
| 8 | show_help bool |
| 9 | cmd_line_opts []string |
| 10 | } |
| 11 | |
| 12 | fn main() { |
| 13 | mut ctx := Context{} |
| 14 | args := arguments() |
| 15 | mut fp := flag.new_flag_parser(args#[1..]) |
| 16 | fp.application('v time') |
| 17 | fp.version('0.0.1') |
| 18 | fp.description('Start a command, and report how much time it took to run, and what its exit code was.') |
| 19 | fp.arguments_description('CMD [ARGS]') |
| 20 | fp.skip_executable() |
| 21 | fp.limit_free_args_to_at_least(1)! |
| 22 | ctx.show_help = fp.bool('help', `h`, false, 'Show this help screen.') |
| 23 | if ctx.show_help { |
| 24 | println(fp.usage()) |
| 25 | exit(0) |
| 26 | } |
| 27 | ctx.cmd_line_opts = fp.finalize() or { |
| 28 | eprintln('Error: ${err}') |
| 29 | exit(1) |
| 30 | } |
| 31 | cmd := ctx.cmd_line_opts.join(' ') |
| 32 | sw := time.new_stopwatch() |
| 33 | ecode := os.system(cmd) |
| 34 | elapsed := sw.elapsed() |
| 35 | stook_time := '${f64(elapsed.microseconds()) / 1000.0:8.3f} ms' |
| 36 | eprintln('> ${term.ecolorize(term.bright_yellow, stook_time)}. Exit code: ${ecode:3}. Command: ${term.ecolorize(term.green, |
| 37 | cmd)}') |
| 38 | exit(ecode) |
| 39 | } |
| 40 | |