Skip to content

Commit

Permalink
docs(table): README and GoDoc edits (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Sep 19, 2023
1 parent a93473d commit 1206aef
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
54 changes: 28 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,16 +391,34 @@ height := lipgloss.Height(block)
w, h := lipgloss.Size(block)
```

### Tables
### Placing Text in Whitespace

Sometimes you’ll simply want to place a block of text in whitespace.

```go
// Center a paragraph horizontally in a space 80 cells wide. The height of
// the block returned will be as tall as the input paragraph.
block := lipgloss.PlaceHorizontal(80, lipgloss.Center, fancyStyledParagraph)

// Place a paragraph at the bottom of a space 30 cells tall. The width of
// the text block returned will be as wide as the input paragraph.
block := lipgloss.PlaceVertical(30, lipgloss.Bottom, fancyStyledParagraph)

// Place a paragraph in the bottom right corner of a 30x80 cell space.
block := lipgloss.Place(30, 80, lipgloss.Right, lipgloss.Bottom, fancyStyledParagraph)
```

You can also style the whitespace. For details, see [the docs][docs].

When you need to render a table in the terminal, Lip Gloss ships with a table
rendering sub-package.
### Rendering Tables

Lip Gloss ships with a table rendering sub-package.

```go
import "github.com/charmbracelet/lipgloss/table"
```

Define some table rows and data.
Define some rows of data.

```go
rows := [][]any{
Expand All @@ -409,7 +427,6 @@ rows := [][]any{
{"Arabic", "أهلين", "أهلا"},
{"Russian", "Здравствуйте", "Привет"},
{"Spanish", "Hola", "¿Qué tal?"},
{"English", "You look absolutely fabulous.", "How's it going?"},
}
```

Expand All @@ -431,35 +448,20 @@ t := table.New().
}).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows...)
```

Print out the table.

```go
fmt.Println(t)
// You can also add tables row-by-row
t.Row("English", "You look absolutely fabulous.", "How's it going?")
```

![Table Printed Example](https://stuff.charm.sh/lipgloss/lipgloss-example-table-2.png)

### Placing Text in Whitespace

Sometimes you’ll simply want to place a block of text in whitespace.
Print the table.

```go
// Center a paragraph horizontally in a space 80 cells wide. The height of
// the block returned will be as tall as the input paragraph.
block := lipgloss.PlaceHorizontal(80, lipgloss.Center, fancyStyledParagraph)

// Place a paragraph at the bottom of a space 30 cells tall. The width of
// the text block returned will be as wide as the input paragraph.
block := lipgloss.PlaceVertical(30, lipgloss.Bottom, fancyStyledParagraph)

// Place a paragraph in the bottom right corner of a 30x80 cell space.
block := lipgloss.Place(30, 80, lipgloss.Right, lipgloss.Bottom, fancyStyledParagraph)
fmt.Println(t)
```

You can also style the whitespace. For details, see [the docs][docs].
![Table Example](https://stuff.charm.sh/lipgloss/lipgloss-example-table-2.png)

For more on tables see [the docs](https://pkg.go.dev/github.com/charmbracelet/lipgloss?tab=doc).

***

Expand Down
18 changes: 18 additions & 0 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ import (
//
// It takes the row and column of the cell as an input and determines the
// lipgloss Style to use for that cell position.
//
// Example:
//
// t := table.New().
// Headers("Name", "Age").
// Row("Kini", 4).
// Row("Eli", 1).
// Row("Iris", 102).
// StyleFunc(func(row, col int) lipgloss.Style {
// switch {
// case row == 0:
// return HeaderStyle
// case row%2 == 0:
// return EvenRowStyle
// default:
// return OddRowStyle
// }
// })
type StyleFunc func(row, col int) lipgloss.Style

// NoTableStyle is a TableStyleFunc that returns a new Style with no attributes.
Expand Down

0 comments on commit 1206aef

Please sign in to comment.