~adnano/astronaut

0f6cb2233246f6e417b65242516b2c2a9c0697c6 — Adnan Maolood 1 year, 11 months ago 58d8db9
browser: Refactor follow mode to use input events
2 files changed, 46 insertions(+), 37 deletions(-)

M browser.go
M ui/input.go
M browser.go => browser.go +33 -25
@@ 65,7 65,6 @@ type Browser struct {
	// follow mode
	hint    string
	hints   []Hint
	hintMap map[string]Hint
	hintlen int

	message struct {


@@ 320,35 319,46 @@ func (b *Browser) Event(event tcell.Event) {
		}

		switch event := event.(type) {
		case *tcell.EventKey:
			switch event.Key() {
			case tcell.KeyRune:
				r := event.Rune()
				b.hint += string(r)
				if hint, ok := b.hintMap[b.hint]; ok {
					text.focus = hint.Index
					b.Message("=> " + text.LinkURL(hint.Index))
					b.mode = ModeNormal
					b.hint = ""
					b.hints = nil
					b.hintMap = nil
					b.hintlen = 0
				}
		case *ui.EventTextInput:
			if event.NoInput() {
				b.mode = ModeNormal
				b.hint = ""
				b.hints = nil
				b.hintlen = 0
				b.view.Invalidate()
				return
			}

			case tcell.KeyBackspace:
				if len(b.hint) != 0 {
					b.hint = b.hint[:len(b.hint)-1]
		case *ui.EventTextEdit:
			s := event.Text()
			i := -1
			match := false
			for j, hint := range b.hints {
				if strings.HasPrefix(hint.Label, s) {
					i = j
					if len(hint.Label) == len(s) {
						match = true
					}
					break
				}

			case tcell.KeyESC:
			}
			if i == -1 {
				// Restore previous input
				b.input.SetInput(b.hint)
				return
			}
			b.hint = s
			if match {
				hint := b.hints[i]
				text.focus = hint.Index
				b.Message("=> " + text.LinkURL(hint.Index))
				b.mode = ModeNormal
				b.hint = ""
				b.hints = nil
				b.hintMap = nil
				b.hintlen = 0
				b.view.Invalidate()
				b.input.Reset()
			}
			b.view.Invalidate()
		}

	default:


@@ 726,18 736,16 @@ func (b *Browser) FollowMode() error {
	}

	// Label hints
	hintMap := map[string]Hint{}
	labels, needed := makeHintLabels(letters, len(hints))
	for i, label := range labels {
		hints[i].Label = label
		hintMap[label] = hints[i]
	}

	b.mode = ModeFollow
	b.hints = hints
	b.hintMap = hintMap
	b.hintlen = needed
	b.view.Invalidate()
	b.input.Prompt("")
	return nil
}


M ui/input.go => ui/input.go +13 -12
@@ 88,7 88,7 @@ func (i *Input) Draw() {
// Prompt sets the text input prompt.
func (i *Input) Prompt(prompt string) {
	i.view.Invalidate()
	i.reset()
	i.Reset()
	i.active = true
	i.prompt = prompt
}


@@ 104,6 104,16 @@ func (i *Input) SetInput(input string) {
	i.index = len(i.input)
}

// Reset resets the text input.
func (i *Input) Reset() {
	i.active = false
	i.sensitive = false
	i.input = nil
	i.prompt = ""
	i.index = 0
	i.scroll = 0
}

// Event handles events.
func (i *Input) Event(event tcell.Event) tcell.Event {
	if !i.active {


@@ 144,7 154,7 @@ func (i *Input) Event(event tcell.Event) tcell.Event {
			i.index = len(i.input)
			i.view.Invalidate()
		case tcell.KeyESC:
			i.reset()
			i.Reset()
			i.view.HideCursor()
			i.view.Invalidate()
			return &EventTextInput{


@@ 160,7 170,7 @@ func (i *Input) Event(event tcell.Event) tcell.Event {
			}
		case tcell.KeyEnter:
			input := string(i.input)
			i.reset()
			i.Reset()
			i.view.HideCursor()
			i.view.Invalidate()
			return &EventTextInput{


@@ 200,15 210,6 @@ func (i *Input) Event(event tcell.Event) tcell.Event {
	return nil
}

func (i *Input) reset() {
	i.active = false
	i.sensitive = false
	i.input = nil
	i.prompt = ""
	i.index = 0
	i.scroll = 0
}

func (i *Input) backspace() {
	if i.scroll > 0 {
		i.scroll--