Use bubbletea instead of survey for interacting with TUI (#786)

Fix #772

Reviewed-on: https://gitea.com/gitea/tea/pulls/786
Reviewed-by: Bo-Yi Wu (吳柏毅) <appleboy.tw@gmail.com>
This commit is contained in:
Lunny Xiao
2025-08-11 18:23:52 +00:00
parent c0eb30af03
commit 4c00b8b571
27 changed files with 553 additions and 318 deletions

View File

@ -17,8 +17,9 @@ import (
"time"
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/modules/theme"
"code.gitea.io/tea/modules/utils"
"github.com/AlecAivazis/survey/v2"
"github.com/charmbracelet/huh"
"golang.org/x/oauth2"
)
@ -278,8 +279,13 @@ func (l *Login) Client(options ...gitea.ClientOption) *gitea.Client {
options = append(options, gitea.SetToken(l.Token), gitea.SetHTTPClient(httpClient))
if ok, err := utils.IsKeyEncrypted(l.SSHKey); ok && err == nil && l.SSHPassphrase == "" {
promptPW := &survey.Password{Message: "ssh-key is encrypted please enter the passphrase: "}
if err = survey.AskOne(promptPW, &l.SSHPassphrase, survey.WithValidator(survey.Required)); err != nil {
if err := huh.NewInput().
Title("ssh-key is encrypted please enter the passphrase: ").
Validate(huh.ValidateNotEmpty()).
EchoMode(huh.EchoModePassword).
Value(&l.SSHPassphrase).
WithTheme(theme.GetTheme()).
Run(); err != nil {
log.Fatal(err)
}
}

View File

@ -10,8 +10,9 @@ import (
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/modules/context"
"code.gitea.io/tea/modules/print"
"code.gitea.io/tea/modules/theme"
"github.com/AlecAivazis/survey/v2"
"github.com/charmbracelet/huh"
"golang.org/x/term"
)
@ -46,9 +47,12 @@ func ShowCommentsPaginated(ctx *context.TeaContext, idx int64, totalComments int
// NOTE: as of gitea 1.13, pagination is not provided by this endpoint, but handles
// this function gracefully anyways.
for {
loadComments := false
confirm := survey.Confirm{Message: prompt, Default: true}
if err := survey.AskOne(&confirm, &loadComments); err != nil {
loadComments := true
if err := huh.NewConfirm().
Title(prompt).
Value(&loadComments).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
} else if !loadComments {
break

View File

@ -4,19 +4,28 @@
package interact
import (
"strings"
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/modules/config"
"code.gitea.io/tea/modules/task"
"code.gitea.io/tea/modules/theme"
"github.com/AlecAivazis/survey/v2"
"github.com/charmbracelet/huh"
)
// IsQuitting checks if the user has aborted the interactive prompt
func IsQuitting(err error) bool {
return err == huh.ErrUserAborted
}
// CreateIssue interactively creates an issue
func CreateIssue(login *config.Login, owner, repo string) error {
owner, repo, err := promptRepoSlug(owner, repo)
if err != nil {
return err
}
printTitleAndContent("Target repo:", owner+"/"+repo)
var opts gitea.CreateIssueOption
if err := promptIssueProperties(login, owner, repo, &opts); err != nil {
@ -28,29 +37,36 @@ func CreateIssue(login *config.Login, owner, repo string) error {
func promptIssueProperties(login *config.Login, owner, repo string, o *gitea.CreateIssueOption) error {
var milestoneName string
var labels []string
var err error
selectableChan := make(chan (issueSelectables), 1)
go fetchIssueSelectables(login, owner, repo, selectableChan)
// title
promptOpts := survey.WithValidator(survey.Required)
promptI := &survey.Input{Message: "Issue title:", Default: o.Title}
if err = survey.AskOne(promptI, &o.Title, promptOpts); err != nil {
if err := huh.NewInput().
Title("Issue title:").
Value(&o.Title).
Validate(huh.ValidateNotEmpty()).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("Issue title:", o.Title)
// description
promptD := NewMultiline(Multiline{
Message: "Issue description:",
Default: o.Body,
Syntax: "md",
UseEditor: config.GetPreferences().Editor,
})
if err = survey.AskOne(promptD, &o.Body); err != nil {
if err := huh.NewForm(
huh.NewGroup(
huh.NewText().
Title("Issue description(markdown):").
ExternalEditor(config.GetPreferences().Editor).
EditorExtension("md").
Value(&o.Body),
),
).WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("Issue description(markdown):", o.Body)
// wait until selectables are fetched
selectables := <-selectableChan
@ -67,6 +83,7 @@ func promptIssueProperties(login *config.Login, owner, repo string, o *gitea.Cre
if o.Assignees, err = promptMultiSelect("Assignees:", selectables.Assignees, "[other]"); err != nil {
return err
}
printTitleAndContent("Assignees:", strings.Join(o.Assignees, "\n"))
// milestone
if len(selectables.MilestoneList) != 0 {
@ -74,24 +91,40 @@ func promptIssueProperties(login *config.Login, owner, repo string, o *gitea.Cre
return err
}
o.Milestone = selectables.MilestoneMap[milestoneName]
printTitleAndContent("Milestone:", milestoneName)
}
// labels
if len(selectables.LabelList) != 0 {
promptL := &survey.MultiSelect{Message: "Labels:", Options: selectables.LabelList, VimMode: true, Default: o.Labels}
if err := survey.AskOne(promptL, &labels); err != nil {
options := make([]huh.Option[int64], 0, len(selectables.LabelList))
labelsMap := make(map[int64]string, len(selectables.LabelList))
for _, l := range selectables.LabelList {
options = append(options, huh.Option[int64]{Key: l, Value: selectables.LabelMap[l]})
labelsMap[selectables.LabelMap[l]] = l
}
if err := huh.NewMultiSelect[int64]().
Title("Labels:").
Options(options...).
Value(&o.Labels).
Run(); err != nil {
return err
}
o.Labels = make([]int64, len(labels))
for i, l := range labels {
o.Labels[i] = selectables.LabelMap[l]
var labels []string
for _, labelID := range o.Labels {
labels = append(labels, labelsMap[labelID])
}
printTitleAndContent("Labels:", strings.Join(labels, "\n"))
}
// deadline
if o.Deadline, err = promptDatetime("Due date:"); err != nil {
return err
}
deadlineStr := "No due date"
if o.Deadline != nil && !o.Deadline.IsZero() {
deadlineStr = o.Deadline.Format("2006-01-02")
}
printTitleAndContent("Due date:", deadlineStr)
return nil
}

View File

@ -5,23 +5,26 @@ package interact
import (
"slices"
"strings"
"code.gitea.io/tea/modules/config"
"code.gitea.io/tea/modules/context"
"code.gitea.io/tea/modules/task"
"code.gitea.io/tea/modules/theme"
"github.com/AlecAivazis/survey/v2"
"github.com/charmbracelet/huh"
)
// EditIssue interactively edits an issue
func EditIssue(ctx context.TeaContext, index int64) (*task.EditIssueOption, error) {
var opts = task.EditIssueOption{}
opts := task.EditIssueOption{}
var err error
ctx.Owner, ctx.Repo, err = promptRepoSlug(ctx.Owner, ctx.Repo)
if err != nil {
return &opts, err
}
printTitleAndContent("Target repo:", ctx.Owner+"/"+ctx.Repo)
c := ctx.Login.Client()
i, _, err := c.GetIssue(ctx.Owner, ctx.Repo, index)
@ -68,25 +71,31 @@ func promptIssueEditProperties(ctx *context.TeaContext, o *task.EditIssueOption)
go fetchIssueSelectables(ctx.Login, ctx.Owner, ctx.Repo, selectableChan)
// title
promptOpts := survey.WithValidator(survey.Required)
promptI := &survey.Input{Message: "Issue title:", Default: *o.Title}
if err = survey.AskOne(promptI, o.Title, promptOpts); err != nil {
if err := huh.NewInput().
Title("Issue title:").
Value(o.Title).
Validate(huh.ValidateNotEmpty()).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("Issue title:", *o.Title)
// description
promptD := NewMultiline(Multiline{
Message: "Issue description:",
Default: *o.Body,
Syntax: "md",
UseEditor: config.GetPreferences().Editor,
EditorAppendDefault: true,
EditorHideDefault: true,
})
if err = survey.AskOne(promptD, o.Body); err != nil {
if err := huh.NewForm(
huh.NewGroup(
huh.NewText().
Title("Issue description(markdown):").
ExternalEditor(config.GetPreferences().Editor).
EditorExtension("md").
Value(o.Body),
),
).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("Issue description(markdown):", *o.Body)
// wait until selectables are fetched
selectables := <-selectableChan
@ -112,6 +121,7 @@ func promptIssueEditProperties(ctx *context.TeaContext, o *task.EditIssueOption)
if o.AddAssignees, err = promptMultiSelect("Add Assignees:", newAssignees, "[other]"); err != nil {
return err
}
printTitleAndContent("Assignees:", strings.Join(o.AddAssignees, "\n"))
// milestone
if len(selectables.MilestoneList) != 0 {
@ -123,14 +133,22 @@ func promptIssueEditProperties(ctx *context.TeaContext, o *task.EditIssueOption)
return err
}
o.Milestone = &milestoneName
printTitleAndContent("Milestone:", milestoneName)
}
// labels
if len(selectables.LabelList) != 0 {
promptL := &survey.MultiSelect{Message: "Labels:", Options: selectables.LabelList, VimMode: true, Default: o.AddLabels}
if err := survey.AskOne(promptL, &labelsSelected); err != nil {
copy(labelsSelected, o.AddLabels)
if err := huh.NewMultiSelect[string]().
Title("Labels:").
Options(huh.NewOptions(selectables.LabelList...)...).
Value(&labelsSelected).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("Labels:", strings.Join(labelsSelected, "\n"))
// removed labels
for _, l := range o.AddLabels {
if !slices.Contains(labelsSelected, l) {
@ -148,6 +166,11 @@ func promptIssueEditProperties(ctx *context.TeaContext, o *task.EditIssueOption)
if o.Deadline, err = promptDatetime("Due date:"); err != nil {
return err
}
deadlineStr := "No due date"
if o.Deadline != nil && !o.Deadline.IsZero() {
deadlineStr = o.Deadline.Format("2006-01-02")
}
printTitleAndContent("Due date:", deadlineStr)
return nil
}

View File

@ -4,113 +4,175 @@
package interact
import (
"errors"
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/modules/auth"
"code.gitea.io/tea/modules/task"
"code.gitea.io/tea/modules/theme"
"github.com/AlecAivazis/survey/v2"
"github.com/charmbracelet/huh"
)
// CreateLogin create an login interactive
func CreateLogin() error {
var (
name, token, user, passwd, otp, scopes, sshKey, giteaURL, sshCertPrincipal, sshKeyFingerprint string
insecure, sshAgent, versionCheck, helper bool
name, token, user, passwd, otp, scopes, sshKey, sshCertPrincipal, sshKeyFingerprint string
insecure, sshAgent, versionCheck, helper bool
)
versionCheck = true
helper = false
promptI := &survey.Input{Message: "URL of Gitea instance: "}
if err := survey.AskOne(promptI, &giteaURL, survey.WithValidator(survey.Required)); err != nil {
giteaURL := "https://gitea.com"
if err := huh.NewInput().
Title("URL of Gitea instance: ").
Value(&giteaURL).
Validate(func(s string) error {
s = strings.TrimSpace(s)
if len(s) == 0 {
return fmt.Errorf("URL is required")
}
_, err := url.Parse(s)
if err != nil {
return fmt.Errorf("Invalid URL: %v", err)
}
return nil
}).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("URL of Gitea instance: ", giteaURL)
giteaURL = strings.TrimSuffix(strings.TrimSpace(giteaURL), "/")
if len(giteaURL) == 0 {
fmt.Println("URL is required!")
return nil
}
name, err := task.GenerateLoginName(giteaURL, "")
if err != nil {
return err
}
promptI = &survey.Input{Message: "Name of new Login: ", Default: name}
if err := survey.AskOne(promptI, &name); err != nil {
if err := huh.NewInput().
Title("Name of new Login: ").
Value(&name).
Validate(huh.ValidateNotEmpty()).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("Name of new Login: ", name)
loginMethod, err := promptSelectV2("Login with: ", []string{"token", "ssh-key/certificate", "oauth"})
if err != nil {
return err
}
printTitleAndContent("Login with: ", loginMethod)
switch loginMethod {
case "oauth":
promptYN := &survey.Confirm{
Message: "Allow Insecure connections: ",
Default: false,
}
if err = survey.AskOne(promptYN, &insecure); err != nil {
if err := huh.NewConfirm().
Title("Allow Insecure connections:").
Value(&insecure).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("Allow Insecure connections:", strconv.FormatBool(insecure))
return auth.OAuthLoginWithOptions(name, giteaURL, insecure)
default: // token
var hasToken bool
promptYN := &survey.Confirm{
Message: "Do you have an access token?",
Default: false,
}
if err = survey.AskOne(promptYN, &hasToken); err != nil {
if err := huh.NewConfirm().
Title("Do you have an access token?").
Value(&hasToken).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("Do you have an access token?", strconv.FormatBool(hasToken))
if hasToken {
promptI = &survey.Input{Message: "Token: "}
if err := survey.AskOne(promptI, &token, survey.WithValidator(survey.Required)); err != nil {
if err := huh.NewInput().
Title("Token:").
Value(&token).
Validate(huh.ValidateNotEmpty()).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("Token:", token)
} else {
promptI = &survey.Input{Message: "Username: "}
if err = survey.AskOne(promptI, &user, survey.WithValidator(survey.Required)); err != nil {
if err := huh.NewInput().
Title("Username:").
Value(&user).
Validate(huh.ValidateNotEmpty()).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("Username:", user)
promptPW := &survey.Password{Message: "Password: "}
if err = survey.AskOne(promptPW, &passwd, survey.WithValidator(survey.Required)); err != nil {
if err := huh.NewInput().
Title("Password:").
Value(&passwd).
Validate(huh.ValidateNotEmpty()).
EchoMode(huh.EchoModePassword).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("Password:", "********")
var tokenScopes []string
promptS := &survey.MultiSelect{Message: "Token Scopes:", Options: tokenScopeOpts}
if err := survey.AskOne(promptS, &tokenScopes, survey.WithValidator(survey.Required)); err != nil {
if err := huh.NewMultiSelect[string]().
Title("Token Scopes:").
Options(huh.NewOptions(tokenScopeOpts...)...).
Value(&tokenScopes).
Validate(func(s []string) error {
if len(s) == 0 {
return errors.New("At least one scope is required")
}
return nil
}).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("Token Scopes:", strings.Join(tokenScopes, "\n"))
scopes = strings.Join(tokenScopes, ",")
// Ask for OTP last so it's less likely to timeout
promptO := &survey.Input{Message: "OTP (if applicable)"}
if err := survey.AskOne(promptO, &otp); err != nil {
if err := huh.NewInput().
Title("OTP (if applicable):").
Value(&otp).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("OTP (if applicable):", otp)
}
case "ssh-key/certificate":
promptI = &survey.Input{Message: "SSH Key/Certificate Path (leave empty for auto-discovery in ~/.ssh and ssh-agent):"}
if err := survey.AskOne(promptI, &sshKey); err != nil {
if err := huh.NewInput().
Title("SSH Key/Certificate Path (leave empty for auto-discovery in ~/.ssh and ssh-agent):").
Value(&sshKey).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("SSH Key/Certificate Path (leave empty for auto-discovery in ~/.ssh and ssh-agent):", sshKey)
if sshKey == "" {
sshKey, err = promptSelect("Select ssh-key: ", task.ListSSHPubkey(), "", "", "")
if err != nil {
return err
}
printTitleAndContent("Selected ssh-key:", sshKey)
// ssh certificate
if strings.Contains(sshKey, "principals") {
@ -136,42 +198,51 @@ func CreateLogin() error {
}
var optSettings bool
promptYN := &survey.Confirm{
Message: "Set Optional settings: ",
Default: false,
}
if err = survey.AskOne(promptYN, &optSettings); err != nil {
if err := huh.NewConfirm().
Title("Set Optional settings:").
Value(&optSettings).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("Set Optional settings:", strconv.FormatBool(optSettings))
if optSettings {
promptI = &survey.Input{Message: "SSH Key Path (leave empty for auto-discovery):"}
if err := survey.AskOne(promptI, &sshKey); err != nil {
if err := huh.NewInput().
Title("SSH Key Path (leave empty for auto-discovery):").
Value(&sshKey).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("SSH Key Path (leave empty for auto-discovery):", sshKey)
promptYN = &survey.Confirm{
Message: "Allow Insecure connections: ",
Default: false,
}
if err = survey.AskOne(promptYN, &insecure); err != nil {
if err := huh.NewConfirm().
Title("Allow Insecure connections:").
Value(&insecure).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("Allow Insecure connections:", strconv.FormatBool(insecure))
promptYN = &survey.Confirm{
Message: "Add git helper: ",
Default: false,
}
if err = survey.AskOne(promptYN, &helper); err != nil {
if err := huh.NewConfirm().
Title("Add git helper:").
Value(&helper).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("Add git helper:", strconv.FormatBool(helper))
promptYN = &survey.Confirm{
Message: "Check version of Gitea instance: ",
Default: true,
}
if err = survey.AskOne(promptYN, &versionCheck); err != nil {
if err := huh.NewConfirm().
Title("Check version of Gitea instance:").
Value(&versionCheck).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("Check version of Gitea instance:", strconv.FormatBool(versionCheck))
}
return task.CreateLogin(name, token, user, passwd, otp, scopes, sshKey, giteaURL, sshCertPrincipal, sshKeyFingerprint, insecure, sshAgent, versionCheck, helper)

View File

@ -4,46 +4,59 @@
package interact
import (
"fmt"
"time"
"code.gitea.io/tea/modules/config"
"code.gitea.io/tea/modules/task"
"code.gitea.io/tea/modules/theme"
"code.gitea.io/sdk/gitea"
"github.com/AlecAivazis/survey/v2"
"github.com/charmbracelet/huh"
)
// CreateMilestone interactively creates a milestone
func CreateMilestone(login *config.Login, owner, repo string) error {
var title, description string
var deadline *time.Time
var title, description, deadline string
// owner, repo
owner, repo, err := promptRepoSlug(owner, repo)
if err != nil {
return err
}
printTitleAndContent("Target repo:", fmt.Sprintf("%s/%s", owner, repo))
// title
promptOpts := survey.WithValidator(survey.Required)
promptI := &survey.Input{Message: "Milestone title:"}
if err := survey.AskOne(promptI, &title, promptOpts); err != nil {
if err := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("Milestone title:").
Validate(huh.ValidateNotEmpty()).
Value(&title),
huh.NewText().
Title("Milestone description(markdown):").
ExternalEditor(config.GetPreferences().Editor).
EditorExtension("md").
Value(&description),
huh.NewInput().
Title("Milestone deadline:").
Placeholder("YYYY-MM-DD").
Validate(func(s string) error {
if s == "" {
return nil // no deadline
}
_, err := time.Parse("2006-01-02", s)
return err
}).
Value(&deadline),
),
).WithTheme(theme.GetTheme()).Run(); err != nil {
return err
}
// description
promptM := NewMultiline(Multiline{
Message: "Milestone description:",
Syntax: "md",
UseEditor: config.GetPreferences().Editor,
})
if err := survey.AskOne(promptM, &description); err != nil {
return err
}
// deadline
if deadline, err = promptDatetime("Milestone deadline:"); err != nil {
return err
var deadlineTM *time.Time
if deadline != "" {
tm, _ := time.Parse("2006-01-02", deadline)
deadlineTM = &tm
}
return task.CreateMilestone(
@ -52,6 +65,6 @@ func CreateMilestone(login *config.Login, owner, repo string) error {
repo,
title,
description,
deadline,
deadlineTM,
gitea.StateOpen)
}

20
modules/interact/print.go Normal file
View File

@ -0,0 +1,20 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package interact
import (
"fmt"
"code.gitea.io/tea/modules/theme"
"github.com/charmbracelet/lipgloss"
)
// printTitleAndContent prints a title and content with the gitea theme
func printTitleAndContent(title, content string) {
style := lipgloss.NewStyle().
Foreground(theme.GetTheme().Blurred.Title.GetForeground()).Bold(true).
Padding(0, 1)
fmt.Print(style.Render(title), content+"\n")
}

View File

@ -8,42 +8,19 @@ import (
"strings"
"time"
"code.gitea.io/tea/modules/theme"
"code.gitea.io/tea/modules/utils"
"github.com/AlecAivazis/survey/v2"
"github.com/araddon/dateparse"
"github.com/charmbracelet/huh"
)
// Multiline represents options for a prompt that expects multiline input
type Multiline struct {
Message string
Default string
Syntax string
UseEditor bool
EditorAppendDefault bool
EditorHideDefault bool
}
// NewMultiline creates a prompt that switches between the inline multiline text
// and a texteditor based prompt
func NewMultiline(opts Multiline) (prompt survey.Prompt) {
if opts.UseEditor {
prompt = &survey.Editor{
Message: opts.Message,
Default: opts.Default,
FileName: "*." + opts.Syntax,
AppendDefault: opts.EditorAppendDefault,
HideDefault: opts.EditorHideDefault,
}
} else {
prompt = &survey.Multiline{Message: opts.Message, Default: opts.Default}
}
return
}
// PromptPassword asks for a password and blocks until input was made.
func PromptPassword(name string) (pass string, err error) {
promptPW := &survey.Password{Message: name + " password:"}
err = survey.AskOne(promptPW, &pass, survey.WithValidator(survey.Required))
err = huh.NewInput().
Title(name + " password:").
Validate(huh.ValidateNotEmpty()).EchoMode(huh.EchoModePassword).
Value(&pass).
WithTheme(theme.GetTheme()).
Run()
return
}
@ -60,28 +37,21 @@ func promptRepoSlug(defaultOwner, defaultRepo string) (owner, repo string, err e
owner = defaultOwner
repo = defaultRepo
repoSlug = defaultVal
err = survey.AskOne(
&survey.Input{
Message: prompt,
Default: defaultVal,
},
&repoSlug,
survey.WithValidator(func(input interface{}) error {
if str, ok := input.(string); ok {
if !required && len(str) == 0 {
return nil
}
split := strings.Split(str, "/")
if len(split) != 2 || len(split[0]) == 0 || len(split[1]) == 0 {
return fmt.Errorf("must follow the <owner>/<repo> syntax")
}
} else {
return fmt.Errorf("invalid result type")
err = huh.NewInput().
Title(prompt).
Value(&repoSlug).
Validate(func(str string) error {
if !required && len(str) == 0 {
return nil
}
split := strings.Split(str, "/")
if len(split) != 2 || len(split[0]) == 0 || len(split[1]) == 0 {
return fmt.Errorf("must follow the <owner>/<repo> syntax")
}
return nil
}),
)
}).WithTheme(theme.GetTheme()).Run()
if err == nil && len(repoSlug) != 0 {
repoSlugSplit := strings.Split(repoSlug, "/")
@ -94,38 +64,39 @@ func promptRepoSlug(defaultOwner, defaultRepo string) (owner, repo string, err e
// promptDatetime prompts for a date or datetime string.
// Supports all formats understood by araddon/dateparse.
func promptDatetime(prompt string) (val *time.Time, err error) {
var input string
err = survey.AskOne(
&survey.Input{Message: prompt},
&input,
survey.WithValidator(func(input interface{}) error {
if str, ok := input.(string); ok {
if len(str) == 0 {
return nil
}
t, err := dateparse.ParseAny(str)
if err != nil {
return err
}
val = &t
} else {
return fmt.Errorf("invalid result type")
var date string
if err := huh.NewInput().
Title(prompt).
Placeholder("YYYY-MM-DD").
Validate(func(s string) error {
if s == "" {
return nil
}
return nil
}),
)
return
_, err := time.Parse("2006-01-02", s)
return err
}).
Value(&date).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return nil, err
}
if date == "" {
return nil, nil // no date
}
t, _ := time.Parse("2006-01-02", date)
return &t, nil
}
// promptSelect creates a generic multiselect prompt, with processing of custom values.
func promptMultiSelect(prompt string, options []string, customVal string) ([]string, error) {
var selection []string
promptA := &survey.MultiSelect{
Message: prompt,
Options: makeSelectOpts(options, customVal, ""),
VimMode: true,
}
if err := survey.AskOne(promptA, &selection); err != nil {
if err := huh.NewMultiSelect[string]().
Title(prompt).
Options(huh.NewOptions(makeSelectOpts(options, customVal, "")...)...).
Value(&selection).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return nil, err
}
return promptCustomVal(prompt, customVal, selection)
@ -136,14 +107,13 @@ func promptSelectV2(prompt string, options []string) (string, error) {
if len(options) == 0 {
return "", nil
}
var selection string
promptA := &survey.Select{
Message: prompt,
Options: options,
VimMode: true,
Default: options[0],
}
if err := survey.AskOne(promptA, &selection); err != nil {
selection := options[0]
if err := huh.NewSelect[string]().
Title(prompt).
Options(huh.NewOptions(options...)...).
Value(&selection).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return "", err
}
return selection, nil
@ -154,17 +124,18 @@ func promptSelect(prompt string, options []string, customVal, noneVal, defaultVa
var selection string
if defaultVal == "" && noneVal != "" {
defaultVal = noneVal
}
}
promptA := &survey.Select{
Message: prompt,
Options: makeSelectOpts(options, customVal, noneVal),
VimMode: true,
Default: defaultVal,
}
if err := survey.AskOne(promptA, &selection); err != nil {
selection = defaultVal
if err := huh.NewSelect[string]().
Title(prompt).
Options(huh.NewOptions(makeSelectOpts(options, customVal, noneVal)...)...).
Value(&selection).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return "", err
}
if noneVal != "" && selection == noneVal {
return "", nil
}
@ -193,11 +164,14 @@ func makeSelectOpts(opts []string, customVal, noneVal string) []string {
// for custom input to add to the selection instead.
func promptCustomVal(prompt, customVal string, selection []string) ([]string, error) {
// check for custom value & prompt again with text input
// HACK until https://github.com/AlecAivazis/survey/issues/339 is implemented
if otherIndex := utils.IndexOf(selection, customVal); otherIndex != -1 {
var customAssignees string
promptA := &survey.Input{Message: prompt, Help: "comma separated list"}
if err := survey.AskOne(promptA, &customAssignees); err != nil {
if err := huh.NewInput().
Title(prompt).
Description("comma separated list").
Value(&customAssignees).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return nil, err
}
selection = append(selection[:otherIndex], selection[otherIndex+1:]...)

View File

@ -8,14 +8,14 @@ import (
"code.gitea.io/tea/modules/context"
"code.gitea.io/tea/modules/task"
"github.com/AlecAivazis/survey/v2"
"github.com/charmbracelet/huh"
)
// CreatePull interactively creates a PR
func CreatePull(ctx *context.TeaContext) (err error) {
var (
base, head string
allowMaintainerEdits bool
allowMaintainerEdits = true
)
// owner, repo
@ -27,32 +27,37 @@ func CreatePull(ctx *context.TeaContext) (err error) {
if base, err = task.GetDefaultPRBase(ctx.Login, ctx.Owner, ctx.Repo); err != nil {
return err
}
promptI := &survey.Input{Message: "Target branch:", Default: base}
if err := survey.AskOne(promptI, &base); err != nil {
return err
}
// head
var headOwner, headBranch string
promptOpts := survey.WithValidator(survey.Required)
validator := huh.ValidateNotEmpty()
if ctx.LocalRepo != nil {
headOwner, headBranch, err = task.GetDefaultPRHead(ctx.LocalRepo)
if err == nil {
promptOpts = nil
validator = nil
}
}
promptI = &survey.Input{Message: "Source repo owner:", Default: headOwner}
if err := survey.AskOne(promptI, &headOwner); err != nil {
return err
}
promptI = &survey.Input{Message: "Source branch:", Default: headBranch}
if err := survey.AskOne(promptI, &headBranch, promptOpts); err != nil {
return err
}
promptC := &survey.Confirm{Message: "Allow Maintainers to push to the base branch", Default: true}
if err := survey.AskOne(promptC, &allowMaintainerEdits); err != nil {
if err := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("Target branch:").
Value(&base).
Validate(huh.ValidateNotEmpty()),
huh.NewInput().
Title("Source repo owner:").
Value(&headOwner),
huh.NewInput().
Title("Source branch:").
Value(&headBranch).
Validate(validator),
huh.NewConfirm().
Title("Allow maintainers to push to the base branch:").
Value(&allowMaintainerEdits),
),
).Run(); err != nil {
return err
}

View File

@ -7,12 +7,12 @@ import (
"fmt"
"strings"
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/modules/context"
"code.gitea.io/tea/modules/task"
"code.gitea.io/tea/modules/utils"
"github.com/AlecAivazis/survey/v2"
"code.gitea.io/sdk/gitea"
"github.com/charmbracelet/huh"
)
// MergePull interactively creates a PR
@ -76,15 +76,15 @@ func getPullIndex(ctx *context.TeaContext, branch string) (int64, error) {
prOptions = append(prOptions, loadMoreOption)
q := &survey.Select{
Message: "Select a PR to merge",
Options: prOptions,
PageSize: 10,
}
err = survey.AskOne(q, &selected)
if err != nil {
if err := huh.NewSelect[string]().
Title("Select a PR to merge:").
Options(huh.NewOptions(prOptions...)...).
Value(&selected).
Filtering(true).
Run(); err != nil {
return 0, err
}
if selected != loadMoreOption {
break
}

View File

@ -6,13 +6,15 @@ package interact
import (
"fmt"
"os"
"strconv"
"code.gitea.io/tea/modules/config"
"code.gitea.io/tea/modules/context"
"code.gitea.io/tea/modules/task"
"code.gitea.io/tea/modules/theme"
"code.gitea.io/sdk/gitea"
"github.com/AlecAivazis/survey/v2"
"github.com/charmbracelet/huh"
)
var reviewStates = map[string]gitea.ReviewStateType{
@ -30,11 +32,16 @@ func ReviewPull(ctx *context.TeaContext, idx int64) error {
var err error
// codeComments
var reviewDiff bool
promptDiff := &survey.Confirm{Message: "Review / comment the diff?", Default: true}
if err = survey.AskOne(promptDiff, &reviewDiff); err != nil {
reviewDiff := true
if err := huh.NewConfirm().
Title("Review / comment the diff?").
Value(&reviewDiff).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("Review / comment the diff?", strconv.FormatBool(reviewDiff))
if reviewDiff {
if codeComments, err = DoDiffReview(ctx, idx); err != nil {
fmt.Printf("Error during diff review: %s\n", err)
@ -44,25 +51,31 @@ func ReviewPull(ctx *context.TeaContext, idx int64) error {
// state
var stateString string
promptState := &survey.Select{Message: "Your assessment:", Options: reviewStateOptions, VimMode: true}
if err = survey.AskOne(promptState, &stateString); err != nil {
if err := huh.NewSelect[string]().
Title("Your assessment:").
Options(huh.NewOptions(reviewStateOptions...)...).
Value(&stateString).
WithTheme(theme.GetTheme()).
Run(); err != nil {
return err
}
printTitleAndContent("Your assessment:", stateString)
state = reviewStates[stateString]
// comment
var promptOpts survey.AskOpt
field := huh.NewText().
Title("Concluding comment(markdown):").
ExternalEditor(config.GetPreferences().Editor).
EditorExtension("md").
Value(&comment)
if (state == gitea.ReviewStateComment && len(codeComments) == 0) || state == gitea.ReviewStateRequestChanges {
promptOpts = survey.WithValidator(survey.Required)
field = field.Validate(huh.ValidateNotEmpty())
}
err = survey.AskOne(NewMultiline(Multiline{
Message: "Concluding comment:",
Syntax: "md",
UseEditor: config.GetPreferences().Editor,
}), &comment, promptOpts)
if err != nil {
if err := huh.NewForm(huh.NewGroup(field)).WithTheme(theme.GetTheme()).Run(); err != nil {
return err
}
printTitleAndContent("Concluding comment(markdown):", comment)
return task.CreatePullReview(ctx, idx, state, comment, codeComments)
}

View File

@ -15,7 +15,7 @@ func MilestoneDetails(milestone *gitea.Milestone) {
milestone.Title,
)
if len(milestone.Description) != 0 {
fmt.Printf("\n%s\n", milestone.Description)
outputMarkdown(milestone.Description, "")
}
if milestone.Deadline != nil && !milestone.Deadline.IsZero() {
fmt.Printf("\nDeadline: %s\n", FormatTime(*milestone.Deadline, false))
@ -24,7 +24,7 @@ func MilestoneDetails(milestone *gitea.Milestone) {
// MilestonesList prints a listing of milestones
func MilestonesList(news []*gitea.Milestone, output string, fields []string) {
var printables = make([]printable, len(news))
printables := make([]printable, len(news))
for i, x := range news {
printables[i] = &printableMilestone{x}
}

View File

@ -13,7 +13,6 @@ import (
// CreateIssue creates an issue in the given repo and prints the result
func CreateIssue(login *config.Login, repoOwner, repoName string, opts gitea.CreateIssueOption) error {
// title is required
if len(opts.Title) == 0 {
return fmt.Errorf("Title is required")

23
modules/theme/theme.go Normal file
View File

@ -0,0 +1,23 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package theme
import (
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
)
var giteaTheme = func() *huh.Theme {
theme := huh.ThemeCharm()
title := lipgloss.AdaptiveColor{Light: "#02BA84", Dark: "#02BF87"}
theme.Focused.Title = theme.Focused.Title.Foreground(title).Bold(true)
theme.Blurred = theme.Focused
return theme
}()
// GetTheme returns the Gitea theme for Huh
func GetTheme() *huh.Theme {
return giteaTheme
}