forked from Mirrors/bubbletea
Make type documentation godoc-compliant
This commit is contained in:
parent
bb19d42cc0
commit
0957c58e05
22
boba.go
22
boba.go
|
@ -31,17 +31,17 @@ func Batch(cmds ...Cmd) Cmd {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init is the first function that will be called. It returns your initial
|
// Init is the first function that will be called. It returns your initial
|
||||||
// model and runs an optional command
|
// model and runs an optional command.
|
||||||
type Init func() (Model, Cmd)
|
type Init func() (Model, Cmd)
|
||||||
|
|
||||||
// Update is called when a message is received. It may update the model and/or
|
// Update is called when a message is received. It may update the model and/or
|
||||||
// send a command.
|
// send a command.
|
||||||
type Update func(Msg, Model) (Model, Cmd)
|
type Update func(Msg, Model) (Model, Cmd)
|
||||||
|
|
||||||
// View produces a string which will be rendered to the terminal
|
// View produces a string which will be rendered to the terminal.
|
||||||
type View func(Model) string
|
type View func(Model) string
|
||||||
|
|
||||||
// Program is a terminal user interface
|
// Program is a terminal user interface.
|
||||||
type Program struct {
|
type Program struct {
|
||||||
init Init
|
init Init
|
||||||
update Update
|
update Update
|
||||||
|
@ -50,18 +50,18 @@ type Program struct {
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// Quit is a command that tells the program to exit
|
// Quit is a command that tells the program to exit.
|
||||||
func Quit() Msg {
|
func Quit() Msg {
|
||||||
return quitMsg{}
|
return quitMsg{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Signals that the program should quit
|
// Signals that the program should quit.
|
||||||
type quitMsg struct{}
|
type quitMsg struct{}
|
||||||
|
|
||||||
// batchMsg is used to perform a bunch of commands
|
// batchMsg is used to perform a bunch of commands.
|
||||||
type batchMsg []Cmd
|
type batchMsg []Cmd
|
||||||
|
|
||||||
// NewProgram creates a new Program
|
// NewProgram creates a new Program.
|
||||||
func NewProgram(init Init, update Update, view View) *Program {
|
func NewProgram(init Init, update Update, view View) *Program {
|
||||||
return &Program{
|
return &Program{
|
||||||
init: init,
|
init: init,
|
||||||
|
@ -72,7 +72,7 @@ func NewProgram(init Init, update Update, view View) *Program {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start initializes the program
|
// Start initializes the program.
|
||||||
func (p *Program) Start() error {
|
func (p *Program) Start() error {
|
||||||
var (
|
var (
|
||||||
model Model
|
model Model
|
||||||
|
@ -164,7 +164,7 @@ func (p *Program) render(model Model, linesRendered int) int {
|
||||||
p.mutex.Lock()
|
p.mutex.Lock()
|
||||||
|
|
||||||
// We need to add carriage returns to ensure that the cursor travels to the
|
// We need to add carriage returns to ensure that the cursor travels to the
|
||||||
// start of a column after a newline
|
// start of a column after a newline.
|
||||||
view = strings.Replace(view, "\n", "\r\n", -1)
|
view = strings.Replace(view, "\n", "\r\n", -1)
|
||||||
|
|
||||||
if linesRendered > 0 {
|
if linesRendered > 0 {
|
||||||
|
@ -177,13 +177,13 @@ func (p *Program) render(model Model, linesRendered int) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// AltScreen exits the altscreen. This is just a wrapper around the termenv
|
// AltScreen exits the altscreen. This is just a wrapper around the termenv
|
||||||
// function
|
// function.
|
||||||
func AltScreen() {
|
func AltScreen() {
|
||||||
termenv.AltScreen()
|
termenv.AltScreen()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExitAltScreen exits the altscreen. This is just a wrapper around the termenv
|
// ExitAltScreen exits the altscreen. This is just a wrapper around the termenv
|
||||||
// function
|
// function.
|
||||||
func ExitAltScreen() {
|
func ExitAltScreen() {
|
||||||
termenv.ExitAltScreen()
|
termenv.ExitAltScreen()
|
||||||
}
|
}
|
||||||
|
|
12
key.go
12
key.go
|
@ -7,10 +7,10 @@ import (
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
// KeyPressMsg contains information about a keypress
|
// KeyMsg contains information about a keypress.
|
||||||
type KeyMsg Key
|
type KeyMsg Key
|
||||||
|
|
||||||
// String returns a friendly name for a key
|
// String returns a friendly name for a key.
|
||||||
func (k *KeyMsg) String() (str string) {
|
func (k *KeyMsg) String() (str string) {
|
||||||
if k.Alt {
|
if k.Alt {
|
||||||
str += "alt+"
|
str += "alt+"
|
||||||
|
@ -25,19 +25,19 @@ func (k *KeyMsg) String() (str string) {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRune returns weather or not the key is a rune
|
// IsRune returns weather or not the key is a rune.
|
||||||
func (k *KeyMsg) IsRune() bool {
|
func (k *KeyMsg) IsRune() bool {
|
||||||
return k.Type == KeyRune
|
return k.Type == KeyRune
|
||||||
}
|
}
|
||||||
|
|
||||||
// Key contains information about a keypress
|
// Key contains information about a keypress.
|
||||||
type Key struct {
|
type Key struct {
|
||||||
Type KeyType
|
Type KeyType
|
||||||
Rune rune
|
Rune rune
|
||||||
Alt bool
|
Alt bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// KeyType indicates the key pressed
|
// KeyType indicates the key pressed.
|
||||||
type KeyType int
|
type KeyType int
|
||||||
|
|
||||||
// Control keys. I know we could do this with an iota, but the values are very
|
// Control keys. I know we could do this with an iota, but the values are very
|
||||||
|
@ -228,7 +228,7 @@ var hexes = map[string]Key{
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadKey reads keypress input from a TTY and returns a string representation
|
// ReadKey reads keypress input from a TTY and returns a string representation
|
||||||
// of a key
|
// of a key.
|
||||||
func ReadKey(r io.Reader) (Key, error) {
|
func ReadKey(r io.Reader) (Key, error) {
|
||||||
var buf [256]byte
|
var buf [256]byte
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
"github.com/charmbracelet/boba"
|
"github.com/charmbracelet/boba"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Type specifies the way we render pagination
|
// Type specifies the way we render pagination.
|
||||||
type Type int
|
type Type int
|
||||||
|
|
||||||
// Pagination rendering options
|
// Pagination rendering options
|
||||||
|
@ -19,7 +19,7 @@ const (
|
||||||
Dots
|
Dots
|
||||||
)
|
)
|
||||||
|
|
||||||
// Model is the Boba model for this user interface
|
// Model is the Boba model for this user interface.
|
||||||
type Model struct {
|
type Model struct {
|
||||||
Type Type
|
Type Type
|
||||||
Page int
|
Page int
|
||||||
|
@ -87,7 +87,7 @@ func (m *Model) NextPage() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// LastPage returns whether or not we're on the last page
|
// LastPage returns whether or not we're on the last page.
|
||||||
func (m Model) OnLastPage() bool {
|
func (m Model) OnLastPage() bool {
|
||||||
if m.Page == m.TotalPages-1 {
|
if m.Page == m.TotalPages-1 {
|
||||||
return true
|
return true
|
||||||
|
@ -95,7 +95,7 @@ func (m Model) OnLastPage() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewModel creates a new model with defaults
|
// NewModel creates a new model with defaults.
|
||||||
func NewModel() Model {
|
func NewModel() Model {
|
||||||
return Model{
|
return Model{
|
||||||
Type: Arabic,
|
Type: Arabic,
|
||||||
|
@ -112,7 +112,7 @@ func NewModel() Model {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update is the Boba update function which binds keystrokes to pagination
|
// Update is the Boba update function which binds keystrokes to pagination.
|
||||||
func Update(msg boba.Msg, m Model) (Model, boba.Cmd) {
|
func Update(msg boba.Msg, m Model) (Model, boba.Cmd) {
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case boba.KeyMsg:
|
case boba.KeyMsg:
|
||||||
|
@ -153,7 +153,7 @@ func Update(msg boba.Msg, m Model) (Model, boba.Cmd) {
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// View renders the pagination to a string
|
// View renders the pagination to a string.
|
||||||
func View(model boba.Model) string {
|
func View(model boba.Model) string {
|
||||||
m, ok := model.(Model)
|
m, ok := model.(Model)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
|
@ -57,7 +57,7 @@ type Model struct {
|
||||||
frame int
|
frame int
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewModel returns a model with default values
|
// NewModel returns a model with default values.
|
||||||
func NewModel() Model {
|
func NewModel() Model {
|
||||||
return Model{
|
return Model{
|
||||||
Type: Line,
|
Type: Line,
|
||||||
|
@ -65,7 +65,7 @@ func NewModel() Model {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TickMsg indicates that the timer has ticked and we should render a frame
|
// TickMsg indicates that the timer has ticked and we should render a frame.
|
||||||
type TickMsg struct{}
|
type TickMsg struct{}
|
||||||
|
|
||||||
// Update is the Boba update function. This will advance the spinner one frame
|
// Update is the Boba update function. This will advance the spinner one frame
|
||||||
|
@ -82,7 +82,7 @@ func Update(msg boba.Msg, m Model) (Model, boba.Cmd) {
|
||||||
return m, Tick(m)
|
return m, Tick(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
// View renders the model's view
|
// View renders the model's view.
|
||||||
func View(model Model) string {
|
func View(model Model) string {
|
||||||
s := spinners[model.Type]
|
s := spinners[model.Type]
|
||||||
if model.frame >= len(s) {
|
if model.frame >= len(s) {
|
||||||
|
|
|
@ -18,7 +18,7 @@ var (
|
||||||
// this text input.
|
// this text input.
|
||||||
type ErrMsg error
|
type ErrMsg error
|
||||||
|
|
||||||
// Model is the Boba model for this text input element
|
// Model is the Boba model for this text input element.
|
||||||
type Model struct {
|
type Model struct {
|
||||||
Err error
|
Err error
|
||||||
Prompt string
|
Prompt string
|
||||||
|
@ -57,7 +57,7 @@ type Model struct {
|
||||||
offset int
|
offset int
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetValue sets the value of the text input
|
// SetValue sets the value of the text input.
|
||||||
func (m *Model) SetValue(s string) {
|
func (m *Model) SetValue(s string) {
|
||||||
if m.CharLimit > 0 && len(s) > m.CharLimit {
|
if m.CharLimit > 0 && len(s) > m.CharLimit {
|
||||||
m.value = s[:m.CharLimit]
|
m.value = s[:m.CharLimit]
|
||||||
|
@ -70,7 +70,7 @@ func (m *Model) SetValue(s string) {
|
||||||
m.handleOverflow()
|
m.handleOverflow()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value returns the value of the text input
|
// Value returns the value of the text input.
|
||||||
func (m Model) Value() string {
|
func (m Model) Value() string {
|
||||||
return m.value
|
return m.value
|
||||||
}
|
}
|
||||||
|
@ -82,30 +82,30 @@ func (m *Model) SetCursor(pos int) {
|
||||||
m.handleOverflow()
|
m.handleOverflow()
|
||||||
}
|
}
|
||||||
|
|
||||||
// CursorStart moves the cursor to the start of the field
|
// CursorStart moves the cursor to the start of the field.
|
||||||
func (m *Model) CursorStart() {
|
func (m *Model) CursorStart() {
|
||||||
m.pos = 0
|
m.pos = 0
|
||||||
m.handleOverflow()
|
m.handleOverflow()
|
||||||
}
|
}
|
||||||
|
|
||||||
// CursorEnd moves the cursor to the end of the field
|
// CursorEnd moves the cursor to the end of the field.
|
||||||
func (m *Model) CursorEnd() {
|
func (m *Model) CursorEnd() {
|
||||||
m.pos = len(m.value)
|
m.pos = len(m.value)
|
||||||
m.handleOverflow()
|
m.handleOverflow()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Focused returns the focus state on the model
|
// Focused returns the focus state on the model.
|
||||||
func (m Model) Focused() bool {
|
func (m Model) Focused() bool {
|
||||||
return m.focus
|
return m.focus
|
||||||
}
|
}
|
||||||
|
|
||||||
// Focus sets the focus state on the model
|
// Focus sets the focus state on the model.
|
||||||
func (m *Model) Focus() {
|
func (m *Model) Focus() {
|
||||||
m.focus = true
|
m.focus = true
|
||||||
m.blink = false
|
m.blink = false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Blur removes the focus state on the model
|
// Blur removes the focus state on the model.
|
||||||
func (m *Model) Blur() {
|
func (m *Model) Blur() {
|
||||||
m.focus = false
|
m.focus = false
|
||||||
m.blink = true
|
m.blink = true
|
||||||
|
@ -134,7 +134,7 @@ func (m *Model) handleOverflow() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// colorText colorizes a given string according to the TextColor value of the
|
// colorText colorizes a given string according to the TextColor value of the
|
||||||
// model
|
// model.
|
||||||
func (m *Model) colorText(s string) string {
|
func (m *Model) colorText(s string) string {
|
||||||
return termenv.
|
return termenv.
|
||||||
String(s).
|
String(s).
|
||||||
|
@ -144,7 +144,7 @@ func (m *Model) colorText(s string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// colorPlaceholder colorizes a given string according to the TextColor value
|
// colorPlaceholder colorizes a given string according to the TextColor value
|
||||||
// of the model
|
// of the model.
|
||||||
func (m *Model) colorPlaceholder(s string) string {
|
func (m *Model) colorPlaceholder(s string) string {
|
||||||
return termenv.
|
return termenv.
|
||||||
String(s).
|
String(s).
|
||||||
|
@ -153,10 +153,10 @@ func (m *Model) colorPlaceholder(s string) string {
|
||||||
String()
|
String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// BlinkMsg is sent when the cursor should alternate it's blinking state
|
// BlinkMsg is sent when the cursor should alternate it's blinking state.
|
||||||
type BlinkMsg struct{}
|
type BlinkMsg struct{}
|
||||||
|
|
||||||
// NewModel creates a new model with default settings
|
// NewModel creates a new model with default settings.
|
||||||
func NewModel() Model {
|
func NewModel() Model {
|
||||||
return Model{
|
return Model{
|
||||||
Prompt: "> ",
|
Prompt: "> ",
|
||||||
|
@ -174,7 +174,7 @@ func NewModel() Model {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update is the Boba update loop
|
// Update is the Boba update loop.
|
||||||
func Update(msg boba.Msg, m Model) (Model, boba.Cmd) {
|
func Update(msg boba.Msg, m Model) (Model, boba.Cmd) {
|
||||||
if !m.focus {
|
if !m.focus {
|
||||||
m.blink = true
|
m.blink = true
|
||||||
|
@ -239,7 +239,7 @@ func Update(msg boba.Msg, m Model) (Model, boba.Cmd) {
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// View renders the textinput in its current state
|
// View renders the textinput in its current state.
|
||||||
func View(model boba.Model) string {
|
func View(model boba.Model) string {
|
||||||
m, ok := model.(Model)
|
m, ok := model.(Model)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -309,7 +309,7 @@ func placeholderView(m Model) string {
|
||||||
return m.Prompt + v
|
return m.Prompt + v
|
||||||
}
|
}
|
||||||
|
|
||||||
// cursorView styles the cursor
|
// cursorView styles the cursor.
|
||||||
func cursorView(s string, m Model) string {
|
func cursorView(s string, m Model) string {
|
||||||
if m.blink {
|
if m.blink {
|
||||||
if m.TextColor != "" || m.BackgroundColor != "" {
|
if m.TextColor != "" || m.BackgroundColor != "" {
|
||||||
|
|
|
@ -17,7 +17,7 @@ type Model struct {
|
||||||
lines []string
|
lines []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scrollpercent returns the amount scrolled as a float between 0 and 1
|
// Scrollpercent returns the amount scrolled as a float between 0 and 1.
|
||||||
func (m Model) ScrollPercent() float64 {
|
func (m Model) ScrollPercent() float64 {
|
||||||
if m.Height >= len(m.lines) {
|
if m.Height >= len(m.lines) {
|
||||||
return 1.0
|
return 1.0
|
||||||
|
@ -28,7 +28,7 @@ func (m Model) ScrollPercent() float64 {
|
||||||
return y / (t - h)
|
return y / (t - h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetContent set the pager's text content
|
// SetContent set the pager's text content.
|
||||||
func (m *Model) SetContent(s string) {
|
func (m *Model) SetContent(s string) {
|
||||||
s = strings.Replace(s, "\r\n", "\n", -1) // normalize line endings
|
s = strings.Replace(s, "\r\n", "\n", -1) // normalize line endings
|
||||||
m.lines = strings.Split(s, "\n")
|
m.lines = strings.Split(s, "\n")
|
||||||
|
@ -43,22 +43,22 @@ func NewModel(width, height int) Model {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ViewDown moves the view down by the number of lines in the viewport.
|
// ViewDown moves the view down by the number of lines in the viewport.
|
||||||
// Basically, "page down."
|
// Basically, "page down".
|
||||||
func (m *Model) ViewDown() {
|
func (m *Model) ViewDown() {
|
||||||
m.Y = min(len(m.lines)-m.Height, m.Y+m.Height)
|
m.Y = min(len(m.lines)-m.Height, m.Y+m.Height)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ViewUp moves the view up by one height of the viewport. Basically, "page up."
|
// ViewUp moves the view up by one height of the viewport. Basically, "page up".
|
||||||
func (m *Model) ViewUp() {
|
func (m *Model) ViewUp() {
|
||||||
m.Y = max(0, m.Y-m.Height)
|
m.Y = max(0, m.Y-m.Height)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HalfViewUp moves the view up by half the height of the viewport
|
// HalfViewUp moves the view up by half the height of the viewport.
|
||||||
func (m *Model) HalfViewUp() {
|
func (m *Model) HalfViewUp() {
|
||||||
m.Y = max(0, m.Y-m.Height/2)
|
m.Y = max(0, m.Y-m.Height/2)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HalfViewDown moves the view down by half the height of the viewport
|
// HalfViewDown moves the view down by half the height of the viewport.
|
||||||
func (m *Model) HalfViewDown() {
|
func (m *Model) HalfViewDown() {
|
||||||
m.Y = min(len(m.lines)-m.Height, m.Y+m.Height/2)
|
m.Y = min(len(m.lines)-m.Height, m.Y+m.Height/2)
|
||||||
}
|
}
|
||||||
|
@ -131,7 +131,7 @@ func Update(msg boba.Msg, m Model) (Model, boba.Cmd) {
|
||||||
|
|
||||||
// VIEW
|
// VIEW
|
||||||
|
|
||||||
// View renders the viewport into a string
|
// View renders the viewport into a string.
|
||||||
func View(m Model) string {
|
func View(m Model) string {
|
||||||
if m.Err != nil {
|
if m.Err != nil {
|
||||||
return m.Err.Error()
|
return m.Err.Error()
|
||||||
|
|
Loading…
Reference in New Issue