"FieldLength" is now "FieldWidth" everywhere for clarity and consistency. Sorry for this breaking change. It is better in the long run.

This commit is contained in:
Oliver 2018-01-17 21:17:59 +01:00
parent 258f212e5e
commit 4db30f9ed4
8 changed files with 78 additions and 77 deletions

View File

@ -45,7 +45,7 @@ func NewApplication() *Application {
//
// Note that this also affects the default event handling of the application
// itself: Such a handler can intercept the Ctrl-C event which closes the
// applicaton.
// applicatoon.
func (a *Application) SetInputCapture(capture func(event *tcell.EventKey) *tcell.EventKey) *Application {
a.inputCapture = capture
return a

View File

@ -96,8 +96,8 @@ func (c *Checkbox) SetFormAttributes(label string, labelColor, bgColor, fieldTex
return c
}
// GetFieldLength returns this primitive's field length.
func (c *Checkbox) GetFieldLength() int {
// GetFieldWidth returns this primitive's field width.
func (c *Checkbox) GetFieldWidth() int {
return 1
}

View File

@ -10,7 +10,7 @@ func main() {
app := tview.NewApplication()
inputField := tview.NewInputField().
SetLabel("Enter a number: ").
SetFieldLength(10).
SetFieldWidth(10).
SetAcceptanceFunc(tview.InputFieldInteger).
SetDoneFunc(func(key tcell.Key) {
app.Stop()

View File

@ -42,9 +42,9 @@ type DropDown struct {
// The text color of the input area.
fieldTextColor tcell.Color
// The length of the input area. A value of 0 means extend as much as
// The screen width of the input area. A value of 0 means extend as much as
// possible.
fieldLength int
fieldWidth int
// An optional function which is called when the user indicated that they
// are done selecting options. The key which was pressed is provided (tab,
@ -130,26 +130,26 @@ func (d *DropDown) SetFormAttributes(label string, labelColor, bgColor, fieldTex
return d
}
// SetFieldLength sets the length of the options area. A value of 0 means extend
// to as long as the longest option text.
func (d *DropDown) SetFieldLength(length int) *DropDown {
d.fieldLength = length
// SetFieldWidth sets the screen width of the options area. A value of 0 means
// extend to as long as the longest option text.
func (d *DropDown) SetFieldWidth(width int) *DropDown {
d.fieldWidth = width
return d
}
// GetFieldLength returns this primitive's field length.
func (d *DropDown) GetFieldLength() int {
if d.fieldLength > 0 {
return d.fieldLength
// GetFieldWidth returns this primitive's field screen width.
func (d *DropDown) GetFieldWidth() int {
if d.fieldWidth > 0 {
return d.fieldWidth
}
fieldLength := 0
fieldWidth := 0
for _, option := range d.options {
length := StringWidth(option.Text)
if length > fieldLength {
fieldLength = length
width := StringWidth(option.Text)
if width > fieldWidth {
fieldWidth = width
}
}
return fieldLength
return fieldWidth
}
// AddOption adds a new selectable option to this drop-down. The "selected"
@ -212,27 +212,27 @@ func (d *DropDown) Draw(screen tcell.Screen) {
x += drawnWidth
// What's the longest option text?
maxLength := 0
maxWidth := 0
for _, option := range d.options {
length := StringWidth(option.Text)
if length > maxLength {
maxLength = length
strWidth := StringWidth(option.Text)
if strWidth > maxWidth {
maxWidth = strWidth
}
}
// Draw selection area.
fieldLength := d.fieldLength
if fieldLength == 0 {
fieldLength = maxLength
fieldWidth := d.fieldWidth
if fieldWidth == 0 {
fieldWidth = maxWidth
}
if rightLimit-x < fieldLength {
fieldLength = rightLimit - x
if rightLimit-x < fieldWidth {
fieldWidth = rightLimit - x
}
fieldStyle := tcell.StyleDefault.Background(d.fieldBackgroundColor)
if d.GetFocusable().HasFocus() && !d.open {
fieldStyle = fieldStyle.Background(d.fieldTextColor)
}
for index := 0; index < fieldLength; index++ {
for index := 0; index < fieldWidth; index++ {
screen.SetContent(x+index, y, ' ', nil, fieldStyle)
}
@ -242,7 +242,7 @@ func (d *DropDown) Draw(screen tcell.Screen) {
if d.GetFocusable().HasFocus() && !d.open {
color = d.fieldBackgroundColor
}
Print(screen, d.options[d.currentOption].Text, x, y, fieldLength, AlignLeft, color)
Print(screen, d.options[d.currentOption].Text, x, y, fieldWidth, AlignLeft, color)
}
// Draw options list.
@ -250,7 +250,7 @@ func (d *DropDown) Draw(screen tcell.Screen) {
// We prefer to drop down but if there is no space, maybe drop up?
lx := x
ly := y + 1
lwidth := maxLength
lwidth := maxWidth
lheight := len(d.options)
_, sheight := screen.Size()
if ly+lheight >= sheight && ly-lheight-1 >= 0 {

54
form.go
View File

@ -6,10 +6,10 @@ import (
"github.com/gdamore/tcell"
)
// DefaultFormFieldLength is the default field length of form elements whose
// field length is flexible (0). This is used in the Form class for horizontal
// layouts.
var DefaultFormFieldLength = 10
// DefaultFormFieldWidth is the default field screen width of form elements
// whose field width is flexible (0). This is used in the Form class for
// horizontal layouts.
var DefaultFormFieldWidth = 10
// FormItem is the interface all form items must implement to be able to be
// included in a form.
@ -22,10 +22,11 @@ type FormItem interface {
// SetFormAttributes sets a number of item attributes at once.
SetFormAttributes(label string, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem
// GetFieldLength returns the length of the form item's field (the area which
// is manipulated by the user). A value of 0 indicates the the field length
// is flexible and may use as much space as required.
GetFieldLength() int
// GetFieldWidth returns the width of the form item's field (the area which
// is manipulated by the user) in number of screen cells. A value of 0
// indicates the the field width is flexible and may use as much space as
// required.
GetFieldWidth() int
// SetEnteredFunc sets the handler function for when the user finished
// entering data into the item. The handler may receive events for the
@ -153,15 +154,15 @@ func (f *Form) SetButtonTextColor(color tcell.Color) *Form {
}
// AddInputField adds an input field to the form. It has a label, an optional
// initial value, a field length (a value of 0 extends it as far as possible),
// initial value, a field width (a value of 0 extends it as far as possible),
// an optional accept function to validate the item's value (set to nil to
// accept any text), and an (optional) callback function which is invoked when
// the input field's text has changed.
func (f *Form) AddInputField(label, value string, fieldLength int, accept func(textToCheck string, lastChar rune) bool, changed func(text string)) *Form {
func (f *Form) AddInputField(label, value string, fieldWidth int, accept func(textToCheck string, lastChar rune) bool, changed func(text string)) *Form {
f.items = append(f.items, NewInputField().
SetLabel(label).
SetText(value).
SetFieldLength(fieldLength).
SetFieldWidth(fieldWidth).
SetAcceptanceFunc(accept).
SetChangedFunc(changed))
return f
@ -170,17 +171,17 @@ func (f *Form) AddInputField(label, value string, fieldLength int, accept func(t
// AddPasswordField adds a password field to the form. This is similar to an
// input field except that the user's input not shown. Instead, a "mask"
// character is displayed. The password field has a label, an optional initial
// value, a field length (a value of 0 extends it as far as possible), and an
// value, a field width (a value of 0 extends it as far as possible), and an
// (optional) callback function which is invoked when the input field's text has
// changed.
func (f *Form) AddPasswordField(label, value string, fieldLength int, mask rune, changed func(text string)) *Form {
func (f *Form) AddPasswordField(label, value string, fieldWidth int, mask rune, changed func(text string)) *Form {
if mask == 0 {
mask = '*'
}
f.items = append(f.items, NewInputField().
SetLabel(label).
SetText(value).
SetFieldLength(fieldLength).
SetFieldWidth(fieldWidth).
SetMaskCharacter(mask).
SetChangedFunc(changed))
return f
@ -222,6 +223,7 @@ func (f *Form) Clear(includeButtons bool) *Form {
if includeButtons {
f.buttons = nil
}
f.focusedElement = 0
return f
}
@ -258,15 +260,15 @@ func (f *Form) Draw(screen tcell.Screen) {
startX := x
// Find the longest label.
var labelLength int
var maxLabelWidth int
for _, item := range f.items {
label := strings.TrimSpace(item.GetLabel())
labelWidth := StringWidth(label)
if labelWidth > labelLength {
labelLength = labelWidth
if labelWidth > maxLabelWidth {
maxLabelWidth = labelWidth
}
}
labelLength++ // Add one space.
maxLabelWidth++ // Add one space.
// Set up and draw the input fields.
for _, item := range f.items {
@ -280,16 +282,16 @@ func (f *Form) Draw(screen tcell.Screen) {
labelWidth := StringWidth(label)
var itemWidth int
if f.horizontal {
fieldLength := item.GetFieldLength()
if fieldLength == 0 {
fieldLength = DefaultFormFieldLength
fieldWidth := item.GetFieldWidth()
if fieldWidth == 0 {
fieldWidth = DefaultFormFieldWidth
}
label += " "
labelWidth++
itemWidth = labelWidth + fieldLength
itemWidth = labelWidth + fieldWidth
} else {
// We want all fields to align vertically.
label += strings.Repeat(" ", labelLength-labelWidth)
label += strings.Repeat(" ", maxLabelWidth-labelWidth)
itemWidth = width
}
@ -330,9 +332,9 @@ func (f *Form) Draw(screen tcell.Screen) {
buttonWidths := make([]int, len(f.buttons))
buttonsWidth := 0
for index, button := range f.buttons {
width := StringWidth(button.GetLabel()) + 4
buttonWidths[index] = width
buttonsWidth += width + 1
w := StringWidth(button.GetLabel()) + 4
buttonWidths[index] = w
buttonsWidth += w + 1
}
buttonsWidth--

View File

@ -35,9 +35,9 @@ type InputField struct {
// The text color of the input area.
fieldTextColor tcell.Color
// The length of the input area. A value of 0 means extend as much as
// The screen width of the input area. A value of 0 means extend as much as
// possible.
fieldLength int
fieldWidth int
// A character to mask entered text (useful for password fields). A value of 0
// disables masking.
@ -118,16 +118,16 @@ func (i *InputField) SetFormAttributes(label string, labelColor, bgColor, fieldT
return i
}
// SetFieldLength sets the length of the input area. A value of 0 means extend
// as much as possible.
func (i *InputField) SetFieldLength(length int) *InputField {
i.fieldLength = length
// SetFieldWidth sets the screen width of the input area. A value of 0 means
// extend as much as possible.
func (i *InputField) SetFieldWidth(width int) *InputField {
i.fieldWidth = width
return i
}
// GetFieldLength returns this primitive's field length.
func (i *InputField) GetFieldLength() int {
return i.fieldLength
// GetFieldWidth returns this primitive's field width.
func (i *InputField) GetFieldWidth() int {
return i.fieldWidth
}
// SetMaskCharacter sets a character that masks user input on a screen. A value
@ -188,7 +188,7 @@ func (i *InputField) Draw(screen tcell.Screen) {
x += drawnWidth
// Draw input area.
fieldWidth := i.fieldLength
fieldWidth := i.fieldWidth
if fieldWidth == 0 {
fieldWidth = math.MaxInt32
}
@ -206,8 +206,8 @@ func (i *InputField) Draw(screen tcell.Screen) {
text = strings.Repeat(string(i.maskCharacter), utf8.RuneCountInString(i.text))
}
fieldWidth-- // We need one cell for the cursor.
if fieldWidth < runewidth.StringWidth(i.text) {
runes := []rune(i.text)
if fieldWidth < runewidth.StringWidth(text) {
runes := []rune(text)
for pos := len(runes) - 1; pos >= 0; pos-- {
ch := runes[pos]
w := runewidth.RuneWidth(ch)
@ -252,11 +252,11 @@ func (i *InputField) setCursor(screen tcell.Screen) {
y++
rightLimit -= 2
}
fieldLength := runewidth.StringWidth(i.text)
if i.fieldLength > 0 && fieldLength > i.fieldLength-1 {
fieldLength = i.fieldLength - 1
fieldWidth := runewidth.StringWidth(i.text)
if i.fieldWidth > 0 && fieldWidth > i.fieldWidth-1 {
fieldWidth = i.fieldWidth - 1
}
x += StringWidth(i.label) + fieldLength
x += StringWidth(i.label) + fieldWidth
if x >= rightLimit {
x = rightLimit - 1
}

View File

@ -209,8 +209,8 @@ func (l *List) Draw(screen tcell.Screen) {
// Background color of selected text.
if index == l.currentItem {
textLength := StringWidth(item.MainText)
for bx := 0; bx < textLength && bx < width; bx++ {
textWidth := StringWidth(item.MainText)
for bx := 0; bx < textWidth && bx < width; bx++ {
m, c, style, _ := screen.GetContent(x+bx, y)
fg, _, _ := style.Decompose()
if fg == l.mainTextColor {

View File

@ -550,7 +550,7 @@ func (t *TextView) reindexBuffer(width int) {
}
// Create index from split lines.
var startPos, originalPos, colorPos, regionPos, escapePos int
var originalPos, colorPos, regionPos, escapePos int
for _, splitLine := range splitLines {
line := &textViewIndex{
Line: bufferIndex,
@ -594,7 +594,6 @@ func (t *TextView) reindexBuffer(width int) {
}
// Advance to next line.
startPos += lineLength
originalPos += lineLength
// Append this line.