feat(terminal): add confirmation defaults and fix form input loss

Every confirmation defaulted to No, so Enter after a filled-in form
canceled it; prompts were titled with raw flag names and aborted on an
empty answer; and the shared issue and pull request form lost input:

- Multi-selects hid their last option, so a single assignee looked like
  an empty list and labels showed four of five. They now get an explicit
  height and scroll past eight options.
- A pty without a window size, such as `script` run without a terminal,
  made bubbles panic. Forms now refuse it with "terminal size unknown;
  pass the values as flags or use --no-input".
- Without a configured editor, the form ran to the end and then failed
  with "configure an editor", discarding everything typed. The editor is
  now resolved before the form; without one the body is typed into an
  inline multi-line field.
- The pull request form said "Issue title" and "Issue description". The
  shared fields read "Title (required):" and "Body (markdown):", and the
  review body label gained its missing space.

New prompt API for the area commands: confirmAction with a
terminal.Confirmation (DefaultYes for constructive actions, Typed for
type-to-confirm, NonTTY to name the action in the --yes error) and
requireInput with a human label and inline validation. Declined or
aborted prompts return terminal.ErrCanceled (exit code 3). The existing
confirm and require helpers keep working on top of them, and prompters
without the new terminal.Asker methods keep the plain behavior.

Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
bircni committed 2026-10-11 22:43:11 +02:00
1 parent 4fddcc6781
commit 62962b4f70
14 files changed
+539 -32

No files matched your search

+10
View File
@@ -49,7 +49,17 @@ func (a *application) editDraft(cmd *cobra.Command, body string) (terminal.Draft
return editor.Edit(cmd.Context(), command, body)
}
// bodyEditor collects the body of an interactive form after the form ran. The editor is resolved
// first: without one, the body is typed into an inline multi-line field instead, so the input
// already given is never lost to a missing editor.
func (a *application) bodyEditor(cmd *cobra.Command) terminal.BodyEditor {
preferences, err := a.preferences()
if asker, ok := a.deps.Prompter.(terminal.Asker); ok && err == nil && preferences.Editor == "" {
return func(ctx context.Context, initial string) (terminal.Draft, error) {
body, err := asker.AskText(ctx, terminal.BodyTitle, initial)
return terminal.Draft{Body: body}, err
}
}
return func(_ context.Context, initial string) (terminal.Draft, error) { return a.editDraft(cmd, initial) }
}
+28
View File
@@ -13,6 +13,7 @@ import (
"gitea.dev/tea/internal/clierr"
"gitea.dev/tea/internal/services"
"gitea.dev/tea/internal/terminal"
"gitea.dev/tea/internal/transport"
"github.com/stretchr/testify/require"
)
@@ -308,3 +309,30 @@ func TestReviewLifecycleEditorRejectsEmptyRequiredText(t *testing.T) {
})
}
}
func TestBodyEditorTypesInlineWithoutAnEditor(t *testing.T) {
deps, _, _ := testDeps(t)
prompt := &askingPrompter{}
deps.Prompter = prompt
a, cmd := outputApplication(deps, "")
draft, err := a.bodyEditor(cmd)(context.Background(), "## Steps")
require.NoError(t, err)
require.Equal(t, terminal.Draft{Body: "typed body"}, draft)
require.Equal(t, []string{terminal.BodyTitle + " ## Steps"}, prompt.texts)
editor, path := newDraftEditor(t, "edited body")
deps.Editor = editor
deps.Getenv = envFunc(map[string]string{"EDITOR": "vi"})
a, cmd = outputApplication(deps, "")
draft, err = a.bodyEditor(cmd)(context.Background(), "## Steps")
require.NoError(t, err)
require.Equal(t, terminal.Draft{Path: path, Body: "edited body"}, draft)
require.Equal(t, "## Steps", editor.initial)
require.Len(t, prompt.texts, 1)
deps.Prompter = &scriptedPrompter{}
deps.Getenv = envFunc(nil)
a, cmd = outputApplication(deps, "")
_, err = a.bodyEditor(cmd)(context.Background(), "")
require.ErrorContains(t, err, "configure an editor")
}
+31
View File
@@ -0,0 +1,31 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package app
import (
"gitea.dev/tea/internal/terminal"
"github.com/spf13/cobra"
)
// confirmAction asks c under the prompt policy unless --yes was supplied. Constructive actions set
// DefaultYes, destructive ones keep the default no, and irreversible ones set Typed. A declined
// confirmation returns terminal.ErrCanceled.
func (a *application) confirmAction(cmd *cobra.Command, c terminal.Confirmation) error {
s, err := a.deps.Settings.Load()
if err != nil {
return err
}
return terminal.ConfirmAction(cmd.Context(), c, a.options.yes, a.deps.Streams, a.policy(s), a.deps.Prompter)
}
// requireInput fills a missing value from a prompt titled label, such as "Label name", re-prompting
// until the answer is non-empty and validate, when set, accepts it. Without prompts it returns a
// usage error naming --flag. It reports whether it prompted.
func (a *application) requireInput(cmd *cobra.Command, value *string, flag, label string, validate func(string) error) (bool, error) {
s, err := a.deps.Settings.Load()
if err != nil {
return false, err
}
return terminal.RequireInput(cmd.Context(), value, flag, label, validate, a.deps.Streams, a.policy(s), a.deps.Prompter)
}
+75
View File
@@ -0,0 +1,75 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package app
import (
"context"
"testing"
"gitea.dev/tea/internal/clierr"
"gitea.dev/tea/internal/terminal"
"github.com/stretchr/testify/require"
)
// askingPrompter is scriptedPrompter with the terminal.Asker extension that FormPrompter has.
type askingPrompter struct {
scriptedPrompter
texts []string
defaults []bool
}
func (p *askingPrompter) Ask(ctx context.Context, title string, _ func(string) error) (string, error) {
return p.Input(ctx, title)
}
func (p *askingPrompter) AskText(_ context.Context, title, initial string) (string, error) {
p.texts = append(p.texts, title+" "+initial)
return "typed body", nil
}
func (p *askingPrompter) ConfirmDefault(ctx context.Context, title string, defaultYes bool) (bool, error) {
p.defaults = append(p.defaults, defaultYes)
return p.Confirm(ctx, title)
}
func TestConfirmActionFollowsThePromptPolicy(t *testing.T) {
c := terminal.Confirmation{Prompt: `Delete label "bug" from alice/x?`, NonTTY: `delete label "bug" from alice/x`}
deps, _, _ := testDeps(t)
a, cmd := outputApplication(deps, "")
err := a.confirmAction(cmd, c)
require.Equal(t, clierr.Usage, clierr.ExitCode(err))
require.EqualError(t, err, `confirmation required to delete label "bug" from alice/x; rerun with --yes`)
a.options.yes = true
require.NoError(t, a.confirmAction(cmd, c))
prompt := &askingPrompter{}
deps.Prompter = prompt
deps.Streams.InTTY, deps.Streams.ErrTTY = true, true
a, cmd = outputApplication(deps, "")
err = a.confirmAction(cmd, c)
require.ErrorIs(t, err, terminal.ErrCanceled)
require.Equal(t, clierr.Canceled, clierr.ExitCode(err))
prompt.accept = true
require.NoError(t, a.confirmAction(cmd, terminal.Confirmation{Prompt: "Create pull request?", DefaultYes: true}))
require.Equal(t, []string{c.Prompt, "Create pull request?"}, prompt.confirmations)
require.Equal(t, []bool{false, true}, prompt.defaults)
}
func TestRequireInputPromptsWithTheLabel(t *testing.T) {
prompt := &scriptedPrompter{input: "bug"}
deps, _, _ := testDeps(t)
deps.Prompter = prompt
a, cmd := outputApplication(deps, "")
name := ""
_, err := a.requireInput(cmd, &name, "name", "Label name", nil)
require.EqualError(t, err, "missing required input: supply --name")
deps.Streams.InTTY, deps.Streams.ErrTTY = true, true
a, cmd = outputApplication(deps, "")
asked, err := a.requireInput(cmd, &name, "name", "Label name", nil)
require.NoError(t, err)
require.True(t, asked)
require.Equal(t, "bug", name)
require.Equal(t, []string{"Label name"}, prompt.asked)
}