|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package md |
| 22 | + |
| 23 | +import ( |
| 24 | + "sync" |
| 25 | + |
| 26 | + "github.com/arangodb/kube-arangodb/pkg/util/errors" |
| 27 | +) |
| 28 | + |
| 29 | +func NewTable(columns ...Column) Table { |
| 30 | + return &table{ |
| 31 | + columns: columns, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +type Table interface { |
| 36 | + Render() string |
| 37 | + |
| 38 | + AddRow(in map[Column]string) error |
| 39 | +} |
| 40 | + |
| 41 | +type table struct { |
| 42 | + lock sync.Mutex |
| 43 | + |
| 44 | + columns Columns |
| 45 | + rows []map[string]string |
| 46 | +} |
| 47 | + |
| 48 | +func (t *table) AddRow(in map[Column]string) error { |
| 49 | + t.lock.Lock() |
| 50 | + defer t.lock.Unlock() |
| 51 | + |
| 52 | + r := map[string]string{} |
| 53 | + |
| 54 | + for k, v := range in { |
| 55 | + if _, ok := t.columns.Get(k.ID()); !ok { |
| 56 | + return errors.Newf("Column not found") |
| 57 | + } |
| 58 | + |
| 59 | + r[k.ID()] = v |
| 60 | + } |
| 61 | + |
| 62 | + t.rows = append(t.rows, r) |
| 63 | + |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +func (t *table) fillString(base, filler string, align ColumnAlign, size int) string { |
| 68 | + for len(base) < size { |
| 69 | + switch align { |
| 70 | + case ColumnLeftAlign: |
| 71 | + base += filler |
| 72 | + case ColumnRightAlign: |
| 73 | + base = filler + base |
| 74 | + case ColumnCenterAlign: |
| 75 | + base += filler |
| 76 | + if len(base) < size { |
| 77 | + base = filler + base |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return base |
| 83 | +} |
| 84 | + |
| 85 | +func (t *table) Render() string { |
| 86 | + t.lock.Lock() |
| 87 | + defer t.lock.Unlock() |
| 88 | + |
| 89 | + ks := map[string]int{} |
| 90 | + |
| 91 | + for _, c := range t.columns { |
| 92 | + ks[c.ID()] = len(c.Name()) |
| 93 | + } |
| 94 | + |
| 95 | + for _, r := range t.rows { |
| 96 | + for _, c := range t.columns { |
| 97 | + if q := len(r[c.ID()]); q > ks[c.ID()] { |
| 98 | + ks[c.ID()] = q |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + buff := "" |
| 104 | + |
| 105 | + buff += "|" |
| 106 | + |
| 107 | + for _, c := range t.columns { |
| 108 | + buff += " " |
| 109 | + buff += t.fillString(c.Name(), " ", c.Align(), ks[c.ID()]) |
| 110 | + buff += " |" |
| 111 | + } |
| 112 | + buff += "\n|" |
| 113 | + |
| 114 | + for _, c := range t.columns { |
| 115 | + switch c.Align() { |
| 116 | + case ColumnLeftAlign, ColumnCenterAlign: |
| 117 | + buff += ":" |
| 118 | + default: |
| 119 | + buff += "-" |
| 120 | + } |
| 121 | + |
| 122 | + buff += t.fillString("", "-", ColumnLeftAlign, ks[c.ID()]) |
| 123 | + switch c.Align() { |
| 124 | + case ColumnRightAlign, ColumnCenterAlign: |
| 125 | + buff += ":" |
| 126 | + default: |
| 127 | + buff += "-" |
| 128 | + } |
| 129 | + buff += "|" |
| 130 | + } |
| 131 | + buff += "\n" |
| 132 | + |
| 133 | + for _, r := range t.rows { |
| 134 | + buff += "|" |
| 135 | + |
| 136 | + for _, c := range t.columns { |
| 137 | + buff += " " |
| 138 | + buff += t.fillString(r[c.ID()], " ", c.Align(), ks[c.ID()]) |
| 139 | + buff += " |" |
| 140 | + } |
| 141 | + buff += "\n" |
| 142 | + } |
| 143 | + |
| 144 | + return buff |
| 145 | +} |
0 commit comments