mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 19:54:17 +02:00
115 lines
2.5 KiB
Go
115 lines
2.5 KiB
Go
/*
|
|
Copyright © 2026 Thomas von Dein
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
package es
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/olekukonko/tablewriter"
|
|
"github.com/olekukonko/tablewriter/renderer"
|
|
"github.com/olekukonko/tablewriter/tw"
|
|
)
|
|
|
|
type Table struct {
|
|
headers []string
|
|
entries [][]string
|
|
}
|
|
|
|
func NewTable(columns, rows int) *Table {
|
|
table := Table{}
|
|
|
|
table.headers = make([]string, columns)
|
|
table.entries = make([][]string, rows)
|
|
|
|
return &table
|
|
}
|
|
|
|
func (data *Table) PrintMarkdown() error {
|
|
tableString := &strings.Builder{}
|
|
|
|
table := tablewriter.NewTable(tableString,
|
|
tablewriter.WithRenderer(
|
|
renderer.NewBlueprint(
|
|
tw.Rendition{
|
|
Borders: tw.Border{
|
|
Left: tw.On,
|
|
Right: tw.On,
|
|
Top: tw.Off,
|
|
Bottom: tw.Off,
|
|
},
|
|
Settings: tw.Settings{
|
|
Separators: tw.Separators{
|
|
ShowHeader: tw.On,
|
|
ShowFooter: tw.Off,
|
|
BetweenRows: tw.Off,
|
|
BetweenColumns: 0,
|
|
},
|
|
},
|
|
Symbols: tw.NewSymbols(tw.StyleMarkdown),
|
|
})),
|
|
|
|
tablewriter.WithConfig(
|
|
tablewriter.Config{
|
|
Header: tw.CellConfig{
|
|
Formatting: tw.CellFormatting{
|
|
Alignment: tw.AlignLeft,
|
|
AutoFormat: tw.Off,
|
|
},
|
|
},
|
|
Row: tw.CellConfig{
|
|
Formatting: tw.CellFormatting{
|
|
Alignment: tw.AlignLeft,
|
|
},
|
|
},
|
|
},
|
|
),
|
|
)
|
|
|
|
table.Header(data.headers)
|
|
|
|
if err := table.Bulk(data.entries); err != nil {
|
|
return fmt.Errorf("failed to add data to table renderer: %s", err)
|
|
}
|
|
|
|
if err := table.Render(); err != nil {
|
|
return fmt.Errorf("failed to render table: %s", err)
|
|
}
|
|
|
|
fmt.Println(tableString.String())
|
|
|
|
return nil
|
|
}
|
|
|
|
func (data *Table) Sort() {
|
|
// sanity checks
|
|
if len(data.entries) == 0 {
|
|
return
|
|
}
|
|
|
|
sort.Slice(data.entries, func(i, j int) bool {
|
|
return data.entries[i][0] < data.entries[j][0]
|
|
})
|
|
}
|
|
|
|
func (data *Table) Addheaders(headers ...string) {
|
|
for idx, header := range headers {
|
|
data.headers[idx] = bold(strings.ToUpper(header))
|
|
}
|
|
}
|