curd-editor 0.27.2

A terminal code editor: file tree, tabs, git, language servers, run and jump to errors, and a panel for Claude or any other assistant
//! Running a command and showing what it prints.

use super::*;

impl Workspace {
    // ---- Running a command ---------------------------------------------

    /// Ctrl+T: asks for a command to run, offering the last one again.
    pub(super) fn ask_command(&mut self) {
        self.pick(Picker::Run, Vec::new());
        if let Some(finder) = &mut self.finder {
            finder.query = self.output.command.clone();
        }
    }

    /// Runs `command` in the project folder and shows what it prints.
    pub(super) fn run_command(&mut self, command: &str) {
        self.stop_command();
        let Some(events) = &self.events else {
            return self.say("Commands cannot be run here".into());
        };
        self.run_number += 1;
        let run = self.run_number;
        let events = events.clone();
        let forward = move |event| {
            let _ = events.send(AppEvent::Run(run, event));
        };
        self.output.begin(command);
        self.show_output = true;
        if !self.agent_is_beside() {
            self.show_agent = false;
            if self.focus == Focus::Agent {
                self.leave_agent();
            }
        }
        match runner::start(command, self.tree.root(), forward) {
            Ok(child) => self.run_child = Some(child),
            Err(error) => {
                self.output.push(&format!("Could not run it: {error}"));
                self.output.finish(None);
            }
        }
    }

    /// F5: runs the last command again, or asks for one the first time.
    pub(super) fn run_again(&mut self) {
        if self.output.command.is_empty() {
            self.ask_command();
        } else {
            let command = self.output.command.clone();
            self.run_command(&command);
        }
    }

    /// Stops the command if it is running.
    pub(super) fn stop_command(&mut self) {
        if let Some(mut child) = self.run_child.take() {
            let _ = child.kill();
            let _ = child.wait();
            // Whatever it still prints belongs to no run now.
            self.run_number += 1;
            self.output.finish(None);
        }
    }

    /// Something from the running command: a line, or its end.
    pub(super) fn handle_run(&mut self, run: u64, event: runner::Event) {
        if run != self.run_number {
            return;
        }
        match event {
            runner::Event::Line(line) => self.output.push(&line),
            runner::Event::Exit => {
                let code = self
                    .run_child
                    .take()
                    .and_then(|mut child| child.wait().ok())
                    .and_then(|status| status.code());
                self.output.finish(code);
                // A build or a test run can make and change files.
                self.tree.refresh();
                self.reload_changed();
                self.refresh_git();
            }
        }
    }

    /// Alt+T: shows what the command printed and puts the keys there;
    /// pressed again there, hides it.
    pub(super) fn toggle_output(&mut self) {
        if self.show_output && self.focus == Focus::Output {
            self.show_output = false;
            self.leave_agent();
        } else if self.output.command.is_empty() {
            self.ask_command();
        } else {
            self.show_output = true;
            self.focus = Focus::Output;
            if !self.agent_is_beside() {
                self.show_agent = false;
            }
        }
    }

    pub(super) fn output_key(&mut self, key: KeyEvent) {
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
        let page = self.layout(self.size.0, self.size.1).output.saturating_sub(1).max(1) as isize;
        match key.code {
            KeyCode::Char('c') if ctrl => self.stop_command(),
            KeyCode::Esc => self.leave_agent(),
            KeyCode::Up => self.output.move_by(-1),
            KeyCode::Down => self.output.move_by(1),
            KeyCode::PageUp => self.output.move_by(-page),
            KeyCode::PageDown => self.output.move_by(page),
            KeyCode::Home => self.output.move_by(isize::MIN),
            KeyCode::End => self.output.move_by(isize::MAX),
            KeyCode::Enter => {
                let place = self.output.selected_location();
                self.go_to_place(place);
            }
            _ => {}
        }
    }

    /// Opens the file a line of the output names, at the line it names.
    pub(super) fn go_to_place(&mut self, place: Option<(String, usize, usize)>) {
        let Some((file, row, column)) = place else {
            return self.say("No file and line named there".into());
        };
        let path = self.tree.root().join(&file);
        if path.is_file() && self.open(&path) {
            self.editors[self.active].jump_to(column - 1, row - 1);
        } else {
            self.say(format!("{file} is not a file in this project"));
        }
    }

    /// F8 and Shift+F8: goes to the next place the output names (the one
    /// before, with Shift), which is how to walk through a build's errors.
    pub(super) fn next_error(&mut self, forward: bool) {
        match self.output.next_location(forward) {
            Some(place) => self.go_to_place(Some(place)),
            None => self.say("No more places named in what the command printed".into()),
        }
    }
}