title: Is there any way to set the left and right margin of a console using Terminal sequences? the same way we can currently set Top and Bottom?
body: <p><strong>some background,</strong> </p>
  
  <p>I'm the author of a C# console library called Konsole (link below). </p>
  
  <p><a href="https://github.com/goblinfactory/konsole" rel="nofollow noreferrer">https://github.com/goblinfactory/konsole</a></p>
  
  <p>The primary feature of the library is to provide a simple DSL for creating scrollable console windows like so ...</p>
  
  <p><a href="https://i.stack.imgur.com/iKvxl.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iKvxl.jpg" alt="enter image description here"></a></p>
  
  <p>This works really well on windows, but currently does not work on 'nix' because the <code>.NET Core</code> port of <code>Console</code> is incomplete. Yeah, it's incomplete Sobs! if you call <code>Console.MoveBufferArea(...)</code>, you'll receive a stern <code>PlatformNotSupported</code> rebuking, like so ...</p>
  
  <pre><code>Unhandled exception. System.PlatformNotSupportedException: Operation is not supported on this platform.
     at System.ConsolePal.MoveBufferArea(Int32 sourceLeft, Int32 sourceTop, Int32 sourceWidth, Int32 sourceHeight, Int32 targetLeft, Int32 targetTop, Char sourceChar, ConsoleColor sourceForeColor, ConsoleColor sourceBackColor)
     at System.Console.MoveBufferArea(Int32 sourceLeft, Int32 sourceTop, Int32 sourceWidth, Int32 sourceHeight, Int32 targetLeft, Int32 targetTop)
  </code></pre>
  
  <p>I've managed to get my library to work around everything except the MoveBufferArea method, and almost have a nice hack work around using <code>Virtual-terminal-sequences</code>, hence this SO question.</p>
  
  <p>...when running on Mac in a terminal window, I can scroll a (100% wide) region of the window, by setting the Scrolling Region using <a href="https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#viewport-positioning" rel="nofollow noreferrer"><strong>terminal-sequences</strong></a>, but can only set the top and bottom margin, using <code>virtual-terminal-sequences.</code></p>
  
  <p>from : <a href="https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#viewport-positioning" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#viewport-positioning</a></p>
  
  <p>So, using terminal sequences I can set the Top and Bottom margin, then I can call scroll, then I reset the scroll regions and return to my library for all other window writing.</p>
  
  <p>The problem is (as per the title) that I can not find any way to set the left and right margin, which means I cannot scroll a neat (startx, startY, width, height) portion of the screen, I am forced to scroll the whole width. </p>
  
  <p>This feels SOOO close. if I can find a hack to tell a Mac (Nix) window to please scroll a region of a console window, or, I can set the Left and Right margin before calling scrollup, or ScrollDown, then that would also solve me my problem and my Konsole library would be fully cross platform.</p>
  
  <p>This can apparently be done using something called <code>ncurses</code> but all the documentation I have found for that appear to require that you do some wierd mind meld compile time magic to get them to work.</p>
  
  <p>Any help would be amazing and super super appreciated!</p>
  
  <p>here's some code that shows how the terminal sequences are used</p>
  
  <pre class="lang-cs prettyprint-override"><code>        private static void SetScrollMargins(int top, int bottom) =&gt; Console.Write("\x1b[" + top + ";" + bottom + "r");
          private static void ScrollUp() =&gt; Console.Write("\x1b[1S");
          private static void ScrollDown() =&gt; Console.Write("\x1b[1T");
  
          static void Main(string[] args)
          {
              try
              {
              Console.Clear();
  
              // fill the screen with random stuff so that we can see
              // that a small portion (region) of the screen is moved up and down
              // with each call to scroll up/down without the rest of the screen 
              // being impacted. Demonstrates that all I am missing is left and right margins
              // and my problem is solved. Sooo close, but sooo far!
  
              for (int i = 0; i &lt; 800; i++) Console.Write($" ({i} ) ");
              Console.WriteLine();
              Console.WriteLine("set scrollmargins, press a key");
              Console.ReadKey(true);
              SetScrollMargins(2, 5);
              for (int i = 0; i &lt; 20; i++)
              {
  
                  Console.ReadKey(true);
                  ScrollUp();
                  Console.ReadKey(true);
                  ScrollDown();
              }
          }
  </code></pre>
  
  <p>So ideally I'm hoping I've missed an obvious trick where you can in fact set the left and right margin, using terminal-sequences, or via some other terminal sequence code that Mac Terminals support. </p>
  
  <p>Thanks to everyone in advance. Happy new year too :D</p>
  
  <p>update:</p>
  
  <p>I managed to find some tests that hint at the functionality being plausible. e.g. <code>DECLRMM</code> (<a href="https://vt100.net/docs/vt510-rm/DECLRMM.html" rel="nofollow noreferrer">https://vt100.net/docs/vt510-rm/DECLRMM.html</a>)  </p>
  
  <p><a href="https://www.codota.com/code/java/methods/com.termux.terminal.ScrollRegionTest/withTerminalSized" rel="nofollow noreferrer">This Java test for example</a> </p>
  
  <pre class="lang-java prettyprint-override"><code>public void testScrollRegionRight() {
    // ${CSI}?69h for DECLRMM enabling, ${CSI}${LEFTMARGIN};${RIGHTMARGIN}s for DECSLRM margin setting.
    withTerminalSized(3, 3).enterString("YYY\033[?69h\033[1;2sABCDEF").assertLinesAre("ABY", "CD ", "EF ");
    enterString("GH").assertLinesAre("CDY", "EF ", "GH ").enterString("IJ").assertLinesAre("EFY", "GH ", "IJ ");
    enterString("\n").assertLinesAre("GHY", "IJ ", "   ");
  }
  </code></pre>
  
  <p>I've tried this, but can't get this to work, seems to be ignored, and possibly this open happens with <code>terminal.app</code> on Mac, as  @Thomas Dickey has suggested in the comments. </p>