Skip to content

Commit

Permalink
feat: change config.vi_keybindings to config.keybindings (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Oct 27, 2023
1 parent 5ce2b9c commit aa43612
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ highlight: true # Set false to turn highlight
conversation_first: false # If set true, start a conversation immediately upon repl
light_theme: false # If set true, use light theme
auto_copy: false # Automatically copy the last output to the clipboard
vi_keybindings: false # If set ture, switch repl keybindings from emacs to vi
keybindings: emacs # REPL keybindings, possible values: emacs (default), vi

clients: # Setup LLM platforms

Expand Down
29 changes: 25 additions & 4 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ pub struct Config {
pub light_theme: bool,
/// Automatically copy the last output to the clipboard
pub auto_copy: bool,
/// Use vi keybindings, overriding the default Emacs keybindings
pub vi_keybindings: bool,
/// REPL keybindings, possible values: emacs (default), vi
pub keybindings: Keybindings,
/// Setup LLM platforms
pub clients: Vec<ClientConfig>,
/// Predefined roles
Expand All @@ -86,7 +86,7 @@ impl Default for Config {
conversation_first: false,
light_theme: false,
auto_copy: false,
vi_keybindings: false,
keybindings: Default::default(),
roles: vec![],
clients: vec![ClientConfig::OpenAI(OpenAIConfig::default())],
role: None,
Expand Down Expand Up @@ -321,7 +321,7 @@ impl Config {
("conversation_first", self.conversation_first.to_string()),
("light_theme", self.light_theme.to_string()),
("dry_run", self.dry_run.to_string()),
("vi_keybindings", self.vi_keybindings.to_string()),
("keybindings", self.keybindings.stringify().into()),
];
let mut output = String::new();
for (name, value) in items {
Expand Down Expand Up @@ -497,6 +497,27 @@ impl Config {
}
}

#[derive(Debug, Clone, Deserialize, Default)]
pub enum Keybindings {
#[serde(rename = "emacs")]
#[default]
Emacs,
#[serde(rename = "vi")]
Vi,
}

impl Keybindings {
pub fn is_vi(&self) -> bool {
matches!(self, Keybindings::Vi)
}
pub fn stringify(&self) -> &str {
match self {
Keybindings::Emacs => "emacs",
Keybindings::Vi => "vi",
}
}
}

fn create_config_file(config_path: &Path) -> Result<()> {
let ans = Confirm::new("No config file, create a new one?")
.with_default(true)
Expand Down
2 changes: 1 addition & 1 deletion src/repl/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Repl {
let highlighter = ReplHighlighter::new(config.clone(), commands);
let history = Self::create_history()?;
let menu = Self::create_menu();
let edit_mode: Box<dyn EditMode> = if config.read().vi_keybindings {
let edit_mode: Box<dyn EditMode> = if config.read().keybindings.is_vi() {
let mut normal_keybindings = default_vi_normal_keybindings();
let mut insert_keybindings = default_vi_insert_keybindings();
Self::extra_keybindings(&mut normal_keybindings);
Expand Down

0 comments on commit aa43612

Please sign in to comment.