Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

### Features include:

- **Response formatting**: automatically formats and colors output (json, html, msgpack, protobuf, xml, etc.)
- **Response formatting**: automatically formats and colors output (json, html, csv, msgpack, protobuf, xml, etc.)
- **Image rendering**: render images directly in your terminal
- **Compression**: automatic gzip and zstd response body decompression
- **Authentication**: support for Basic Auth, Bearer Token, and AWS Signature V4
Expand Down
3 changes: 3 additions & 0 deletions docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,14 @@ Supported formats for automatic formatting and syntax highlighting:
- JSON (`application/json`)
- HTML (`text/html`)
- XML (`application/xml`, `text/xml`)
- CSV (`text/csv`)
- MessagePack (`application/msgpack`)
- NDJSON/JSONLines (`application/x-ndjson`)
- Protobuf (`application/x-protobuf`, `application/protobuf`)
- Server-Sent Events (`text/event-stream`)

CSV output is automatically column-aligned. When output is too wide for the terminal, it switches to a vertical "record view" format where each row is displayed with field names as labels.

```sh
fetch --format off example.com
fetch --format on example.com
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.25.6

require (
github.com/klauspost/compress v1.18.3
github.com/mattn/go-runewidth v0.0.19
github.com/quic-go/quic-go v0.59.0
github.com/tinylib/msgp v1.6.3
golang.org/x/image v0.35.0
Expand All @@ -13,6 +14,7 @@ require (
)

require (
github.com/clipperhouse/uax29/v2 v2.2.0 // indirect
github.com/philhofer/fwd v1.2.0 // indirect
github.com/quic-go/qpack v0.6.0 // indirect
golang.org/x/crypto v0.47.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
github.com/clipperhouse/uax29/v2 v2.2.0 h1:ChwIKnQN3kcZteTXMgb1wztSgaU+ZemkgWdohwgs8tY=
github.com/clipperhouse/uax29/v2 v2.2.0/go.mod h1:EFJ2TJMRUaplDxHKj1qAEhCtQPW2tJSwu5BF98AuoVM=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/klauspost/compress v1.18.3 h1:9PJRvfbmTabkOX8moIpXPbMMbYN60bWImDDU7L+/6zw=
github.com/klauspost/compress v1.18.3/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4=
github.com/mattn/go-runewidth v0.0.19 h1:v++JhqYnZuu5jSKrk9RbgF5v4CGUjqRfBm05byFGLdw=
github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM=
github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
33 changes: 33 additions & 0 deletions internal/core/term_size_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//go:build unix

package core

import (
"os"

"golang.org/x/sys/unix"
)

// GetTerminalSize returns the terminal size, or an error if unavailable.
func GetTerminalSize() (TerminalSize, error) {
var ts TerminalSize
ws, err := unix.IoctlGetWinsize(int(os.Stdout.Fd()), unix.TIOCGWINSZ)
if err != nil {
return ts, err
}

ts.Cols = int(ws.Col)
ts.Rows = int(ws.Row)
ts.WidthPx = int(ws.Xpixel)
ts.HeightPx = int(ws.Ypixel)
return ts, nil
}

// GetTerminalCols returns the number of columns in the terminal, or 0 if unavailable.
func GetTerminalCols() int {
ts, err := GetTerminalSize()
if err != nil {
return 0
}
return ts.Cols
}
37 changes: 37 additions & 0 deletions internal/core/term_size_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//go:build windows

package core

import (
"os"

"golang.org/x/sys/windows"
)

// GetTerminalSize returns the terminal size, or an error if unavailable.
func GetTerminalSize() (TerminalSize, error) {
var ts TerminalSize

var info windows.ConsoleScreenBufferInfo
handle := windows.Handle(int(os.Stdout.Fd()))
err := windows.GetConsoleScreenBufferInfo(handle, &info)
if err != nil {
return ts, err
}

ts.Cols = int(info.Window.Right - info.Window.Left + 1)
ts.Rows = int(info.Window.Bottom - info.Window.Top + 1)
// Windows console doesn't provide pixel dimensions
ts.WidthPx = 0
ts.HeightPx = 0
return ts, nil
}

// GetTerminalCols returns the number of columns in the terminal, or 0 if unavailable.
func GetTerminalCols() int {
ts, err := GetTerminalSize()
if err != nil {
return 0
}
return ts.Cols
}
8 changes: 8 additions & 0 deletions internal/core/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import (
"runtime/debug"
)

// TerminalSize represents the dimensions of the terminal.
type TerminalSize struct {
Cols int // Number of columns (characters)
Rows int // Number of rows (characters)
WidthPx int // Width in pixels (0 if unavailable)
HeightPx int // Height in pixels (0 if unavailable)
}

var (
IsStderrTerm bool
IsStdoutTerm bool
Expand Down
9 changes: 9 additions & 0 deletions internal/fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ContentType int

const (
TypeUnknown ContentType = iota
TypeCSV
TypeHTML
TypeImage
TypeJSON
Expand Down Expand Up @@ -285,6 +286,10 @@ func formatResponse(ctx context.Context, r *Request, resp *http.Response) (io.Re
}

switch contentType {
case TypeCSV:
if format.FormatCSV(buf, p) == nil {
buf = p.Bytes()
}
case TypeHTML:
if format.FormatHTML(buf, p) == nil {
buf = p.Bytes()
Expand Down Expand Up @@ -328,6 +333,8 @@ func getContentType(headers http.Header) ContentType {
return TypeImage
case "application":
switch subtype {
case "csv":
return TypeCSV
case "json":
return TypeJSON
case "msgpack", "x-msgpack", "vnd.msgpack":
Expand All @@ -350,6 +357,8 @@ func getContentType(headers http.Header) ContentType {
}
case "text":
switch subtype {
case "csv":
return TypeCSV
case "html":
return TypeHTML
case "event-stream":
Expand Down
209 changes: 209 additions & 0 deletions internal/format/csv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
package format

import (
"bytes"
"encoding/csv"
"fmt"
"strings"

"github.com/ryanfowler/fetch/internal/core"

"github.com/mattn/go-runewidth"
)

// FormatCSV formats the provided CSV data to the Printer.
func FormatCSV(buf []byte, p *core.Printer) error {
err := formatCSV(buf, p)
if err != nil {
p.Reset()
}
return err
}

func formatCSV(buf []byte, p *core.Printer) error {
if len(buf) == 0 {
return nil
}

delimiter := detectDelimiter(buf)

reader := csv.NewReader(bytes.NewReader(buf))
reader.Comma = delimiter
reader.FieldsPerRecord = -1 // Allow ragged rows
reader.LazyQuotes = true // Lenient parsing

records, err := reader.ReadAll()
if err != nil {
return err
}
if len(records) == 0 {
return nil
}

// Calculate column widths
colWidths := calculateColumnWidths(records)
totalWidth := calculateTotalWidth(colWidths)
termCols := core.GetTerminalCols()

// Use vertical mode if terminal width is known and content exceeds it
if termCols > 0 && totalWidth > termCols && len(records) > 1 {
return writeVertical(p, records)
}

// Output formatted rows (horizontal mode)
for i, row := range records {
if i > 0 {
p.WriteString("\n")
}
writeRow(p, row, colWidths, i == 0)
}
p.WriteString("\n")

return nil
}

// detectDelimiter auto-detects the delimiter from the first line.
// Checks comma, tab, semicolon, and pipe. Defaults to comma.
func detectDelimiter(buf []byte) rune {
// Find the first line
firstLine, _, _ := bytes.Cut(buf, []byte{'\n'})

delimiters := []rune{',', '\t', ';', '|'}
counts := make(map[rune]int)

for _, d := range delimiters {
counts[d] = strings.Count(string(firstLine), string(d))
}

// Pick the delimiter with the highest count
maxCount := 0
bestDelimiter := ','
for _, d := range delimiters {
if counts[d] > maxCount {
maxCount = counts[d]
bestDelimiter = d
}
}

return bestDelimiter
}

// calculateColumnWidths finds the max display width per column across all rows.
func calculateColumnWidths(records [][]string) []int {
if len(records) == 0 {
return nil
}

// Find max number of columns
maxCols := 0
for _, row := range records {
if len(row) > maxCols {
maxCols = len(row)
}
}

widths := make([]int, maxCols)
for _, row := range records {
for j, cell := range row {
w := runewidth.StringWidth(cell)
if w > widths[j] {
widths[j] = w
}
}
}

return widths
}

// calculateTotalWidth returns the total display width of horizontal output.
func calculateTotalWidth(colWidths []int) int {
total := 0
for _, w := range colWidths {
total += w
}
if len(colWidths) > 1 {
total += (len(colWidths) - 1) * 2 // separator width (" ")
}
return total
}

// writeRow writes a single row with proper alignment and coloring.
func writeRow(p *core.Printer, row []string, colWidths []int, isHeader bool) {
for j, cell := range row {
if j > 0 {
p.WriteString(" ") // Column separator
}

// Apply color
if isHeader {
p.Set(core.Blue)
p.Set(core.Bold)
} else {
p.Set(core.Green)
}

p.WriteString(cell)
p.Reset()

// Add padding for alignment (except for the last column)
if j < len(colWidths)-1 {
cellWidth := runewidth.StringWidth(cell)
padding := colWidths[j] - cellWidth
for range padding {
p.WriteString(" ")
}
}
}
}

// writeVertical renders each data row as a vertical record with field labels.
func writeVertical(p *core.Printer, records [][]string) error {
headers := records[0]

// Calculate max header display width for right-alignment
maxHeaderWidth := 0
for _, h := range headers {
if w := runewidth.StringWidth(h); w > maxHeaderWidth {
maxHeaderWidth = w
}
}

for i, row := range records[1:] { // Skip header row
if i > 0 {
p.WriteString("\n")
}

// Row separator
p.Set(core.Dim)
fmt.Fprintf(p, "--- Row %d ---\n", i+1)
p.Reset()

for j, cell := range row {
header := ""
if j < len(headers) {
header = headers[j]
}

// Right-align header using display width
padding := maxHeaderWidth - runewidth.StringWidth(header)
for range padding {
p.WriteString(" ")
}

// Header in blue+bold
p.Set(core.Blue)
p.Set(core.Bold)
p.WriteString(header)
p.Reset()
p.WriteString(": ")

// Value in green
p.Set(core.Green)
p.WriteString(cell)
p.Reset()
p.WriteString("\n")
}
}

return nil
}
Loading