1
0
mirror of https://github.com/mum4k/termdash.git synced 2025-05-08 19:29:25 +08:00

Adding options for text trimming.

This commit is contained in:
Jakub Sobon 2019-03-11 22:20:23 -04:00
parent b11eb11378
commit d47c6cf0ff
No known key found for this signature in database
GPG Key ID: F2451A77FB05D3B7
5 changed files with 125 additions and 6 deletions

View File

@ -1,3 +1,17 @@
// Copyright 2019 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package table
// content.go defines a type that allow callers to populate the table with
@ -8,6 +22,7 @@ import (
"github.com/mum4k/termdash/align"
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/internal/wrap"
"github.com/mum4k/termdash/linestyle"
)
@ -37,7 +52,8 @@ type hierarchicalOptions struct {
verticalCellPadding *int
alignHorizontal *align.Horizontal
alignVertical *align.Vertical
height int
height *int
wrapMode *wrap.Mode
}
// contentOption implements ContentOption.
@ -67,7 +83,9 @@ func BorderCellOpts(cellOpts ...cell.Option) ContentOption {
// The number of values must match the number of Columns specified on the call
// to NewContent. All the values must be in the range 0 < v <= 100 and the sum
// of the values must be 100.
// Defaults to column width automatically adjusted to the content.
// If content wrapping isn't enabled (see WrapContent), defaults to column
// width automatically adjusted to the content. When wrapping is enabled, all
// columns will have equal width.
func ColumnWidthsPercent(widths ...int) ContentOption {
return contentOption(func(cOpts *contentOptions) {
cOpts.columnWidthsPercent = widths
@ -111,7 +129,7 @@ func ContentCellOpts(cellOpts ...cell.Option) ContentOption {
// level.
func ContentRowHeight(height int) ContentOption {
return contentOption(func(cOpts *contentOptions) {
cOpts.hierarchical.height = height
cOpts.hierarchical.height = &height
})
}
@ -159,6 +177,18 @@ func AlignVertical(v align.Vertical) ContentOption {
})
}
// WrapContent sets the content of individual cells to be wrapped if it
// cannot fit fully.
// Defaults is to not wrap, text that is too long will be trimmed instead.
// This is a hierarchical option and can be overridden when provided at Row
// or Cell level.
func WrapContent() ContentOption {
return contentOption(func(cOpts *contentOptions) {
wm := wrap.AtWords
cOpts.hierarchical.wrapMode = &wm
})
}
// Columns specifies the number of columns in the table.
type Columns int

View File

@ -1,3 +1,17 @@
// Copyright 2019 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package table
// content_cell.go defines a type that represents a single cell in the table.
@ -5,6 +19,7 @@ package table
import (
"github.com/mum4k/termdash/align"
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/internal/wrap"
)
// CellOption is used to provide options to NewCellWithOpts.
@ -56,7 +71,7 @@ func CellOpts(cellOpts ...cell.Option) CellOption {
// Row level.
func CellHeight(height int) CellOption {
return cellOption(func(c *Cell) {
c.hierarchical.height = height
c.hierarchical.height = &height
})
}
@ -104,6 +119,18 @@ func CellAlignVertical(v align.Vertical) CellOption {
})
}
// CellWrapContent sets the content of individual cells to be wrapped if it
// cannot fit fully.
// Defaults is to not wrap, text that is too long will be trimmed instead.
// This is a hierarchical option, it overrides the one provided at Content or
// Row level.
func CellWrapContent() CellOption {
return cellOption(func(c *Cell) {
wm := wrap.AtWords
c.hierarchical.wrapMode = &wm
})
}
// Cell is one cell in a Row.
type Cell struct {
data []*Data
@ -115,6 +142,11 @@ type Cell struct {
// NewCell returns a new Cell with the provided text.
// If you need to apply options at the Cell or Data level use NewCellWithOpts.
// The text contain cannot control characters (unicode.IsControl) or space
// character (unicode.IsSpace) other than:
// ' ', '\n'
// Any newline ('\n') characters are interpreted as newlines when displaying
// the text.
func NewCell(text string) *Cell {
return nil
}

View File

@ -1,3 +1,17 @@
// Copyright 2019 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package table
// content_data.go defines a type that represents data within a table cell.
@ -39,6 +53,11 @@ type Data struct {
}
// NewData creates new Data with the provided text and applies the options.
// The text contain cannot control characters (unicode.IsControl) or space
// character (unicode.IsSpace) other than:
// ' ', '\n'
// Any newline ('\n') characters are interpreted as newlines when displaying
// the text.
func NewData(text string, opts ...DataOption) *Data {
dOpts := &dataOptions{}
for _, opt := range opts {

View File

@ -1,3 +1,17 @@
// Copyright 2019 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package table
// content_row.go defines a type that represents a single row in the table.
@ -75,7 +89,7 @@ func RowCellOpts(cellOpts ...cell.Option) RowOption {
// level and can be overridden when provided at the Cell level.
func RowHeight(height int) RowOption {
return rowOption(func(r *Row) {
r.hierarchical.height = height
r.hierarchical.height = &height
})
}
@ -136,7 +150,17 @@ type Row struct {
// The header remains visible while scrolling and allows for sorting of content
// based on its values. Header row cannot be highlighted.
// Content can only have one header Row.
func NewHeader(cells []*Cell, opts ...RowOption) *Row {
// If you need to apply options at the Row level, use NewHeaderWithOpts.
func NewHeader(cells ...*Cell) *Row {
return nil
}
// NewHeaderWithOpts returns a new Row that will be the header of the table and
// applies the provided options.
// The header remains visible while scrolling and allows for sorting of content
// based on its values. Header row cannot be highlighted.
// Content can only have one header Row.
func NewHeaderWithOpts(cells []*Cell, opts ...RowOption) *Row {
return nil
}

View File

@ -1,3 +1,17 @@
// Copyright 2019 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package table
func ExampleContent() {