Skip to content

Commit

Permalink
fix(table): ignore BOM
Browse files Browse the repository at this point in the history
closes #520

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Dec 10, 2024
1 parent b58aad1 commit 72889bd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 changes: 4 additions & 0 deletions table/bom.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"first_name","last_name","username"
"Rob","Pike",rob
Ken,Thompson,ken
"Robert","Griesemer","gri"
13 changes: 10 additions & 3 deletions table/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package table
import (
"encoding/csv"
"fmt"
"io"
"os"

"github.com/charmbracelet/bubbles/table"
Expand All @@ -12,24 +13,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 in io.Reader
if o.File != "" {
file, err := os.Open(o.File)
if err != nil {
return fmt.Errorf("could not find file at path %s", o.File)
}
reader = csv.NewReader(file)
defer file.Close()

Check failure on line 29 in table/command.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `file.Close` is not checked (errcheck)

Check failure on line 29 in table/command.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `file.Close` is not checked (errcheck)
in = file
} else {
if stdin.IsEmpty() {
return fmt.Errorf("no data provided")
}
reader = csv.NewReader(os.Stdin)
in = os.Stdin
}

transformer := unicode.BOMOverride(encoding.Nop.NewDecoder())
reader := csv.NewReader(transform.NewReader(in, transformer))
separatorRunes := []rune(o.Separator)
if len(separatorRunes) != 1 {
return fmt.Errorf("separator must be single character")
Expand Down

0 comments on commit 72889bd

Please sign in to comment.