Skip to content

Commit

Permalink
Add key config (#33)
Browse files Browse the repository at this point in the history
* implement key config

* use key config in help component

* use key config in databases component

* use key config in error component

* use key config in connections component

* set commands

* use database/table as a table component name

* fix a test for get_text

* use key config in tab component

* fix function name

* add focus_connections key

* use quit exit key
  • Loading branch information
TaKO8Ki authored Jul 31, 2021
1 parent 3c00d78 commit 07b2b50
Show file tree
Hide file tree
Showing 22 changed files with 559 additions and 431 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ $ cargo install --version 0.1.0-alpha.0 gobang

| Key | Description |
| ---- | ---- |
| <kbd>h</kbd> | move left |
| <kbd>j</kbd> | move down |
| <kbd>k</kbd> | move up |
| <kbd>l</kbd> | move right |
| <kbd>h</kbd> | scroll left |
| <kbd>j</kbd> | scroll down |
| <kbd>k</kbd> | scroll up |
| <kbd>l</kbd> | scroll right |
| <kbd>Ctrl</kbd> + <kbd>d</kbd> | scroll down multiple lines |
| <kbd>Ctrl</kbd> + <kbd>u</kbd> | scroll up multiple lines |
9 changes: 3 additions & 6 deletions database-tree/src/databasetree.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::Table;
use crate::{
databasetreeitems::DatabaseTreeItems, error::Result, item::DatabaseTreeItemKind,
tree_iter::TreeIterator,
};
use crate::{Database, Table};
use std::{collections::BTreeSet, usize};

///
Expand All @@ -14,8 +14,6 @@ pub enum MoveSelection {
Right,
Top,
End,
PageDown,
PageUp,
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -76,13 +74,13 @@ impl DatabaseTree {
.and_then(|index| self.items.tree_items.get(index))
}

pub fn selected_table(&self) -> Option<(Table, String)> {
pub fn selected_table(&self) -> Option<(Database, Table)> {
self.selection.and_then(|index| {
let item = &self.items.tree_items[index];
match item.kind() {
DatabaseTreeItemKind::Database { .. } => None,
DatabaseTreeItemKind::Table { table, database } => {
Some((table.clone(), database.clone()))
Some((database.clone(), table.clone()))
}
}
})
Expand All @@ -109,7 +107,6 @@ impl DatabaseTree {
MoveSelection::Right => self.selection_right(selection),
MoveSelection::Top => Self::selection_start(selection),
MoveSelection::End => self.selection_end(selection),
MoveSelection::PageDown | MoveSelection::PageUp => None,
};

let changed_index = new_index.map(|i| i != selection).unwrap_or_default();
Expand Down
6 changes: 3 additions & 3 deletions database-tree/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl TreeItemInfo {
#[derive(PartialEq, Debug, Clone)]
pub enum DatabaseTreeItemKind {
Database { name: String, collapsed: bool },
Table { database: String, table: Table },
Table { database: Database, table: Table },
}

impl DatabaseTreeItemKind {
Expand Down Expand Up @@ -63,7 +63,7 @@ impl DatabaseTreeItemKind {
pub fn database_name(&self) -> Option<String> {
match self {
Self::Database { .. } => None,
Self::Table { database, .. } => Some(database.clone()),
Self::Table { database, .. } => Some(database.name.clone()),
}
}
}
Expand All @@ -82,7 +82,7 @@ impl DatabaseTreeItem {
Ok(Self {
info: TreeItemInfo::new(indent, false),
kind: DatabaseTreeItemKind::Table {
database: database.name.clone(),
database: database.clone(),
table: table.clone(),
},
})
Expand Down
2 changes: 1 addition & 1 deletion database-tree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use crate::{
item::{DatabaseTreeItem, TreeItemInfo},
};

#[derive(Clone)]
#[derive(Clone, PartialEq, Debug)]
pub struct Database {
pub name: String,
pub tables: Vec<Table>,
Expand Down
127 changes: 58 additions & 69 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use crate::utils::{MySqlPool, Pool};
use crate::{
components::tab::Tab,
components::{
ConnectionsComponent, DatabasesComponent, ErrorComponent, HelpComponent,
command, ConnectionsComponent, DatabasesComponent, ErrorComponent, HelpComponent,
RecordTableComponent, TabComponent, TableComponent, TableStatusComponent,
},
user_config::UserConfig,
config::Config,
};
use database_tree::Database;
use tui::{
Expand All @@ -33,36 +33,25 @@ pub struct App {
table_status: TableStatusComponent,
clipboard: Clipboard,
pool: Option<Box<dyn Pool>>,
pub user_config: Option<UserConfig>,
pub config: Config,
pub error: ErrorComponent,
}

impl Default for App {
fn default() -> App {
App {
record_table: RecordTableComponent::default(),
structure_table: TableComponent::default(),
focus: Focus::DabataseList,
tab: TabComponent::default(),
help: HelpComponent::new(),
user_config: None,
databases: DatabasesComponent::new(),
connections: ConnectionsComponent::default(),
table_status: TableStatusComponent::default(),
clipboard: Clipboard::new(),
pool: None,
error: ErrorComponent::default(),
}
}
}

impl App {
pub fn new(user_config: UserConfig) -> App {
pub fn new(config: Config) -> App {
Self {
user_config: Some(user_config.clone()),
connections: ConnectionsComponent::new(user_config.conn),
config: config.clone(),
connections: ConnectionsComponent::new(config.key_config.clone(), config.conn),
record_table: RecordTableComponent::new(config.key_config.clone()),
structure_table: TableComponent::new(config.key_config.clone()),
tab: TabComponent::new(config.key_config.clone()),
help: HelpComponent::new(config.key_config.clone()),
databases: DatabasesComponent::new(config.key_config.clone()),
table_status: TableStatusComponent::default(),
error: ErrorComponent::new(config.key_config),
focus: Focus::ConnectionList,
..App::default()
clipboard: Clipboard::new(),
pool: None,
}
}

Expand Down Expand Up @@ -119,34 +108,24 @@ impl App {
}

fn commands(&self) -> Vec<CommandInfo> {
let res = vec![
CommandInfo::new(crate::components::command::move_left("h"), true, true),
CommandInfo::new(crate::components::command::move_down("j"), true, true),
CommandInfo::new(crate::components::command::move_up("k"), true, true),
CommandInfo::new(crate::components::command::move_right("l"), true, true),
CommandInfo::new(crate::components::command::filter("/"), true, true),
CommandInfo::new(
crate::components::command::move_focus_to_right_widget(
Key::Right.to_string().as_str(),
),
true,
true,
),
let mut res = vec![
CommandInfo::new(command::scroll(&self.config.key_config)),
CommandInfo::new(command::scroll_to_top_bottom(&self.config.key_config)),
CommandInfo::new(command::move_focus(&self.config.key_config)),
CommandInfo::new(command::filter(&self.config.key_config)),
CommandInfo::new(command::help(&self.config.key_config)),
CommandInfo::new(command::toggle_tabs(&self.config.key_config)),
];

self.databases.commands(&mut res);
self.record_table.commands(&mut res);

res
}

pub async fn event(&mut self, key: Key) -> anyhow::Result<EventState> {
self.update_commands();

if let Key::Esc = key {
if self.error.error.is_some() {
self.error.error = None;
return Ok(EventState::Consumed);
}
}

if self.components_event(key).await?.is_consumed() {
return Ok(EventState::Consumed);
};
Expand All @@ -158,7 +137,11 @@ impl App {
}

pub async fn components_event(&mut self, key: Key) -> anyhow::Result<EventState> {
if self.help.event(key)?.is_consumed() {
if self.error.event(key)?.is_consumed() {
return Ok(EventState::Consumed);
}

if !matches!(self.focus, Focus::ConnectionList) && self.help.event(key)?.is_consumed() {
return Ok(EventState::Consumed);
}

Expand All @@ -168,8 +151,7 @@ impl App {
return Ok(EventState::Consumed);
}

if let Key::Enter = key {
self.record_table.reset();
if key == self.config.key_config.enter {
if let Some(conn) = self.connections.selected_connection() {
if let Some(pool) = self.pool.as_ref() {
pool.close().await;
Expand All @@ -189,7 +171,8 @@ impl App {
None => self.pool.as_ref().unwrap().get_databases().await?,
};
self.databases.update(databases.as_slice()).unwrap();
self.focus = Focus::DabataseList
self.focus = Focus::DabataseList;
self.record_table.reset();
}
return Ok(EventState::Consumed);
}
Expand All @@ -199,25 +182,30 @@ impl App {
return Ok(EventState::Consumed);
}

if matches!(key, Key::Enter) && self.databases.tree_focused() {
if let Some((table, database)) = self.databases.tree().selected_table() {
if key == self.config.key_config.enter && self.databases.tree_focused() {
if let Some((database, table)) = self.databases.tree().selected_table() {
self.focus = Focus::Table;
let (headers, records) = self
.pool
.as_ref()
.unwrap()
.get_records(&database, &table.name, 0, None)
.get_records(&database.name, &table.name, 0, None)
.await?;
self.record_table = RecordTableComponent::new(records, headers);
self.record_table.set_table(table.name.to_string());
self.record_table
.update(records, headers, database.clone(), table.clone());

let (headers, records) = self
.pool
.as_ref()
.unwrap()
.get_columns(&database, &table.name)
.get_columns(&database.name, &table.name)
.await?;
self.structure_table = TableComponent::new(records, headers);
self.structure_table.update(
records,
headers,
database.clone(),
table.clone(),
);
self.table_status
.update(self.record_table.len() as u64, table);
}
Expand All @@ -231,22 +219,23 @@ impl App {
return Ok(EventState::Consumed);
};

if let Key::Char('y') = key {
if key == self.config.key_config.copy {
if let Some(text) = self.record_table.table.selected_cells() {
self.clipboard.store(text)
}
}

if matches!(key, Key::Enter) && self.record_table.filter_focused() {
if key == self.config.key_config.enter && self.record_table.filter_focused()
{
self.record_table.focus = crate::components::record_table::Focus::Table;
if let Some((table, database)) = self.databases.tree().selected_table()
if let Some((database, table)) = self.databases.tree().selected_table()
{
let (headers, records) = self
.pool
.as_ref()
.unwrap()
.get_records(
&database.clone(),
&database.name.clone(),
&table.name,
0,
if self.record_table.filter.input.is_empty() {
Expand All @@ -256,7 +245,7 @@ impl App {
},
)
.await?;
self.record_table.update(records, headers);
self.record_table.update(records, headers, database, table);
}
}

Expand All @@ -269,15 +258,15 @@ impl App {
% crate::utils::RECORDS_LIMIT_PER_PAGE as usize
== 0
{
if let Some((table, database)) =
if let Some((database, table)) =
self.databases.tree().selected_table()
{
let (_, records) = self
.pool
.as_ref()
.unwrap()
.get_records(
&database.clone(),
&database.name.clone(),
&table.name,
index as u16,
if self.record_table.filter.input.is_empty() {
Expand All @@ -301,7 +290,7 @@ impl App {
return Ok(EventState::Consumed);
};

if let Key::Char('y') = key {
if key == self.config.key_config.copy {
if let Some(text) = self.structure_table.selected_cells() {
self.clipboard.store(text)
}
Expand All @@ -314,7 +303,7 @@ impl App {
}

pub fn move_focus(&mut self, key: Key) -> anyhow::Result<EventState> {
if let Key::Char('c') = key {
if key == self.config.key_config.focus_connections {
self.focus = Focus::ConnectionList;
return Ok(EventState::Consumed);
}
Expand All @@ -323,19 +312,19 @@ impl App {
}
match self.focus {
Focus::ConnectionList => {
if let Key::Enter = key {
if key == self.config.key_config.enter {
self.focus = Focus::DabataseList;
return Ok(EventState::Consumed);
}
}
Focus::DabataseList => {
if matches!(key, Key::Right) && self.databases.tree_focused() {
if key == self.config.key_config.focus_right && self.databases.tree_focused() {
self.focus = Focus::Table;
return Ok(EventState::Consumed);
}
}
Focus::Table => {
if let Key::Left = key {
if key == self.config.key_config.focus_left {
self.focus = Focus::DabataseList;
return Ok(EventState::Consumed);
}
Expand Down
Loading

0 comments on commit 07b2b50

Please sign in to comment.