Clearing a line when terminal window is narrowed

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















Consider the following code (written in PHP, but that's not important. Answers may use pure Bash). The goal is to fill a terminal line with characters for the entire width of the line.



Now, if you minimise the terminal width during execution, excess characters from the previous time have to be cleared out to avoid a mess.



However, then, the previously echoed line will have been split into multiple lines, and echoing r will return the cursor to the last of those lines, rather than the first, leaving behind garbage.



Is there any simple cure to this? The only thing I can think of is echoing the ANSI code for "CURSOR UP" as many times as the line was broken (minus one).



#!/usr/bin/env php
<?php
// Make sure window is 80 characters wide when running script
echo "00000000001111111111222222222233333333334444444444555555555566666666667777777777";
sleep(3); // Narrow window here (to 40-50 characters or so)
echo "33[2K"; // CLEAR LINE
echo "33[A"; // CURSOR UP
echo "33[2K"; // CLEAR LINE
echo "r"; // GOTO BEGINNING OF LINE
echo "Now we're back were we begun (where 000... used to be)";

?>









share|improve this question




























    0















    Consider the following code (written in PHP, but that's not important. Answers may use pure Bash). The goal is to fill a terminal line with characters for the entire width of the line.



    Now, if you minimise the terminal width during execution, excess characters from the previous time have to be cleared out to avoid a mess.



    However, then, the previously echoed line will have been split into multiple lines, and echoing r will return the cursor to the last of those lines, rather than the first, leaving behind garbage.



    Is there any simple cure to this? The only thing I can think of is echoing the ANSI code for "CURSOR UP" as many times as the line was broken (minus one).



    #!/usr/bin/env php
    <?php
    // Make sure window is 80 characters wide when running script
    echo "00000000001111111111222222222233333333334444444444555555555566666666667777777777";
    sleep(3); // Narrow window here (to 40-50 characters or so)
    echo "33[2K"; // CLEAR LINE
    echo "33[A"; // CURSOR UP
    echo "33[2K"; // CLEAR LINE
    echo "r"; // GOTO BEGINNING OF LINE
    echo "Now we're back were we begun (where 000... used to be)";

    ?>









    share|improve this question


























      0












      0








      0








      Consider the following code (written in PHP, but that's not important. Answers may use pure Bash). The goal is to fill a terminal line with characters for the entire width of the line.



      Now, if you minimise the terminal width during execution, excess characters from the previous time have to be cleared out to avoid a mess.



      However, then, the previously echoed line will have been split into multiple lines, and echoing r will return the cursor to the last of those lines, rather than the first, leaving behind garbage.



      Is there any simple cure to this? The only thing I can think of is echoing the ANSI code for "CURSOR UP" as many times as the line was broken (minus one).



      #!/usr/bin/env php
      <?php
      // Make sure window is 80 characters wide when running script
      echo "00000000001111111111222222222233333333334444444444555555555566666666667777777777";
      sleep(3); // Narrow window here (to 40-50 characters or so)
      echo "33[2K"; // CLEAR LINE
      echo "33[A"; // CURSOR UP
      echo "33[2K"; // CLEAR LINE
      echo "r"; // GOTO BEGINNING OF LINE
      echo "Now we're back were we begun (where 000... used to be)";

      ?>









      share|improve this question
















      Consider the following code (written in PHP, but that's not important. Answers may use pure Bash). The goal is to fill a terminal line with characters for the entire width of the line.



      Now, if you minimise the terminal width during execution, excess characters from the previous time have to be cleared out to avoid a mess.



      However, then, the previously echoed line will have been split into multiple lines, and echoing r will return the cursor to the last of those lines, rather than the first, leaving behind garbage.



      Is there any simple cure to this? The only thing I can think of is echoing the ANSI code for "CURSOR UP" as many times as the line was broken (minus one).



      #!/usr/bin/env php
      <?php
      // Make sure window is 80 characters wide when running script
      echo "00000000001111111111222222222233333333334444444444555555555566666666667777777777";
      sleep(3); // Narrow window here (to 40-50 characters or so)
      echo "33[2K"; // CLEAR LINE
      echo "33[A"; // CURSOR UP
      echo "33[2K"; // CLEAR LINE
      echo "r"; // GOTO BEGINNING OF LINE
      echo "Now we're back were we begun (where 000... used to be)";

      ?>






      terminal ansi






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 26 at 8:56







      forthrin

















      asked Feb 22 at 13:13









      forthrinforthrin

      9331122




      9331122




















          1 Answer
          1






          active

          oldest

          votes


















          0














          Note that the "traditional" behavior of terminal emulators is to chop long lines when the window is made narrower, while some terminals rewrap their contents on resize. Your question is relevant for the latter kind only.



          The "erase in display" escape sequence (command: tput ed, sequence: e[J) clears everything from the cursor (inclusive) to the end of the display. This is probably the simplest choice.



          Note however that after printing into the last column of a row, the cursor is still logically in that last column (even though some emulators hide it or display it elsewhere), so this escape sequence clears that character in this case. In order to make sure you don't wipe out the last character of your message, you should probably emit it while the cursor is still in the first column, before printing the new message.






          share|improve this answer























          • See updated post. This code works, but I'm just wondering if doing CURSOR UP is required. (From your comment, the answer seems to be "Yes", since my terminal rewraps the content.)

            – forthrin
            Feb 26 at 8:32











          • Well, if you emit one cursor up, the code will work only with emulators that rewrap their contents, and only if the width was narrowed but not more than to the half of the previous. Of course you can do the math of how many ups are required based on the previous width, but it'll still only work with rewrapping emulators if you move the cursor back after the delay.

            – egmont
            Feb 26 at 12:11











          • So CURSOR UP is the only way with wrapping terminals? Is there any way you can detect if the terminal is wrapping or not?

            – forthrin
            Feb 26 at 16:02











          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2funix.stackexchange.com%2fquestions%2f502295%2fclearing-a-line-when-terminal-window-is-narrowed%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Note that the "traditional" behavior of terminal emulators is to chop long lines when the window is made narrower, while some terminals rewrap their contents on resize. Your question is relevant for the latter kind only.



          The "erase in display" escape sequence (command: tput ed, sequence: e[J) clears everything from the cursor (inclusive) to the end of the display. This is probably the simplest choice.



          Note however that after printing into the last column of a row, the cursor is still logically in that last column (even though some emulators hide it or display it elsewhere), so this escape sequence clears that character in this case. In order to make sure you don't wipe out the last character of your message, you should probably emit it while the cursor is still in the first column, before printing the new message.






          share|improve this answer























          • See updated post. This code works, but I'm just wondering if doing CURSOR UP is required. (From your comment, the answer seems to be "Yes", since my terminal rewraps the content.)

            – forthrin
            Feb 26 at 8:32











          • Well, if you emit one cursor up, the code will work only with emulators that rewrap their contents, and only if the width was narrowed but not more than to the half of the previous. Of course you can do the math of how many ups are required based on the previous width, but it'll still only work with rewrapping emulators if you move the cursor back after the delay.

            – egmont
            Feb 26 at 12:11











          • So CURSOR UP is the only way with wrapping terminals? Is there any way you can detect if the terminal is wrapping or not?

            – forthrin
            Feb 26 at 16:02
















          0














          Note that the "traditional" behavior of terminal emulators is to chop long lines when the window is made narrower, while some terminals rewrap their contents on resize. Your question is relevant for the latter kind only.



          The "erase in display" escape sequence (command: tput ed, sequence: e[J) clears everything from the cursor (inclusive) to the end of the display. This is probably the simplest choice.



          Note however that after printing into the last column of a row, the cursor is still logically in that last column (even though some emulators hide it or display it elsewhere), so this escape sequence clears that character in this case. In order to make sure you don't wipe out the last character of your message, you should probably emit it while the cursor is still in the first column, before printing the new message.






          share|improve this answer























          • See updated post. This code works, but I'm just wondering if doing CURSOR UP is required. (From your comment, the answer seems to be "Yes", since my terminal rewraps the content.)

            – forthrin
            Feb 26 at 8:32











          • Well, if you emit one cursor up, the code will work only with emulators that rewrap their contents, and only if the width was narrowed but not more than to the half of the previous. Of course you can do the math of how many ups are required based on the previous width, but it'll still only work with rewrapping emulators if you move the cursor back after the delay.

            – egmont
            Feb 26 at 12:11











          • So CURSOR UP is the only way with wrapping terminals? Is there any way you can detect if the terminal is wrapping or not?

            – forthrin
            Feb 26 at 16:02














          0












          0








          0







          Note that the "traditional" behavior of terminal emulators is to chop long lines when the window is made narrower, while some terminals rewrap their contents on resize. Your question is relevant for the latter kind only.



          The "erase in display" escape sequence (command: tput ed, sequence: e[J) clears everything from the cursor (inclusive) to the end of the display. This is probably the simplest choice.



          Note however that after printing into the last column of a row, the cursor is still logically in that last column (even though some emulators hide it or display it elsewhere), so this escape sequence clears that character in this case. In order to make sure you don't wipe out the last character of your message, you should probably emit it while the cursor is still in the first column, before printing the new message.






          share|improve this answer













          Note that the "traditional" behavior of terminal emulators is to chop long lines when the window is made narrower, while some terminals rewrap their contents on resize. Your question is relevant for the latter kind only.



          The "erase in display" escape sequence (command: tput ed, sequence: e[J) clears everything from the cursor (inclusive) to the end of the display. This is probably the simplest choice.



          Note however that after printing into the last column of a row, the cursor is still logically in that last column (even though some emulators hide it or display it elsewhere), so this escape sequence clears that character in this case. In order to make sure you don't wipe out the last character of your message, you should probably emit it while the cursor is still in the first column, before printing the new message.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 22 at 22:06









          egmontegmont

          2,6821912




          2,6821912












          • See updated post. This code works, but I'm just wondering if doing CURSOR UP is required. (From your comment, the answer seems to be "Yes", since my terminal rewraps the content.)

            – forthrin
            Feb 26 at 8:32











          • Well, if you emit one cursor up, the code will work only with emulators that rewrap their contents, and only if the width was narrowed but not more than to the half of the previous. Of course you can do the math of how many ups are required based on the previous width, but it'll still only work with rewrapping emulators if you move the cursor back after the delay.

            – egmont
            Feb 26 at 12:11











          • So CURSOR UP is the only way with wrapping terminals? Is there any way you can detect if the terminal is wrapping or not?

            – forthrin
            Feb 26 at 16:02


















          • See updated post. This code works, but I'm just wondering if doing CURSOR UP is required. (From your comment, the answer seems to be "Yes", since my terminal rewraps the content.)

            – forthrin
            Feb 26 at 8:32











          • Well, if you emit one cursor up, the code will work only with emulators that rewrap their contents, and only if the width was narrowed but not more than to the half of the previous. Of course you can do the math of how many ups are required based on the previous width, but it'll still only work with rewrapping emulators if you move the cursor back after the delay.

            – egmont
            Feb 26 at 12:11











          • So CURSOR UP is the only way with wrapping terminals? Is there any way you can detect if the terminal is wrapping or not?

            – forthrin
            Feb 26 at 16:02

















          See updated post. This code works, but I'm just wondering if doing CURSOR UP is required. (From your comment, the answer seems to be "Yes", since my terminal rewraps the content.)

          – forthrin
          Feb 26 at 8:32





          See updated post. This code works, but I'm just wondering if doing CURSOR UP is required. (From your comment, the answer seems to be "Yes", since my terminal rewraps the content.)

          – forthrin
          Feb 26 at 8:32













          Well, if you emit one cursor up, the code will work only with emulators that rewrap their contents, and only if the width was narrowed but not more than to the half of the previous. Of course you can do the math of how many ups are required based on the previous width, but it'll still only work with rewrapping emulators if you move the cursor back after the delay.

          – egmont
          Feb 26 at 12:11





          Well, if you emit one cursor up, the code will work only with emulators that rewrap their contents, and only if the width was narrowed but not more than to the half of the previous. Of course you can do the math of how many ups are required based on the previous width, but it'll still only work with rewrapping emulators if you move the cursor back after the delay.

          – egmont
          Feb 26 at 12:11













          So CURSOR UP is the only way with wrapping terminals? Is there any way you can detect if the terminal is wrapping or not?

          – forthrin
          Feb 26 at 16:02






          So CURSOR UP is the only way with wrapping terminals? Is there any way you can detect if the terminal is wrapping or not?

          – forthrin
          Feb 26 at 16:02


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • 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%2funix.stackexchange.com%2fquestions%2f502295%2fclearing-a-line-when-terminal-window-is-narrowed%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

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?