Skip to content

Commit

Permalink
feat: handle ctrl+c
Browse files Browse the repository at this point in the history
When ctrl+c is pressed, exit with error (as if it was an interrupt).

The SIGINT signal is never actually caught because the terminal is in
raw mode, so it just sends the keys to the Update method.

closes #45
closes #40

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Jan 7, 2023
1 parent 6e7650b commit 8674e43
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import (
)

type model struct {
name string
duration time.Duration
start time.Time
timer timer.Model
progress progress.Model
quitting bool
name string
duration time.Duration
start time.Time
timer timer.Model
progress progress.Model
quitting bool
interrupting bool
}

func (m model) Init() tea.Cmd {
Expand All @@ -36,11 +37,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd

step := 100.0 / (m.duration).Seconds()

cmds = append(cmds, m.progress.IncrPercent(step/100.0))
// pm, cmd := m.progress.Update(msg)
// cmds = append(cmds, cmd)
// m.progress = pm.(progress.Model)

m.timer, cmd = m.timer.Update(msg)
cmds = append(cmds, cmd)
Expand Down Expand Up @@ -72,13 +69,17 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.quitting = true
return m, tea.Quit
}
if key.Matches(msg, intKeys) {
m.interrupting = true
return m, tea.Quit
}
}

return m, nil
}

func (m model) View() string {
if m.quitting {
if m.quitting || m.interrupting {
return "\n"
}

Expand All @@ -91,12 +92,10 @@ func (m model) View() string {
}

var (
name string
version = "dev"
quitKeys = key.NewBinding(
key.WithKeys("q", "ctrl+c"),
key.WithHelp("q", "quit"),
)
name string
version = "dev"
quitKeys = key.NewBinding(key.WithKeys("esc", "q"))
intKeys = key.NewBinding(key.WithKeys("ctrl+c"))
boldStyle = lipgloss.NewStyle().Bold(true)
italicStyle = lipgloss.NewStyle().Italic(true)
)
Expand All @@ -118,14 +117,20 @@ var rootCmd = &coral.Command{
if err != nil {
return err
}
m := model{
m, err := tea.NewProgram(model{
duration: duration,
timer: timer.NewWithInterval(duration, time.Second),
progress: progress.New(progress.WithDefaultGradient()),
name: name,
start: time.Now(),
}).Run()
if err != nil {
return err
}
if m.(model).interrupting {
return fmt.Errorf("interrupted")
}
return tea.NewProgram(m).Start()
return nil
},
}

Expand Down

0 comments on commit 8674e43

Please sign in to comment.