From 5c5c2f87e0e74c940210744b525cb6e9b302c188 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Mon, 9 May 2022 16:16:25 +0200 Subject: [PATCH] feat(cli): lists are now properly formatted in an ascii table --- src/console/console.v | 55 +++++++++++++++++++++++++++++++++++++++++ src/console/git/git.v | 6 ++--- src/console/logs/logs.v | 14 ++++++----- 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/src/console/console.v b/src/console/console.v index 6f296bdd..dfb2fac8 100644 --- a/src/console/console.v +++ b/src/console/console.v @@ -1 +1,56 @@ module console + +import arrays +import strings + +// pretty_table converts a list of string data into a pretty table. Many thanks +// to @hungrybluedev in the Vlang Discord for providing this code! +// https://ptb.discord.com/channels/592103645835821068/592106336838352923/970278787143045192 +pub fn pretty_table(header []string, data [][]string) ?string { + column_count := header.len + + mut column_widths := []int{len: column_count, init: header[it].len} + + for values in data { + for col, value in values { + if column_widths[col] < value.len { + column_widths[col] = value.len + } + } + } + + single_line_length := arrays.sum(column_widths)? + (column_count + 1) * 3 - 4 + + horizontal_line := '+' + strings.repeat(`-`, single_line_length) + '+' + mut buffer := strings.new_builder(data.len * single_line_length) + + buffer.writeln(horizontal_line) + + buffer.write_string('| ') + for col, head in header { + if col != 0 { + buffer.write_string(' | ') + } + buffer.write_string(head) + buffer.write_string(strings.repeat(` `, column_widths[col] - head.len)) + } + buffer.writeln(' |') + + buffer.writeln(horizontal_line) + + for values in data { + buffer.write_string('| ') + for col, value in values { + if col != 0 { + buffer.write_string(' | ') + } + buffer.write_string(value) + buffer.write_string(strings.repeat(` `, column_widths[col] - value.len)) + } + buffer.writeln(' |') + } + + buffer.writeln(horizontal_line) + + return buffer.str() +} diff --git a/src/console/git/git.v b/src/console/git/git.v index 83837446..db9dec59 100644 --- a/src/console/git/git.v +++ b/src/console/git/git.v @@ -4,6 +4,7 @@ import cli import env import cron.expression { parse_expression } import client +import console struct Config { address string [required] @@ -122,10 +123,9 @@ pub fn cmd() cli.Command { fn list(conf Config) ? { c := client.new(conf.address, conf.api_key) repos := c.get_git_repos()? + data := repos.map([it.id.str(), it.url, it.branch, it.repo]) - for repo in repos { - println('$repo.id\t$repo.url\t$repo.branch\t$repo.repo') - } + println(console.pretty_table(['id', 'url', 'branch', 'repo'], data)?) } // add adds a new repository to the server's list. diff --git a/src/console/logs/logs.v b/src/console/logs/logs.v index 0e965232..6400e801 100644 --- a/src/console/logs/logs.v +++ b/src/console/logs/logs.v @@ -4,6 +4,7 @@ import cli import env import client import db +import console struct Config { address string [required] @@ -66,10 +67,11 @@ pub fn cmd() cli.Command { } // print_log_list prints a list of logs. -fn print_log_list(logs []db.BuildLog) { - for log in logs { - println('$log.id\t$log.start_time\t$log.exit_code') - } +fn print_log_list(logs []db.BuildLog) ? { + data := logs.map([it.id.str(), it.repo_id.str(), it.start_time.str(), + it.exit_code.str()]) + + println(console.pretty_table(['id', 'repo', 'start time', 'exit code'], data)?) } // list prints a list of all build logs. @@ -77,7 +79,7 @@ fn list(conf Config) ? { c := client.new(conf.address, conf.api_key) logs := c.get_build_logs()?.data - print_log_list(logs) + print_log_list(logs)? } // list prints a list of all build logs for a given repo. @@ -85,7 +87,7 @@ fn list_for_repo(conf Config, repo_id int) ? { c := client.new(conf.address, conf.api_key) logs := c.get_build_logs_for_repo(repo_id)?.data - print_log_list(logs) + print_log_list(logs)? } // info print the detailed info for a given build log.