refactor: make main smaller

- extract logic in function in shell
- add default configuration + comments in config.toml
- make format! strings clearer
This commit is contained in:
Bertrand Chardon 2025-01-25 22:22:07 +01:00
parent d2e51854d8
commit 1ad646307e
4 changed files with 43 additions and 27 deletions

View File

@ -195,3 +195,11 @@ toggle_preview = "ctrl-o"
# gitrepos channel
"nvim" = "git-repos"
[shell_integration.keybindings]
# controls which key binding should trigger tv
# for shell autocomplete
"smart_autocomplete" = "ctrl-t"
# controls which keybinding should trigger tv
# for command history
"command_history" = "ctrl-r"

View File

@ -15,7 +15,7 @@ use ui::UiConfig;
mod keybindings;
mod previewers;
mod shell_integration;
pub mod shell_integration;
mod themes;
mod ui;

View File

@ -17,8 +17,9 @@ use television::cli::{
};
use television::config::Config;
use television::utils::shell::render_autocomplete_script_template;
use television::utils::{
shell::{completion_script, ctrl_keybinding, Shell},
shell::{completion_script, Shell},
stdin::is_readable_stdin,
};
@ -43,27 +44,11 @@ async fn main() -> Result<()> {
// the completion scripts for the various shells are templated
// so that it's possible to override the keybindings triggering
// shell autocomplete and command history in tv
let templated_script = completion_script(target_shell)?;
let script = templated_script
.replace(
"{tv_smart_autocomplete_keybinding}",
&ctrl_keybinding(
let script = render_autocomplete_script_template(
target_shell,
config
.shell_integration
.get_shell_autocomplete_keybinding_character(),
)?,
)
.replace(
"{tv_shell_history_keybinding}",
&ctrl_keybinding(
target_shell,
config
.shell_integration
.get_command_history_keybinding_character(),
)?,
);
completion_script(target_shell)?,
&config.shell_integration,
)?;
println!("{script}");
exit(0);
}

View File

@ -1,5 +1,5 @@
use crate::config::shell_integration::ShellIntegrationConfig;
use anyhow::Result;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Shell {
Bash,
@ -16,9 +16,9 @@ const COMPLETION_FISH: &str = include_str!("shell/completion.fish");
// create the appropriate key binding for each supported shell
pub fn ctrl_keybinding(shell: Shell, character: char) -> Result<String> {
match shell {
Shell::Bash => Ok(format!(r"\C-{}", character)),
Shell::Zsh => Ok(format!(r"^{}", character)),
Shell::Fish => Ok(format!(r"\c{}", character)),
Shell::Bash => Ok(format!(r"\C-{character}")),
Shell::Zsh => Ok(format!(r"^{character}")),
Shell::Fish => Ok(format!(r"\c{character}")),
_ => anyhow::bail!("This shell is not yet supported: {:?}", shell),
}
}
@ -32,6 +32,29 @@ pub fn completion_script(shell: Shell) -> Result<&'static str> {
}
}
pub fn render_autocomplete_script_template(
shell: Shell,
template: &str,
config: &ShellIntegrationConfig,
) -> Result<String> {
let script = template
.replace(
"{tv_smart_autocomplete_keybinding}",
&ctrl_keybinding(
shell,
config.get_shell_autocomplete_keybinding_character(),
)?,
)
.replace(
"{tv_shell_history_keybinding}",
&ctrl_keybinding(
shell,
config.get_command_history_keybinding_character(),
)?,
);
Ok(script)
}
#[cfg(test)]
mod tests {
use super::*;