Skip to content

Commit

Permalink
wip: restore and release terminal commands
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
  • Loading branch information
caarlos0 committed Apr 11, 2022
1 parent f5ff931 commit fcc926c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,26 @@ func Exec(c ExecCommand, fn ExecCallback) Cmd {
}
}

func RestoreTerminal(fn ExecCallback) Cmd {
return func() Msg {
return restoreTerminalMsg{fn: fn}
}
}

type restoreTerminalMsg struct {
fn ExecCallback
}

func ReleaseTerminal(fn ExecCallback) Cmd {
return func() Msg {
return releaseTerminalMsg{fn: fn}
}
}

type releaseTerminalMsg struct {
fn ExecCallback
}

// ExecCallback is used when executing an *exec.Command to return a message
// with an error, which may or may not be nil.
type ExecCallback func(error) Msg
Expand Down Expand Up @@ -563,6 +583,20 @@ func (p *Program) StartReturningModel() (Model, error) {
case hideCursorMsg:
hideCursor(p.output)

case restoreTerminalMsg:
if err := p.RestoreTerminal(); err != nil {
if msg.fn != nil {
go p.Send(msg.fn(err))
}
}

case releaseTerminalMsg:
if err := p.ReleaseTerminal(); err != nil {
if msg.fn != nil {
go p.Send(msg.fn(err))
}
}

case execMsg:
// Note: this blocks.
p.exec(msg.cmd, msg.fn)
Expand Down

0 comments on commit fcc926c

Please sign in to comment.