Skip to content

Clear the rest of line after drawing to the terminal#790

Merged
djc merged 1 commit intoconsole-rs:mainfrom
ChrisDenton:clear-eol
Apr 14, 2026
Merged

Clear the rest of line after drawing to the terminal#790
djc merged 1 commit intoconsole-rs:mainfrom
ChrisDenton:clear-eol

Conversation

@ChrisDenton
Copy link
Copy Markdown
Contributor

@ChrisDenton ChrisDenton commented Apr 12, 2026

EDIT: Updated to avoid changing the TermLike trait.

This will help fix rust-lang/rustup#4808

Copy link
Copy Markdown
Member

@djc djc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a default implementation to keep this semver-compatible?

@ChrisDenton
Copy link
Copy Markdown
Contributor Author

Sure. Although the only compatible default would be a no-op, which may be slightly surprising.

@djc
Copy link
Copy Markdown
Member

djc commented Apr 12, 2026

Sure. Although the only compatible default would be a no-op, which may be slightly surprising.

What would the behavior be for downstream impls that get the default impl?

@ChrisDenton
Copy link
Copy Markdown
Contributor Author

ChrisDenton commented Apr 13, 2026

The behaviour for downstream impls that use the default will be the same as in the current version (which doesn't clear the end of the line). The only difference is if they make use of the TermLike trait themselves and then only if they use the new method and expect it to work.

Btw, in the future I would love it if move_cursor was set to true by default (or always be true and remove the option) but that would likely warrant a breaking version change (and some confidence that we've found all the bugs).

@djc
Copy link
Copy Markdown
Member

djc commented Apr 13, 2026

The behaviour for downstream impls that use the default will be the same as in the current version (which doesn't clear the end of the line). The only difference is if they make use of the TermLike trait themselves and then only if they use the new method and expect it to work.

Yes, I meant, how would downstreams that have their own TermLike impl would be impacted?

Btw, in the future I would love it if move_cursor was set to true by default (or always be true and remove the option) but that would likely warrant a breaking version change (and some confidence that we've found all the bugs).

See discussion here: #759 (comment). In my mind the cost to doing semver-incompatible releases in this crate is relatively high, so if at all possible I'd really prefer to improve it without making semver-incompatible changes.

@ChrisDenton
Copy link
Copy Markdown
Contributor Author

Yes, I meant, how would downstreams that have their own TermLike impl would be impacted?

The only place this crate makes use of the implementation is in this PR where the default no-op implementation would be the same as the old behaviour. I.e. if move_cursor is set to true they will not have this bug fix but it will otherwise work.

Comment thread src/term_like.rs Outdated
}

fn clear_to_end_of_line(&self) -> io::Result<()> {
self.clear_chars(0)
Copy link
Copy Markdown
Member

@djc djc Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this the right thing to be doing? clear_chars() is documented to "[c]lear the last n characters of the current line", why does passing 0 here have the desired effect? Maybe add a comment to explain this a little?

@ChrisDenton
Copy link
Copy Markdown
Contributor Author

ChrisDenton commented Apr 14, 2026

After thinking about this some more, and considering the desire to not have a breaking release, I think this PR should be changed to do this unconditionally:

let last_line_filler = line_height.as_usize() * term_width - line.console_width();
term.write_str(&" ".repeat(last_line_filler))?;

That would entirely avoid the need for changing TermLike. It does move the cursor to the end but that's fine so long as we also avoid writing a new line character on the next write. So remove this:

if idx != 0 {
term.write_line("")?;
}

@djc
Copy link
Copy Markdown
Member

djc commented Apr 14, 2026

Sounds good!

@ChrisDenton
Copy link
Copy Markdown
Contributor Author

Ok, done and also tested via rustup's use of this crate.

@ChrisDenton ChrisDenton changed the title When drawing to the terminal, clear the rest of line when move_cursor is true Clear the rest of line after drawing to the terminal Apr 14, 2026
@djc djc merged commit dd5ff26 into console-rs:main Apr 14, 2026
11 checks passed
@djc
Copy link
Copy Markdown
Member

djc commented Apr 14, 2026

Good stuff, thanks!

@ChrisDenton ChrisDenton deleted the clear-eol branch April 15, 2026 02:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Progress bar UI corruption with .set_move_cursor()

2 participants