mirror of
https://github.com/charmbracelet/lipgloss.git
synced 2024-09-11 14:07:21 +03:00
* feat: implement list renderer * feat: data model for list * feat: add Offset, Height, Indent * feat: tree renderer * fix: list example * test: last tree node is a subtree Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * fix: tree Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * fix: lint Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * docs: example Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * test: inc cov Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * fix: multiline items Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * feat: one approach to sublists Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * fix: tree improvements * wip Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * wip Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * wip Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * wip Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * fix: prevent having to pass renderer to all trees * test: force linux line endings * fix: windows * fix: lint issues * fix: ignore lint issue failing forever * fix: renames * fix: renderer * ci: fix coveralls hopefully * wip Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * fix: style * docs: update Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * fix: renderer Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * test: cover with tests Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * feat: more list enumerators * fix: drop renderer api Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * feat: improve api Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * fix: prealloc * fix: clean up * fix: list setup * fix: improve sublist tests * fix: improve sublists * refactor: simplify * docs: more examples, readme updates Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * docs: readme * fix: set item style * fix(tree): do not use golden files * test: do not use golden files * refactor: remove unused code * feat: hide nodes * feat: filter * fix: allow fmt.Stringer * test: table within a tree * docs: update * fix: rename atter to data * test: test public api only * docs: sublist with table example * docs: update example * fix: lint issues Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * docs: update examples * docs: update * feat: offset * feat: offsetstart && offsetend * fix: remove last from enumerator * refactor: make tree more explicit * `New()` now take no args * added a new `Root(string)` method * added a new `Items(...any)` method * refactor: improve list * docs: fix examples * test: ensure embed lists in trees * docs: sublist example with list as tree node Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * refactor: first pass at examples cleanup * refactor: first pass at examples cleanup * fix: support multi-line prefixes * test: more glow-style lists within trees * docs: simplify examples * fix: improve handling of different types * chore: go mod tidy Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * fix: code review suggestions Co-authored-by: Ayman Bagabas <ayman.bagabas@gmail.com> Co-authored-by: bashbunni <15822994+bashbunni@users.noreply.github.com> * fix: apply code review suggestion * feat(tree): rounded enumerator (#280) * chore(examples): go mod tidy * feat(tree): add rounded corner enumerator * test: rounded enumerator test * docs: readme updates Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * docs: readme updates Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * chore: fmt Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * docs: godoc * docs: godoc * fix: bash's issue * fix: bad example Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * docs: improve godoc for trees and lists (#296) * docs(godoc): add overview * docs(godoc): include examples in godoc * docs(godoc): fix roman numerals example * docs(godoc): fix tree examples * docs(godoc): attempt to fix list Enumerator examples * docs: added some examples to godoc * fix(list): New(items...) with sublist * docs: update examples * chore(tree): improve var names in rounded example * fix: use padding instead of margin to better styles * fix: lint --------- Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> Co-authored-by: Maas Lalani <maas@lalani.dev> Co-authored-by: Ayman Bagabas <ayman.bagabas@gmail.com> Co-authored-by: bashbunni <15822994+bashbunni@users.noreply.github.com> Co-authored-by: Christian Rocha <christian@rocha.is>
114 lines
2.3 KiB
Go
114 lines
2.3 KiB
Go
package table
|
|
|
|
// Data is the interface that wraps the basic methods of a table model.
|
|
type Data interface {
|
|
// At returns the contents of the cell at the given index.
|
|
At(row, cell int) string
|
|
|
|
// Rows returns the number of rows in the table.
|
|
Rows() int
|
|
|
|
// Columns returns the number of columns in the table.
|
|
Columns() int
|
|
}
|
|
|
|
// StringData is a string-based implementation of the Data interface.
|
|
type StringData struct {
|
|
rows [][]string
|
|
columns int
|
|
}
|
|
|
|
// NewStringData creates a new StringData with the given number of columns.
|
|
func NewStringData(rows ...[]string) *StringData {
|
|
m := StringData{columns: 0}
|
|
|
|
for _, row := range rows {
|
|
m.columns = max(m.columns, len(row))
|
|
m.rows = append(m.rows, row)
|
|
}
|
|
|
|
return &m
|
|
}
|
|
|
|
// Append appends the given row to the table.
|
|
func (m *StringData) Append(row []string) {
|
|
m.columns = max(m.columns, len(row))
|
|
m.rows = append(m.rows, row)
|
|
}
|
|
|
|
// At returns the contents of the cell at the given index.
|
|
func (m *StringData) At(row, cell int) string {
|
|
if row >= len(m.rows) || cell >= len(m.rows[row]) {
|
|
return ""
|
|
}
|
|
|
|
return m.rows[row][cell]
|
|
}
|
|
|
|
// Columns returns the number of columns in the table.
|
|
func (m *StringData) Columns() int {
|
|
return m.columns
|
|
}
|
|
|
|
// Item appends the given row to the table.
|
|
func (m *StringData) Item(rows ...string) *StringData {
|
|
m.columns = max(m.columns, len(rows))
|
|
m.rows = append(m.rows, rows)
|
|
return m
|
|
}
|
|
|
|
// Rows returns the number of rows in the table.
|
|
func (m *StringData) Rows() int {
|
|
return len(m.rows)
|
|
}
|
|
|
|
// Filter applies a filter on some data.
|
|
type Filter struct {
|
|
data Data
|
|
filter func(row int) bool
|
|
}
|
|
|
|
// NewFilter initializes a new Filter.
|
|
func NewFilter(data Data) *Filter {
|
|
return &Filter{data: data}
|
|
}
|
|
|
|
// Filter applies the given filter function to the data.
|
|
func (m *Filter) Filter(f func(row int) bool) *Filter {
|
|
m.filter = f
|
|
return m
|
|
}
|
|
|
|
// At returns the row at the given index.
|
|
func (m *Filter) At(row, cell int) string {
|
|
j := 0
|
|
for i := 0; i < m.data.Rows(); i++ {
|
|
if m.filter(i) {
|
|
if j == row {
|
|
return m.data.At(i, cell)
|
|
}
|
|
|
|
j++
|
|
}
|
|
}
|
|
|
|
return ""
|
|
}
|
|
|
|
// Columns returns the number of columns in the table.
|
|
func (m *Filter) Columns() int {
|
|
return m.data.Columns()
|
|
}
|
|
|
|
// Rows returns the number of rows in the table.
|
|
func (m *Filter) Rows() int {
|
|
j := 0
|
|
for i := 0; i < m.data.Rows(); i++ {
|
|
if m.filter(i) {
|
|
j++
|
|
}
|
|
}
|
|
|
|
return j
|
|
}
|