From 3521c0026f721a854ec135467660a94c5b47adf8 Mon Sep 17 00:00:00 2001 From: Patrick Winter Date: Wed, 18 Jan 2017 18:05:08 +0100 Subject: [PATCH] added whitespace padded representation of `show` subcommand --- src/show.go | 14 +++++++++++++- src/show_test.go | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/show.go b/src/show.go index 343ba5b..9beba4b 100644 --- a/src/show.go +++ b/src/show.go @@ -34,8 +34,20 @@ func (c *ShowCommand) Run(args []string) int { return 1 } + // Get length of the largest key in order to calculate the + // "whitespace padded" representation of `show` + max_key_len := 0 + for k, _ := range secret.Data { + if key_len := len(k); key_len > max_key_len { + max_key_len = key_len + } + } + + // Add an additional X whitespaces between "key:" and "value" + max_key_len += 4 + for k, v := range secret.Data { - c.Ui.Output(fmt.Sprintf("%v: %v", k, v)) + c.Ui.Output(fmt.Sprintf("%-"+fmt.Sprint(max_key_len)+"v %v", fmt.Sprint(k, ":"), v)) } return 0 diff --git a/src/show_test.go b/src/show_test.go index 0185780..b4a73a9 100644 --- a/src/show_test.go +++ b/src/show_test.go @@ -65,7 +65,7 @@ func TestShow(t *testing.T) { t.Fatalf("expected error:\n%s\n\nto include: %q", actual, expectedErr) } - expectedOutput := "key: value" + expectedOutput := "key: value" if actual := ui.OutputWriter.String(); !strings.Contains(actual, expectedOutput) { t.Fatalf("expected output:\n%s\n\nto include: %q", actual, expectedOutput) }