| 57 | func (m runModel) Init() tea.Cmd { return nil } |
| 58 | |
| 59 | func (m runModel) Update(msg tea.Msg) (runModel, tea.Cmd) { |
| 60 | switch msg := msg.(type) { |
| 61 | case tea.WindowSizeMsg: |
| 62 | m.vp.Width = msg.Width - 4 |
| 63 | m.vp.Height = msg.Height - 12 |
| 64 | |
| 65 | case tea.KeyMsg: |
| 66 | switch msg.String() { |
| 67 | case "s": |
| 68 | if !m.isRunning() { |
| 69 | return m, m.start() |
| 70 | } |
| 71 | case "x": |
| 72 | if m.isRunning() { |
| 73 | return m, m.stop() |
| 74 | } |
| 75 | case "r": |
| 76 | cmds := []tea.Cmd{} |
| 77 | if m.isRunning() { |
| 78 | cmds = append(cmds, m.stop()) |
| 79 | } |
| 80 | cmds = append(cmds, m.start()) |
| 81 | return m, tea.Sequence(cmds...) |
| 82 | case "c": |
| 83 | m.logs = nil |
| 84 | m.vp.SetContent("") |
| 85 | } |
| 86 | |
| 87 | case runLogMsg: |
| 88 | m.appendLog(string(msg)) |
| 89 | if ch := m.stChan(); ch != nil { |
| 90 | return m, pumpCmd(ch) |
| 91 | } |
| 92 | case runExitMsg: |
| 93 | m.st.mu.Lock() |
| 94 | m.st.running = false |
| 95 | m.st.msgCh = nil |
| 96 | m.st.mu.Unlock() |
| 97 | line := "[engine exited cleanly]" |
| 98 | if msg.err != nil { |
| 99 | line = fmt.Sprintf("[engine exited: %v]", msg.err) |
| 100 | } |
| 101 | m.appendLog(errStyle.Render(line)) |
| 102 | } |
| 103 | |
| 104 | var cmd tea.Cmd |
| 105 | m.vp, cmd = m.vp.Update(msg) |
| 106 | return m, cmd |
| 107 | } |
| 108 | |
| 109 | func (m *runModel) appendLog(line string) { |
| 110 | m.logs = append(m.logs, line) |