refactor: linter issues

This commit is contained in:
Dieter Eickstaedt 2023-01-08 09:04:20 +01:00
parent d00b8dcdd1
commit c01f6acfee
9 changed files with 22 additions and 20 deletions

View file

@ -56,7 +56,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
return m, nil return m, nil
case timeout.TimeoutMsg: case timeout.TickTimeoutMsg:
if msg.TimeoutValue <= 0 { if msg.TimeoutValue <= 0 {
m.quitting = true m.quitting = true
return m, tea.Quit return m, tea.Quit
@ -158,7 +158,7 @@ func (m model) View() string {
} }
var s strings.Builder var s strings.Builder
var timeoutStr string = "" var timeoutStr string
start, end := m.paginator.GetSliceBounds(len(m.items)) start, end := m.paginator.GetSliceBounds(len(m.items))
for i, item := range m.items[start:end] { for i, item := range m.items[start:end] {

View file

@ -2,6 +2,7 @@ package confirm
import ( import (
"fmt" "fmt"
"github.com/charmbracelet/gum/internal/exit"
"os" "os"
"github.com/alecthomas/kong" "github.com/alecthomas/kong"
@ -31,7 +32,7 @@ func (o Options) Run() error {
} }
if m.(model).aborted { if m.(model).aborted {
os.Exit(130) os.Exit(exit.StatusAborted)
} else if m.(model).confirmation { } else if m.(model).confirmation {
os.Exit(0) os.Exit(0)
} else { } else {

View file

@ -67,7 +67,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.confirmation = true m.confirmation = true
return m, tea.Quit return m, tea.Quit
} }
case timeout.TimeoutMsg: case timeout.TickTimeoutMsg:
if msg.TimeoutValue <= 0 { if msg.TimeoutValue <= 0 {
m.quitting = true m.quitting = true

View file

@ -104,7 +104,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case readDirMsg: case readDirMsg:
m.files = msg m.files = msg
case timeout.TimeoutMsg: case timeout.TickTimeoutMsg:
if msg.TimeoutValue <= 0 { if msg.TimeoutValue <= 0 {
m.quitting = true m.quitting = true
m.aborted = true m.aborted = true

View file

@ -136,7 +136,7 @@ func (m model) View() string {
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd var cmd tea.Cmd
switch msg := msg.(type) { switch msg := msg.(type) {
case timeout.TimeoutMsg: case timeout.TickTimeoutMsg:
if msg.TimeoutValue <= 0 { if msg.TimeoutValue <= 0 {
m.quitting = true m.quitting = true
m.aborted = true m.aborted = true

View file

@ -35,7 +35,7 @@ func (m model) View() string {
if m.quitting { if m.quitting {
return "" return ""
} }
var timeStr string = "" var timeStr string
if m.hasTimeout { if m.hasTimeout {
timeStr = timeout.TimeoutStr(m.timeout) timeStr = timeout.TimeoutStr(m.timeout)
} }
@ -49,7 +49,7 @@ func (m model) View() string {
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case timeout.TimeoutMsg: case timeout.TickTimeoutMsg:
if msg.TimeoutValue <= 0 { if msg.TimeoutValue <= 0 {
m.quitting = true m.quitting = true
m.aborted = true m.aborted = true

View file

@ -32,7 +32,7 @@ func (m model) Init() tea.Cmd {
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case timeout.TimeoutMsg: case timeout.TickTimeoutMsg:
if msg.TimeoutValue <= 0 { if msg.TimeoutValue <= 0 {
return m, tea.Quit return m, tea.Quit
} }

View file

@ -80,7 +80,7 @@ func (m model) Init() tea.Cmd {
) )
} }
func (m model) View() string { func (m model) View() string {
var str string = "" var str string
if m.hasTimeout { if m.hasTimeout {
str = timeout.TimeoutStr(m.timeout) str = timeout.TimeoutStr(m.timeout)
} }
@ -95,7 +95,7 @@ func (m model) View() string {
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd var cmd tea.Cmd
switch msg := msg.(type) { switch msg := msg.(type) {
case timeout.TimeoutMsg: case timeout.TickTimeoutMsg:
if msg.TimeoutValue <= 0 { if msg.TimeoutValue <= 0 {
m.status = 130 m.status = 130
return m, tea.Quit return m, tea.Quit

View file

@ -6,28 +6,28 @@ import (
"time" "time"
) )
// CmdOptions Common Timeout Option // CmdOptions Common Timeout Option.
type CmdOptions struct { type CmdOptions struct {
Timeout time.Duration `help:"Timeout until command exits" default:"0" env:"GUM_CONFIRM_TIMEOUT"` Timeout time.Duration `help:"Timeout until command exits" default:"0" env:"GUM_CONFIRM_TIMEOUT"`
} }
// HasTimeout checks for a given timeout parameter // HasTimeout checks for a given timeout parameter.
func (o CmdOptions) HasTimeout() (hasTimeout bool) { func (o CmdOptions) HasTimeout() (hasTimeout bool) {
return o.Timeout > 0 return o.Timeout > 0
} }
// Tick interval // Tick interval.
const tickInterval = time.Second const tickInterval = time.Second
// TimeoutMsg will be dispatched for every tick. // TickTimeoutMsg will be dispatched for every tick.
// Containing current timeout value // Containing current timeout value
// and optional parameter to be used when handling the timeout msg // and optional parameter to be used when handling the timeout msg.
type TimeoutMsg struct { type TickTimeoutMsg struct {
TimeoutValue time.Duration TimeoutValue time.Duration
Data interface{} Data interface{}
} }
// Init Start Timeout ticker using with timeout in seconds and optional data // Init Start Timeout ticker using with timeout in seconds and optional data.
func Init(timeout time.Duration, data interface{}) tea.Cmd { func Init(timeout time.Duration, data interface{}) tea.Cmd {
if timeout > 0 { if timeout > 0 {
return Tick(timeout, data) return Tick(timeout, data)
@ -35,14 +35,14 @@ func Init(timeout time.Duration, data interface{}) tea.Cmd {
return nil return nil
} }
// Start ticker // Start ticker.
func Tick(timeoutValue time.Duration, data interface{}) tea.Cmd { func Tick(timeoutValue time.Duration, data interface{}) tea.Cmd {
return tea.Tick(tickInterval, func(time.Time) tea.Msg { return tea.Tick(tickInterval, func(time.Time) tea.Msg {
// every tick checks if the timeout needs to be decremented // every tick checks if the timeout needs to be decremented
// and send as message // and send as message
if timeoutValue >= 0 { if timeoutValue >= 0 {
timeoutValue -= tickInterval timeoutValue -= tickInterval
return TimeoutMsg{ return TickTimeoutMsg{
TimeoutValue: timeoutValue, TimeoutValue: timeoutValue,
Data: data, Data: data,
} }
@ -51,6 +51,7 @@ func Tick(timeoutValue time.Duration, data interface{}) tea.Cmd {
}) })
} }
// TimeoutStr produce Timeout String to be rendered.
func TimeoutStr(timeout time.Duration) string { func TimeoutStr(timeout time.Duration) string {
return fmt.Sprintf(" (%d)", Max(0, int(timeout.Seconds()))) return fmt.Sprintf(" (%d)", Max(0, int(timeout.Seconds())))
} }