mirror of
https://github.com/charmbracelet/gum
synced 2026-03-14 13:45:45 +01:00
fix(table): ignore BOM (#757)
* fix(table): ignore BOM closes #520 Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * fix: lint issue Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * fix: simplify * fix: better error --------- Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
parent
b58aad189a
commit
cc71f600f2
3 changed files with 16 additions and 6 deletions
2
go.mod
2
go.mod
|
|
@ -17,6 +17,7 @@ require (
|
|||
github.com/muesli/roff v0.1.0
|
||||
github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a
|
||||
github.com/sahilm/fuzzy v0.1.1
|
||||
golang.org/x/text v0.18.0
|
||||
)
|
||||
|
||||
require (
|
||||
|
|
@ -45,5 +46,4 @@ require (
|
|||
golang.org/x/sync v0.10.0 // indirect
|
||||
golang.org/x/sys v0.28.0 // indirect
|
||||
golang.org/x/term v0.22.0 // indirect
|
||||
golang.org/x/text v0.18.0 // indirect
|
||||
)
|
||||
|
|
|
|||
4
table/bom.csv
Normal file
4
table/bom.csv
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
"first_name","last_name","username"
|
||||
"Rob","Pike",rob
|
||||
Ken,Thompson,ken
|
||||
"Robert","Griesemer","gri"
|
||||
|
|
|
@ -12,24 +12,30 @@ import (
|
|||
"github.com/charmbracelet/gum/style"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
ltable "github.com/charmbracelet/lipgloss/table"
|
||||
"golang.org/x/text/encoding"
|
||||
"golang.org/x/text/encoding/unicode"
|
||||
"golang.org/x/text/transform"
|
||||
)
|
||||
|
||||
// Run provides a shell script interface for rendering tabular data (CSV).
|
||||
func (o Options) Run() error {
|
||||
var reader *csv.Reader
|
||||
var input *os.File
|
||||
if o.File != "" {
|
||||
file, err := os.Open(o.File)
|
||||
var err error
|
||||
input, err = os.Open(o.File)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not find file at path %s", o.File)
|
||||
return fmt.Errorf("could not render file: %w", err)
|
||||
}
|
||||
reader = csv.NewReader(file)
|
||||
} else {
|
||||
if stdin.IsEmpty() {
|
||||
return fmt.Errorf("no data provided")
|
||||
}
|
||||
reader = csv.NewReader(os.Stdin)
|
||||
input = os.Stdin
|
||||
}
|
||||
defer input.Close() //nolint: errcheck
|
||||
|
||||
transformer := unicode.BOMOverride(encoding.Nop.NewDecoder())
|
||||
reader := csv.NewReader(transform.NewReader(input, transformer))
|
||||
separatorRunes := []rune(o.Separator)
|
||||
if len(separatorRunes) != 1 {
|
||||
return fmt.Errorf("separator must be single character")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue