refactor: add terminal output and forms package (#1124)

- `internal/terminal`: streams and the prompting policy, interactive forms (issue, PR, review, merge, milestone), editor / pager / browser launching, JSON and `--jq` output.
- An aligned table renderer and a record renderer for human output; both strip control and escape characters from server-supplied text so titles and bodies cannot inject terminal sequences.

Reviewed-on: https://gitea.com/gitea/tea/pulls/1124
Reviewed-by: TheFox0x7 <95654+thefox0x7@noreply.gitea.com>
This commit is contained in:
bircni committed 2026-10-11 17:12:23 +00:00
1 parent 88b91faa54
commit faea86cd94
27 files changed
+1941 -1

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package terminal
import (
"bytes"
"context"
"errors"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestEditIssueEditBodyPreservesDraftOnFailure(t *testing.T) {
p, _ := testPrompter(bytes.NewBufferString("New Title\r"))
path := filepath.Join(t.TempDir(), "draft.md")
values, err := p.EditIssue(context.Background(), IssueEditFormChoices{
Title: "", Body: "current",
EditBody: func(context.Context, string) (Draft, error) {
return Draft{Path: path, Body: "partial"}, errors.New("editor failed")
},
})
require.ErrorContains(t, err, "editor failed")
require.Equal(t, path, values.Draft.Path)
require.Equal(t, "New Title", values.Title)
}
func TestEditIssueKeepsUnchangedDeadline(t *testing.T) {
deadline := time.Date(2026, 10, 6, 23, 59, 59, 0, time.FixedZone("server", -5*3600))
p, _ := testPrompter(bytes.NewBufferString("\r\r\r"))
values, err := p.EditIssue(context.Background(), IssueEditFormChoices{Title: "Title", Deadline: &deadline, CanPush: true})
require.NoError(t, err)
require.Nil(t, values.Deadline)
require.False(t, values.ClearDeadline)
}
func TestWithCurrentKeepsSeededValuesSelectable(t *testing.T) {
options := []IssueLabelChoice{{Name: "bug", ID: 2}}
merged := withCurrent(options, IssueLabelChoice{Name: "bug", ID: 2}, IssueLabelChoice{Name: "org", ID: 7})
require.Equal(t, []IssueLabelChoice{{Name: "bug", ID: 2}, {Name: "org", ID: 7}}, merged)
require.Len(t, options, 1)
}