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

Add an option similar to -c for resetting the terminal history #141

Closed
ghost opened this issue Feb 27, 2020 · 6 comments
Closed

Add an option similar to -c for resetting the terminal history #141

ghost opened this issue Feb 27, 2020 · 6 comments

Comments

@ghost
Copy link

ghost commented Feb 27, 2020

The -c option clears the terminal, but doesn't reset the historical buffer. So running

cargo watch -c -s 'cargo check'

allows to scroll up to the previous checks, while

cargo watch -s clear -s 'cargo check'

completely removes the history.

It is useful to have an ability to remove the history before each check if the output of cargo check doesn't fit into a single window. In that case scrolling up "as much us possible" always shows the beginning of the output of the most recent check, but not of past ones.

@passcod
Copy link
Member

passcod commented Feb 27, 2020

  1. Option would be -C ("strong clear") or -cc and -ccc (see below).
  2. Should this call ESC c (full reset) or CSI n 3 (scrollback reset)? Alternatively, should both be accessible, one with -ccc (full) and the other with -cc (scrollback)?
  3. What should this do on Windows?

@ghost
Copy link
Author

ghost commented Feb 28, 2020

Should this call ESC c (full reset) or CSI n 3 (scrollback reset)? Alternatively, should both be accessible, one with -ccc (full) and the other with -cc (scrollback)?

In tmux where I run it only CSI 3 J deletes the history buffer. It is interesting that clear sends <esc>[H<esc>[2J<esc>[3J:

$ clear | xxd
00000000: 1b5b 481b 5b32 4a1b 5b33 4a              .[H.[2J.[3J

What should this do on Windows?

On Windows it works only in an alternative buffer (CSI ? 1049 h). But that fact made me think that it would be actually more correct to always create an alternative buffer when cargo watch starts end exit from it when it exits, so that after exit from cargo watch it would be possible to scroll the history that happened before running cargo watch, similar to how for example vim clears the screen when starts, but after exiting from it the history is available.

So I think to make it cross-platform and not damage terminal history before starting cargo watch it should be like this:

  1. Print \x1b[?1049h on start of cargo-watch.
  2. Print \x1b[H\x1b[2J\x1b[3J on each modification of the files.
  3. Print \x1b[?1049l before exit from cargo-watch.

Not sure about naming of the command line parameters though. With the buffers approach it might even make sense to just replace -c flag to the described above to not confuse the users by having multiple flags with similar functionality, but there could be reasons to have both as well.

@passcod
Copy link
Member

passcod commented Feb 28, 2020

I like this! I like it enough that I think I want to make it default.

How about this:

  • default is changed to new reset behaviour (as this is breaking, bump to v8)
  • -c, --clear does nothing ("soft" deprecation until removal in v9)
  • --no-clear flag disables clearing
  • --no-reset flag or similar can be introduced later if old clearing behaviour is wanted (unlikely)

(In upstream, new behaviour is added as a new feature (non-breaking), and will be considered separately for addition to its cli flags.)

@ghost
Copy link
Author

ghost commented Feb 29, 2020

I experimented with the alternate buffer using a simple wrapper shell script

#!/bin/bash

trap "printf '\x1b[?1049l'" EXIT
printf '\x1b[?1049h'
cargo watch -s clear -s 'cargo check'

However, there turns out to be a huge issue with alternate buffers: the scrolling doesn't work in the alternate buffer, so if the output of the watched command is larger than one terminal screen, then its beginning becomes completely lost. And implementing scrolling in cargo-watch itself is possible (for example, using tui-rs), but is a much large task than printing a few escape sequences.

So for now maybe it makes more sense to rather continue with the approach from #141 (comment):

  1. -c works as it works now.
  2. -cc clears screen using \x1b[H\x1b[2J\x1b[3J.

@passcod
Copy link
Member

passcod commented Mar 1, 2020

Sounds good. I'll shelve the more advanced behaviour for the ever-upcoming cargo-overwatch :)

@passcod
Copy link
Member

passcod commented Apr 11, 2021

Closing in favour of upstream issue: watchexec/watchexec#171.

@passcod passcod closed this as completed Apr 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant