exec runs an ExecCommand and delivers the results to the program as a Msg.
(c ExecCommand, fn ExecCallback)
| 104 | |
| 105 | // exec runs an ExecCommand and delivers the results to the program as a Msg. |
| 106 | func (p *Program) exec(c ExecCommand, fn ExecCallback) { |
| 107 | // Blank the current view before releasing the terminal so the renderer's |
| 108 | // final flush doesn't write the last View() output to stdout. Without |
| 109 | // this, the view content leaks to the terminal and persists after the |
| 110 | // subprocess exits. Terminal mode flags such as AltScreen are preserved so |
| 111 | // the screen mode is restored correctly when the program resumes. See #431. |
| 112 | if p.renderer != nil { |
| 113 | p.renderer.clearView() |
| 114 | } |
| 115 | |
| 116 | if err := p.releaseTerminal(false); err != nil { |
| 117 | // If we can't release input, abort. |
| 118 | if fn != nil { |
| 119 | go p.Send(fn(err)) |
| 120 | } |
| 121 | return |
| 122 | } |
| 123 | |
| 124 | c.SetStdin(p.input) |
| 125 | c.SetStdout(p.output) |
| 126 | c.SetStderr(os.Stderr) |
| 127 | |
| 128 | // A program that resumes itself brings back what it started, so the |
| 129 | // child must not be resumed in its place. |
| 130 | if oc, ok := c.(*osExecCommand); ok && p.renderer != nil && p.renderer.reportsProgramResume() { |
| 131 | oc.Env = slices.DeleteFunc(oc.Environ(), func(kv string) bool { |
| 132 | return strings.HasPrefix(kv, ansi.ProgramResumeTokenEnv+"=") |
| 133 | }) |
| 134 | } |
| 135 | |
| 136 | // Execute system command. |
| 137 | if err := c.Run(); err != nil { |
| 138 | _ = p.RestoreTerminal() // also try to restore the terminal. |
| 139 | if fn != nil { |
| 140 | go p.Send(fn(err)) |
| 141 | } |
| 142 | return |
| 143 | } |
| 144 | |
| 145 | // Have the program re-capture input. |
| 146 | err := p.RestoreTerminal() |
| 147 | if fn != nil { |
| 148 | go p.Send(fn(err)) |
| 149 | } |
| 150 | } |
no test coverage detected