mirror of
https://github.com/alexpasmantier/television.git
synced 2025-06-07 12:05:34 +00:00
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:
parent
d2e51854d8
commit
1ad646307e
@ -195,3 +195,11 @@ toggle_preview = "ctrl-o"
|
|||||||
|
|
||||||
# gitrepos channel
|
# gitrepos channel
|
||||||
"nvim" = "git-repos"
|
"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"
|
@ -15,7 +15,7 @@ use ui::UiConfig;
|
|||||||
|
|
||||||
mod keybindings;
|
mod keybindings;
|
||||||
mod previewers;
|
mod previewers;
|
||||||
mod shell_integration;
|
pub mod shell_integration;
|
||||||
mod themes;
|
mod themes;
|
||||||
mod ui;
|
mod ui;
|
||||||
|
|
||||||
|
@ -17,8 +17,9 @@ use television::cli::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use television::config::Config;
|
use television::config::Config;
|
||||||
|
use television::utils::shell::render_autocomplete_script_template;
|
||||||
use television::utils::{
|
use television::utils::{
|
||||||
shell::{completion_script, ctrl_keybinding, Shell},
|
shell::{completion_script, Shell},
|
||||||
stdin::is_readable_stdin,
|
stdin::is_readable_stdin,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -43,27 +44,11 @@ async fn main() -> Result<()> {
|
|||||||
// the completion scripts for the various shells are templated
|
// the completion scripts for the various shells are templated
|
||||||
// so that it's possible to override the keybindings triggering
|
// so that it's possible to override the keybindings triggering
|
||||||
// shell autocomplete and command history in tv
|
// shell autocomplete and command history in tv
|
||||||
let templated_script = completion_script(target_shell)?;
|
let script = render_autocomplete_script_template(
|
||||||
let script = templated_script
|
target_shell,
|
||||||
.replace(
|
completion_script(target_shell)?,
|
||||||
"{tv_smart_autocomplete_keybinding}",
|
&config.shell_integration,
|
||||||
&ctrl_keybinding(
|
)?;
|
||||||
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(),
|
|
||||||
)?,
|
|
||||||
);
|
|
||||||
|
|
||||||
println!("{script}");
|
println!("{script}");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
|
use crate::config::shell_integration::ShellIntegrationConfig;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub enum Shell {
|
pub enum Shell {
|
||||||
Bash,
|
Bash,
|
||||||
@ -16,9 +16,9 @@ const COMPLETION_FISH: &str = include_str!("shell/completion.fish");
|
|||||||
// create the appropriate key binding for each supported shell
|
// create the appropriate key binding for each supported shell
|
||||||
pub fn ctrl_keybinding(shell: Shell, character: char) -> Result<String> {
|
pub fn ctrl_keybinding(shell: Shell, character: char) -> Result<String> {
|
||||||
match shell {
|
match shell {
|
||||||
Shell::Bash => Ok(format!(r"\C-{}", character)),
|
Shell::Bash => Ok(format!(r"\C-{character}")),
|
||||||
Shell::Zsh => Ok(format!(r"^{}", character)),
|
Shell::Zsh => Ok(format!(r"^{character}")),
|
||||||
Shell::Fish => Ok(format!(r"\c{}", character)),
|
Shell::Fish => Ok(format!(r"\c{character}")),
|
||||||
_ => anyhow::bail!("This shell is not yet supported: {:?}", shell),
|
_ => 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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user