// Deprecated: use [cursor.BlinkSpeed] instead. BlinkSpeed time.Duration
// Styles. These will be applied as inline styles. // // For an introduction to styling with Lip Gloss see: // https://github.com/charmbracelet/lipgloss PromptStyle lipgloss.Style TextStyle lipgloss.Style PlaceholderStyle lipgloss.Style CompletionStyle lipgloss.Style
// Deprecated: use Cursor.Style instead. CursorStyle lipgloss.Style
// CharLimit is the maximum amount of characters this input element will // accept. If 0 or less, there's no limit. CharLimit int
// Width is the maximum number of characters that can be displayed at once. // It essentially treats the text field like a horizontally scrolling // viewport. If 0 or less this setting is ignored. Width int
// KeyMap encodes the keybindings recognized by the widget. KeyMap KeyMap
// Validate is a function that checks whether or not the text within the // input is valid. If it is not valid, the `Err` field will be set to the // error returned by the function. If the function is not defined, all // input is considered valid. Validate ValidateFunc
// Should the input suggest to complete ShowSuggestions bool // contains filtered or unexported fields }
设置 placeholder
修改其 Placeholder 成员:
1 2
ti := textinput.New() ti.Placeholder = "this is a placeholder"
设置输入框宽度
修改其 Width 成员:
1 2
ti := textinput.New() ti.Width = 20
Init(), Update(), View() 三个基本函数的使用
如果这里的 model 定义为:
1 2 3 4
type model struct { textInput textinput.Model err error }
// Prompt is printed at the beginning of each line. // // When changing the value of Prompt after the model has been // initialized, ensure that SetWidth() gets called afterwards. // // See also SetPromptFunc(). Prompt string
// Placeholder is the text displayed when the user // hasn't entered anything yet. Placeholder string
// ShowLineNumbers, if enabled, causes line numbers to be printed // after the prompt. ShowLineNumbers bool
// EndOfBufferCharacter is displayed at the end of the input. EndOfBufferCharacter rune
// KeyMap encodes the keybindings recognized by the widget. KeyMap KeyMap
// Styling. FocusedStyle and BlurredStyle are used to style the textarea in // focused and blurred states. FocusedStyle Style BlurredStyle Style
// Cursor is the text area cursor. Cursor cursor.Model
// CharLimit is the maximum number of characters this input element will // accept. If 0 or less, there's no limit. CharLimit int
// MaxHeight is the maximum height of the text area in rows. If 0 or less, // there's no limit. MaxHeight int
// MaxWidth is the maximum width of the text area in columns. If 0 or less, // there's no limit. MaxWidth int // contains filtered or unexported fields }
type LineInfo struct { // Width is the number of columns in the line. Width int // CharWidth is the number of characters in the line to account for // double-width runes. CharWidth int // Height is the number of rows in the line. Height int // StartColumn is the index of the first column of the line. StartColumn int // ColumnOffset is the number of columns that the cursor is offset from the // start of the line. ColumnOffset int // RowOffset is the number of rows that the cursor is offset from the start // of the line. RowOffset int // CharOffset is the number of characters that the cursor is offset // from the start of the line. This will generally be equivalent to // ColumnOffset, but will be different there are double-width runes before // the cursor. CharOffset int }
type Model struct { // Type configures how the pagination is rendered (Arabic, Dots). Type Type // Page is the current page number. Page int // PerPage is the number of items per page. PerPage int // TotalPages is the total number of pages. TotalPages int // ActiveDot is used to mark the current page under the Dots display type. ActiveDot string // InactiveDot is used to mark inactive pages under the Dots display type. InactiveDot string // ArabicFormat is the printf-style format to use for the Arabic display type. ArabicFormat string
// KeyMap encodes the keybindings recognized by the widget. KeyMap KeyMap }
type Type
1
type Type int
定义有:
1 2 3 4
const ( Arabic Type = iota Dots )
New 创建一个 paginator model
1
funcNew() model
GetSliceBounds 获取一个 slice 的开始和结束索引
1
func(m *Model) GetSliceBounds(length int) (start int, end int)
如:
1 2 3
bunchOfStuff := []stuff{...} start, end := model.GetSliceBounds(len(bunchOfStuff)) sliceToRender := bunchOfStuff[start:end]
type Model struct { Width int Height int KeyMap KeyMap
// Whether or not to respond to the mouse. The mouse must be enabled in // Bubble Tea for this to work. For details, see the Bubble Tea docs. MouseWheelEnabled bool
// The number of lines the mouse wheel will scroll. By default, this is 3. MouseWheelDelta int
// YOffset is the vertical scroll position. YOffset int
// YPosition is the position of the viewport in relation to the terminal // window. It's used in high performance rendering only. YPosition int
// Style applies a lipgloss style to the viewport. Realistically, it's most // useful for setting borders, margins and padding. Style lipgloss.Style
// HighPerformanceRendering bypasses the normal Bubble Tea renderer to // provide higher performance rendering. Most of the time the normal Bubble // Tea rendering methods will suffice, but if you're passing content with // a lot of ANSI escape codes you may see improved rendering in certain // terminals with this enabled. // // This should only be used in program occupying the entire terminal, // which is usually via the alternate screen buffer. HighPerformanceRendering bool // contains filtered or unexported fields }
type KeyMap
有预定义的 keybinding:
1 2 3 4 5 6 7 8
type KeyMap struct { PageDown key.Binding PageUp key.Binding HalfPageUp key.Binding HalfPageDown key.Binding Down key.Binding Up key.Binding }
// ShortHelp returns a slice of bindings to be displayed in the short // version of the help. The help bubble will render help in the order in // which the help items are returned here. ShortHelp() []key.Binding
// FullHelp returns an extended group of help items, grouped by columns. // The help bubble will render the help in the order in which the help // items are returned here. FullHelp() [][]key.Binding }
// keyMap defines a set of keybindings. To work for help it must satisfy // key.Map. It could also very easily be a map[string]key.Binding. type keyMap struct { Up key.Binding Down key.Binding Left key.Binding Right key.Binding Help key.Binding Quit key.Binding }
// ShortHelp returns keybindings to be shown in the mini help view. It's part // of the key.Map interface. func(k keyMap) ShortHelp() []key.Binding { return []key.Binding{k.Help, k.Quit} }
// FullHelp returns keybindings for the expanded help view. It's part of the // key.Map interface. func(k keyMap) FullHelp() [][]key.Binding { return [][]key.Binding{ {k.Up, k.Down, k.Left, k.Right}, // first column {k.Help, k.Quit}, // second column } }
type Model
1 2 3 4 5 6 7 8 9 10 11 12 13
type Model struct { Width int ShowAll bool// if true, render the "full" help menu
ShortSeparator string FullSeparator string
// The symbol we use in the short help when help items have been truncated // due to width. Periods of ellipsis by default. Ellipsis string
type KeyMap struct { Up key.Binding Down key.Binding }
var DefaultKeyMap = KeyMap{ Up: key.NewBinding( key.WithKeys("k", "up"), // actual keybindings key.WithHelp("↑/k", "move up"), // corresponding help text ), Down: key.NewBinding( key.WithKeys("j", "down"), key.WithHelp("↓/j", "move down"), ), }
func(m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: switch { case key.Matches(msg, DefaultKeyMap.Up): // The user pressed up case key.Matches(msg, DefaultKeyMap.Down): // The user pressed down } }