Skip to content

Commit

Permalink
feat(cli): allow passing --input <STRING> to prefill input prompt (#153)
Browse files Browse the repository at this point in the history
Fixes #152
  • Loading branch information
alexpasmantier authored Dec 28, 2024
1 parent c3b8c68 commit 309ff53
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion crates/television-screen/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion crates/television/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions crates/television/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions crates/television/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ async fn main() -> Result<()> {
},
config,
&args.passthrough_keybindings,
args.input,
) {
Ok(mut app) => {
stdout().flush()?;
Expand Down
6 changes: 3 additions & 3 deletions crates/television/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())),
}
}

Expand Down
16 changes: 10 additions & 6 deletions crates/television/television.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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,
Expand Down

0 comments on commit 309ff53

Please sign in to comment.