1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use std::ops::ControlFlow;

use crate::{
    config::{self, StartTogetherOptions},
    errors::TogetherResult,
    log, log_err,
    manager::{self, ProcessAction},
    t_println,
    terminal::Terminal,
    terminal_ext::TerminalExt,
};

#[derive(Default)]
struct InputState {
    requested_quit: bool,
    awaiting_quit_command: bool,
    last_command: Option<String>,
}

enum Key {
    #[cfg(termion)]
    CtrlC,
    Char(char),
}

#[cfg(termion)]
impl TryFrom<termion::event::Key> for Key {
    type Error = ();

    fn try_from(key: termion::event::Key) -> Result<Self, Self::Error> {
        match key {
            termion::event::Key::Ctrl('c') => Ok(Self::CtrlC),
            termion::event::Key::Char(c) => Ok(Self::Char(c)),
            _ => Err(()),
        }
    }
}

impl From<char> for Key {
    fn from(c: char) -> Self {
        Self::Char(c)
    }
}

#[cfg(termion)]
pub fn block_for_user_input(
    start_opts: &StartTogetherOptions,
    sender: manager::ProcessManagerHandle,
) -> TogetherResult<()> {
    use std::io::Write;
    // use termion::event::Key;
    use termion::input::TermRead;

    let mut state = InputState::default();

    // let mut stdout = std::io::stdout().into_raw_mode().unwrap();
    let mut stdout = std::io::stdout();
    let stdin = std::io::stdin();

    for k in stdin.keys() {
        let Ok(k): Result<Key, ()> = k?.try_into() else {
            continue;
        };

        match handle_key_press(k, &mut state, start_opts, &sender)? {
            ControlFlow::Continue(_) => {
                write!(stdout, "{}", termion::cursor::Show).unwrap();
                stdout.flush().unwrap();
            }
            ControlFlow::Break(_) => break,
        }
    }

    drop(stdout);
    Ok(())
}

#[cfg(not(termion))]
pub fn block_for_user_input(
    start_opts: &StartTogetherOptions,
    sender: manager::ProcessManagerHandle,
) -> TogetherResult<()> {
    let mut state = InputState::default();
    let mut input = String::new();
    loop {
        std::io::stdin().read_line(&mut input)?;
        let Some(key) = input.trim().chars().next() else {
            continue;
        };

        match handle_key_press(key.into(), &mut state, start_opts, &sender)? {
            ControlFlow::Continue(_) => {}
            ControlFlow::Break(_) => break,
        }

        input.clear();
    }
    Ok(())
}

fn handle_key_press(
    key: Key,
    state: &mut InputState,
    start_opts: &StartTogetherOptions,
    sender: &manager::ProcessManagerHandle,
) -> TogetherResult<ControlFlow<()>> {
    if state.requested_quit {
        state.requested_quit = false;
        state.awaiting_quit_command = true;
    }

    match key {
        #[cfg(termion)]
        Key::CtrlC => {
            log!("Ctrl-C pressed, stopping all processes...");
            sender
                .send(ProcessAction::KillAll)
                .expect("Could not send signal on channel.");
        }
        Key::Char('h') | Key::Char('?') => {
            log!("[help]");
            t_println!("together is a tool to run multiple commands in parallel selectively by an interactive prompt.");

            t_println!();
            t_println!("Press 't' to trigger a one-time run");
            t_println!("Press '.' to re-trigger the last one-time run");
            t_println!("Press 'b' to batch trigger commands by recipe");
            t_println!("Press 'z' to switch to running a single recipe");
            t_println!("Press 'k' to kill a running command");
            t_println!("Press 'r' to restart a running command");
            t_println!("Press 'l' to list all running commands");
            t_println!("Press 'd' to dump the current configuration");
            t_println!("Press 'h' or '?' to show this help message");
            t_println!("Press 'q' to stop");
            t_println!();

            t_println!();
            log!("[status]");
            match sender.list() {
                Ok(list) => {
                    t_println!("together is running {} commands in parallel:", list.len());
                    for command in list {
                        t_println!("  {}", command);
                    }
                }
                Err(_) => {
                    t_println!("together is running in an unknown state");
                }
            }
        }
        Key::Char('q') => {
            if state.awaiting_quit_command {
                log!("Quitting together...");
                sender.send(ProcessAction::KillAll)?;
                return Ok(ControlFlow::Break(()));
            }

            log!("Press 'q' again to quit together");
            state.requested_quit = true;
            return Ok(ControlFlow::Break(()));
        }
        Key::Char('l') => {
            for command in sender.list()? {
                t_println!("{}", command);
            }
        }
        Key::Char('d') => {
            let list = sender.list()?;
            let running: Vec<_> = list.iter().map(|c| c.command()).collect();
            let config = start_opts.config.clone();
            let config = config.with_running(&running);
            config::dump(&config)?;
        }
        Key::Char('k') => {
            let list = sender.list()?;
            let command = Terminal::select_single_process(
                "Pick command to kill, or press 'q' to cancel",
                &sender,
                &list,
            )?;
            if let Some(command) = command {
                sender.send(ProcessAction::Kill(command.clone()))?;
            }
        }
        Key::Char('r') => {
            let list = sender.list()?;
            let command = Terminal::select_single_process(
                "Pick command to restart, or press 'q' to cancel",
                &sender,
                &list,
            )?;
            if let Some(command) = command {
                sender.send(ProcessAction::Kill(command.clone()))?;
                sender.send(ProcessAction::Create(command.command().to_string()))?;
            }
        }
        Key::Char('t') => {
            let command = Terminal::select_single_command(
                "Pick command to run, or press 'q' to cancel",
                &sender,
                &start_opts.config.start_options.commands,
            )?;
            if let Some(command) = command {
                sender.send(ProcessAction::Create(command.to_string()))?;
                state.last_command = Some(command.to_string());
            }
        }
        Key::Char('.') => {
            if let Some(command) = &state.last_command {
                sender.send(ProcessAction::Create(command.clone()))?;
            } else {
                log!("No last command to re-trigger");
            }
        }
        Key::Char('b') => {
            let all_recipes = config::get_unique_recipes(&start_opts.config.start_options);
            let all_recipes = all_recipes.into_iter().cloned().collect::<Vec<_>>();
            let recipes = Terminal::select_multiple_recipes(
                "Select one or more recipes to start running, or press 'q' to cancel",
                &sender,
                &all_recipes,
            )?;
            let commands =
                config::collect_commands_by_recipes(&start_opts.config.start_options, &recipes);
            for command in commands {
                sender.send(ProcessAction::Create(command.clone()))?;
            }
        }
        Key::Char('z') => {
            let all_recipes = config::get_unique_recipes(&start_opts.config.start_options);
            let all_recipes = all_recipes.into_iter().cloned().collect::<Vec<_>>();
            let recipe = Terminal::select_single_recipe(
                "Select a recipe to start running, or press 'q' to cancel (note: this will stop all other commands)",
                &sender,
                &all_recipes,
            )?;
            if let Some(recipe) = recipe {
                let recipe = recipe.clone();
                let recipe_commands = config::collect_commands_by_recipes(
                    &start_opts.config.start_options,
                    &[recipe],
                );
                let list = sender.list()?;
                let kill_commands: Vec<_> = list
                    .iter()
                    .filter(|c| !recipe_commands.contains(&c.command().to_string()))
                    .collect();

                for command in kill_commands {
                    sender.send(ProcessAction::Kill(command.clone()))?;
                }
                for command in recipe_commands {
                    sender.send(ProcessAction::Create(command.clone()))?;
                }
            }
        }
        Key::Char('\n') => {}
        Key::Char(c) => {
            log_err!("Unknown command: '{}'", c);
            log!("Press 'h' or '?' for help");
        }
    }
    state.awaiting_quit_command = false;

    Ok(ControlFlow::Continue(()))
}