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

View file

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

View file

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

View file

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

View file

@ -35,7 +35,7 @@ func (m model) View() string {
if m.quitting {
return ""
}
var timeStr string = ""
var timeStr string
if m.hasTimeout {
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) {
switch msg := msg.(type) {
case timeout.TimeoutMsg:
case timeout.TickTimeoutMsg:
if msg.TimeoutValue <= 0 {
m.quitting = 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) {
switch msg := msg.(type) {
case timeout.TimeoutMsg:
case timeout.TickTimeoutMsg:
if msg.TimeoutValue <= 0 {
return m, tea.Quit
}

View file

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

View file

@ -6,28 +6,28 @@ import (
"time"
)
// CmdOptions Common Timeout Option
// CmdOptions Common Timeout Option.
type CmdOptions struct {
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) {
return o.Timeout > 0
}
// Tick interval
// Tick interval.
const tickInterval = time.Second
// TimeoutMsg will be dispatched for every tick.
// TickTimeoutMsg will be dispatched for every tick.
// Containing current timeout value
// and optional parameter to be used when handling the timeout msg
type TimeoutMsg struct {
// and optional parameter to be used when handling the timeout msg.
type TickTimeoutMsg struct {
TimeoutValue time.Duration
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 {
if timeout > 0 {
return Tick(timeout, data)
@ -35,14 +35,14 @@ func Init(timeout time.Duration, data interface{}) tea.Cmd {
return nil
}
// Start ticker
// Start ticker.
func Tick(timeoutValue time.Duration, data interface{}) tea.Cmd {
return tea.Tick(tickInterval, func(time.Time) tea.Msg {
// every tick checks if the timeout needs to be decremented
// and send as message
if timeoutValue >= 0 {
timeoutValue -= tickInterval
return TimeoutMsg{
return TickTimeoutMsg{
TimeoutValue: timeoutValue,
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 {
return fmt.Sprintf(" (%d)", Max(0, int(timeout.Seconds())))
}