forked from toolshed/abra
refactor: tablewriter -> lipgloss
Also the jsontable impl. is dropped also. Output is unchanged.
This commit is contained in:
@ -1,18 +1,25 @@
|
||||
package formatter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/charmbracelet/lipgloss/table"
|
||||
"github.com/docker/go-units"
|
||||
// "github.com/olekukonko/tablewriter"
|
||||
"coopcloud.tech/abra/pkg/jsontable"
|
||||
"golang.org/x/term"
|
||||
|
||||
"coopcloud.tech/abra/pkg/log"
|
||||
"github.com/schollz/progressbar/v3"
|
||||
)
|
||||
|
||||
var BoldStyle = lipgloss.NewStyle().
|
||||
Bold(true).
|
||||
Underline(true)
|
||||
|
||||
func ShortenID(str string) string {
|
||||
return str[:12]
|
||||
}
|
||||
@ -33,12 +40,53 @@ func HumanDuration(timestamp int64) string {
|
||||
return units.HumanDuration(now.Sub(date)) + " ago"
|
||||
}
|
||||
|
||||
// CreateTable prepares a table layout for output.
|
||||
func CreateTable(columns []string) *jsontable.JSONTable {
|
||||
table := jsontable.NewJSONTable(os.Stdout)
|
||||
table.SetAutoWrapText(false)
|
||||
table.SetHeader(columns)
|
||||
return table
|
||||
// CreateTable2 prepares a table layout for output.
|
||||
func CreateTable2() (*table.Table, error) {
|
||||
width, _, err := term.GetSize(0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if width-10 < 79 {
|
||||
width = 79
|
||||
}
|
||||
|
||||
return table.New().
|
||||
Width(width - 10).
|
||||
Border(lipgloss.ThickBorder()).
|
||||
BorderStyle(
|
||||
lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color("63")),
|
||||
), nil
|
||||
}
|
||||
|
||||
// ToJSON converts a lipgloss.Table to JSON representation. It's not a robust
|
||||
// implementation and mainly caters for our current use case which is basically
|
||||
// a bunch of strings. See https://github.com/charmbracelet/lipgloss/issues/335
|
||||
// for the real thing (hopefully).
|
||||
func ToJSON(headers []string, rows [][]string) (string, error) {
|
||||
var buff bytes.Buffer
|
||||
|
||||
buff.Write([]byte("["))
|
||||
|
||||
for _, row := range rows {
|
||||
payload := make(map[string]string)
|
||||
|
||||
for idx, header := range headers {
|
||||
payload[strings.ToLower(header)] = row[idx]
|
||||
}
|
||||
|
||||
serialized, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
buff.Write(serialized)
|
||||
}
|
||||
|
||||
buff.Write([]byte("]"))
|
||||
|
||||
return buff.String(), nil
|
||||
}
|
||||
|
||||
// CreateProgressbar generates a progress bar
|
||||
|
Reference in New Issue
Block a user