File: listbox_state.go

package info (click to toggle)
elvish 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,372 kB
  • sloc: javascript: 236; sh: 130; python: 104; makefile: 88; xml: 9
file content (50 lines) | stat: -rw-r--r-- 1,241 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package tk

import (
	"fmt"

	"src.elv.sh/pkg/ui"
)

// ListBoxState keeps the mutable state ListBox.
type ListBoxState struct {
	Items    Items
	Selected int
	// The first element to show. Used when rendering and adjusted accordingly
	// when the terminal size changes or the user has scrolled.
	First int
	// Height of the listbox, excluding horizontal scrollbar when using the
	// horizontal layout. Stored in the state for commands to move the cursor by
	// page (for vertical layout) or column (for horizontal layout).
	ContentHeight int
}

// Items is an interface for accessing multiple items.
type Items interface {
	// Show renders the item at the given zero-based index.
	Show(i int) ui.Text
	// Len returns the number of items.
	Len() int
}

// TestItems is an implementation of Items useful for testing.
type TestItems struct {
	Prefix string
	Style  ui.Styling
	NItems int
}

// Show returns a plain text consisting of the prefix and i. If the prefix is
// empty, it defaults to "item ".
func (it TestItems) Show(i int) ui.Text {
	prefix := it.Prefix
	if prefix == "" {
		prefix = "item "
	}
	return ui.T(fmt.Sprintf("%s%d", prefix, i), it.Style)
}

// Len returns it.NItems.
func (it TestItems) Len() int {
	return it.NItems
}