-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introducing keymapping and nested mappings
- Loading branch information
Showing
14 changed files
with
570 additions
and
279 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use crate::{ | ||
default_config::DEFAULT_CONFIG, EditorMode, APP_NAME, CONFIG_FILE, XDG_DEFAULTS, XDG_ENV_VARS, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::{collections::HashMap, path::PathBuf}; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] | ||
pub enum Action { | ||
Undo, | ||
FindNext, | ||
FindPrevious, | ||
|
||
NextWord, | ||
PreviousWord, | ||
MoveLeft, | ||
MoveDown, | ||
MoveUp, | ||
MoveRight, | ||
MoveToBottom, | ||
MoveToTop, | ||
MoveToLineEnd, | ||
MoveToLineStart, | ||
PageDown, | ||
PageUp, | ||
DeleteWord, | ||
DeleteLine, | ||
DeleteBack, | ||
DeleteUntilEOL, | ||
DeleteCurrentChar, | ||
InsertLineBelow, | ||
InsertLineAbove, | ||
PasteBelow, | ||
InsertAhead, | ||
EnterMode(EditorMode), | ||
InsertAtEOL, | ||
MoveAfterWhitespaceReverse, | ||
MoveAfterWhitespace, | ||
DeletePreviousNonWrapping, | ||
|
||
InsertChar(char), | ||
InsertTab, | ||
InsertLine, | ||
DeletePreviousChar, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct Config { | ||
pub editor_keys: Keys, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, Default)] | ||
pub struct Keys { | ||
pub normal: HashMap<String, KeyAction>, | ||
pub insert: HashMap<String, KeyAction>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone)] | ||
#[serde(untagged)] | ||
pub enum KeyAction { | ||
Simple(Action), | ||
Multiple(Vec<Action>), | ||
Complex(HashMap<String, KeyAction>), | ||
} | ||
|
||
pub fn get_config_dir() -> PathBuf { | ||
let path = std::env::var(XDG_ENV_VARS[0]) | ||
.map(PathBuf::from) | ||
.unwrap_or_else(|_| PathBuf::from(XDG_DEFAULTS[0])); | ||
|
||
dirs::home_dir().unwrap_or_default().join(path) | ||
} | ||
|
||
#[tracing::instrument] | ||
pub fn load_config() -> Config { | ||
let config_file = get_config_dir().join(APP_NAME).join(CONFIG_FILE); | ||
|
||
std::fs::read_to_string(config_file) | ||
.map(|toml| toml::from_str::<Config>(&toml)) | ||
.unwrap_or_else(|_| toml::from_str::<Config>(DEFAULT_CONFIG)) | ||
.expect("failed to load default config") | ||
} | ||
|
||
impl std::fmt::Display for EditorMode { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Self::Normal => f.write_str("NORMAL"), | ||
Self::Insert => f.write_str("INSERT"), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use std::path::PathBuf; | ||
|
||
use crate::{APP_NAME, SCHEMAS_DIR, XDG_DEFAULTS, XDG_ENV_VARS}; | ||
|
||
pub fn setup_data_dir() -> anyhow::Result<PathBuf> { | ||
let data_dir = get_data_dir(); | ||
if !data_dir.exists() && !data_dir.is_dir() { | ||
std::fs::create_dir(&data_dir)?; | ||
} | ||
|
||
Ok(data_dir) | ||
} | ||
|
||
fn get_data_dir() -> PathBuf { | ||
let path = std::env::var(XDG_ENV_VARS[1]) | ||
.map(PathBuf::from) | ||
.unwrap_or_else(|_| PathBuf::from(XDG_DEFAULTS[1])); | ||
|
||
dirs::home_dir() | ||
.unwrap_or_default() | ||
.join(path) | ||
.join(APP_NAME) | ||
} | ||
|
||
#[tracing::instrument(err)] | ||
pub fn get_schemas_dir() -> anyhow::Result<PathBuf> { | ||
let data_dir = get_data_dir(); | ||
let schemas_dir = data_dir.join(SCHEMAS_DIR); | ||
|
||
if !schemas_dir.exists() && !schemas_dir.is_dir() { | ||
std::fs::create_dir(&schemas_dir)?; | ||
} | ||
|
||
Ok(schemas_dir) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
pub static DEFAULT_CONFIG: &str = r##" | ||
[editor_keys.normal] | ||
"u" = "Undo" | ||
"n" = "FindNext" | ||
"S-N" = "FindPrevious" | ||
"w" = "NextWord" | ||
"b" = "PreviousWord" | ||
"h" = "MoveLeft" | ||
"Left" = "MoveLeft" | ||
"j" = "MoveDown" | ||
"Down" = "MoveDown" | ||
"k" = "MoveUp" | ||
"Up" = "MoveUp" | ||
"l" = "MoveRight" | ||
"Right" = "MoveRight" | ||
"S-G" = "MoveToBottom" | ||
"g" = { "g" = "MoveToTop" } | ||
"$" = "MoveToLineEnd" | ||
"End" = "MoveToLineEnd" | ||
"Home" = "MoveToLineStart" | ||
"0" = "MoveToLineStart" | ||
"C-d" = "PageDown" | ||
"C-u" = "PageUp" | ||
"d" = { "w" = "DeleteWord", "d" = "DeleteLine", "b" = "DeleteBack" } | ||
"S-D" = "DeleteUntilEOL" | ||
"x" = "DeleteCurrentChar" | ||
"o" = "InsertLineBelow" | ||
"S-O" = "InsertLineAbove" | ||
"p" = "PasteBelow" | ||
"a" = "InsertAhead" | ||
#"/" = { EnterMode = "Search" } | ||
"i" = { EnterMode = "Insert" } | ||
"S-I" = ["MoveToLineStart", { EnterMode = "Insert" }] | ||
"S-A" = "InsertAtEOL" | ||
"S-B" = "MoveAfterWhitespaceReverse" | ||
"S-W" = "MoveAfterWhitespace" | ||
"S-X" = "DeletePreviousNonWrapping" | ||
[editor_keys.insert] | ||
"Tab" = "InsertTab" | ||
"Enter" = "InsertLine" | ||
"Backspace" = "DeletePreviousChar" | ||
"Esc" = { EnterMode = "Normal" } | ||
"C-c" = { EnterMode = "Normal" } | ||
"C-W" = "DeleteBack" | ||
"##; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,31 @@ | ||
use directories::ProjectDirs; | ||
use std::path::PathBuf; | ||
|
||
fn get_data_dir() -> anyhow::Result<PathBuf> { | ||
let directory = if let Ok(s) = std::env::var("REQTUI_DATA") { | ||
PathBuf::from(s) | ||
} else if let Some(proj_dirs) = ProjectDirs::from("com", "reqtui", "reqtui") { | ||
proj_dirs.data_local_dir().to_path_buf() | ||
} else { | ||
anyhow::bail!("data directory not found"); | ||
}; | ||
|
||
Ok(directory) | ||
mod config; | ||
mod data; | ||
mod default_config; | ||
|
||
pub use config::{load_config, Action, Config, KeyAction}; | ||
pub use data::{get_schemas_dir, setup_data_dir}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(PartialEq, Deserialize, Serialize, Debug, Clone)] | ||
pub enum EditorMode { | ||
Insert, | ||
Normal, | ||
} | ||
|
||
pub fn setup_data_dir() -> anyhow::Result<PathBuf> { | ||
let data_dir = get_data_dir()?; | ||
pub static LOG_FILE: &str = "reqtui.log"; | ||
pub static APP_NAME: &str = "reqtui"; | ||
pub static SCHEMAS_DIR: &str = "schemas"; | ||
pub static CONFIG_FILE: &str = "reqtui.toml"; | ||
pub static THEMES_DIR: &str = "themes"; | ||
|
||
if !data_dir.exists() && !data_dir.is_dir() { | ||
std::fs::create_dir(&data_dir)?; | ||
} | ||
#[cfg(unix)] | ||
static XDG_ENV_VARS: [&str; 2] = ["XDG_CONFIG_HOME", "XDG_DATA_HOME"]; | ||
|
||
Ok(data_dir) | ||
} | ||
#[cfg(windows)] | ||
static XDG_ENV_VARS: [&str; 2] = ["LOCALAPPDATA", "LOCALAPPDATA"]; | ||
|
||
pub fn get_logfile() -> &'static str { | ||
"reqtui.log" | ||
} | ||
#[cfg(unix)] | ||
static XDG_DEFAULTS: [&str; 2] = [".config", ".local/share"]; | ||
|
||
#[tracing::instrument(err)] | ||
pub fn get_schemas_dir() -> anyhow::Result<PathBuf> { | ||
let data_dir = get_data_dir()?; | ||
let schemas_dir = data_dir.join("schemas"); | ||
|
||
if !schemas_dir.exists() && !schemas_dir.is_dir() { | ||
std::fs::create_dir(&schemas_dir)?; | ||
} | ||
|
||
Ok(schemas_dir) | ||
} | ||
#[cfg(windows)] | ||
static XDG_DEFAULTS: [&str; 2] = ["AppData\\Local", "AppData\\Local"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.