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 {
#[serde(rename = "top")]
Top,

View File

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

View File

@ -44,6 +44,10 @@ pub struct Cli {
#[arg(long, value_name = "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
#[arg(value_name = "PATH", index = 2)]
pub working_directory: Option<String>,
@ -97,6 +101,7 @@ pub struct PostProcessedCli {
pub tick_rate: Option<f64>,
pub frame_rate: Option<f64>,
pub passthrough_keybindings: Vec<String>,
pub input: Option<String>,
pub command: Option<Command>,
pub working_directory: Option<String>,
pub autocomplete_prompt: Option<String>,
@ -145,6 +150,7 @@ impl From<Cli> for PostProcessedCli {
tick_rate: cli.tick_rate,
frame_rate: cli.frame_rate,
passthrough_keybindings,
input: cli.input,
command: cli.command,
working_directory,
autocomplete_prompt: cli.autocomplete_prompt,
@ -332,6 +338,7 @@ mod tests {
tick_rate: Some(50.0),
frame_rate: Some(60.0),
passthrough_keybindings: Some("q,ctrl-w,ctrl-t".to_string()),
input: None,
command: None,
working_directory: Some("/home/user".to_string()),
autocomplete_prompt: None,
@ -372,6 +379,7 @@ mod tests {
tick_rate: Some(50.0),
frame_rate: Some(60.0),
passthrough_keybindings: None,
input: None,
command: None,
working_directory: None,
autocomplete_prompt: None,

View File

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

View File

@ -11,17 +11,17 @@ pub struct Picker {
impl Default for Picker {
fn default() -> Self {
Self::new()
Self::new(None)
}
}
impl Picker {
fn new() -> Self {
pub fn new(input: Option<String>) -> Self {
Self {
state: ListState::default(),
relative_state: ListState::default(),
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 {
#[must_use]
pub fn new(mut channel: TelevisionChannel, config: Config) -> Self {
let results_picker = match config.ui.input_bar_position {
InputPosition::Bottom => Picker::default().inverted(),
InputPosition::Top => Picker::default(),
};
pub fn new(
mut channel: TelevisionChannel,
config: Config,
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 keymap = Keymap::from(&config.keybindings);
let builtin_channels = load_builtin_channels();
@ -79,7 +83,7 @@ impl Television {
);
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();
Self {
action_tx: None,