github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/cmds/core/elvish/edit/tty/setup.go (about)

     1  package tty
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/u-root/u-root/cmds/core/elvish/sys"
     8  	"github.com/u-root/u-root/cmds/core/elvish/util"
     9  )
    10  
    11  // Setup sets up the terminal so that it is suitable for the Reader and
    12  // Writer to use. It returns a function that can be used to restore the
    13  // original terminal config.
    14  func Setup(in, out *os.File) (func() error, error) {
    15  	return setup(in, out)
    16  }
    17  
    18  const (
    19  	lackEOLRune    = '\u23ce'
    20  	lackEOL        = "\033[7m" + string(lackEOLRune) + "\033[m"
    21  	enableSGRMouse = false
    22  )
    23  
    24  // setupVT performs setup for VT-like terminals.
    25  func setupVT(out *os.File) error {
    26  	_, width := sys.GetWinsize(out)
    27  
    28  	s := ""
    29  	/*
    30  		Write a lackEOLRune if the cursor is not in the leftmost column. This is
    31  		done as follows:
    32  
    33  		1. Turn on autowrap;
    34  
    35  		2. Write lackEOL along with enough padding, so that the total width is
    36  		   equal to the width of the screen.
    37  
    38  		   If the cursor was in the first column, we are still in the same line,
    39  		   just off the line boundary. Otherwise, we are now in the next line.
    40  
    41  		3. Rewind to the first column, write one space and rewind again. If the
    42  		   cursor was in the first column to start with, we have just erased the
    43  		   LackEOL character. Otherwise, we are now in the next line and this is
    44  		   a no-op. The LackEOL character remains.
    45  	*/
    46  	s += fmt.Sprintf("\033[?7h%s%*s\r \r", lackEOL, width-util.Wcwidth(lackEOLRune), "")
    47  
    48  	/*
    49  		Turn off autowrap.
    50  
    51  		The terminals sometimes has different opinions about how wide some
    52  		characters are (notably emojis and some dingbats) with elvish. When that
    53  		happens, elvish becomes wrong about where the cursor is when it writes
    54  		its output, and the effect can be disastrous.
    55  
    56  		If we turn off autowrap, the terminal won't insert any newlines behind
    57  		the scene, so elvish is always right about which line the cursor is.
    58  		With a bit more caution, this can restrict the consequence of the
    59  		mismatch within one line.
    60  	*/
    61  	s += "\033[?7l"
    62  
    63  	// Turn on SGR-style mouse tracking.
    64  	if enableSGRMouse {
    65  		s += "\033[?1000;1006h"
    66  	}
    67  
    68  	// Enable bracketed paste.
    69  	s += "\033[?2004h"
    70  
    71  	_, err := out.WriteString(s)
    72  	return err
    73  }
    74  
    75  // restoreVT performs restore for VT-like terminals.
    76  func restoreVT(out *os.File) error {
    77  	s := ""
    78  	// Turn on autowrap.
    79  	s += "\033[?7h"
    80  	// Turn off mouse tracking.
    81  	if enableSGRMouse {
    82  		s += "\033[?1000;1006l"
    83  	}
    84  	// Disable bracketed paste.
    85  	s += "\033[?2004l"
    86  	// Move the cursor to the first row, even if we haven't written anything
    87  	// visible. This is because the terminal driver might not be smart enough to
    88  	// recognize some escape sequences as invisible and wrongly assume that we
    89  	// are not in the first column, which can mess up with tabs. See
    90  	// https://github.com/elves/elvish/issues/629 for an example.
    91  	s += "\r"
    92  	_, err := out.WriteString(s)
    93  	return err
    94  }