Skip to content

Commit 1557ce2

Browse files
author
tlipinski
committed
refactor: handle stdin read errors explicitly and clean up error handling in Output
1 parent 49083f9 commit 1557ce2

2 files changed

Lines changed: 41 additions & 16 deletions

File tree

src/app.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::Args;
2-
use crate::app::Action::{CommandCompleted, Debounced, ResetHighlight, StdinRead, UserInput};
2+
use crate::app::Action::{
3+
CommandCompleted, Debounced, ResetHighlight, StdinRead, StdinReadFailed, UserInput,
4+
};
35
use crate::cmd_runner::CmdRunner;
46
use crate::completion::ShCompleter;
57
use crate::config::{KeyBindingsConfig, ThemeConfig};
@@ -161,6 +163,10 @@ impl App {
161163
.handle_command_output(Output::ok_stdin(&output));
162164
self.stdin = output;
163165
}
166+
StdinReadFailed(output) => {
167+
self.output_widget
168+
.handle_command_output(Output::err_stdin(&output));
169+
}
164170
Debounced => {
165171
match self.input_mode {
166172
InputMode::Normal => {
@@ -562,7 +568,6 @@ fn handle_command_task(
562568
// todo use dedicated status widget for such errors?
563569
action_tx.send(CommandCompleted(Output::err_stdin(
564570
"Failed running command, check logs",
565-
None,
566571
)))?;
567572
error!("{}", e)
568573
}
@@ -580,16 +585,20 @@ fn handle_input_task(tx: Sender<Action>) -> Result<()> {
580585
}
581586
}
582587

583-
fn read_stdin_task(file_opt: Option<String>, tx: Sender<Action>) -> Result<()> {
588+
fn read_stdin_task(file_opt: Option<String>, action_tx: Sender<Action>) -> Result<()> {
584589
if let Some(file) = file_opt {
585-
info!("reading file {file}");
586-
let file_content = std::fs::read_to_string(file);
590+
info!("reading input file {file}");
591+
let file_content = std::fs::read_to_string(file.clone());
587592
match file_content {
588593
Ok(content) => {
589-
tx.send(StdinRead(content))?;
594+
action_tx.send(StdinRead(content))?;
590595
}
591596
Err(e) => {
592-
tx.send(StdinRead(e.to_string()))?;
597+
action_tx.send(StdinReadFailed(format!(
598+
"Failed reading input file {}: {}",
599+
file,
600+
e.to_string()
601+
)))?;
593602
}
594603
}
595604
Ok(())
@@ -601,10 +610,13 @@ fn read_stdin_task(file_opt: Option<String>, tx: Sender<Action>) -> Result<()> {
601610

602611
match result {
603612
Ok(_) => {
604-
tx.send(StdinRead(buff))?;
613+
action_tx.send(StdinRead(buff))?;
605614
}
606615
Err(e) => {
607-
tx.send(StdinRead(e.to_string()))?;
616+
action_tx.send(StdinReadFailed(format!(
617+
"Failed reading stdin: {}",
618+
e.to_string()
619+
)))?;
608620
}
609621
}
610622
Ok(())
@@ -627,6 +639,7 @@ enum Action {
627639
UserInput(Event),
628640
CommandCompleted(Output),
629641
StdinRead(String),
642+
StdinReadFailed(String),
630643
ResetHighlight,
631644
Debounced,
632645
}

src/output_widget.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,11 @@ impl Output {
445445
}
446446
}
447447

448-
pub fn err_stdin(str: &str, status_code: Option<i32>) -> Self {
448+
pub fn err_stdin(str: &str) -> Self {
449449
Self {
450450
command: None,
451451
lines: Self::lines(str),
452-
status_code,
452+
status_code: None,
453453
ok: false,
454454
}
455455
}
@@ -500,8 +500,12 @@ mod tests {
500500
widget.error_pane_placement = ErrorPanePlacement::Top;
501501
widget.error_display_mode = ErrorDisplayMode::Pane;
502502

503-
widget.handle_command_output(Output::ok_stdin("out1\nout2\nout3"));
504-
widget.handle_command_output(Output::err_stdin("errors1\nerrors2\nerrors3", Some(1)));
503+
widget.handle_command_output(Output::ok_command("", "out1\nout2\nout3"));
504+
widget.handle_command_output(Output::err_command(
505+
"",
506+
"errors1\nerrors2\nerrors3",
507+
Some(1),
508+
));
505509

506510
terminal
507511
.draw(|frame| widget.render(frame.area(), frame.buffer_mut()))
@@ -518,8 +522,12 @@ mod tests {
518522
widget.error_pane_placement = ErrorPanePlacement::Bottom;
519523
widget.error_display_mode = ErrorDisplayMode::Pane;
520524

521-
widget.handle_command_output(Output::ok_stdin("out1\nout2\nout3"));
522-
widget.handle_command_output(Output::err_stdin("errors1\nerrors2\nerrors3", Some(1)));
525+
widget.handle_command_output(Output::ok_command("", "out1\nout2\nout3"));
526+
widget.handle_command_output(Output::err_command(
527+
"",
528+
"errors1\nerrors2\nerrors3",
529+
Some(1),
530+
));
523531

524532
terminal
525533
.draw(|frame| widget.render(frame.area(), frame.buffer_mut()))
@@ -550,7 +558,11 @@ mod tests {
550558

551559
widget.handle_command_output(Output::ok_stdin(&generate_lines(3)));
552560

553-
widget.handle_command_output(Output::err_stdin("errors1\nerrors2\nerrors3", Some(1)));
561+
widget.handle_command_output(Output::err_command(
562+
"",
563+
"errors1\nerrors2\nerrors3",
564+
Some(1),
565+
));
554566
terminal
555567
.draw(|frame| widget.render(frame.area(), frame.buffer_mut()))
556568
.unwrap();

0 commit comments

Comments
 (0)