Clearing beyond top of screen with ansi control sequences










0















I'm attempting to build a simple terminal UI that displays a string that is updated every second (somewhat like watch -n 1 <command> would do but without doing a full screen refresh) and I'm running into a problem when the content to be refreshed is larger than the terminal emulator screen. I feel like it should be solvable without using curses but I can't figure it out.



The program I've written is pretty simple, the logic works something like this (this is pseudocode in python, the actual script is in Haskell but that shouldn't change the problem).



ansi_clearline = "33[2K"
ansi_upline = "33[1A"

printed_value = fetch_value() # String formatted for display to the terminal
print(printed_value)
while True:
num_lines = len(printed_value.split("n"))
control_chars = ansi_upline.join(num_lines * [ansi_clearline])
printed_value = fetch_value()
print(control_chars)
print(intial_value)


That is, for every line in the last output we print a control sequence to clear the line, then a control sequence to move up a line, except for the first line where we only print a clear line.



This works fine as long as all the content fits into the terminal emulator screen. Unfortunately it breaks if the output (printed_value) is longer than the emulator screen. Presumably this is because the ansi control sequences don't know anything about moving into the scroll back buffer and don't let you move up one line once you're at the top of the screen. What this looks like is the following:



lineone 
linetwo
------top of screen
linethree
linefour
linefive
-------Bottom of screen

Scroll up a bit with the mouseo

lineone
linetwo
------top of screen
lineone
linetwo
lineone
-------Bottom of screen
linetwo
linethree
linefour
linefive


Hopefully that graphic makes clear what is happening. Is there any way to achieve what I want to do (effectively using control sequences to clear part of the scrollback buffer) without using curses?










share|improve this question


























    0















    I'm attempting to build a simple terminal UI that displays a string that is updated every second (somewhat like watch -n 1 <command> would do but without doing a full screen refresh) and I'm running into a problem when the content to be refreshed is larger than the terminal emulator screen. I feel like it should be solvable without using curses but I can't figure it out.



    The program I've written is pretty simple, the logic works something like this (this is pseudocode in python, the actual script is in Haskell but that shouldn't change the problem).



    ansi_clearline = "33[2K"
    ansi_upline = "33[1A"

    printed_value = fetch_value() # String formatted for display to the terminal
    print(printed_value)
    while True:
    num_lines = len(printed_value.split("n"))
    control_chars = ansi_upline.join(num_lines * [ansi_clearline])
    printed_value = fetch_value()
    print(control_chars)
    print(intial_value)


    That is, for every line in the last output we print a control sequence to clear the line, then a control sequence to move up a line, except for the first line where we only print a clear line.



    This works fine as long as all the content fits into the terminal emulator screen. Unfortunately it breaks if the output (printed_value) is longer than the emulator screen. Presumably this is because the ansi control sequences don't know anything about moving into the scroll back buffer and don't let you move up one line once you're at the top of the screen. What this looks like is the following:



    lineone 
    linetwo
    ------top of screen
    linethree
    linefour
    linefive
    -------Bottom of screen

    Scroll up a bit with the mouseo

    lineone
    linetwo
    ------top of screen
    lineone
    linetwo
    lineone
    -------Bottom of screen
    linetwo
    linethree
    linefour
    linefive


    Hopefully that graphic makes clear what is happening. Is there any way to achieve what I want to do (effectively using control sequences to clear part of the scrollback buffer) without using curses?










    share|improve this question
























      0












      0








      0








      I'm attempting to build a simple terminal UI that displays a string that is updated every second (somewhat like watch -n 1 <command> would do but without doing a full screen refresh) and I'm running into a problem when the content to be refreshed is larger than the terminal emulator screen. I feel like it should be solvable without using curses but I can't figure it out.



      The program I've written is pretty simple, the logic works something like this (this is pseudocode in python, the actual script is in Haskell but that shouldn't change the problem).



      ansi_clearline = "33[2K"
      ansi_upline = "33[1A"

      printed_value = fetch_value() # String formatted for display to the terminal
      print(printed_value)
      while True:
      num_lines = len(printed_value.split("n"))
      control_chars = ansi_upline.join(num_lines * [ansi_clearline])
      printed_value = fetch_value()
      print(control_chars)
      print(intial_value)


      That is, for every line in the last output we print a control sequence to clear the line, then a control sequence to move up a line, except for the first line where we only print a clear line.



      This works fine as long as all the content fits into the terminal emulator screen. Unfortunately it breaks if the output (printed_value) is longer than the emulator screen. Presumably this is because the ansi control sequences don't know anything about moving into the scroll back buffer and don't let you move up one line once you're at the top of the screen. What this looks like is the following:



      lineone 
      linetwo
      ------top of screen
      linethree
      linefour
      linefive
      -------Bottom of screen

      Scroll up a bit with the mouseo

      lineone
      linetwo
      ------top of screen
      lineone
      linetwo
      lineone
      -------Bottom of screen
      linetwo
      linethree
      linefour
      linefive


      Hopefully that graphic makes clear what is happening. Is there any way to achieve what I want to do (effectively using control sequences to clear part of the scrollback buffer) without using curses?










      share|improve this question














      I'm attempting to build a simple terminal UI that displays a string that is updated every second (somewhat like watch -n 1 <command> would do but without doing a full screen refresh) and I'm running into a problem when the content to be refreshed is larger than the terminal emulator screen. I feel like it should be solvable without using curses but I can't figure it out.



      The program I've written is pretty simple, the logic works something like this (this is pseudocode in python, the actual script is in Haskell but that shouldn't change the problem).



      ansi_clearline = "33[2K"
      ansi_upline = "33[1A"

      printed_value = fetch_value() # String formatted for display to the terminal
      print(printed_value)
      while True:
      num_lines = len(printed_value.split("n"))
      control_chars = ansi_upline.join(num_lines * [ansi_clearline])
      printed_value = fetch_value()
      print(control_chars)
      print(intial_value)


      That is, for every line in the last output we print a control sequence to clear the line, then a control sequence to move up a line, except for the first line where we only print a clear line.



      This works fine as long as all the content fits into the terminal emulator screen. Unfortunately it breaks if the output (printed_value) is longer than the emulator screen. Presumably this is because the ansi control sequences don't know anything about moving into the scroll back buffer and don't let you move up one line once you're at the top of the screen. What this looks like is the following:



      lineone 
      linetwo
      ------top of screen
      linethree
      linefour
      linefive
      -------Bottom of screen

      Scroll up a bit with the mouseo

      lineone
      linetwo
      ------top of screen
      lineone
      linetwo
      lineone
      -------Bottom of screen
      linetwo
      linethree
      linefour
      linefive


      Hopefully that graphic makes clear what is happening. Is there any way to achieve what I want to do (effectively using control sequences to clear part of the scrollback buffer) without using curses?







      terminal






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 9:55









      Alex jgAlex jg

      540520




      540520






















          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297364%2fclearing-beyond-top-of-screen-with-ansi-control-sequences%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297364%2fclearing-beyond-top-of-screen-with-ansi-control-sequences%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo