From 74be0eaef060f80875754b71fc0123682493500b Mon Sep 17 00:00:00 2001 From: Azalea Date: Sat, 17 Oct 2026 09:01:39 +0000 Subject: [PATCH] [+] Alignment - Make random0 sequential before re-roll when preset matches slot count (#543) --- crates/hyfetch/src/bin/hyfetch.rs | 42 +++++++++++++++++++++++-------- hyfetch/main.py | 25 +++++++++++++----- 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/crates/hyfetch/src/bin/hyfetch.rs b/crates/hyfetch/src/bin/hyfetch.rs index 805696a6..d634c007 100644 --- a/crates/hyfetch/src/bin/hyfetch.rs +++ b/crates/hyfetch/src/bin/hyfetch.rs @@ -1405,6 +1405,7 @@ fn create_config( // Loop for random rolling let mut rng = fastrand::Rng::new(); + let mut is_first = true; loop { clear_screen(Some(&title), color_mode, debug_mode).context("failed to clear screen")?; @@ -1420,18 +1421,36 @@ fn create_config( .take(1000) .collect(); let random_count = (ascii_per_row as usize * ascii_rows as usize).saturating_sub(arrangements.len()).clamp(0, u8::MAX.into()) as u8; - let other_permutations: IndexSet> = preset_index_permutations - .into_iter() - .filter(|p| p != &ordered_p) - .collect(); - let mut choices: IndexSet> = IndexSet::new(); - choices.insert(ordered_p); - let sample_count = (usize::from(random_count)).saturating_sub(1); - if sample_count >= other_permutations.len() { - choices.extend(other_permutations); + + let is_sequential = is_first && color_profile.unique_colors().colors.len() == slots.len(); + let choices: IndexSet> = if random_count == 0 { + IndexSet::new() + } else if is_sequential { + let other_permutations: IndexSet> = preset_index_permutations + .into_iter() + .filter(|p| p != &ordered_p) + .collect(); + let mut choices: IndexSet> = IndexSet::new(); + choices.insert(ordered_p); + let sample_count = usize::from(random_count) - 1; + if sample_count >= other_permutations.len() { + choices.extend(other_permutations); + } else { + choices.extend(rng.choose_multiple(other_permutations.into_iter(), sample_count)); + } + choices } else { - choices.extend(rng.choose_multiple(other_permutations.into_iter(), sample_count)); - } + if usize::from(random_count) >= preset_index_permutations.len() { + preset_index_permutations + } else { + rng.choose_multiple( + preset_index_permutations.into_iter(), + usize::from(random_count), + ) + .into_iter() + .collect() + } + }; let choices: Vec> = choices .into_iter() .map(|c| { @@ -1482,6 +1501,7 @@ fn create_config( if choice == "roll" { arrangements.clear(); + is_first = false; continue; } diff --git a/hyfetch/main.py b/hyfetch/main.py index 5c03c50f..1852f0dd 100755 --- a/hyfetch/main.py +++ b/hyfetch/main.py @@ -545,6 +545,7 @@ def create_config(path: Path | None = None) -> Config: arrangements = hv_arrangements.copy() # Loop for random rolling + is_first = True while True: clear_screen(title) @@ -554,14 +555,25 @@ def create_config(path: Path | None = None) -> Config: while len(pis) < len(slots): pis += pis perm = {p[:len(slots)] for p in islice(permutations(pis), 1000)} - ordered_p = tuple(pis[:len(slots)]) - other_perms = [p for p in perm if p != ordered_p] random_count = max(0, ascii_per_row * ascii_rows - len(arrangements)) - if random_count > len(perm): - choices = [ordered_p] + other_perms + + is_sequential = is_first and len(_prs.unique_colors().colors) == len(slots) + if random_count == 0: + choices = [] + elif is_sequential: + ordered_p = tuple(pis[:len(slots)]) + other_perms = [p for p in perm if p != ordered_p] + sample_count = random_count - 1 + if sample_count >= len(other_perms): + choices = [ordered_p] + other_perms + else: + choices = [ordered_p] + random.sample(sorted(other_perms), min(sample_count, len(other_perms))) else: - sample_count = max(0, random_count - 1) - choices = [ordered_p] + random.sample(sorted(other_perms), min(sample_count, len(other_perms))) + if random_count >= len(perm): + choices = list(perm) + else: + choices = random.sample(sorted(perm), random_count) + choices = [{slots[i]: n for i, n in enumerate(c)} for c in choices] arrangements += [(f'random{i}', ColorAlignment('custom', r)) for i, r in enumerate(choices)] asciis = [[*ca.recolor_ascii(asc, _prs).split('\n'), k.center(asc_width)] for k, ca in arrangements] @@ -581,6 +593,7 @@ def create_config(path: Path | None = None) -> Config: if choice == 'roll': arrangements = [] + is_first = False continue # Save choice