Skip to content
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
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ chrono = "0.4"
tokio = { version = "1.11.0", features = ["full"] }
futures = "0.3.5"
serde_json = "1.0"
serde = "1.0"
ron = "0.7"
serde = { version = "1", features = ["derive"] }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toml.rs cannot deserialize enums with values, so I use ron.

toml = "0.4"
strum = "0.21"
strum_macros = "0.21"
Expand Down
41 changes: 13 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ If you want to add connections, you need to edit your config file. For more info

## Keymap

### Default keymap

| Key | Description |
| ---- | ---- |
| <kbd>h</kbd>, <kbd>j</kbd>, <kbd>k</kbd>, <kbd>l</kbd> | Scroll left/down/up/right |
Expand All @@ -111,6 +113,16 @@ If you want to add connections, you need to edit your config file. For more info
| <kbd>?</kbd> | Help |
| <kbd>1</kbd>, <kbd>2</kbd>, <kbd>3</kbd>, <kbd>4</kbd>, <kbd>5</kbd> | Switch to records/columns/constraints/foreign keys/indexes tab |

### Custom keymap
The location of the file depends on your OS:

- macOS: `$HOME/.config/gobang/key_bind.ron`
- Linux: `$HOME/.config/gobang/key_bind.ron`
- Windows: `%APPDATA%/gobang/key_bind.ron`

A sample `key_bind.ron` is [here](https://github.com/TaKO8Ki/gobang/tree/main/examples/key_bind.ron).


## Configuration

The location of the file depends on your OS:
Expand All @@ -119,34 +131,7 @@ The location of the file depends on your OS:
- Linux: `$HOME/.config/gobang/config.toml`
- Windows: `%APPDATA%/gobang/config.toml`

The following is a sample config.toml file:

```toml
[[conn]]
type = "mysql"
user = "root"
host = "localhost"
port = 3306

[[conn]]
type = "mysql"
user = "root"
host = "localhost"
port = 3306
password = "password"
database = "foo"

[[conn]]
type = "postgres"
user = "root"
host = "localhost"
port = 5432
database = "bar"

[[conn]]
type = "sqlite"
path = "/path/to/baz.db"
```
A sample `config.toml` file is [here](https://github.com/TaKO8Ki/gobang/tree/main/examples/config.toml)

## Contribution

Expand Down
24 changes: 24 additions & 0 deletions examples/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[[conn]]
type = "mysql"
user = "root"
host = "localhost"
port = 3306

[[conn]]
type = "mysql"
user = "root"
host = "localhost"
port = 3306
password = "password"
database = "foo"

[[conn]]
type = "postgres"
user = "root"
host = "localhost"
port = 5432
database = "bar"

[[conn]]
type = "sqlite"
path = "/path/to/baz.db"
46 changes: 46 additions & 0 deletions examples/key_bind.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* This file is a custom key configuration file.
* Place this file in `$HOME/.config/gobang/key_bind.ron`.
*
* You can find the available keys here
* https://github.com/TaKO8Ki/gobang/blob/b13e4bb255ea533db16240e1cb5138fca4241264/src/event/key.rs
*
* You can get the latest custom key configuration file here
* https://github.com/TaKO8Ki/gobang/blob/b13e4bb255ea533db16240e1cb5138fca4241264/examples/key_bind.ron
*/
(
scroll_up: Some(Char('k')),
scroll_down: Some(Char('j')),
scroll_right: Some(Char('l')),
scroll_left: Some(Char('h')),
move_up: Some(Up),
move_down: Some(Down),
copy: Some(Char('y')),
enter: Some(Enter),
exit: Some(Ctrl('c')),
quit: Some(Char('q')),
exit_popup: Some(Esc),
focus_right: Some(Right),
focus_left: Some(Left),
focus_above: Some(Up),
focus_connections: Some(Char('c')),
open_help: Some(Char('?')),
filter: Some(Char('/')),
scroll_down_multiple_lines: Some(Ctrl('d')),
scroll_up_multiple_lines: Some(Ctrl('u')),
scroll_to_top: Some(Char('g')),
scroll_to_bottom: Some(Char('G')),
extend_selection_by_one_cell_left: Some(Char('H')),
extend_selection_by_one_cell_right: Some(Char('L')),
extend_selection_by_one_cell_down: Some(Char('J')),
extend_selection_by_one_cell_up: Some(Char('K')),
tab_records: Some(Char('1')),
tab_properties: Some(Char('2')),
tab_sql_editor: Some(Char('3')),
tab_columns: Some(Char('4')),
tab_constraints: Some(Char('5')),
tab_foreign_keys: Some(Char('6')),
tab_indexes: Some(Char('7')),
extend_or_shorten_widget_width_to_right: Some(Char('>')),
extend_or_shorten_widget_width_to_left: Some(Char('<')),
)
48 changes: 43 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::key_bind::KeyBind;
use crate::log::LogLevel;
use crate::Key;
use serde::Deserialize;
Expand All @@ -15,6 +16,10 @@ pub struct CliConfig {
/// Set the config file
#[structopt(long, short, global = true)]
config_path: Option<std::path::PathBuf>,

/// Set the key bind file
#[structopt(long, short, global = true)]
key_bind_path: Option<std::path::PathBuf>,
}

#[derive(Debug, Deserialize, Clone)]
Expand All @@ -26,6 +31,13 @@ pub struct Config {
pub log_level: LogLevel,
}

#[derive(Debug, Deserialize, Clone)]
pub struct ReadConfig {
pub conn: Vec<Connection>,
#[serde(default)]
pub log_level: LogLevel,
}

#[derive(Debug, Deserialize, Clone)]
enum DatabaseType {
#[serde(rename = "mysql")]
Expand Down Expand Up @@ -78,7 +90,7 @@ pub struct Connection {
}

#[derive(Debug, Deserialize, Clone)]
#[cfg_attr(test, derive(Serialize))]
#[cfg_attr(test, derive(Serialize, PartialEq))]
pub struct KeyConfig {
pub scroll_up: Key,
pub scroll_down: Key,
Expand Down Expand Up @@ -164,19 +176,35 @@ impl Config {
} else {
get_app_config_path()?.join("config.toml")
};

let key_bind_path = if let Some(key_bind_path) = &config.key_bind_path {
key_bind_path.clone()
} else {
get_app_config_path()?.join("key_bind.ron")
};

if let Ok(file) = File::open(config_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);
let config: Result<ReadConfig, toml::de::Error> = toml::from_str(&contents);
match config {
Ok(config) => return Ok(config),
Ok(config) => return Ok(Config::build(config, key_bind_path)),
Err(e) => panic!("fail to parse config file: {}", e),
}
}

Ok(Config::default())
}

fn build(read_config: ReadConfig, key_bind_path: PathBuf) -> Self {
let key_bind = KeyBind::load(key_bind_path).unwrap();
Config {
conn: read_config.conn,
log_level: read_config.log_level,
key_config: KeyConfig::from(key_bind),
}
}
}

impl Connection {
Expand Down Expand Up @@ -325,10 +353,20 @@ fn expand_path(path: &Path) -> Option<PathBuf> {

#[cfg(test)]
mod test {
use super::{expand_path, KeyConfig, Path, PathBuf};
use super::{expand_path, CliConfig, Config, KeyConfig, Path, PathBuf};
use serde_json::Value;
use std::env;

#[test]
fn test_load_config() {
let cli_config = CliConfig {
config_path: Some(Path::new("examples/config.toml").to_path_buf()),
key_bind_path: Some(Path::new("examples/key_bind.ron").to_path_buf()),
};

assert_eq!(Config::new(&cli_config).is_ok(), true);
}

#[test]
fn test_overlappted_key() {
let value: Value =
Expand Down
Loading