use super::*;
impl Workspace {
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();
}
}
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);
}
}
}
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);
}
}
pub(super) fn stop_command(&mut self) {
if let Some(mut child) = self.run_child.take() {
let _ = child.kill();
let _ = child.wait();
self.run_number += 1;
self.output.finish(None);
}
}
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);
self.tree.refresh();
self.reload_changed();
self.refresh_git();
}
}
}
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);
}
_ => {}
}
}
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"));
}
}
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()),
}
}
}