feat(cli): allow passing --input <STRING> to prefill input prompt (#153)

Fixes #152
This commit is contained in:
Alex Pasmantier 2024-12-28 17:56:11 +01:00 committed by GitHub
parent c3b8c68d1b
commit 309ff537a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 25 additions and 11 deletions

View File

@ -44,7 +44,7 @@ impl HelpBarLayout {
} }
} }
#[derive(Debug, Clone, Copy, Deserialize, Default)] #[derive(Debug, Clone, Copy, Deserialize, Default, PartialEq)]
pub enum InputPosition { pub enum InputPosition {
#[serde(rename = "top")] #[serde(rename = "top")]
Top, Top,

View File

@ -85,6 +85,7 @@ impl App {
channel: TelevisionChannel, channel: TelevisionChannel,
config: Config, config: Config,
passthrough_keybindings: &[String], passthrough_keybindings: &[String],
input: Option<String>,
) -> Result<Self> { ) -> Result<Self> {
let (action_tx, action_rx) = mpsc::unbounded_channel(); let (action_tx, action_rx) = mpsc::unbounded_channel();
let (render_tx, _) = mpsc::unbounded_channel(); let (render_tx, _) = mpsc::unbounded_channel();
@ -104,7 +105,7 @@ impl App {
)?; )?;
debug!("{:?}", keymap); debug!("{:?}", keymap);
let television = let television =
Arc::new(Mutex::new(Television::new(channel, config))); Arc::new(Mutex::new(Television::new(channel, config, input)));
Ok(Self { Ok(Self {
keymap, keymap,

View File

@ -44,6 +44,10 @@ pub struct Cli {
#[arg(long, value_name = "STRING")] #[arg(long, value_name = "STRING")]
pub passthrough_keybindings: Option<String>, pub passthrough_keybindings: Option<String>,
/// Input text to pass to the channel to prefill the prompt
#[arg(short, long, value_name = "STRING")]
pub input: Option<String>,
/// The working directory to start in /// The working directory to start in
#[arg(value_name = "PATH", index = 2)] #[arg(value_name = "PATH", index = 2)]
pub working_directory: Option<String>, pub working_directory: Option<String>,
@ -97,6 +101,7 @@ pub struct PostProcessedCli {
pub tick_rate: Option<f64>, pub tick_rate: Option<f64>,
pub frame_rate: Option<f64>, pub frame_rate: Option<f64>,
pub passthrough_keybindings: Vec<String>, pub passthrough_keybindings: Vec<String>,
pub input: Option<String>,
pub command: Option<Command>, pub command: Option<Command>,
pub working_directory: Option<String>, pub working_directory: Option<String>,
pub autocomplete_prompt: Option<String>, pub autocomplete_prompt: Option<String>,
@ -145,6 +150,7 @@ impl From<Cli> for PostProcessedCli {
tick_rate: cli.tick_rate, tick_rate: cli.tick_rate,
frame_rate: cli.frame_rate, frame_rate: cli.frame_rate,
passthrough_keybindings, passthrough_keybindings,
input: cli.input,
command: cli.command, command: cli.command,
working_directory, working_directory,
autocomplete_prompt: cli.autocomplete_prompt, autocomplete_prompt: cli.autocomplete_prompt,
@ -332,6 +338,7 @@ mod tests {
tick_rate: Some(50.0), tick_rate: Some(50.0),
frame_rate: Some(60.0), frame_rate: Some(60.0),
passthrough_keybindings: Some("q,ctrl-w,ctrl-t".to_string()), passthrough_keybindings: Some("q,ctrl-w,ctrl-t".to_string()),
input: None,
command: None, command: None,
working_directory: Some("/home/user".to_string()), working_directory: Some("/home/user".to_string()),
autocomplete_prompt: None, autocomplete_prompt: None,
@ -372,6 +379,7 @@ mod tests {
tick_rate: Some(50.0), tick_rate: Some(50.0),
frame_rate: Some(60.0), frame_rate: Some(60.0),
passthrough_keybindings: None, passthrough_keybindings: None,
input: None,
command: None, command: None,
working_directory: None, working_directory: None,
autocomplete_prompt: None, autocomplete_prompt: None,

View File

@ -112,6 +112,7 @@ async fn main() -> Result<()> {
}, },
config, config,
&args.passthrough_keybindings, &args.passthrough_keybindings,
args.input,
) { ) {
Ok(mut app) => { Ok(mut app) => {
stdout().flush()?; stdout().flush()?;

View File

@ -11,17 +11,17 @@ pub struct Picker {
impl Default for Picker { impl Default for Picker {
fn default() -> Self { fn default() -> Self {
Self::new() Self::new(None)
} }
} }
impl Picker { impl Picker {
fn new() -> Self { pub fn new(input: Option<String>) -> Self {
Self { Self {
state: ListState::default(), state: ListState::default(),
relative_state: ListState::default(), relative_state: ListState::default(),
inverted: false, inverted: false,
input: Input::new(EMPTY_STRING.to_string()), input: Input::new(input.unwrap_or(EMPTY_STRING.to_string())),
} }
} }

View File

@ -56,11 +56,15 @@ pub struct Television {
impl Television { impl Television {
#[must_use] #[must_use]
pub fn new(mut channel: TelevisionChannel, config: Config) -> Self { pub fn new(
let results_picker = match config.ui.input_bar_position { mut channel: TelevisionChannel,
InputPosition::Bottom => Picker::default().inverted(), config: Config,
InputPosition::Top => Picker::default(), input: Option<String>,
}; ) -> Self {
let mut results_picker = Picker::new(input.clone());
if config.ui.input_bar_position == InputPosition::Bottom {
results_picker = results_picker.inverted();
}
let previewer = Previewer::new(Some(config.previewers.clone().into())); let previewer = Previewer::new(Some(config.previewers.clone().into()));
let keymap = Keymap::from(&config.keybindings); let keymap = Keymap::from(&config.keybindings);
let builtin_channels = load_builtin_channels(); let builtin_channels = load_builtin_channels();
@ -79,7 +83,7 @@ impl Television {
); );
let colorscheme = (&Theme::from_name(&config.ui.theme)).into(); let colorscheme = (&Theme::from_name(&config.ui.theme)).into();
channel.find(EMPTY_STRING); channel.find(&input.unwrap_or(EMPTY_STRING.to_string()));
let spinner = Spinner::default(); let spinner = Spinner::default();
Self { Self {
action_tx: None, action_tx: None,