// Copyright 2025 The TCell Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !(js && wasm)
// +build !js !wasm

package tcell

import (
	
	
	
	
	
	
	
	
	
	
	

	
	

	
)

// NewTerminfoScreen returns a Screen that uses the stock TTY interface
// and POSIX terminal control, combined with a terminfo description taken from
// the $TERM environment variable.  It returns an error if the terminal
// is not supported for any reason.
//
// For terminals that do not support dynamic resize events, the $LINES
// $COLUMNS environment variables can be set to the actual window size,
// otherwise defaults taken from the terminal database are used.
func () (Screen, error) {
	return NewTerminfoScreenFromTty(nil)
}

// LookupTerminfo attempts to find a definition for the named $TERM falling
// back to attempting to parse the output from infocmp.
func ( string) ( *terminfo.Terminfo,  error) {
	,  = terminfo.LookupTerminfo()
	if  != nil {
		,  = loadDynamicTerminfo()
		if  != nil {
			return nil, 
		}
		terminfo.AddTerminfo()
	}

	return
}

var defaultTerm string

// NewTerminfoScreenFromTtyTerminfo returns a Screen using a custom Tty
// implementation  and custom terminfo specification.
// If the passed in tty is nil, then a reasonable default (typically /dev/tty)
// is presumed, at least on UNIX hosts. (Windows hosts will typically fail this
// call altogether.)
// If passed terminfo is nil, then TERM environment variable is queried for
// terminal specification.
func ( Tty,  *terminfo.Terminfo) ( Screen,  error) {
	 := defaultTerm
	if  == "" {
		 = os.Getenv("TERM")
	}
	if  == nil {
		,  = LookupTerminfo()
		if  != nil {
			return nil, 
		}
	}

	 := &tScreen{ti: , tty: }

	if len(.Mouse) > 0 {
		.mouse = []byte(.Mouse)
	}
	.prepareKeys()
	.buildAcsMap()
	.resizeQ = make(chan bool, 1)
	.fallback = make(map[rune]string)
	maps.Copy(.fallback, RuneFallbacks)

	return &baseScreen{screenImpl: }, nil
}

// NewTerminfoScreenFromTty returns a Screen using a custom Tty implementation.
// If the passed in tty is nil, then a reasonable default (typically /dev/tty)
// is presumed, at least on UNIX hosts. (Windows hosts will typically fail this
// call altogether.)
func ( Tty) (Screen, error) {
	return NewTerminfoScreenFromTtyTerminfo(, nil)
}

// tKeyCode represents a combination of a key code and modifiers.
type tKeyCode struct {
	key Key
	mod ModMask
}

// tScreen represents a screen backed by a terminfo implementation.
type tScreen struct {
	ti             *terminfo.Terminfo
	tty            Tty
	h              int
	w              int
	fini           bool
	cells          CellBuffer
	buffering      bool // true if we are collecting writes to buf instead of sending directly to out
	buf            bytes.Buffer
	curstyle       Style
	style          Style
	resizeQ        chan bool
	quit           chan struct{}
	keychan        chan []byte
	cx             int
	cy             int
	mouse          []byte
	clear          bool
	cursorx        int
	cursory        int
	acs            map[rune]string
	charset        string
	encoder        transform.Transformer
	decoder        transform.Transformer
	fallback       map[rune]string
	colors         map[Color]Color
	palette        []Color
	truecolor      bool
	escaped        bool
	buttondn       bool
	finiOnce       sync.Once
	enablePaste    string
	disablePaste   string
	enterUrl       string
	exitUrl        string
	setWinSize     string
	enableFocus    string
	disableFocus   string
	doubleUnder    string
	curlyUnder     string
	dottedUnder    string
	dashedUnder    string
	underColor     string
	underRGB       string
	underFg        string // reset underline color to foreground
	cursorStyles   map[CursorStyle]string
	cursorStyle    CursorStyle
	cursorColor    Color
	cursorRGB      string
	cursorFg       string
	saved          *term.State
	stopQ          chan struct{}
	eventQ         chan Event
	running        bool
	wg             sync.WaitGroup
	mouseFlags     MouseFlags
	pasteEnabled   bool
	focusEnabled   bool
	setTitle       string
	saveTitle      string
	restoreTitle   string
	title          string
	setClipboard   string
	startSyncOut   string
	endSyncOut     string
	enableCsiU     string
	disableCsiU    string
	disableEmojiWA bool // if true don't try to workaround emoji bugs
	input          InputProcessor

	sync.Mutex
}

func ( *tScreen) () error {
	if  := .initialize();  != nil {
		return 
	}

	.keychan = make(chan []byte, 10)

	.charset = getCharset()
	if  := GetEncoding(.charset);  != nil {
		.encoder = .NewEncoder()
		.decoder = .NewDecoder()
	} else {
		return ErrNoCharset
	}
	 := .ti

	// environment overrides
	 := .Columns
	 := .Lines
	if ,  := strconv.Atoi(os.Getenv("LINES"));  != 0 {
		 = 
	}
	if ,  := strconv.Atoi(os.Getenv("COLUMNS"));  != 0 {
		 = 
	}
	if .ti.SetFgBgRGB != "" || .ti.SetFgRGB != "" || .ti.SetBgRGB != "" {
		.truecolor = true
	}
	// A user who wants to have his themes honored can
	// set this environment variable.
	if os.Getenv("TCELL_TRUECOLOR") == "disable" {
		.truecolor = false
	}
	// clip to reasonable limits
	 := min(.nColors(), 256)
	.colors = make(map[Color]Color, )
	.palette = make([]Color, )
	for  := range  {
		.palette[] = Color() | ColorValid
		// identity map for our builtin colors
		.colors[Color()|ColorValid] = Color() | ColorValid
	}

	.quit = make(chan struct{})
	.eventQ = make(chan Event, 256)
	.input = NewInputProcessor(.eventQ)

	.Lock()
	.cx = -1
	.cy = -1
	.style = StyleDefault
	.cells.Resize(, )
	.cursorx = -1
	.cursory = -1
	.resize()
	.Unlock()

	if  := .engage();  != nil {
		return 
	}

	return nil
}

func ( *tScreen) () {
	// Another workaround for lack of reporting in terminfo.
	// We assume if the terminal has a mouse entry, that it
	// offers bracketed paste.  But we allow specific overrides
	// via our terminal database.
	if .ti.Mouse != "" || .ti.XTermLike {
		.enablePaste = "\x1b[?2004h"
		.disablePaste = "\x1b[?2004l"
	}
}

func ( *tScreen) () {
	if .ti.XTermLike {
		.doubleUnder = "\x1b[4:2m"
		.curlyUnder = "\x1b[4:3m"
		.dottedUnder = "\x1b[4:4m"
		.dashedUnder = "\x1b[4:5m"
		.underColor = "\x1b[58:5:%p1%dm"
		.underRGB = "\x1b[58:2::%p1%d:%p2%d:%p3%dm"
		.underFg = "\x1b[59m"
	}
}

func ( *tScreen) () {
	// Linux is a special beast - because it has a mouse entry, but does
	// not swallow these OSC commands properly.
	if strings.Contains(.ti.Name, "linux") {
		return
	}
	// More stuff for limits in terminfo.  This time we are applying
	// the most common OSC (operating system commands).  Generally
	// terminals that don't understand these will ignore them.
	// Again, we condition this based on mouse capabilities.
	if .ti.Mouse != "" || .ti.XTermLike {
		.enterUrl = "\x1b]8;%p2%s;%p1%s\x1b\\"
		.exitUrl = "\x1b]8;;\x1b\\"
	}

	if .ti.Mouse != "" || .ti.XTermLike {
		.setWinSize = "\x1b[8;%p1%p2%d;%dt"
	}

	if .ti.Mouse != "" || .ti.XTermLike {
		.enableFocus = "\x1b[?1004h"
		.disableFocus = "\x1b[?1004l"
	}

	if .ti.XTermLike {
		.saveTitle = "\x1b[22;2t"
		.restoreTitle = "\x1b[23;2t"
		// this also tries to request that UTF-8 is allowed in the title
		.setTitle = "\x1b[>2t\x1b]2;%p1%s\x1b\\"
	}

	if .setClipboard == "" && .ti.XTermLike {
		// this string takes a base64 string and sends it to the clipboard.
		// it will also be able to retrieve the clipboard using "?" as the
		// sent string, when we support that.
		.setClipboard = "\x1b]52;c;%p1%s\x1b\\"
	}

	if .startSyncOut == "" && .ti.XTermLike {
		// this is in theory a queryable private mode, but we just assume it will be ok
		// The terminals we have been able to test it all either just swallow it, or
		// handle it.
		.startSyncOut = "\x1b[?2026h"
		.endSyncOut = "\x1b[?2026l"
	}

	if .enableCsiU == "" && .ti.XTermLike {
		if runtime.GOOS == "windows" && os.Getenv("TERM") == "" {
			// on Windows, if we don't have a TERM, use only win32-input-mode
			.enableCsiU = "\x1b[?9001h"
			.disableCsiU = "\x1b[?9001l"
		} else {
			// three advanced keyboard protocols:
			// - xterm modifyOtherKeys (uses CSI 27 ~ )
			// - kitty csi-u (uses CSI u)
			// - win32-input-mode (uses CSI _)
			.enableCsiU = "\x1b[>4;2m" + "\x1b[>1u" + "\x1b[?9001h"
			.disableCsiU = "\x1b[?9001l" + "\x1b[<u" + "\x1b[>4;0m"
		}
	}
}

func ( *tScreen) () {
	if .ti.Mouse != "" || .ti.XTermLike {
		.cursorStyles = map[CursorStyle]string{
			CursorStyleDefault:           "\x1b[0 q",
			CursorStyleBlinkingBlock:     "\x1b[1 q",
			CursorStyleSteadyBlock:       "\x1b[2 q",
			CursorStyleBlinkingUnderline: "\x1b[3 q",
			CursorStyleSteadyUnderline:   "\x1b[4 q",
			CursorStyleBlinkingBar:       "\x1b[5 q",
			CursorStyleSteadyBar:         "\x1b[6 q",
		}
		if .cursorRGB == "" {
			.cursorRGB = "\x1b]12;#%p1%02x%p2%02x%p3%02x\007"
			.cursorFg = "\x1b]112\007"
		}
	}
}

func ( *tScreen) () {
	 := .ti
	if strings.HasPrefix(.Name, "xterm") {
		// assume its some form of XTerm clone
		.ti.XTermLike = true
		.XTermLike = true
	}
	.prepareBracketedPaste()
	.prepareCursorStyles()
	.prepareUnderlines()
	.prepareExtendedOSC()
}

func ( *tScreen) () {
	.finiOnce.Do(.finish)
}

func ( *tScreen) () {
	close(.quit)
	.finalize()
}

func ( *tScreen) ( Style) {
	.Lock()
	if !.fini {
		.style = 
	}
	.Unlock()
}

func ( *tScreen) ( string) []byte {

	var  [128]byte
	var  []byte
	 := [:]
	 := 0
	var  error
	if  := .encoder;  != nil {
		.Reset()
		, _,  = .Transform(, []byte(), true)
	}
	if  != nil ||  == 0 || [0] == '\x1a' {
		// Combining characters are elided
		,  := utf8.DecodeRuneInString()
		if len() == 0 {
			if ,  := .acs[];  {
				 = append(, []byte()...)
			} else if ,  := .fallback[];  {
				 = append(, []byte()...)
			} else {
				 = append(, '?')
			}
		}
	} else {
		 = append(, [:]...)
	}

	return 
}

func ( *tScreen) ( Color,  Color,  AttrMask) AttrMask {
	 := .ti
	if .Colors() == 0 {
		// foreground vs background, we calculate luminance
		// and possibly do a reverse video
		if !.Valid() {
			return 
		}
		,  := .colors[]
		if ! {
			 = FindColor(, []Color{ColorBlack, ColorWhite})
			.colors[] = 
		}
		switch  {
		case ColorWhite:
			return 
		case ColorBlack:
			return  ^ AttrReverse
		}
	}

	if  == ColorReset ||  == ColorReset {
		.TPuts(.ResetFgBg)
	}
	if .truecolor {
		if .SetFgBgRGB != "" && .IsRGB() && .IsRGB() {
			, ,  := .RGB()
			, ,  := .RGB()
			.TPuts(.TParm(.SetFgBgRGB,
				int(), int(), int(),
				int(), int(), int()))
			return 
		}

		if .IsRGB() && .SetFgRGB != "" {
			, ,  := .RGB()
			.TPuts(.TParm(.SetFgRGB, int(), int(), int()))
			 = ColorDefault
		}

		if .IsRGB() && .SetBgRGB != "" {
			, ,  := .RGB()
			.TPuts(.TParm(.SetBgRGB,
				int(), int(), int()))
			 = ColorDefault
		}
	}

	if .Valid() {
		if ,  := .colors[];  {
			 = 
		} else {
			 = FindColor(, .palette)
			.colors[] = 
			 = 
		}
	}

	if .Valid() {
		if ,  := .colors[];  {
			 = 
		} else {
			 = FindColor(, .palette)
			.colors[] = 
			 = 
		}
	}

	if .Valid() && .Valid() && .SetFgBg != "" {
		.TPuts(.TParm(.SetFgBg, int(&0xff), int(&0xff)))
	} else {
		if .Valid() && .SetFg != "" {
			.TPuts(.TParm(.SetFg, int(&0xff)))
		}
		if .Valid() && .SetBg != "" {
			.TPuts(.TParm(.SetBg, int(&0xff)))
		}
	}
	return 
}

func ( *tScreen) (,  int) int {

	 := .ti

	, ,  := .cells.Get(, )
	if !.cells.Dirty(, ) {
		return 
	}

	if  == .h-1 &&  == .w-1 && .ti.AutoMargin && .DisableAutoMargin == "" && .InsertChar != "" {
		// our solution is somewhat goofy.
		// we write to the second to the last cell what we want in the last cell, then we
		// insert a character at that 2nd to last position to shift the last column into
		// place, then we rewrite that 2nd to last cell.  Old terminals suck.
		.TPuts(.TGoto(-1, ))
		defer func() {
			.TPuts(.TGoto(-1, ))
			.TPuts(.InsertChar)
			.cy = 
			.cx =  - 1
			.cells.SetDirty(-1, , true)
			_ = .(-1, )
			.TPuts(.ti.TGoto(0, 0))
			.cy = 0
			.cx = 0
		}()
	} else if .cy !=  || .cx !=  {
		.TPuts(.TGoto(, ))
		.cx = 
		.cy = 
	}

	if  == StyleDefault {
		 = .style
	}
	if  != .curstyle {
		, ,  := .fg, .bg, .attrs

		.TPuts(.AttrOff)

		 = .sendFgBg(, , )
		if &AttrBold != 0 {
			.TPuts(.Bold)
		}
		if ,  := .ulStyle, .ulColor;  != UnderlineStyleNone {
			if .underColor != "" || .underRGB != "" {
				if  == ColorReset {
					.TPuts(.underFg)
				} else if .IsRGB() {
					if .underRGB != "" {
						, ,  := .RGB()
						.TPuts(.TParm(.underRGB, int(), int(), int()))
					} else {
						if ,  := .colors[];  {
							 = 
						} else {
							 = FindColor(, .palette)
							.colors[] = 
							 = 
						}
						.TPuts(.TParm(.underColor, int(&0xff)))
					}
				} else if .Valid() {
					.TPuts(.TParm(.underColor, int(&0xff)))
				}
			}
			.TPuts(.Underline) // to ensure everyone gets at least a basic underline
			switch  {
			case UnderlineStyleDouble:
				.TPuts(.doubleUnder)
			case UnderlineStyleCurly:
				.TPuts(.curlyUnder)
			case UnderlineStyleDotted:
				.TPuts(.dottedUnder)
			case UnderlineStyleDashed:
				.TPuts(.dashedUnder)
			}
		}
		if &AttrReverse != 0 {
			.TPuts(.Reverse)
		}
		if &AttrBlink != 0 {
			.TPuts(.Blink)
		}
		if &AttrDim != 0 {
			.TPuts(.Dim)
		}
		if &AttrItalic != 0 {
			.TPuts(.Italic)
		}
		if &AttrStrikeThrough != 0 {
			.TPuts(.StrikeThrough)
		}

		// URL string can be long, so don't send it unless we really need to
		if .enterUrl != "" && .curstyle.url != .url {
			if .url != "" {
				.TPuts(.TParm(.enterUrl, .url, .urlId))
			} else {
				.TPuts(.exitUrl)
			}
		}

		.curstyle = 
	}

	// now emit runes - taking care to not overrun width with a
	// wide character, and to ensure that we emit exactly one regular
	// character followed up by any residual combing characters

	if  < 1 {
		 = 1
	}

	 := .encodeStr()
	 = string()

	if  > 1 &&  == "?" {
		// No FullWidth character support
		 = "? "
		.cx = -1
	}

	if  > .w- {
		// too wide to fit; emit a single space instead
		 = 1
		 = " "
	}
	if  > 1 && + < .w {
		// Clobber over any content in the next cell.
		// This fixes a problem with some terminals where overwriting two
		// adjacent single cells with a wide rune would leave an image
		// of the second cell.  This is a workaround for buggy terminals.
		.writeString("  \b\b")
	}

	.writeString()
	.cx += 
	.cells.SetDirty(, , false)
	if  > 1 {
		.cx = -1
	}

	return 
}

func ( *tScreen) (,  int) {
	.Lock()
	.cursorx = 
	.cursory = 
	.Unlock()
}

func ( *tScreen) ( CursorStyle,  Color) {
	.Lock()
	.cursorStyle = 
	.cursorColor = 
	.Unlock()
}

func ( *tScreen) () {
	.ShowCursor(-1, -1)
}

func ( *tScreen) () {

	,  := .cursorx, .cursory
	,  := .cells.Size()
	if  < 0 ||  < 0 ||  >=  ||  >=  {
		.hideCursor()
		return
	}
	.TPuts(.ti.TGoto(, ))
	.TPuts(.ti.ShowCursor)
	if .cursorStyles != nil {
		if ,  := .cursorStyles[.cursorStyle];  {
			.TPuts()
		}
	}
	if .cursorRGB != "" {
		if .cursorColor == ColorReset {
			.TPuts(.cursorFg)
		} else if .cursorColor.Valid() {
			, ,  := .cursorColor.RGB()
			.TPuts(.ti.TParm(.cursorRGB, int(), int(), int()))
		}
	}
	.cx = 
	.cy = 
}

// writeString sends a string to the terminal. The string is sent as-is and
// this function does not expand inline padding indications (of the form
// $<[delay]> where [delay] is msec). In order to have these expanded, use
// TPuts. If the screen is "buffering", the string is collected in a buffer,
// with the intention that the entire buffer be sent to the terminal in one
// write operation at some point later.
func ( *tScreen) ( string) {
	if .buffering {
		_, _ = io.WriteString(&.buf, )
	} else {
		_, _ = io.WriteString(.tty, )
	}
}

func ( *tScreen) ( string) {
	if .buffering {
		.ti.TPuts(&.buf, )
	} else {
		.ti.TPuts(.tty, )
	}
}

func ( *tScreen) () {
	.Lock()
	if !.fini {
		.resize()
		.draw()
	}
	.Unlock()
}

func ( *tScreen) () {
	.TPuts(.ti.AttrOff)
	.TPuts(.exitUrl)
	_ = .sendFgBg(.style.fg, .style.bg, AttrNone)
	.TPuts(.ti.Clear)
	.clear = false
}

func ( *tScreen) () {
	.TPuts(.startSyncOut)
}

func ( *tScreen) () {
	.TPuts(.endSyncOut)
}

func ( *tScreen) () {
	// does not update cursor position
	if .ti.HideCursor != "" {
		.TPuts(.ti.HideCursor)
	} else {
		// No way to hide cursor, stick it
		// at bottom right of screen
		.cx, .cy = .cells.Size()
		.TPuts(.ti.TGoto(.cx, .cy))
	}
}

func ( *tScreen) () {
	// clobber cursor position, because we're going to change it all
	.cx = -1
	.cy = -1
	// make no style assumptions
	.curstyle = styleInvalid

	.buf.Reset()
	.buffering = true
	.startBuffering()
	defer func() {
		.buffering = false
		.endBuffering()
	}()

	// hide the cursor while we move stuff around
	.hideCursor()

	if .clear {
		.clearScreen()
	}

	for  := 0;  < .h; ++ {
		for  := 0;  < .w; ++ {
			 := .drawCell(, )
			if  > 1 {
				if +1 < .w {
					// this is necessary so that if we ever
					// go back to drawing that cell, we
					// actually will *draw* it.
					.cells.SetDirty(+1, , true)
				}
			}
			 +=  - 1
		}
	}

	// restore the cursor
	.showCursor()

	_, _ = .buf.WriteTo(.tty)
}

func ( *tScreen) ( ...MouseFlags) {
	var  MouseFlags
	 := false
	for ,  := range  {
		 |= 
		 = true
	}
	if ! {
		 = MouseMotionEvents | MouseDragEvents | MouseButtonEvents
	}

	.Lock()
	.mouseFlags = 
	.enableMouse()
	.Unlock()
}

func ( *tScreen) ( MouseFlags) {
	// Rather than using terminfo to find mouse escape sequences, we rely on the fact that
	// pretty much *every* terminal that supports mouse tracking follows the
	// XTerm standards (the modern ones).
	if len(.mouse) != 0 {
		// start by disabling all tracking.
		.TPuts("\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1006l")
		if &MouseButtonEvents != 0 {
			.TPuts("\x1b[?1000h")
		}
		if &MouseDragEvents != 0 {
			.TPuts("\x1b[?1002h")
		}
		if &MouseMotionEvents != 0 {
			.TPuts("\x1b[?1003h")
		}
		if &(MouseButtonEvents|MouseDragEvents|MouseMotionEvents) != 0 {
			.TPuts("\x1b[?1006h")
		}
	}

}

func ( *tScreen) () {
	.Lock()
	.mouseFlags = 0
	.enableMouse(0)
	.Unlock()
}

func ( *tScreen) () {
	.Lock()
	.pasteEnabled = true
	.enablePasting(true)
	.Unlock()
}

func ( *tScreen) () {
	.Lock()
	.pasteEnabled = false
	.enablePasting(false)
	.Unlock()
}

func ( *tScreen) ( bool) {
	var  string
	if  {
		 = .enablePaste
	} else {
		 = .disablePaste
	}
	if  != "" {
		.TPuts()
	}
}

func ( *tScreen) () {
	.Lock()
	.focusEnabled = true
	.enableFocusReporting()
	.Unlock()
}

func ( *tScreen) () {
	.Lock()
	.focusEnabled = false
	.disableFocusReporting()
	.Unlock()
}

func ( *tScreen) () {
	if .enableFocus != "" {
		.TPuts(.enableFocus)
	}
}

func ( *tScreen) () {
	if .disableFocus != "" {
		.TPuts(.disableFocus)
	}
}

func ( *tScreen) () (int, int) {
	.Lock()
	,  := .w, .h
	.Unlock()
	return , 
}

func ( *tScreen) () {
	,  := .tty.WindowSize()
	if  != nil {
		return
	}
	if .Width == .w && .Height == .h {
		return
	}
	.cx = -1
	.cy = -1

	.cells.Resize(.Width, .Height)
	.cells.Invalidate()
	.h = .Height
	.w = .Width
	.input.SetSize(.Width, .Height)
}

func ( *tScreen) () int {
	if os.Getenv("NO_COLOR") != "" {
		return 0
	}
	// this doesn't change, no need for lock
	if .truecolor {
		return 1 << 24
	}
	return .ti.Colors
}

// nColors returns the size of the built-in palette.
// This is distinct from Colors(), as it will generally
// always be a small number. (<= 256)
func ( *tScreen) () int {
	if os.Getenv("NO_COLOR") != "" {
		return 0
	}
	return .ti.Colors
}

// vtACSNames is a map of bytes defined by terminfo that are used in
// the terminals Alternate Character Set to represent other glyphs.
// For example, the upper left corner of the box drawing set can be
// displayed by printing "l" while in the alternate character set.
// It's not quite that simple, since the "l" is the terminfo name,
// and it may be necessary to use a different character based on
// the terminal implementation (or the terminal may lack support for
// this altogether).  See buildAcsMap below for detail.
var vtACSNames = map[byte]rune{
	'+': RuneRArrow,
	',': RuneLArrow,
	'-': RuneUArrow,
	'.': RuneDArrow,
	'0': RuneBlock,
	'`': RuneDiamond,
	'a': RuneCkBoard,
	'b': '␉', // VT100, Not defined by terminfo
	'c': '␌', // VT100, Not defined by terminfo
	'd': '␋', // VT100, Not defined by terminfo
	'e': '␊', // VT100, Not defined by terminfo
	'f': RuneDegree,
	'g': RunePlMinus,
	'h': RuneBoard,
	'i': RuneLantern,
	'j': RuneLRCorner,
	'k': RuneURCorner,
	'l': RuneULCorner,
	'm': RuneLLCorner,
	'n': RunePlus,
	'o': RuneS1,
	'p': RuneS3,
	'q': RuneHLine,
	'r': RuneS7,
	's': RuneS9,
	't': RuneLTee,
	'u': RuneRTee,
	'v': RuneBTee,
	'w': RuneTTee,
	'x': RuneVLine,
	'y': RuneLEqual,
	'z': RuneGEqual,
	'{': RunePi,
	'|': RuneNEqual,
	'}': RuneSterling,
	'~': RuneBullet,
}

// buildAcsMap builds a map of characters that we translate from Unicode to
// alternate character encodings.  To do this, we use the standard VT100 ACS
// maps.  This is only done if the terminal lacks support for Unicode; we
// always prefer to emit Unicode glyphs when we are able.
func ( *tScreen) () {
	 := .ti.AltChars
	.acs = make(map[rune]string)
	for len() > 2 {
		 := [0]
		 := string([1])
		if ,  := vtACSNames[];  {
			.acs[] = .ti.EnterAcs +  + .ti.ExitAcs
		}
		 = [2:]
	}
}

func ( *tScreen) ( *bytes.Buffer) {
	// The end of the buffer isn't necessarily the end of the input, because
	// large inputs are chunked. Set atEOF to false so the UTF-8 validating decoder
	// returns ErrShortSrc instead of ErrInvalidUTF8 for incomplete multi-byte codepoints.
	const  = false

	for .Len() > 0 {
		 := make([]byte, min(8, max(.Len()*2, 128)))
		, ,  := .decoder.Transform(, .Bytes(), )
		_ = .Next()
		.input.ScanUTF8([:])
		if  == transform.ErrShortSrc {
			return
		}
	}
}

func ( *tScreen) ( chan struct{}) {
	defer .wg.Done()
	 := &bytes.Buffer{}
	for {
		select {
		case <-:
			return
		case <-.quit:
			return
		case <-.resizeQ:
			.Lock()
			.cx = -1
			.cy = -1
			.resize()
			.cells.Invalidate()
			.draw()
			.Unlock()
			continue
		case  := <-.keychan:
			.Write()
			.scanInput()
		}
	}
}

func ( *tScreen) ( chan struct{}) {

	defer .wg.Done()
	for {
		select {
		case <-:
			return
		default:
		}
		 := make([]byte, 128)
		,  := .tty.Read()
		switch  {
		case nil:
		default:
			.Lock()
			 := .running
			.Unlock()
			if  {
				select {
				case .eventQ <- NewEventError():
				case <-.quit:
				}
			}
			return
		}
		if  > 0 {
			.keychan <- [:]
		}
	}
}

func ( *tScreen) () {
	.Lock()
	.cx = -1
	.cy = -1
	if !.fini {
		.resize()
		.clear = true
		.cells.Invalidate()
		.draw()
	}
	.Unlock()
}

func ( *tScreen) () string {
	return .charset
}

func ( *tScreen) ( rune,  string) {
	.Lock()
	.fallback[] = 
	.Unlock()
}

func ( *tScreen) ( rune) {
	.Lock()
	delete(.fallback, )
	.Unlock()
}

func ( *tScreen) ( rune,  bool) bool {

	if  := .encoder;  != nil {
		 := make([]byte, 6)

		.Reset()
		, ,  := .Transform(, []byte(string()), true)
		if  != 0 &&  == nil && [0] != '\x1A' {
			return true
		}
	}
	// Terminal fallbacks always permitted, since we assume they are
	// basically nearly perfect renditions.
	if ,  := .acs[];  {
		return true
	}
	if ! {
		return false
	}
	if ,  := .fallback[];  {
		return true
	}
	return false
}

func ( *tScreen) () bool {
	return len(.mouse) != 0
}

func ( *tScreen) ( Key) bool {
	// We always return true
	return true
}

func ( *tScreen) (,  int) {
	if .setWinSize != "" {
		.TPuts(.ti.TParm(.setWinSize, , ))
	}
	.cells.Invalidate()
	.resize()
}

func ( *tScreen) (int, int, int, int) {}

func ( *tScreen) () error {
	.disengage()
	return nil
}

func ( *tScreen) () error {
	return .engage()
}

func ( *tScreen) () (Tty, bool) {
	return .tty, true
}

// engage is used to place the terminal in raw mode and establish screen size, etc.
// Think of this is as tcell "engaging" the clutch, as it's going to be driving the
// terminal interface.
func ( *tScreen) () error {
	.Lock()
	defer .Unlock()
	if .tty == nil {
		return ErrNoScreen
	}
	.tty.NotifyResize(func() {
		select {
		case .resizeQ <- true:
		default:
		}
	})
	if .running {
		return errors.New("already engaged")
	}
	if  := .tty.Start();  != nil {
		return 
	}
	.running = true
	if ,  := .tty.WindowSize();  == nil && .Width != 0 && .Height != 0 {
		.cells.Resize(.Width, .Height)
	}
	 := make(chan struct{})
	.stopQ = 
	.enableMouse(.mouseFlags)
	.enablePasting(.pasteEnabled)
	if .focusEnabled {
		.enableFocusReporting()
	}
	 := .ti
	if os.Getenv("TCELL_ALTSCREEN") != "disable" {
		// Technically this may not be right, but every terminal we know about
		// (even Wyse 60) uses this to enter the alternate screen buffer, and
		// possibly save and restore the window title and/or icon.
		// (In theory there could be terminals that don't support X,Y cursor
		// positions without a setup command, but we don't support them.)
		.TPuts(.EnterCA)
		.TPuts(.saveTitle)
	}
	.TPuts(.EnterKeypad)
	.TPuts(.HideCursor)
	.TPuts(.EnableAcs)
	.TPuts(.DisableAutoMargin)
	.TPuts(.Clear)
	if .title != "" && .setTitle != "" {
		.TPuts(.ti.TParm(.setTitle, .title))
	}
	.TPuts(.enableCsiU)

	.wg.Add(2)
	go .inputLoop()
	go .mainLoop()
	return nil
}

// disengage is used to release the terminal back to support from the caller.
// Think of this as tcell disengaging the clutch, so that another application
// can take over the terminal interface.  This restores the TTY mode that was
// present when the application was first started.
func ( *tScreen) () {

	.Lock()
	if !.running {
		.Unlock()
		return
	}

	.running = false
	 := .stopQ
	close()
	_ = .tty.Drain()
	.Unlock()

	.tty.NotifyResize(nil)
	// wait for everything to shut down
	.wg.Wait()

	// shutdown the screen and disable special modes (e.g. mouse and bracketed paste)
	 := .ti
	.cells.Resize(0, 0)
	.TPuts(.ShowCursor)
	if .cursorStyles != nil && .cursorStyle != CursorStyleDefault {
		.TPuts(.cursorStyles[CursorStyleDefault])
	}
	if .cursorFg != "" && .cursorColor.Valid() {
		.TPuts(.cursorFg)
	}
	.TPuts(.ResetFgBg)
	.TPuts(.AttrOff)
	.TPuts(.ExitKeypad)
	.TPuts(.EnableAutoMargin)
	.TPuts(.disableCsiU)
	if os.Getenv("TCELL_ALTSCREEN") != "disable" {
		if .restoreTitle != "" {
			.TPuts(.restoreTitle)
		}
		.TPuts(.Clear) // only needed if ExitCA is empty
		.TPuts(.ExitCA)
	}
	.enableMouse(0)
	.enablePasting(false)
	.disableFocusReporting()

	_ = .tty.Stop()
}

// Beep emits a beep to the terminal.
func ( *tScreen) () error {
	.writeString(string(byte(7)))
	return nil
}

// finalize is used to at application shutdown, and restores the terminal
// to it's initial state.  It should not be called more than once.
func ( *tScreen) () {
	.disengage()
	_ = .tty.Close()
}

func ( *tScreen) () <-chan struct{} {
	return .quit
}

func ( *tScreen) () chan Event {
	return .eventQ
}

func ( *tScreen) () *CellBuffer {
	return &.cells
}

func ( *tScreen) ( string) {
	.Lock()
	.title = 
	if .setTitle != "" && .running {
		.TPuts(.ti.TParm(.setTitle, ))
	}
	.Unlock()
}

func ( *tScreen) ( []byte) {
	// Post binary data to the system clipboard.  It might be UTF-8, it might not be.
	.Lock()
	if .setClipboard != "" {
		 := base64.StdEncoding.EncodeToString()
		.TPuts(.ti.TParm(.setClipboard, ))
	}
	.Unlock()
}

func ( *tScreen) () {
	.Lock()
	if .setClipboard != "" {
		.TPuts(.ti.TParm(.setClipboard, "?"))
	}
	.Unlock()
}