[O] Shorten

This commit is contained in:
Azalea 2025-10-02 02:11:58 +08:00
parent 666d2dc90a
commit 15ca855a04
4 changed files with 26 additions and 83 deletions

View file

@ -61,8 +61,8 @@ impl RawAsciiArt {
Ok(NormalizedAsciiArt { Ok(NormalizedAsciiArt {
lines, lines,
w, w: w.try_into().context("width does not fit in u8")?,
h, h: h.try_into().context("height does not fit in u8")?,
fg: self.fg.clone(), fg: self.fg.clone(),
}) })
} }

View file

@ -429,8 +429,7 @@ fn create_config(
TerminalTheme::Dark.as_ref(), TerminalTheme::Dark.as_ref(),
true, true,
color_mode, color_mode,
) )?;
.context("failed to ask for choice input")?;
Ok(( Ok((
choice.parse().expect("selected theme should be valid"), choice.parse().expect("selected theme should be valid"),
"Selected background color", "Selected background color",

View file

@ -281,7 +281,7 @@ pub fn run(asc: RecoloredAsciiArt, backend: Backend, args: Option<&Vec<String>>)
} }
/// Gets distro ascii width and height, ignoring color code. /// Gets distro ascii width and height, ignoring color code.
pub fn ascii_size<S>(asc: S) -> Result<(u8, u8)> pub fn ascii_size<S>(asc: S) -> Result<(u16, u16)>
where where
S: AsRef<str>, S: AsRef<str>,
{ {
@ -303,25 +303,11 @@ where
return Ok((0, 0)); return Ok((0, 0));
} }
let width = asc let width = asc.lines()
.lines() .map(|line| line.graphemes(true).count()).max()
.map(|line| line.graphemes(true).count())
.max()
.expect("line iterator should not be empty"); .expect("line iterator should not be empty");
let width: u8 = width.try_into().with_context(|| { let width: u16 = width.try_into().context("ascii art width should fit in u16")?;
format!( let height: u16 = asc.lines().count().try_into().context("ascii art height should fit in u16")?;
"`asc` should not have more than {limit} characters per line",
limit = u8::MAX
)
})?;
let height = asc.lines().count();
let height: u8 = height.try_into().with_context(|| {
format!(
"`asc` should not have more than {limit} lines",
limit = u8::MAX
)
})?;
Ok((width, height)) Ok((width, height))
} }

View file

@ -47,58 +47,32 @@ pub fn start_animation(color_mode: AnsiMode) -> Result<()> {
}; };
let text = &TEXT_ASCII[1..TEXT_ASCII.len().checked_sub(1).unwrap()]; let text = &TEXT_ASCII[1..TEXT_ASCII.len().checked_sub(1).unwrap()];
let (text_width, text_height) = let (text_w, text_h) = ascii_size(text)?;
ascii_size(text).expect("text ascii should have valid width and height");
let (text, text_width, text_height) = { let (text, text_width, text_height) = {
const TEXT_BORDER_WIDTH: u16 = 2; const TEXT_BORDER_WIDTH: u16 = 2;
const NOTICE_BORDER_WIDTH: u16 = 1; const NOTICE_BORDER_WIDTH: u16 = 1;
const VERTICAL_MARGIN: u16 = 1; const VERTICAL_MARGIN: u16 = 1;
let notice_w = NOTICE.len(); let notice_w: u16 = NOTICE.len().try_into()?;
let notice_w: u8 = notice_w let notice_h: u16 = NOTICE.lines().count().try_into()?;
.try_into()
.expect("`NOTICE` width should fit in `u8`");
let notice_h = NOTICE.lines().count();
let notice_h: u8 = notice_h
.try_into()
.expect("`NOTICE` height should fit in `u8`");
let term_w_min = cmp::max( let term_w_min = cmp::max(
u16::from(text_width) text_w + TEXT_BORDER_WIDTH * 2,
.checked_add(TEXT_BORDER_WIDTH.checked_mul(2).unwrap()) notice_w + NOTICE_BORDER_WIDTH * 2,
.unwrap(),
u16::from(notice_w)
.checked_add(NOTICE_BORDER_WIDTH.checked_mul(2).unwrap())
.unwrap(),
); );
let term_h_min = u16::from(text_height) let term_h_min = u16::from(text_h) + notice_h + VERTICAL_MARGIN * 2;
.checked_add(notice_h.into())
.unwrap()
.checked_add(VERTICAL_MARGIN.checked_mul(2).unwrap())
.unwrap();
if w.get() >= term_w_min && h.get() >= term_h_min { if w.get() >= term_w_min && h.get() >= term_h_min {
(text, text_width, text_height) (text, text_w, text_h)
} else { } else {
let text = &TEXT_ASCII_SMALL[1..TEXT_ASCII_SMALL.len().checked_sub(1).unwrap()]; let text = &TEXT_ASCII_SMALL[1..TEXT_ASCII_SMALL.len().checked_sub(1).unwrap()];
let (text_width, text_height) = let (text_w, text_h) = ascii_size(text)?;
ascii_size(text).expect("text ascii should have valid width and height");
let term_w_min = cmp::max( let term_w_min = cmp::max(
u16::from(text_width) text_w + TEXT_BORDER_WIDTH * 2,
.checked_add(TEXT_BORDER_WIDTH.checked_mul(2).unwrap()) notice_w + NOTICE_BORDER_WIDTH * 2,
.unwrap(),
u16::from(notice_w)
.checked_add(NOTICE_BORDER_WIDTH.checked_mul(2).unwrap())
.unwrap(),
); );
let term_h_min = u16::from(text_height) let term_h_min = text_h + notice_h + VERTICAL_MARGIN * 2;
.checked_add(notice_h.into())
.unwrap()
.checked_add(VERTICAL_MARGIN.checked_mul(2).unwrap())
.unwrap();
if w.get() < term_w_min || h.get() < term_h_min { if w.get() < term_w_min || h.get() < term_h_min {
return Err(anyhow!( return Err(anyhow!("terminal size should be at least ({term_w_min} * {term_h_min})"));
"terminal size should be at least ({term_w_min} * {term_h_min})"
));
} }
(text, text_width, text_height) (text, text_w, text_h)
} }
}; };
let text_lines: Vec<&str> = text.lines().collect(); let text_lines: Vec<&str> = text.lines().collect();
@ -175,27 +149,11 @@ pub fn start_animation(color_mode: AnsiMode) -> Result<()> {
.wrapping_add_signed((2.0 * (y as f64 + 0.5 * frame as f64).sin()) as isize); .wrapping_add_signed((2.0 * (y as f64 + 0.5 * frame as f64).sin()) as isize);
let y_text = text_start_y <= y && y < text_end_y; let y_text = text_start_y <= y && y < text_end_y;
let border = 1u16 let border = 1u16 + if y == text_start_y || y == (text_end_y - 1) { 0 } else { 1 };
.checked_add( let text_bounds_x1 = text_start_x - border;
if y == text_start_y || y == text_end_y.checked_sub(1).unwrap() { let text_bounds_x2 = text_end_x - border;
0 let notice_bounds_x1 = notice_start_x - 1;
} else { let notice_bounds_x2 = notice_end_x - 1;
1
},
)
.unwrap();
let text_bounds_x1 = text_start_x
.checked_sub(border)
.expect("`text_start_x - border` should not underflow `u16`");
let text_bounds_x2 = text_end_x
.checked_add(border)
.expect("`text_end_x + border` should not overflow `u16`");
let notice_bounds_x1 = notice_start_x
.checked_sub(1)
.expect("`notice_start_x - 1` should not underflow `u16`");
let notice_bounds_x2 = notice_end_x
.checked_add(1)
.expect("`notice_end_x + 1` should not overflow `u16`");
// If it's a switching point // If it's a switching point
if idx.rem_euclid(NonZeroUsize::from(block_width).get()) == 0 if idx.rem_euclid(NonZeroUsize::from(block_width).get()) == 0