Source File
setup.go
Belonging Package
src.elv.sh/pkg/cli/term
package termimport ()// Setup sets up the terminal so that it is suitable for the Reader and// Writer to use. It returns a function that can be used to restore the// original terminal config.func (, *os.File) (func() error, error) {return setup(, )}// SetupGlobal sets up the terminal for the entire Elvish session.func () func() {return setupGlobal()}// Sanitize sanitizes the terminal after an external command has executed.func (, *os.File) {sanitize(, )}const (lackEOLRune = '\u23ce'lackEOL = "\033[7m" + string(lackEOLRune) + "\033[m"enableSGRMouse = false)// setupVT performs setup for VT-like terminals.func ( *os.File) error {, := sys.GetWinsize():= ""/*Write a lackEOLRune if the cursor is not in the leftmost column. This isdone as follows:1. Turn on autowrap;2. Write lackEOL along with enough padding, so that the total width isequal to the width of the screen.If the cursor was in the first column, we are still in the same line,just off the line boundary. Otherwise, we are now in the next line.3. Rewind to the first column, write one space and rewind again. If thecursor was in the first column to start with, we have just erased theLackEOL character. Otherwise, we are now in the next line and this isa no-op. The LackEOL character remains.*/+= fmt.Sprintf("\033[?7h%s%*s\r \r", lackEOL, -wcwidth.OfRune(lackEOLRune), "")/*Turn off autowrap.The terminals sometimes has different opinions about how wide somecharacters are (notably emojis and some dingbats) with elvish. When thathappens, elvish becomes wrong about where the cursor is when it writesits output, and the effect can be disastrous.If we turn off autowrap, the terminal won't insert any newlines behindthe scene, so elvish is always right about which line the cursor is.With a bit more caution, this can restrict the consequence of themismatch within one line.*/+= "\033[?7l"// Turn on SGR-style mouse tracking.if enableSGRMouse {+= "\033[?1000;1006h"}// Enable bracketed paste.+= "\033[?2004h", := .WriteString()return}// restoreVT performs restore for VT-like terminals.func ( *os.File) error {:= ""// Turn on autowrap.+= "\033[?7h"// Turn off mouse tracking.if enableSGRMouse {+= "\033[?1000;1006l"}// Disable bracketed paste.+= "\033[?2004l"// Move the cursor to the first row, even if we haven't written anything// visible. This is because the terminal driver might not be smart enough to// recognize some escape sequences as invisible and wrongly assume that we// are not in the first column, which can mess up with tabs. See// https://src.elv.sh/pkg/issues/629 for an example.+= "\r", := .WriteString()return}
The pages are generated with Golds v0.2.8-preview. (GOOS=darwin GOARCH=arm64)