Skip to content

Commit

Permalink
Output selected cell to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
YS-L committed Jul 8, 2023
1 parent 3018fd2 commit 2858bf8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ impl App {
return Ok(None);
}
if matches!(control, Control::Select) {
if let Some(column_name) = &self.echo_column {
if let Some(result) = self.rows_view.get_cell_value_from_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));
}
Expand Down Expand Up @@ -229,7 +231,7 @@ impl App {
_ => Some(0),
};
if let Some(new_cols_offset) = new_cols_offset {
self.csv_table_state.set_cols_offset(new_cols_offset);
self.rows_view.set_cols_offset(new_cols_offset);
}
}
Control::ScrollPageRight => {
Expand All @@ -244,7 +246,7 @@ impl App {
self.rows_view.headers().len().saturating_sub(1) as u64,
);
if new_cols_offset != self.csv_table_state.cols_offset {
self.csv_table_state.set_cols_offset(new_cols_offset);
self.rows_view.set_cols_offset(new_cols_offset);
}
}
}
Expand Down Expand Up @@ -373,6 +375,8 @@ impl App {
// TODO: is this update too late?
self.csv_table_state
.set_rows_offset(self.rows_view.rows_from());
self.csv_table_state
.set_cols_offset(self.rows_view.cols_offset());
self.csv_table_state.selection = Some(self.rows_view.selection.clone());

if let Some(n) = self.rows_view.get_total_line_numbers() {
Expand Down Expand Up @@ -409,14 +413,14 @@ impl App {

fn increase_cols_offset(&mut self) {
if self.csv_table_state.has_more_cols_to_show() {
let new_cols_offset = self.csv_table_state.cols_offset.saturating_add(1);
self.csv_table_state.set_cols_offset(new_cols_offset);
let new_cols_offset = self.rows_view.cols_offset().saturating_add(1);
self.rows_view.set_cols_offset(new_cols_offset);
}
}

fn decrease_cols_offset(&mut self) {
let new_cols_offset = self.csv_table_state.cols_offset.saturating_sub(1);
self.csv_table_state.set_cols_offset(new_cols_offset);
let new_cols_offset = self.rows_view.cols_offset().saturating_sub(1);
self.rows_view.set_cols_offset(new_cols_offset);
}

fn render_frame<B: Backend>(&mut self, f: &mut Frame<B>) {
Expand Down
31 changes: 31 additions & 0 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ pub struct RowsView {
rows: Vec<Row>,
num_rows: u64,
rows_from: u64,
cols_offset: u64,
filter: Option<RowsFilter>,
columns_filter: Option<ColumnsFilter>,
pub selection: Selection,
Expand All @@ -255,6 +256,7 @@ impl RowsView {
rows,
num_rows,
rows_from,
cols_offset: 0,
filter: None,
columns_filter: None,
selection: Selection::default(num_rows),
Expand Down Expand Up @@ -289,6 +291,25 @@ impl RowsView {
None
}

/// Get the value of the cell at the current selection. Only returns a value
/// if the selection type is Cell.
pub fn get_cell_value_from_selection(&self) -> Option<String> {
if let (Some(column_index), Some(row_index)) =
(self.selection.column.index(), self.selection.row.index())
{
// Note: row_index and column_index are "local" index.
return self
.rows()
.get(row_index as usize)
.and_then(|row| {
row.fields
.get(column_index.saturating_add(self.cols_offset()) as usize)
})
.cloned();
}
None
}

pub fn num_rows(&self) -> u64 {
self.num_rows
}
Expand Down Expand Up @@ -365,6 +386,16 @@ impl RowsView {
Ok(())
}

/// Offset of the first column to show. All columns are still read into Row
/// (per ColumnsFilter if any).
pub fn cols_offset(&self) -> u64 {
self.cols_offset
}

pub fn set_cols_offset(&mut self, cols_offset: u64) {
self.cols_offset = min(cols_offset, self.headers().len() as u64);
}

pub fn selected_offset(&self) -> Option<u64> {
self.selection
.row
Expand Down

0 comments on commit 2858bf8

Please sign in to comment.