~adnano/astronaut

c5414ccd3c55edb65f60b063d50abadbeff0451d — Adnan Maolood 21 days ago c6343c7
browser: Refactor input
2 files changed, 21 insertions(+), 41 deletions(-)

M browser.go
M tab.go
M browser.go => browser.go +14 -21
@@ 51,10 51,10 @@ type Browser struct {
	tab       int
	tabsize   int

	input   ui.Input
	mode    Mode
	mu      sync.Mutex
	inputch chan string
	mu   sync.Mutex
	mode Mode

	input ui.Input

	hint    string
	hints   []Hint


@@ 252,12 252,16 @@ func (b *Browser) Event(event tcell.Event) {
		}
		b.mode = ModeNormal
		if input == "" {
			close(b.inputch)
			b.inputch = nil
			return
		}
		b.inputch <- input
		b.inputch = nil
		req, err := gemini.NewRequest(tab.URL())
		if err != nil {
			panic(err)
		}
		req.URL.ForceQuery = true
		req.URL.RawQuery = gemini.QueryEscape(input)
		tab.Back()
		tab.DoBackground(req)

	case ModeFollow:
		text := tab.Text()


@@ 604,23 608,12 @@ func (b *Browser) CloseTab(i int) {
	}
}

// Input retrieves input from the user.
func (b *Browser) Input(prompt, input string, sensitive bool) (string, bool) {
	ch := make(chan string)

func (b *Browser) InputMode(prompt string, sensitive bool) {
	b.mu.Lock()
	defer b.mu.Unlock()
	b.mode = ModeInput
	b.input.Prompt(prompt + " ")
	b.input.SetInput(input)
	b.input.Sensitive(sensitive)
	b.inputch = ch
	b.mu.Unlock()

	input, ok := <-ch
	if ok {
		return input, true
	}
	return "", false
}

func (b *Browser) FollowMode() error {

M tab.go => tab.go +7 -20
@@ 402,14 402,8 @@ func (t *Tab) do(ctx context.Context, req *gemini.Request, via []*gemini.Request

	switch resp.Status.Class() {
	case gemini.StatusInput:
		sensitive := resp.Status == gemini.StatusSensitiveInput
		input, ok := t.browser.Input(resp.Meta, "", sensitive)
		if !ok {
			return nil
		}
		req.URL.ForceQuery = true
		req.URL.RawQuery = gemini.QueryEscape(input)
		return t.do(ctx, req, via)
		t.browser.InputMode(resp.Meta, resp.Status == gemini.StatusSensitiveInput)
		return nil

	case gemini.StatusRedirect:
		target, err := url.Parse(resp.Meta)


@@ 718,12 712,9 @@ func (t *Tab) handleDownload(req *gemini.Request, resp *gemini.Response, mediaty
}

func (t *Tab) saveResponse(resp *gemini.Response, filename string) error {
	name, ok := t.browser.Input("Save as:", filename, false)
	if !ok {
		return nil
	}

	f, err := os.Create(name)
	// TODO: Avoid overwriting files
	// TODO: Download directory?
	f, err := os.Create(filename)
	if err != nil {
		return err
	}


@@ 736,11 727,6 @@ func (t *Tab) saveResponse(resp *gemini.Response, filename string) error {
}

func (t *Tab) openResponse(resp *gemini.Response, filename string) error {
	name, ok := t.browser.Input("Open with:", "xdg-open", false)
	if !ok {
		return nil
	}

	f, err := os.CreateTemp("", "astronaut-*-"+filename)
	if err != nil {
		return err


@@ 751,7 737,8 @@ func (t *Tab) openResponse(resp *gemini.Response, filename string) error {
		return err
	}

	cmd := exec.Command(name, f.Name())
	// TODO: Configurable open command?
	cmd := exec.Command("xdg-open", f.Name())
	cmd.Stderr = os.Stderr
	if err := cmd.Start(); err != nil {
		return err