exec runs an ExecCommand and delivers the results to the program as a Msg.
(c ExecCommand, fn ExecCallback)
| 100 | |
| 101 | // exec runs an ExecCommand and delivers the results to the program as a Msg. |
| 102 | func (p *Program) exec(c ExecCommand, fn ExecCallback) { |
| 103 | // Blank the current view before releasing the terminal so the renderer's |
| 104 | // final flush doesn't write the last View() output to stdout. Without |
| 105 | // this, the view content leaks to the terminal and persists after the |
| 106 | // subprocess exits. Terminal mode flags such as AltScreen are preserved so |
| 107 | // the screen mode is restored correctly when the program resumes. See #431. |
| 108 | if p.renderer != nil { |
| 109 | p.renderer.clearView() |
| 110 | } |
| 111 | |
| 112 | if err := p.releaseTerminal(false); err != nil { |
| 113 | // If we can't release input, abort. |
| 114 | if fn != nil { |
| 115 | go p.Send(fn(err)) |
| 116 | } |
| 117 | return |
| 118 | } |
| 119 | |
| 120 | c.SetStdin(p.input) |
| 121 | c.SetStdout(p.output) |
| 122 | c.SetStderr(os.Stderr) |
| 123 | |
| 124 | // Execute system command. |
| 125 | if err := c.Run(); err != nil { |
| 126 | _ = p.RestoreTerminal() // also try to restore the terminal. |
| 127 | if fn != nil { |
| 128 | go p.Send(fn(err)) |
| 129 | } |
| 130 | return |
| 131 | } |
| 132 | |
| 133 | // Have the program re-capture input. |
| 134 | err := p.RestoreTerminal() |
| 135 | if fn != nil { |
| 136 | go p.Send(fn(err)) |
| 137 | } |
| 138 | } |
no test coverage detected