Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Char boundary string splitting error #32

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Bugfix: Char boundary string splitting error

## [0.4.0 - 2024-08-27]

- Fixed no_std examples build failure by checking in lock files
Expand Down
43 changes: 35 additions & 8 deletions noline/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,21 +236,24 @@ impl<'a> Step<'a> {
match self {
Print(s) => {
let columns_remaining = terminal.columns_remaining();
let split_at_char = columns_remaining.min(s.chars().count());
let split_at_byte = s
.char_indices()
.nth(split_at_char)
.map(|(index, _)| index)
.unwrap_or(s.len());

let (s, rest) = s.split_at(columns_remaining.min(s.len()));
let (s, rest) = s.split_at(split_at_byte);

#[cfg(test)]
dbg!(s, rest);

let position = terminal.relative_position(s.chars().count() as isize);
terminal.move_cursor(position);

let step = if s.len() == columns_remaining {
let step = if split_at_char == columns_remaining {
Step::NewlinePrint(rest)
} else {
Step::Done
};

let position = terminal.relative_position(split_at_char as isize);
terminal.move_cursor(position);

self.transition(step, OutputItem::Slice(s.as_bytes()))
}
NewlinePrint(s) => {
Expand Down Expand Up @@ -734,4 +737,28 @@ mod tests {
assert_eq!(result, "\x1b[1;3H");
assert_eq!(terminal.get_cursor(), Cursor::new(0, 2));
}

#[test]
fn split_utf8() {
fn to_string(mut step: Step, terminal: &mut Terminal) -> String {
let mut bytes = Vec::new();

while let Some(item) = step.advance(terminal) {
if let Some(slice) = item.get_bytes() {
for b in slice {
bytes.push(*b);
}
}
}

String::from_utf8(bytes).unwrap()
}

let mut terminal = Terminal::new(4, 10, Cursor::new(0, 0));

assert_eq!(
to_string(Step::Print("aadfåpadfåaåfåaadåappaåadå"), &mut terminal),
"aadfåpadfå\n\raåfåaadåap\n\rpaåadå"
);
}
}
Loading