-
-
Notifications
You must be signed in to change notification settings - Fork 78
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
Added key maps in config #104
Open
Praaa181203
wants to merge
14
commits into
max-niederman:main
Choose a base branch
from
Praaa181203:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+256
−15
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
46cbfc2
Feature: Added basic configs.
Praaa181203 058898d
Feature: Revamp.
Praaa181203 d21b487
Feature: Extracted getting Key from string to a different function.
Praaa181203 1ee8d16
Features:
Praaa181203 c6b16bb
Fix: If Modifier is valid and code is not, the Key was not None, but now
Praaa181203 554fcb6
Feature: Updated Keymaps
Praaa181203 3e1f7dc
Feature: Added functionality for the new key map configs.
Praaa181203 bc19d18
Features:
Praaa181203 05f7cf7
Format
Praaa181203 676c01d
Feature: Added string for the formatter.
Praaa181203 2408735
Feature: str::len returns the length in bytes, not unicode code point…
Praaa181203 4abf39e
Feature: Used config as the determination of state.
Praaa181203 53b0f09
fmt
Praaa181203 d222e1d
Feature: Panics if the entered keymap is invalid.
Praaa181203 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
use core::panic; | ||
|
||
use crossterm::event::{KeyCode, KeyModifiers}; | ||
use serde::{de, Deserialize}; | ||
|
||
#[derive(Debug, Deserialize, Default, Clone)] | ||
#[serde(default)] | ||
pub struct KeyMap { | ||
#[serde(deserialize_with = "deseralize_key")] | ||
pub remove_previous_word: Key, | ||
#[serde(deserialize_with = "deseralize_key")] | ||
pub remove_previous_char: Key, | ||
#[serde(deserialize_with = "deseralize_key")] | ||
pub next_word: Key, | ||
} | ||
|
||
fn deseralize_key<'de, D>(deserializer: D) -> Result<Key, D::Error> | ||
where | ||
D: de::Deserializer<'de>, | ||
{ | ||
struct KeyVisitor; | ||
impl<'de> de::Visitor<'de> for KeyVisitor { | ||
type Value = Key; | ||
|
||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
formatter.write_str("key specification") | ||
} | ||
|
||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> | ||
where | ||
E: de::Error, | ||
{ | ||
match get_key_from_string(v) { | ||
Some(key) => Ok(key), | ||
None => { | ||
panic!("Key map `{}` is invalid", v) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return deserializer.deserialize_str(KeyVisitor); | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Key { | ||
pub code: KeyCode, | ||
pub modifier: KeyModifiers, | ||
} | ||
|
||
impl Default for Key { | ||
fn default() -> Self { | ||
Self { | ||
code: KeyCode::Null, | ||
modifier: KeyModifiers::NONE, | ||
} | ||
} | ||
} | ||
|
||
fn get_key_code_from_string(string: &str) -> KeyCode { | ||
if string.chars().count() == 1 { | ||
let key_code_char = string.chars().next(); | ||
if let Some(key_code_char) = key_code_char { | ||
if key_code_char.is_lowercase() { | ||
return KeyCode::Char(key_code_char); | ||
} | ||
} | ||
} | ||
match string { | ||
"Backspace" => KeyCode::Backspace, | ||
"Enter" => KeyCode::Enter, | ||
"Left" => KeyCode::Left, | ||
"Right" => KeyCode::Right, | ||
"Up" => KeyCode::Up, | ||
"Down" => KeyCode::Down, | ||
"Home" => KeyCode::Home, | ||
"End" => KeyCode::End, | ||
"PageUp" => KeyCode::PageUp, | ||
"PageDown" => KeyCode::PageDown, | ||
"Tab" => KeyCode::Tab, | ||
"BackTab" => KeyCode::BackTab, | ||
"Delete" => KeyCode::Delete, | ||
"Insert" => KeyCode::Insert, | ||
"Esc" => KeyCode::Esc, | ||
"CapsLock" => KeyCode::CapsLock, | ||
"ScrollLock" => KeyCode::ScrollLock, | ||
"NumLock" => KeyCode::NumLock, | ||
"PrintScreen" => KeyCode::PrintScreen, | ||
"Pause" => KeyCode::Pause, | ||
"Menu" => KeyCode::Menu, | ||
"KeypadBegin" => KeyCode::KeypadBegin, | ||
_ => KeyCode::Null, | ||
} | ||
} | ||
|
||
fn get_key_modifier_from_string(string: &str) -> KeyModifiers { | ||
match string { | ||
"C" => KeyModifiers::CONTROL, | ||
"A" => KeyModifiers::ALT, | ||
"W" => KeyModifiers::SUPER, | ||
"H" => KeyModifiers::HYPER, | ||
"M" => KeyModifiers::META, | ||
_ => KeyModifiers::NONE, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An unknown modifier should probably be an error, not a silent fallback to no modifier. |
||
} | ||
} | ||
|
||
fn get_key_from_string(string: &str) -> Option<Key> { | ||
let mut key = Key { | ||
code: KeyCode::Null, | ||
modifier: KeyModifiers::NONE, | ||
}; | ||
match string.split('-').count() { | ||
1 => { | ||
if string.chars().count() == 1 { | ||
key.code = KeyCode::Null; | ||
} else { | ||
key.code = get_key_code_from_string(string); | ||
} | ||
} | ||
2 => { | ||
let mut split = string.split('-'); | ||
let key_code = split.next(); | ||
if let Some(key_code) = key_code { | ||
if key_code.chars().count() == 1 { | ||
key.modifier = get_key_modifier_from_string(key_code); | ||
} | ||
} | ||
if key.modifier != KeyModifiers::NONE { | ||
let key_code = split.next(); | ||
if let Some(key_code) = key_code { | ||
key.code = get_key_code_from_string(key_code); | ||
if key.code == KeyCode::Null { | ||
key.modifier = KeyModifiers::NONE; | ||
} | ||
} | ||
} | ||
} | ||
_ => {} | ||
} | ||
if key.modifier == KeyModifiers::NONE && key.code == KeyCode::Null { | ||
return None; | ||
} | ||
Some(key) | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would probably be good to support non-abbreviated modifiers for each of these also. For example, "Ctrl", "Alt", etc. Some people also call the Meta key "Super" or "Win".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not quite understand this?
Should I allow "Ctrl-Backspace" as an option for config?
And Meta and Super are seperate modifiers, so i dont quite get what you mean.