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 @@ A modern HTTP(S) client for the command line.

## Features

- **Response formatting** - Automatic formatting and syntax highlighting for JSON, XML, YAML, HTML, CSS, CSV, MessagePack, Protocol Buffers, and more
- **Response formatting** - Automatic formatting and syntax highlighting for JSON, XML, YAML, HTML, CSS, CSV, Markdown, MessagePack, Protocol Buffers, and more
- **Image rendering** - Display images directly in your terminal
- **WebSocket support** - Bidirectional WebSocket connections with automatic JSON formatting
- **gRPC support** - Make gRPC calls with automatic JSON-to-protobuf conversion
Expand Down
14 changes: 14 additions & 0 deletions docs/output-formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ Features:
fetch example.com/styles.css
```

### Markdown

**Content-Types**: `text/markdown`, `text/x-markdown`

Features:

- Syntax highlighting for headings, bold, italic, code spans, links, images
- Fenced code block delegation to JSON, YAML, XML, HTML, CSS formatters
- Blockquote and list marker highlighting

```sh
fetch example.com/README.md
```

### CSV

**Content-Types**: `text/csv`, `application/csv`
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/mattn/go-runewidth v0.0.19
github.com/quic-go/quic-go v0.59.0
github.com/tinylib/msgp v1.6.3
github.com/yuin/goldmark v1.7.16
golang.org/x/crypto v0.48.0
golang.org/x/image v0.36.0
golang.org/x/net v0.50.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/tinylib/msgp v1.6.3 h1:bCSxiTz386UTgyT1i0MSCvdbWjVW+8sG3PjkGsZQt4s=
github.com/tinylib/msgp v1.6.3/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA=
github.com/yuin/goldmark v1.7.16 h1:n+CJdUxaFMiDUNnWC3dMWCIQJSkxH4uz3ZwQBkAlVNE=
github.com/yuin/goldmark v1.7.16/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=
go.uber.org/mock v0.5.2 h1:LbtPTcP8A5k9WPXj54PPPbjcI4Y6lhyOZXn+VS7wNko=
go.uber.org/mock v0.5.2/go.mod h1:wLlUxC2vVTPTaE3UD51E0BGOAElKrILxhVSDYQLld5o=
golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts=
Expand Down
33 changes: 31 additions & 2 deletions internal/core/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"io"
"os"
"sync"
)

// Sequence represents an ANSI escape sequence.
Expand Down Expand Up @@ -61,7 +62,7 @@ func (h *Handle) Stdout() *Printer {
// Printer allows for writing data with optional ANSI escape sequences based on
// the color settings for a target.
type Printer struct {
file *os.File
file io.Writer
buf bytes.Buffer
useColor bool
}
Expand All @@ -81,6 +82,25 @@ func newPrinter(file *os.File, isTerm bool, c Color) *Printer {
return &Printer{file: file, useColor: useColor}
}

// TestPrinter returns a Printer suitable for testing. All output, including
// flushed data, is captured and accessible via Bytes.
func TestPrinter(useColor bool) *Printer {
return &Printer{file: &lockedBuffer{}, useColor: useColor}
}

// lockedBuffer is a goroutine-safe bytes.Buffer used as the flush target in
// test printers so that Bytes can return the combined buffered + flushed data.
type lockedBuffer struct {
mu sync.Mutex
buf bytes.Buffer
}

func (lb *lockedBuffer) Write(p []byte) (int, error) {
lb.mu.Lock()
defer lb.mu.Unlock()
return lb.buf.Write(p)
}

// Set writes the provided Sequence.
func (p *Printer) Set(s Sequence) {
if p.useColor {
Expand Down Expand Up @@ -108,8 +128,17 @@ func (p *Printer) Discard() {
p.buf.Reset()
}

// Bytes returns the current contents of the buffer.
// Bytes returns the current contents of the buffer. For test printers created
// with TestPrinter, this also includes previously flushed data.
func (p *Printer) Bytes() []byte {
if lb, ok := p.file.(*lockedBuffer); ok {
lb.mu.Lock()
flushed := lb.buf.Bytes()
lb.mu.Unlock()
if len(flushed) > 0 {
return append(flushed, p.buf.Bytes()...)
}
}
return p.buf.Bytes()
}

Expand Down
3 changes: 3 additions & 0 deletions internal/format/contenttype.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
TypeHTML
TypeImage
TypeJSON
TypeMarkdown
TypeMsgPack
TypeNDJSON
TypeProtobuf
Expand Down Expand Up @@ -82,6 +83,8 @@ func GetContentType(headers http.Header) (ContentType, string) {
return TypeCSV, charset
case "html":
return TypeHTML, charset
case "markdown", "x-markdown":
return TypeMarkdown, charset
case "event-stream":
return TypeSSE, charset
case "xml":
Expand Down
Loading