gum/main.go
Carlos Alexandro Becker c868aa1c6c
fix(confirm,choose,file,input): timeout handling (#718)
* fix(confirm,choose,file,input): timeout handling

- some fields were not actually using the `--timeout` value
- some fields had different behavior when a timeout did occur. On this
  matter, it seems to me the best way forward is to specifically say it
  timed out, and after how long
- added exit status 124 (copied from `timeout` from coreutils) (fixes #684)

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* Update main.go

Co-authored-by: Ayman Bagabas <ayman.bagabas@gmail.com>

* Update internal/exit/exit.go

Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>

* fix: improve

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* fix: stderr

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Co-authored-by: Ayman Bagabas <ayman.bagabas@gmail.com>
Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
2024-11-18 10:49:15 -03:00

86 lines
2.1 KiB
Go

package main
import (
"errors"
"fmt"
"os"
"runtime/debug"
"github.com/alecthomas/kong"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"github.com/charmbracelet/gum/internal/exit"
)
const shaLen = 7
var (
// Version contains the application version number. It's set via ldflags
// when building.
Version = ""
// CommitSHA contains the SHA of the commit that this application was built
// against. It's set via ldflags when building.
CommitSHA = ""
)
var bubbleGumPink = lipgloss.NewStyle().Foreground(lipgloss.Color("212"))
func main() {
lipgloss.SetColorProfile(termenv.NewOutput(os.Stderr).Profile)
if Version == "" {
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {
Version = info.Main.Version
} else {
Version = "unknown (built from source)"
}
}
version := fmt.Sprintf("gum version %s", Version)
if len(CommitSHA) >= shaLen {
version += " (" + CommitSHA[:shaLen] + ")"
}
gum := &Gum{}
ctx := kong.Parse(
gum,
kong.Description(fmt.Sprintf("A tool for %s shell scripts.", bubbleGumPink.Render("glamorous"))),
kong.UsageOnError(),
kong.ConfigureHelp(kong.HelpOptions{
Compact: true,
Summary: false,
NoExpandSubcommands: true,
}),
kong.Vars{
"version": version,
"defaultHeight": "0",
"defaultWidth": "0",
"defaultAlign": "left",
"defaultBorder": "none",
"defaultBorderForeground": "",
"defaultBorderBackground": "",
"defaultBackground": "",
"defaultForeground": "",
"defaultMargin": "0 0",
"defaultPadding": "0 0",
"defaultUnderline": "false",
"defaultBold": "false",
"defaultFaint": "false",
"defaultItalic": "false",
"defaultStrikethrough": "false",
},
)
if err := ctx.Run(); err != nil {
if errors.Is(err, huh.ErrTimeout) {
fmt.Fprintln(os.Stderr, err)
os.Exit(exit.StatusTimeout)
}
if errors.Is(err, huh.ErrUserAborted) {
os.Exit(exit.StatusAborted)
}
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}