Skip to content

Commit

Permalink
Fix formatting; avoid str_split_once feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
grunweg committed May 16, 2022
1 parent 5376c13 commit 0f7bbac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/common_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn get_cursor_position(mut out: &Term) -> io::Result<(u16, u16)> {
// We expect a response of the form "ESC[n;mR", where n and m are the row and column of the cursor.
// n and m are at most 65536, so 4+2*5 bytes should suffice for these purposes.
// TODO: this blocks for user input!
let mut buf = [0u8; 4 + 2*5];
let mut buf = [0u8; 4 + 2 * 5];
let num_read = io::Read::read(&mut out, &mut buf)?;
out.clear_chars(num_read)?;
// FIXME: re-use ANSI code parser instead of rolling my own.
Expand All @@ -59,14 +59,19 @@ pub fn get_cursor_position(mut out: &Term) -> io::Result<(u16, u16)> {
Ok(m) => m,
Err(_) => return Err(io::Error::new(io::ErrorKind::Other, format!("invalid terminal response: middle part of the output {:?} must be valid UTF-8", buf))),
};
let (nstr, mstr) = match middle.split_once(';') {
Some((nstr, mstr)) => (nstr, mstr),
None => return Err(io::Error::new(io::ErrorKind::Other, format!("invalid terminal response: middle part of the output should be of the form n;m, got {}", middle))),
let parts = middle.splitn(2, ';').collect::<Vec<_>>();
let (nstr, mstr) =
match &parts[..] {
[a, b] => (a, b),
_ => return Err(io::Error::new(io::ErrorKind::Other, format!("invalid terminal response: middle part of the output should be of the form n;m, got {}", middle))),
};
let (n, m) = (nstr.parse::<u16>().unwrap(), mstr.parse::<u16>().unwrap());
Ok((n, m))
},
_ => return Err(io::Error::new(io::ErrorKind::Other, "invalid terminal response: should be of the form ESC[n;mR")),
}
_ => Err(io::Error::new(
io::ErrorKind::Other,
"invalid terminal response: should be of the form ESC[n;mR",
)),
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,10 @@ impl Term {
let (current_row, _) = get_cursor_position(self)?;
if usize::from(current_row) < n {
// We cannot move up n lines, only current_row ones.
return Err(io::Error::new(io::ErrorKind::Other, format!("can only move up {} lines, not {}", current_row, n)));
return Err(io::Error::new(
io::ErrorKind::Other,
format!("can only move up {} lines, not {}", current_row, n),
));
}
}
self.move_cursor_up(n)?;
Expand Down

0 comments on commit 0f7bbac

Please sign in to comment.