mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 19:11:25 +01:00
label edit without flags on a terminal now opens a form with the label's name, color, description and flags, like milestone edit and issue edit, instead of failing; without a terminal it reports the shared "nothing to edit" error. The help of issue and milestone close and reopen names its commands without generating the text from the verb. Signed-off-by: bircni <bircni@icloud.com>
50 lines
1.7 KiB
Go
50 lines
1.7 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package terminal
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"charm.land/huh/v2"
|
|
)
|
|
|
|
// LabelFormValues holds interactively edited label fields.
|
|
type LabelFormValues struct {
|
|
Name, Color, Description string
|
|
Exclusive, Archived bool
|
|
}
|
|
|
|
// LabelForm collects interactive label input.
|
|
type LabelForm interface {
|
|
// EditLabel asks for the fields of a label, starting from current, under heading, such as
|
|
// `Editing label "bug" in alice/x`.
|
|
EditLabel(ctx context.Context, heading string, current LabelFormValues) (LabelFormValues, error)
|
|
}
|
|
|
|
var hexColor = regexp.MustCompile(`^#?[0-9a-fA-F]{6}$`)
|
|
|
|
// EditLabel runs the interactive label form on stderr.
|
|
func (p *FormPrompter) EditLabel(ctx context.Context, heading string, current LabelFormValues) (LabelFormValues, error) {
|
|
values := current
|
|
if err := p.runForm(ctx, huh.NewForm(huh.NewGroup(
|
|
huh.NewInput().Title("Label name (required):").Value(&values.Name).Validate(huh.ValidateNotEmpty()),
|
|
huh.NewInput().Title("Color (hex, e.g. d73a4a):").Value(&values.Color).Validate(func(s string) error {
|
|
if !hexColor.MatchString(strings.TrimSpace(s)) {
|
|
return errors.New("use six hexadecimal digits")
|
|
}
|
|
return nil
|
|
}),
|
|
huh.NewInput().Title("Description (optional):").Value(&values.Description),
|
|
huh.NewConfirm().Title("Exclusive (one label of its scope per issue):").Value(&values.Exclusive),
|
|
huh.NewConfirm().Title("Archived:").Value(&values.Archived),
|
|
).Title(heading))); err != nil {
|
|
return LabelFormValues{}, err
|
|
}
|
|
values.Name, values.Color = strings.TrimSpace(values.Name), strings.TrimPrefix(strings.TrimSpace(values.Color), "#")
|
|
return values, nil
|
|
}
|