mirror of https://github.com/TaKO8Ki/gobang
Add key config (#33)
* 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 keypull/36/head
parent
3c00d78326
commit
07b2b5090f
@ -0,0 +1,131 @@
|
||||
use crate::Key;
|
||||
use serde::Deserialize;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, Read};
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct Config {
|
||||
pub conn: Vec<Connection>,
|
||||
#[serde(default)]
|
||||
pub key_config: KeyConfig,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
conn: vec![Connection {
|
||||
name: None,
|
||||
user: "root".to_string(),
|
||||
host: "localhost".to_string(),
|
||||
port: 3306,
|
||||
database: None,
|
||||
}],
|
||||
key_config: KeyConfig::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct Connection {
|
||||
name: Option<String>,
|
||||
user: String,
|
||||
host: String,
|
||||
port: u64,
|
||||
pub database: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct KeyConfig {
|
||||
pub scroll_up: Key,
|
||||
pub scroll_down: Key,
|
||||
pub scroll_right: Key,
|
||||
pub scroll_left: Key,
|
||||
pub copy: Key,
|
||||
pub enter: Key,
|
||||
pub exit: Key,
|
||||
pub quit: Key,
|
||||
pub exit_popup: Key,
|
||||
pub focus_right: Key,
|
||||
pub focus_left: Key,
|
||||
pub focus_connections: Key,
|
||||
pub open_help: Key,
|
||||
pub filter: Key,
|
||||
pub scroll_down_multiple_lines: Key,
|
||||
pub scroll_up_multiple_lines: Key,
|
||||
pub scroll_to_top: Key,
|
||||
pub scroll_to_bottom: Key,
|
||||
pub extend_selection_by_one_cell_left: Key,
|
||||
pub extend_selection_by_one_cell_right: Key,
|
||||
pub extend_selection_by_one_cell_up: Key,
|
||||
pub extend_selection_by_one_cell_down: Key,
|
||||
pub tab_records: Key,
|
||||
pub tab_structure: Key,
|
||||
}
|
||||
|
||||
impl Default for KeyConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
scroll_up: Key::Char('k'),
|
||||
scroll_down: Key::Char('j'),
|
||||
scroll_right: Key::Char('l'),
|
||||
scroll_left: Key::Char('h'),
|
||||
copy: Key::Char('y'),
|
||||
enter: Key::Enter,
|
||||
exit: Key::Ctrl('c'),
|
||||
quit: Key::Char('q'),
|
||||
exit_popup: Key::Esc,
|
||||
focus_right: Key::Right,
|
||||
focus_left: Key::Left,
|
||||
focus_connections: Key::Char('c'),
|
||||
open_help: Key::Char('?'),
|
||||
filter: Key::Char('/'),
|
||||
scroll_down_multiple_lines: Key::Ctrl('d'),
|
||||
scroll_up_multiple_lines: Key::Ctrl('u'),
|
||||
scroll_to_top: Key::Char('g'),
|
||||
scroll_to_bottom: Key::Char('G'),
|
||||
extend_selection_by_one_cell_left: Key::Char('H'),
|
||||
extend_selection_by_one_cell_right: Key::Char('L'),
|
||||
extend_selection_by_one_cell_down: Key::Char('J'),
|
||||
extend_selection_by_one_cell_up: Key::Char('K'),
|
||||
tab_records: Key::Char('1'),
|
||||
tab_structure: Key::Char('2'),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn new(path: &str) -> anyhow::Result<Self> {
|
||||
if let Ok(file) = File::open(path) {
|
||||
let mut buf_reader = BufReader::new(file);
|
||||
let mut contents = String::new();
|
||||
buf_reader.read_to_string(&mut contents)?;
|
||||
|
||||
let config: Result<Config, toml::de::Error> = toml::from_str(&contents);
|
||||
match config {
|
||||
Ok(config) => return Ok(config),
|
||||
Err(e) => panic!("fail to parse config file: {}", e),
|
||||
}
|
||||
}
|
||||
Ok(Config::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl Connection {
|
||||
pub fn database_url(&self) -> String {
|
||||
match &self.database {
|
||||
Some(database) => format!(
|
||||
"mysql://{user}:@{host}:{port}/{database}",
|
||||
user = self.user,
|
||||
host = self.host,
|
||||
port = self.port,
|
||||
database = database
|
||||
),
|
||||
None => format!(
|
||||
"mysql://{user}:@{host}:{port}",
|
||||
user = self.user,
|
||||
host = self.host,
|
||||
port = self.port,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
use serde::Deserialize;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, Read};
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct UserConfig {
|
||||
pub conn: Vec<Connection>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct Connection {
|
||||
name: Option<String>,
|
||||
user: String,
|
||||
host: String,
|
||||
port: u64,
|
||||
pub database: Option<String>,
|
||||
}
|
||||
|
||||
impl UserConfig {
|
||||
pub fn new(path: &str) -> anyhow::Result<Self> {
|
||||
let file = File::open(path)?;
|
||||
let mut buf_reader = BufReader::new(file);
|
||||
let mut contents = String::new();
|
||||
buf_reader.read_to_string(&mut contents)?;
|
||||
|
||||
let config: Result<UserConfig, toml::de::Error> = toml::from_str(&contents);
|
||||
match config {
|
||||
Ok(config) => Ok(config),
|
||||
Err(e) => panic!("fail to parse config file: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Connection {
|
||||
pub fn database_url(&self) -> String {
|
||||
match &self.database {
|
||||
Some(database) => format!(
|
||||
"mysql://{user}:@{host}:{port}/{database}",
|
||||
user = self.user,
|
||||
host = self.host,
|
||||
port = self.port,
|
||||
database = database
|
||||
),
|
||||
None => format!(
|
||||
"mysql://{user}:@{host}:{port}",
|
||||
user = self.user,
|
||||
host = self.host,
|
||||
port = self.port,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue