Skip to content

Commit

Permalink
Fix --echo-column option error
Browse files Browse the repository at this point in the history
Closes #100.
  • Loading branch information
YS-L committed Sep 10, 2024
1 parent d9125f9 commit abc8e43
Showing 1 changed file with 51 additions and 6 deletions.
57 changes: 51 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ impl App {
let rows_view = view::RowsView::new(csvlens_reader, num_rows as u64)?;

if let Some(column_name) = &echo_column {
return Err(CsvlensError::ColumnNameNotFound(column_name.clone()));
if !rows_view.headers().iter().any(|h| h.name == *column_name) {
return Err(CsvlensError::ColumnNameNotFound(column_name.clone()));
}
}

let csv_table_state = CsvTableState::new(
Expand Down Expand Up @@ -256,12 +258,8 @@ impl App {
}
}
if matches!(control, Control::Select) {
if let Some(result) = self.rows_view.get_cell_value_from_selection() {
if let Some(result) = self.get_selection() {
return Ok(Some(result));
} else if let Some(column_name) = &self.echo_column {
if let Some(result) = self.rows_view.get_cell_value(column_name) {
return Ok(Some(result));
}
}
}
if matches!(control, Control::Help) {
Expand Down Expand Up @@ -635,6 +633,17 @@ impl App {
Ok(())
}

fn get_selection(&self) -> Option<String> {
if let Some(result) = self.rows_view.get_cell_value_from_selection() {
return Some(result);
} else if let Some(column_name) = &self.echo_column {
if let Some(result) = self.rows_view.get_cell_value(column_name) {
return Some(result);
}
};
None
}

fn create_finder(&mut self, target: Regex, is_filter: bool, sorter: Option<Arc<sort::Sorter>>) {
self.create_finder_with_column_index(
target,
Expand Down Expand Up @@ -890,6 +899,11 @@ mod tests {
self.filter_regex = filter;
self
}

fn echo_column(mut self, column: &str) -> Self {
self.echo_column = Some(column.to_owned());
self
}
}

fn to_lines(buf: &Buffer) -> Vec<String> {
Expand Down Expand Up @@ -2149,4 +2163,35 @@ mod tests {
let lines = to_lines(&actual_buffer);
assert_eq!(lines, expected);
}

#[test]
fn test_echo_column() {
let mut app = AppBuilder::new("tests/data/cities.csv")
.echo_column("City")
.build()
.unwrap();
thread::sleep(time::Duration::from_millis(100));

let backend = TestBackend::new(180, 8);
let mut terminal = Terminal::new(backend).unwrap();

step_and_draw(&mut app, &mut terminal, Control::ScrollDown);
step_and_draw(&mut app, &mut terminal, Control::ScrollDown);
step_and_draw(&mut app, &mut terminal, Control::ScrollDown);

let selection = app.get_selection();
assert_eq!(selection, Some("Worcester".to_string()));
}

#[test]
fn test_echo_column_not_found() {
let app = AppBuilder::new("tests/data/cities.csv")
.echo_column("Cityz")
.build();
if let Err(e) = app {
assert_eq!(e.to_string(), "Column name not found: Cityz");
} else {
panic!("Expected error");
}
}
}

0 comments on commit abc8e43

Please sign in to comment.