Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 82 additions & 2 deletions crates/fastled-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,20 @@ fn detect_local_fastled_path() -> Option<String> {
project::find_fastled_repo_upwards(&cwd, 10).map(|path| canonical_display_path(&path))
}

enum PromptChoice {
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PromptChoice {
Selected(String),
Narrowed(Vec<String>),
Retry,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SketchSelection {
Selected(String),
Prompt(Vec<String>),
None,
}

fn option_basename(option: &str) -> &str {
Path::new(option)
.file_name()
Expand Down Expand Up @@ -561,7 +569,11 @@ fn fuzzy_match_options(input: &str, options: &[String]) -> Vec<String> {
results
}

fn resolve_prompt_choice(input: &str, options: &[String], default_index: usize) -> PromptChoice {
pub fn resolve_prompt_choice(
input: &str,
options: &[String],
default_index: usize,
) -> PromptChoice {
let trimmed = input.trim();
if trimmed.is_empty() {
return PromptChoice::Selected(options[default_index].clone());
Expand Down Expand Up @@ -600,6 +612,31 @@ fn resolve_prompt_choice(input: &str, options: &[String], default_index: usize)
}
}

pub fn prepare_sketch_selection(
mut sketch_directories: Vec<PathBuf>,
cwd_is_fastled: bool,
is_followup: bool,
) -> SketchSelection {
if cwd_is_fastled {
sketch_directories.retain(|path| {
let text = path.to_string_lossy().replace('\\', "/");
!matches!(text.as_str(), "src" | "dev" | "tests")
});
}

match sketch_directories.len() {
0 => SketchSelection::None,
1 => SketchSelection::Selected(display_path(&sketch_directories[0])),
_ if !is_followup && sketch_directories.len() > 4 => SketchSelection::None,
_ => SketchSelection::Prompt(
sketch_directories
.iter()
.map(|path| path.to_string_lossy().into_owned())
.collect(),
),
}
}

fn prompt_for_choice(
options: &[String],
prompt: &str,
Expand Down Expand Up @@ -1269,4 +1306,47 @@ mod tests {
_ => panic!("expected fuzzy selection"),
}
}

#[test]
fn prepare_sketch_selection_auto_selects_single_directory() {
let result = prepare_sketch_selection(vec![PathBuf::from("sketch1")], false, false);
assert_eq!(result, SketchSelection::Selected("sketch1".to_string()));
}

#[test]
fn prepare_sketch_selection_defers_large_first_scan() {
let sketches = (0..5)
.map(|index| PathBuf::from(format!("sketch{index}")))
.collect();
let result = prepare_sketch_selection(sketches, false, false);
assert_eq!(result, SketchSelection::None);
}

#[test]
fn prepare_sketch_selection_prompts_large_followup_scan() {
let sketches: Vec<PathBuf> = (0..5)
.map(|index| PathBuf::from(format!("sketch{index}")))
.collect();
let result = prepare_sketch_selection(sketches, false, true);
assert_eq!(
result,
SketchSelection::Prompt(
(0..5)
.map(|index| format!("sketch{index}"))
.collect::<Vec<_>>()
)
);
}

#[test]
fn prepare_sketch_selection_filters_fastled_repo_dirs() {
let sketches = vec![
PathBuf::from("src"),
PathBuf::from("dev"),
PathBuf::from("tests"),
PathBuf::from("examples"),
];
let result = prepare_sketch_selection(sketches, true, false);
assert_eq!(result, SketchSelection::Selected("examples".to_string()));
}
}
52 changes: 52 additions & 0 deletions crates/fastled-py/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use fastled_cli::install;
use fastled_cli::project;
use fastled_cli::{PromptChoice, SketchSelection};
use pyo3::exceptions::{PyRuntimeError, PyValueError};
use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyDict, PyModule};
Expand Down Expand Up @@ -865,6 +866,55 @@ fn find_sketch_by_partial_name(partial_name: &str, search_dir: Option<&str>) ->
.map_err(|err| PyValueError::new_err(err.to_string()))
}

fn selection_payload(selection: SketchSelection) -> (String, Option<String>, Vec<String>) {
match selection {
SketchSelection::Selected(value) => ("selected".to_string(), Some(value), Vec::new()),
SketchSelection::Prompt(options) => ("prompt".to_string(), None, options),
SketchSelection::None => ("none".to_string(), None, Vec::new()),
}
}

fn prompt_choice_payload(choice: PromptChoice) -> (String, Option<String>, Vec<String>) {
match choice {
PromptChoice::Selected(value) => ("selected".to_string(), Some(value), Vec::new()),
PromptChoice::Narrowed(options) => ("narrowed".to_string(), None, options),
PromptChoice::Retry => ("retry".to_string(), None, Vec::new()),
}
}

#[pyfunction]
fn prepare_sketch_selection(
sketch_directories: Vec<String>,
cwd_is_fastled: bool,
is_followup: bool,
) -> (String, Option<String>, Vec<String>) {
let paths = sketch_directories.into_iter().map(PathBuf::from).collect();
selection_payload(fastled_cli::prepare_sketch_selection(
paths,
cwd_is_fastled,
is_followup,
))
}

#[pyfunction(signature = (input, options, default_index=0))]
fn resolve_prompt_choice(
input: &str,
options: Vec<String>,
default_index: usize,
) -> PyResult<(String, Option<String>, Vec<String>)> {
if options.is_empty() {
return Err(PyValueError::new_err("options must not be empty"));
}
if default_index >= options.len() {
return Err(PyValueError::new_err("default_index out of range"));
}
Ok(prompt_choice_payload(fastled_cli::resolve_prompt_choice(
input,
&options,
default_index,
)))
}

#[pyfunction(signature = (path=None))]
fn looks_like_fastled_repo(path: Option<&str>) -> PyResult<bool> {
let directory = path.map(PathBuf::from).unwrap_or(std::env::current_dir()?);
Expand Down Expand Up @@ -974,6 +1024,8 @@ fn _native(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(project_available, m)?)?;
m.add_function(wrap_pyfunction!(find_sketch_directories, m)?)?;
m.add_function(wrap_pyfunction!(find_sketch_by_partial_name, m)?)?;
m.add_function(wrap_pyfunction!(prepare_sketch_selection, m)?)?;
m.add_function(wrap_pyfunction!(resolve_prompt_choice, m)?)?;
m.add_function(wrap_pyfunction!(looks_like_fastled_repo, m)?)?;
m.add_function(wrap_pyfunction!(looks_like_sketch_directory, m)?)?;
m.add_function(wrap_pyfunction!(find_fastled_repo_upwards, m)?)?;
Expand Down
Loading
Loading