Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding key for sorting #143

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ If you want to add connections, you need to edit your config file. For more info
| <kbd>h</kbd>, <kbd>j</kbd>, <kbd>k</kbd>, <kbd>l</kbd> | Scroll left/down/up/right |
| <kbd>Ctrl</kbd> + <kbd>u</kbd>, <kbd>Ctrl</kbd> + <kbd>d</kbd> | Scroll up/down multiple lines |
| <kbd>g</kbd> , <kbd>G</kbd> | Scroll to top/bottom |
| <kbd>s</lbd> | Sort by selected column |
| <kbd>H</kbd>, <kbd>J</kbd>, <kbd>K</kbd>, <kbd>L</kbd> | Extend selection by one cell left/down/up/right |
| <kbd>y</kbd> | Copy a cell value |
| <kbd>←</kbd>, <kbd>→</kbd> | Move focus to left/right |
Expand Down
84 changes: 77 additions & 7 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@ impl App {
Ok(())
}

async fn update_record_table(&mut self) -> anyhow::Result<()> {
async fn update_record_table(
&mut self,
orders: Option<String>,
header_icons: Option<Vec<String>>,
hold_cursor_position: bool,
) -> anyhow::Result<()> {
if let Some((database, table)) = self.databases.tree().selected_table() {
let (headers, records) = self
.pool
Expand All @@ -177,10 +182,16 @@ impl App {
} else {
Some(self.record_table.filter.input_str())
},
orders,
)
.await?;
self.record_table
.update(records, headers, database.clone(), table.clone());
self.record_table.update(
records,
self.concat_headers(headers, header_icons),
database.clone(),
table.clone(),
hold_cursor_position,
);
}
Ok(())
}
Expand Down Expand Up @@ -230,10 +241,15 @@ impl App {
.pool
.as_ref()
.unwrap()
.get_records(&database, &table, 0, None)
.get_records(&database, &table, 0, None, None)
.await?;
self.record_table
.update(records, headers, database.clone(), table.clone());
self.record_table.update(
records,
headers,
database.clone(),
table.clone(),
false,
);
self.properties
.update(database.clone(), table.clone(), self.pool.as_ref().unwrap())
.await?;
Expand All @@ -249,6 +265,17 @@ impl App {
return Ok(EventState::Consumed);
};

if key == self.config.key_config.sort_by_column
&& !self.record_table.table.headers.is_empty()
{
self.record_table.table.add_order();
let order_query = self.record_table.table.generate_order_query();
let header_icons = self.record_table.table.generate_header_icons();
self.update_record_table(order_query, Some(header_icons), true)
.await?;
return Ok(EventState::Consumed);
};

if key == self.config.key_config.copy {
if let Some(text) = self.record_table.table.selected_cells() {
copy_to_clipboard(text.as_str())?
Expand All @@ -258,7 +285,10 @@ impl App {
if key == self.config.key_config.enter && self.record_table.filter_focused()
{
self.record_table.focus = crate::components::record_table::Focus::Table;
self.update_record_table().await?;
let order_query = self.record_table.table.generate_order_query();
let header_icons = self.record_table.table.generate_header_icons();
self.update_record_table(order_query, Some(header_icons), false)
.await?;
}

if self.record_table.table.eod {
Expand All @@ -283,6 +313,7 @@ impl App {
} else {
Some(self.record_table.filter.input_str())
},
None,
)
.await?;
if !records.is_empty() {
Expand Down Expand Up @@ -373,6 +404,24 @@ impl App {
}
Ok(EventState::NotConsumed)
}

fn concat_headers(
&self,
headers: Vec<String>,
header_icons: Option<Vec<String>>,
) -> Vec<String> {
if let Some(header_icons) = &header_icons {
let mut new_headers = vec![String::new(); headers.len()];
for (index, header) in headers.iter().enumerate() {
new_headers[index] = format!("{} {}", header, header_icons[index])
.trim()
.to_string();
}
return new_headers;
}

headers
}
}

#[cfg(test)]
Expand Down Expand Up @@ -408,4 +457,25 @@ mod test {
);
assert_eq!(app.left_main_chunk_percentage, 15);
}

#[test]
fn test_concat_headers() {
let app = App::new(Config::default());
let headers = vec![
"ID".to_string(),
"NAME".to_string(),
"TIMESTAMP".to_string(),
];
let header_icons = vec!["".to_string(), "↑1".to_string(), "↓2".to_string()];
let concat_headers: Vec<String> = app.concat_headers(headers, Some(header_icons));

assert_eq!(
concat_headers,
vec![
"ID".to_string(),
"NAME ↑1".to_string(),
"TIMESTAMP ↓2".to_string()
]
)
}
}
7 changes: 7 additions & 0 deletions src/components/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ pub fn scroll_to_top_bottom(key: &KeyConfig) -> CommandText {
)
}

pub fn sort_by_column(key: &KeyConfig) -> CommandText {
CommandText::new(
format!("Sort by column [{}]", key.sort_by_column),
CMD_GROUP_TABLE,
)
}

pub fn expand_collapse(key: &KeyConfig) -> CommandText {
CommandText::new(
format!("Expand/Collapse [{},{}]", key.scroll_right, key.scroll_left,),
Expand Down
4 changes: 4 additions & 0 deletions src/components/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl PropertiesComponent {
columns.get(0).unwrap().fields(),
database.clone(),
table.clone(),
false,
);
}
self.constraint_table.reset();
Expand All @@ -90,6 +91,7 @@ impl PropertiesComponent {
constraints.get(0).unwrap().fields(),
database.clone(),
table.clone(),
false,
);
}
self.foreign_key_table.reset();
Expand All @@ -103,6 +105,7 @@ impl PropertiesComponent {
foreign_keys.get(0).unwrap().fields(),
database.clone(),
table.clone(),
false,
);
}
self.index_table.reset();
Expand All @@ -116,6 +119,7 @@ impl PropertiesComponent {
indexes.get(0).unwrap().fields(),
database.clone(),
table.clone(),
false,
);
}
Ok(())
Expand Down
4 changes: 3 additions & 1 deletion src/components/record_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ impl RecordTableComponent {
headers: Vec<String>,
database: Database,
table: DTable,
hold_cusor_position: bool,
) {
self.table.update(rows, headers, database, table.clone());
self.table
.update(rows, headers, database, table.clone(), hold_cusor_position);
self.filter.table = Some(table);
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/sql_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ impl Component for SqlEditorComponent {
database,
table,
} => {
self.table.update(rows, headers, database, table);
self.table.update(rows, headers, database, table, false);
self.focus = Focus::Table;
self.query_result = None;
}
Expand Down
Loading