hold + better display

you can do `8:30 d :20 2s :4 6 2 3w :6 2 3 6 2 3 6s` now
This commit is contained in:
mehbark 2025-12-29 03:20:35 -05:00
parent e0e6b68a5b
commit 9d3c6c2495
3 changed files with 45 additions and 24 deletions

View file

@ -17,17 +17,17 @@ pub enum Dir {
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum DirFromStrError { pub enum FromStrError {
Empty, Empty,
InvalidChar(char), InvalidChar(char),
} }
impl FromStr for Dir { impl FromStr for Dir {
type Err = DirFromStrError; type Err = FromStrError;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let Some(first) = s.chars().next() else { let Some(first) = s.chars().next() else {
return Err(DirFromStrError::Empty); return Err(FromStrError::Empty);
}; };
Ok(match first { Ok(match first {
'1' => Self::D1, '1' => Self::D1,
@ -39,7 +39,7 @@ impl FromStr for Dir {
'7' => Self::D7, '7' => Self::D7,
'8' => Self::D8, '8' => Self::D8,
'9' => Self::D9, '9' => Self::D9,
c => return Err(DirFromStrError::InvalidChar(c)), c => return Err(FromStrError::InvalidChar(c)),
}) })
} }
} }

View file

@ -1,4 +1,4 @@
use std::{collections::HashSet, fmt, str::FromStr}; use std::{collections::HashSet, fmt, num::ParseIntError, str::FromStr};
use enigo::{Enigo, InputResult, Key, Keyboard}; use enigo::{Enigo, InputResult, Key, Keyboard};
@ -13,35 +13,56 @@ pub struct Input {
} }
#[derive(Debug)] #[derive(Debug)]
pub enum InputFromStrError {} pub enum FromStrError {
HoldTimeParseError(ParseIntError),
}
impl From<ParseIntError> for FromStrError {
fn from(value: <u32 as FromStr>::Err) -> Self {
Self::HoldTimeParseError(value)
}
}
impl FromStr for Input { impl FromStr for Input {
type Err = InputFromStrError; type Err = FromStrError;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Some(Ok(dir)) = s.chars().next().map(|c| String::from(c).parse()) { let (s, held_frames) = if let Some((s, held_frames)) = s.split_once(':') {
Ok(Self { (s, held_frames.parse()?)
dir,
keys: s.chars().skip(1).collect(),
held_frames: 0,
})
} else { } else {
Ok(Self { (s, 0)
dir: Dir::default(), };
keys: s.chars().collect(),
held_frames: 0, let (dir, keys) = if let Some(Ok(dir)) = s.chars().next().map(|c| String::from(c).parse()) {
}) (dir, s.chars().skip(1).collect())
} } else {
(Dir::default(), s.chars().collect())
};
Ok(Self {
dir,
keys,
held_frames,
})
} }
} }
impl fmt::Display for Input { impl fmt::Display for Input {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut keys: Vec<_> = self.keys.iter().collect(); let mut keys: Vec<_> = self.keys.iter().copied().collect();
keys.sort_unstable(); keys.sort_unstable();
write!(f, "{}", self.dir)?;
for key in keys { if self.dir == Dir::default() {
write!(f, "{key}")?; write!(f, " ")?;
} else {
write!(f, "{}", self.dir)?;
}
write!(f, "{:3}", keys.into_iter().collect::<String>())?;
if self.held_frames > 0 {
write!(f, ":{:<2}", self.held_frames)?;
} else {
write!(f, " ")?;
} }
Ok(()) Ok(())
} }

View file

@ -16,7 +16,7 @@ fn main() -> Result<(), Box<dyn Error>> {
for input in inputs.windows(2) { for input in inputs.windows(2) {
if let [a, b] = input { if let [a, b] = input {
println!("Release: {a}, Press: {b}"); println!("{:>8}", format!("{b}"));
a.release(&mut enigo)?; a.release(&mut enigo)?;
b.press(&mut enigo)?; b.press(&mut enigo)?;
thread::sleep(FRAME * b.held_frames); thread::sleep(FRAME * b.held_frames);