2

I wrote a program to simulate twitch chat where everything is randomly generated.

To be more convincing I'd like to have the top-most line show a twitch url.

I know that for the last line I could use \r, but is there a method to do something similar for the first one?

Or more generally on line number x

Akshat Zala
  • 710
  • 1
  • 8
  • 23
iaquobe
  • 555
  • 1
  • 6
  • 23
  • 4
    I think you are looking for ncurses (https://en.wikipedia.org/wiki/Ncurses), https://docs.python.org/3/howto/curses.html – tbrugere Jul 17 '20 at 13:56
  • I kinda heard of it. But it seems pretty complicated and a bit overkill for my 40 line script. There's really no easier way? – iaquobe Jul 17 '20 at 13:59
  • 2
    It's not overkill: standard output is a *stream*, with no concept of where things appear on screen. (It might not even *be* a screen; you could be writing directly to a file on disk.) If you want to target the *terminal* precisely, you need a library that does that. – chepner Jul 17 '20 at 14:00
  • 1
    @jaklh: If you want to control the terminal, you have to control the terminal. There's no shortcuts to generalized "write to line number x", sorry. You could use a third-party library like `prompt_toolkit` to make a more portable solution (`curses` being a *NIX-thing), but it's not going to reduce complexity. – ShadowRanger Jul 17 '20 at 14:01
  • 1
    I don’t think there is any easier way (irc in a standard terminal, without curses or such, you can only write linearly on the last line (that is because of historical reasons)) – tbrugere Jul 17 '20 at 14:01
  • 1
    Even `\r` is just a byte, which your terminal *might* happen to "display" by moving the cursor rather than showing a particular glyph. – chepner Jul 17 '20 at 14:01
  • Writing to first-line without always keeping it there (if you don't want to use a library like curses), you need to continuely clear the screen and reprint your message (redraw the screen), to make sure your first line stays there. For that you cn use clear screen sequence https://stackoverflow.com/a/2084628/1573477 and just print your URL first. – Shahin Jul 17 '20 at 14:03
  • That said, there are [other ANSI escape sequences](https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences) you can use that the terminal will interpret similarly to `\r`, moving the cursor to specific location, if your terminal supports them. The entire purpose of `curses`, though, is to abstract this away from your terminal's particular implementation. – chepner Jul 17 '20 at 14:03
  • @Shahinism Wouldn’t that flicker a lot ? – tbrugere Jul 17 '20 at 14:03
  • I don't think so, there are some other libraries out there which use the same approach. (The curses itself uses it as well I guess) – Shahin Jul 17 '20 at 14:05
  • @chepner ah ok, I get it. `\r` is just an oddity but generally there is no way to change what has been printed. I guess I'm checking out ncurses then – iaquobe Jul 17 '20 at 14:07
  • 1
    Even with `\r`, a terminal doesn't have to erase anything. It's perfectly legal to display `S\r|` as something resembling a `$`, rather than `|` alone. – chepner Jul 17 '20 at 14:10
  • @chepner So is there any reason to use `\r` if the behavior is not even defined? – iaquobe Jul 17 '20 at 14:16
  • 1
    It depends on how willing you are to *assume* your terminal will do what you want. I'm just trying to make you aware of what *can* happen. – chepner Jul 17 '20 at 14:19

0 Answers0