diff options
| author | Omar Magdy <99906646+omagdy7@users.noreply.github.com> | 2022-10-23 23:04:16 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-23 23:04:16 +0200 |
| commit | f22324451891c0af32d6a0e3ca913670072205e9 (patch) | |
| tree | 0de8f3c3b1c5b35b012dc683d66371a0e0f02d4f | |
| parent | 0cae5089cb84478060794cadbbfc924d4b9ae9cd (diff) | |
| parent | c012264363d026e93c7683c079095d452c3eee80 (diff) | |
| download | gof-rs-f22324451891c0af32d6a0e3ca913670072205e9.tar.xz gof-rs-f22324451891c0af32d6a0e3ca913670072205e9.zip | |
Merge pull request #1 from pbspbsingh/master
Replace hardcoded file names with reading directory
| -rw-r--r-- | src/ui.rs | 49 |
1 files changed, 30 insertions, 19 deletions
@@ -21,7 +21,6 @@ use tui::{ use crate::generation::*; - pub struct StatefulList<T> { state: ListState, items: Vec<T>, @@ -83,14 +82,29 @@ pub struct App { impl App { pub fn new() -> App { - let mut v = vec![]; - for i in 1..=513 { - let name = format!("pattern{}", i); - let p = format!("./presets/patterns/pattern{}.txt", i); - v.push((name.to_owned(), Path::new(p.as_str()).to_owned())); + fn read_presets() -> io::Result<Vec<(String, PathBuf)>> { + let mut result = Vec::new(); + for path in std::fs::read_dir("./presets/patterns")? { + let path = path?.path(); + if !path.is_file() { + continue; + } + if let Some(file_name) = path.file_name() { + let file_name = file_name.to_string_lossy(); + if file_name.starts_with("pattern") && file_name.ends_with(".txt") { + result.push((file_name.trim_end_matches(".txt").to_owned(), path)); + } + } + } + result.sort_by_key(|(name, _)| { + name.trim_start_matches("pattern") + .parse::<u32>() + .unwrap_or(0) + }); + Ok(result) } App { - items: StatefulList::with_items(v), + items: StatefulList::with_items(read_presets().unwrap_or_else(|_| Vec::new())), flag_cur: false, layout: Layout::default() .direction(Direction::Horizontal) @@ -100,10 +114,7 @@ impl App { } } -pub fn run_app<B: Backend>( - terminal: &mut Terminal<B>, - mut app: App, -) -> io::Result<()> { +pub fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<()> { let mut last_tick = Instant::now(); let mut item_cnt = 1; loop { @@ -121,19 +132,16 @@ pub fn run_app<B: Backend>( item_cnt += 1; } app.items.next(); - app.cur_gen = - gen_from_file(&app.items.items[item_cnt].1); + app.cur_gen = gen_from_file(&app.items.items[item_cnt].1); } KeyCode::Up | KeyCode::Char('k') => { if item_cnt == 0 { item_cnt = app.items.items.len() - 1; - } - else { + } else { item_cnt -= 1; } app.items.previous(); - app.cur_gen = - gen_from_file(&app.items.items[item_cnt].1); + app.cur_gen = gen_from_file(&app.items.items[item_cnt].1); } KeyCode::Char('n') => { terminal.draw(|f| ui_game(f, &mut app))?; @@ -203,7 +211,11 @@ fn ui_game<B: Backend>(f: &mut Frame<B>, app: &mut App) { // Create a List from all list items and highlight the currently selected one let items = List::new(items) - .block(Block::default().borders(Borders::ALL).title("Cool Patterns")) + .block( + Block::default() + .borders(Borders::ALL) + .title("Cool Patterns"), + ) .highlight_style( Style::default() .bg(Color::Blue) @@ -213,7 +225,6 @@ fn ui_game<B: Backend>(f: &mut Frame<B>, app: &mut App) { f.render_stateful_widget(items, chunks[0], &mut app.items.state); - let nxt = next_gen(app); let spans = gen_to_spans(&nxt); render_gen(f, chunks[1], &spans) |
