Skip to content
Myr

tcell.go

This file

Path

src/tui/tcell.go

1,183 lines · 26.5 KiB · Go

Last changed by

Set inline separator caps per sideJunegunn Choibefore adoptiond247284

History of this file

Take it

1//go:build tcell || windows23package tui45import (6	"os"7	"regexp"8	"strings"9	"time"1011	"github.com/gdamore/tcell/v2"12	"github.com/junegunn/fzf/src/util"1314	"github.com/rivo/uniseg"15)1617func HasFullscreenRenderer() bool {18	return true19}2021var DefaultBorderShape BorderShape = BorderSharp2223func asTcellColor(color Color) tcell.Color {24	if color == colDefault {25		return tcell.ColorDefault26	}2728	value := uint64(tcell.ColorValid) + uint64(color)29	if color.is24() {30		value = value | uint64(tcell.ColorIsRGB)31	}32	return tcell.Color(value)33}3435func (p ColorPair) style() tcell.Style {36	style := tcell.StyleDefault37	return style.Foreground(asTcellColor(p.Fg())).Background(asTcellColor(p.Bg()))38}3940type TcellWindow struct {41	color         bool42	windowType    WindowType43	top           int44	left          int45	width         int46	height        int47	normal        ColorPair48	lastX         int49	lastY         int50	moveCursor    bool51	borderStyle   BorderStyle52	uri           *string53	params        *string54	showCursor    bool55	wrapSign      string56	wrapSignWidth int57	tabstop       int58}5960func (w *TcellWindow) Top() int {61	return w.top62}6364func (w *TcellWindow) Left() int {65	return w.left66}6768func (w *TcellWindow) Width() int {69	return w.width70}7172func (w *TcellWindow) Height() int {73	return w.height74}7576func (w *TcellWindow) Refresh() {77	if w.moveCursor {78		if w.showCursor {79			_screen.ShowCursor(w.left+w.lastX, w.top+w.lastY)80		}81		w.moveCursor = false82	}83	w.lastX = 084	w.lastY = 085}8687func (w *TcellWindow) FinishFill() {88	// NO-OP89}9091const (92	Bold          Attr = Attr(tcell.AttrBold)93	Dim                = Attr(tcell.AttrDim)94	Blink              = Attr(tcell.AttrBlink)95	Reverse            = Attr(tcell.AttrReverse)96	Underline          = Attr(tcell.AttrUnderline)97	StrikeThrough      = Attr(tcell.AttrStrikeThrough)98	Italic             = Attr(tcell.AttrItalic)99)100101func (r *FullscreenRenderer) Bell() {102	_screen.Beep()103}104105func (r *FullscreenRenderer) HideCursor() {106	r.showCursor = false107}108109func (r *FullscreenRenderer) ShowCursor() {110	r.showCursor = true111}112113func (r *FullscreenRenderer) PassThrough(str string) {114	// No-op115	// https://github.com/gdamore/tcell/pull/650#issuecomment-1806442846116}117118func (r *FullscreenRenderer) Resize(maxHeightFunc func(int) int) {}119120func (r *FullscreenRenderer) DefaultTheme() *ColorTheme {121	s, e := r.getScreen()122	if e != nil {123		return Default16124	}125	if s.Colors() >= 256 {126		return Dark256127	}128	return Default16129}130131var (132	_colorToAttribute = []tcell.Color{133		tcell.ColorBlack,134		tcell.ColorRed,135		tcell.ColorGreen,136		tcell.ColorYellow,137		tcell.ColorBlue,138		tcell.ColorDarkMagenta,139		tcell.ColorLightCyan,140		tcell.ColorWhite,141	}142)143144func (c Color) Style() tcell.Color {145	if c <= colDefault {146		return tcell.ColorDefault147	} else if c >= colBlack && c <= colWhite {148		return _colorToAttribute[int(c)]149	} else {150		return tcell.Color(c)151	}152}153154// handle the following as private members of FullscreenRenderer instance155// they are declared here to prevent introducing tcell library in non-windows builds156var (157	_screen          tcell.Screen158	_prevMouseButton tcell.ButtonMask159	_initialResize   bool = true160)161162func (r *FullscreenRenderer) getScreen() (tcell.Screen, error) {163	if _screen == nil {164		s, e := tcell.NewScreen()165		if e != nil {166			return nil, e167		}168		if !r.showCursor {169			s.HideCursor()170		}171		_screen = s172	}173	return _screen, nil174}175176func (r *FullscreenRenderer) initScreen() error {177	s, e := r.getScreen()178	if e != nil {179		return e180	}181	if e = s.Init(); e != nil {182		return e183	}184	s.EnablePaste()185	if r.mouse {186		s.EnableMouse()187	} else {188		s.DisableMouse()189	}190191	return nil192}193194func (r *FullscreenRenderer) Init() error {195	if os.Getenv("TERM") == "cygwin" {196		os.Setenv("TERM", "")197	}198199	if err := r.initScreen(); err != nil {200		return err201	}202203	return nil204}205206func (r *FullscreenRenderer) Top() int {207	return 0208}209210func (r *FullscreenRenderer) MaxX() int {211	ncols, _ := _screen.Size()212	return int(ncols)213}214215func (r *FullscreenRenderer) MaxY() int {216	_, nlines := _screen.Size()217	return int(nlines)218}219220func (w *TcellWindow) X() int {221	return w.lastX222}223224func (w *TcellWindow) Y() int {225	return w.lastY226}227228func (r *FullscreenRenderer) Clear() {229	_screen.Sync()230	_screen.Clear()231}232233func (r *FullscreenRenderer) NeedScrollbarRedraw() bool {234	return true235}236237func (r *FullscreenRenderer) ShouldEmitResizeEvent() bool {238	return true239}240241func (r *FullscreenRenderer) Refresh() {242	// noop243}244245// TODO: Pixel width and height not implemented246func (r *FullscreenRenderer) Size() TermSize {247	cols, lines := _screen.Size()248	return TermSize{lines, cols, 0, 0}249}250251func (r *FullscreenRenderer) GetChar(cancellable bool) Event {252	ev := _screen.PollEvent()253	switch ev := ev.(type) {254	case *tcell.EventPaste:255		if ev.Start() {256			return Event{BracketedPasteBegin, 0, nil}257		}258		return Event{BracketedPasteEnd, 0, nil}259	case *tcell.EventResize:260		// Ignore the first resize event261		// https://github.com/gdamore/tcell/blob/v2.7.0/TUTORIAL.md?plain=1#L18262		if _initialResize {263			_initialResize = false264			return Event{Invalid, 0, nil}265		}266		return Event{Resize, 0, nil}267268	// process mouse events:269	case *tcell.EventMouse:270		// mouse down events have zeroed buttons, so we can't use them271		// mouse up event consists of two events, 1. (main) event with modifier and other metadata, 2. event with zeroed buttons272		// so mouse click is three consecutive events, but the first and last are indistinguishable from movement events (with released buttons)273		// dragging has same structure, it only repeats the middle (main) event appropriately274		x, y := ev.Position()275276		mod := ev.Modifiers()277		ctrl := (mod & tcell.ModCtrl) > 0278		alt := (mod & tcell.ModAlt) > 0279		shift := (mod & tcell.ModShift) > 0280281		// since we dont have mouse down events (unlike LightRenderer), we need to track state in prevButton282		prevButton, button := _prevMouseButton, ev.Buttons()283		_prevMouseButton = button284		drag := prevButton == button285286		switch {287		case button&tcell.WheelDown != 0:288			return Event{Mouse, 0, &MouseEvent{y, x, -1, false, false, false, ctrl, alt, shift}}289		case button&tcell.WheelUp != 0:290			return Event{Mouse, 0, &MouseEvent{y, x, +1, false, false, false, ctrl, alt, shift}}291		case button&tcell.Button1 != 0:292			double := false293			if !drag {294				// all potential double click events put their coordinates in the clicks array295				// double click event has two conditions, temporal and spatial, the first is checked here296				now := time.Now()297				if now.Sub(r.prevDownTime) < doubleClickDuration {298					r.clicks = append(r.clicks, [2]int{x, y})299				} else {300					r.clicks = [][2]int{{x, y}}301				}302				r.prevDownTime = now303304				// detect double clicks (also check for spatial condition)305				n := len(r.clicks)306				double = n > 1 && r.clicks[n-2][0] == r.clicks[n-1][0] && r.clicks[n-2][1] == r.clicks[n-1][1]307				if double {308					// make sure two consecutive double clicks require four clicks309					r.clicks = [][2]int{}310				}311			}312			// fire single or double click event313			return Event{Mouse, 0, &MouseEvent{y, x, 0, true, !double, double, ctrl, alt, shift}}314		case button&tcell.Button2 != 0:315			return Event{Mouse, 0, &MouseEvent{y, x, 0, false, true, false, ctrl, alt, shift}}316		default:317			// double and single taps on Windows don't quite work due to318			// the console acting on the events and not allowing us319			// to consume them.320			left := button&tcell.Button1 != 0321			down := left || button&tcell.Button3 != 0322			double := false323324			// No need to report mouse movement events when no button is pressed325			if drag {326				return Event{Invalid, 0, nil}327			}328			return Event{Mouse, 0, &MouseEvent{y, x, 0, left, down, double, ctrl, alt, shift}}329		}330331		// process keyboard:332	case *tcell.EventKey:333		mods := ev.Modifiers()334		none := mods == tcell.ModNone335		alt := (mods & tcell.ModAlt) > 0336		ctrl := (mods & tcell.ModCtrl) > 0337		shift := (mods & tcell.ModShift) > 0338		ctrlAlt := ctrl && alt339		altShift := alt && shift340		ctrlShift := ctrl && shift341		ctrlAltShift := ctrl && alt && shift342343		keyfn := func(r rune) Event {344			if alt {345				return CtrlAltKey(r)346			}347			return EventType(CtrlA.Int() - 'a' + int(r)).AsEvent()348		}349		switch ev.Key() {350		// section 1: Ctrl+(Alt)+[a-z]351		case tcell.KeyCtrlA:352			return keyfn('a')353		case tcell.KeyCtrlB:354			return keyfn('b')355		case tcell.KeyCtrlC:356			return keyfn('c')357		case tcell.KeyCtrlD:358			return keyfn('d')359		case tcell.KeyCtrlE:360			return keyfn('e')361		case tcell.KeyCtrlF:362			return keyfn('f')363		case tcell.KeyCtrlG:364			return keyfn('g')365		case tcell.KeyCtrlH:366			switch ev.Rune() {367			case 0:368				if ctrlAlt {369					return Event{CtrlAltBackspace, 0, nil}370				}371				if ctrl {372					return Event{CtrlBackspace, 0, nil}373				}374			case rune(tcell.KeyCtrlH):375				switch {376				case ctrl:377					return keyfn('h')378				case alt:379					return Event{AltBackspace, 0, nil}380				case none, shift:381					return Event{Backspace, 0, nil}382				}383			}384		case tcell.KeyCtrlI:385			return keyfn('i')386		case tcell.KeyCtrlJ:387			return keyfn('j')388		case tcell.KeyCtrlK:389			return keyfn('k')390		case tcell.KeyCtrlL:391			return keyfn('l')392		case tcell.KeyCtrlM:393			return keyfn('m')394		case tcell.KeyCtrlN:395			return keyfn('n')396		case tcell.KeyCtrlO:397			return keyfn('o')398		case tcell.KeyCtrlP:399			return keyfn('p')400		case tcell.KeyCtrlQ:401			return keyfn('q')402		case tcell.KeyCtrlR:403			return keyfn('r')404		case tcell.KeyCtrlS:405			return keyfn('s')406		case tcell.KeyCtrlT:407			return keyfn('t')408		case tcell.KeyCtrlU:409			return keyfn('u')410		case tcell.KeyCtrlV:411			return keyfn('v')412		case tcell.KeyCtrlW:413			return keyfn('w')414		case tcell.KeyCtrlX:415			return keyfn('x')416		case tcell.KeyCtrlY:417			return keyfn('y')418		case tcell.KeyCtrlZ:419			return keyfn('z')420		// section 2: Ctrl+[ \]_]421		case tcell.KeyCtrlSpace:422			return Event{CtrlSpace, 0, nil}423		case tcell.KeyCtrlBackslash:424			return Event{CtrlBackSlash, 0, nil}425		case tcell.KeyCtrlRightSq:426			return Event{CtrlRightBracket, 0, nil}427		case tcell.KeyCtrlCarat:428			return Event{CtrlCaret, 0, nil}429		case tcell.KeyCtrlUnderscore:430			return Event{CtrlSlash, 0, nil}431		// section 3: (Alt)+Backspace2432		case tcell.KeyBackspace2:433			if ctrl {434				return Event{CtrlBackspace, 0, nil}435			}436			if alt {437				return Event{AltBackspace, 0, nil}438			}439			return Event{Backspace, 0, nil}440441		// section 4: (Alt+Shift)+Key(Up|Down|Left|Right)442		case tcell.KeyUp:443			if ctrlAltShift {444				return Event{CtrlAltShiftUp, 0, nil}445			}446			if ctrlAlt {447				return Event{CtrlAltUp, 0, nil}448			}449			if ctrlShift {450				return Event{CtrlShiftUp, 0, nil}451			}452			if altShift {453				return Event{AltShiftUp, 0, nil}454			}455			if ctrl {456				return Event{CtrlUp, 0, nil}457			}458			if shift {459				return Event{ShiftUp, 0, nil}460			}461			if alt {462				return Event{AltUp, 0, nil}463			}464			return Event{Up, 0, nil}465		case tcell.KeyDown:466			if ctrlAltShift {467				return Event{CtrlAltShiftDown, 0, nil}468			}469			if ctrlAlt {470				return Event{CtrlAltDown, 0, nil}471			}472			if ctrlShift {473				return Event{CtrlShiftDown, 0, nil}474			}475			if altShift {476				return Event{AltShiftDown, 0, nil}477			}478			if ctrl {479				return Event{CtrlDown, 0, nil}480			}481			if shift {482				return Event{ShiftDown, 0, nil}483			}484			if alt {485				return Event{AltDown, 0, nil}486			}487			return Event{Down, 0, nil}488		case tcell.KeyLeft:489			if ctrlAltShift {490				return Event{CtrlAltShiftLeft, 0, nil}491			}492			if ctrlAlt {493				return Event{CtrlAltLeft, 0, nil}494			}495			if ctrlShift {496				return Event{CtrlShiftLeft, 0, nil}497			}498			if altShift {499				return Event{AltShiftLeft, 0, nil}500			}501			if ctrl {502				return Event{CtrlLeft, 0, nil}503			}504			if shift {505				return Event{ShiftLeft, 0, nil}506			}507			if alt {508				return Event{AltLeft, 0, nil}509			}510			return Event{Left, 0, nil}511		case tcell.KeyRight:512			if ctrlAltShift {513				return Event{CtrlAltShiftRight, 0, nil}514			}515			if ctrlAlt {516				return Event{CtrlAltRight, 0, nil}517			}518			if ctrlShift {519				return Event{CtrlShiftRight, 0, nil}520			}521			if altShift {522				return Event{AltShiftRight, 0, nil}523			}524			if ctrl {525				return Event{CtrlRight, 0, nil}526			}527			if shift {528				return Event{ShiftRight, 0, nil}529			}530			if alt {531				return Event{AltRight, 0, nil}532			}533			return Event{Right, 0, nil}534535		// section 5: (Insert|Home|Delete|End|PgUp|PgDn|BackTab|F1-F12)536		case tcell.KeyInsert:537			return Event{Insert, 0, nil}538		case tcell.KeyHome:539			if ctrlAltShift {540				return Event{CtrlAltShiftHome, 0, nil}541			}542			if ctrlAlt {543				return Event{CtrlAltHome, 0, nil}544			}545			if ctrlShift {546				return Event{CtrlShiftHome, 0, nil}547			}548			if altShift {549				return Event{AltShiftHome, 0, nil}550			}551			if ctrl {552				return Event{CtrlHome, 0, nil}553			}554			if shift {555				return Event{ShiftHome, 0, nil}556			}557			if alt {558				return Event{AltHome, 0, nil}559			}560			return Event{Home, 0, nil}561		case tcell.KeyDelete:562			if ctrlAltShift {563				return Event{CtrlAltShiftDelete, 0, nil}564			}565			if ctrlAlt {566				return Event{CtrlAltDelete, 0, nil}567			}568			if ctrlShift {569				return Event{CtrlShiftDelete, 0, nil}570			}571			if altShift {572				return Event{AltShiftDelete, 0, nil}573			}574			if ctrl {575				return Event{CtrlDelete, 0, nil}576			}577			if alt {578				return Event{AltDelete, 0, nil}579			}580			if shift {581				return Event{ShiftDelete, 0, nil}582			}583			return Event{Delete, 0, nil}584		case tcell.KeyEnd:585			if ctrlAltShift {586				return Event{CtrlAltShiftEnd, 0, nil}587			}588			if ctrlAlt {589				return Event{CtrlAltEnd, 0, nil}590			}591			if ctrlShift {592				return Event{CtrlShiftEnd, 0, nil}593			}594			if altShift {595				return Event{AltShiftEnd, 0, nil}596			}597			if ctrl {598				return Event{CtrlEnd, 0, nil}599			}600			if shift {601				return Event{ShiftEnd, 0, nil}602			}603			if alt {604				return Event{AltEnd, 0, nil}605			}606			return Event{End, 0, nil}607		case tcell.KeyPgUp:608			if ctrlAltShift {609				return Event{CtrlAltShiftPageUp, 0, nil}610			}611			if ctrlAlt {612				return Event{CtrlAltPageUp, 0, nil}613			}614			if ctrlShift {615				return Event{CtrlShiftPageUp, 0, nil}616			}617			if altShift {618				return Event{AltShiftPageUp, 0, nil}619			}620			if ctrl {621				return Event{CtrlPageUp, 0, nil}622			}623			if shift {624				return Event{ShiftPageUp, 0, nil}625			}626			if alt {627				return Event{AltPageUp, 0, nil}628			}629			return Event{PageUp, 0, nil}630		case tcell.KeyPgDn:631			if ctrlAltShift {632				return Event{CtrlAltShiftPageDown, 0, nil}633			}634			if ctrlAlt {635				return Event{CtrlAltPageDown, 0, nil}636			}637			if ctrlShift {638				return Event{CtrlShiftPageDown, 0, nil}639			}640			if altShift {641				return Event{AltShiftPageDown, 0, nil}642			}643			if ctrl {644				return Event{CtrlPageDown, 0, nil}645			}646			if shift {647				return Event{ShiftPageDown, 0, nil}648			}649			if alt {650				return Event{AltPageDown, 0, nil}651			}652			return Event{PageDown, 0, nil}653		case tcell.KeyBacktab:654			return Event{ShiftTab, 0, nil}655		case tcell.KeyF1:656			return Event{F1, 0, nil}657		case tcell.KeyF2:658			return Event{F2, 0, nil}659		case tcell.KeyF3:660			return Event{F3, 0, nil}661		case tcell.KeyF4:662			return Event{F4, 0, nil}663		case tcell.KeyF5:664			return Event{F5, 0, nil}665		case tcell.KeyF6:666			return Event{F6, 0, nil}667		case tcell.KeyF7:668			return Event{F7, 0, nil}669		case tcell.KeyF8:670			return Event{F8, 0, nil}671		case tcell.KeyF9:672			return Event{F9, 0, nil}673		case tcell.KeyF10:674			return Event{F10, 0, nil}675		case tcell.KeyF11:676			return Event{F11, 0, nil}677		case tcell.KeyF12:678			return Event{F12, 0, nil}679680		// section 6: (Ctrl+Alt)+'rune'681		case tcell.KeyRune:682			r := ev.Rune()683684			switch {685			// translate native key events to ascii control characters686			case r == ' ' && ctrl:687				return Event{CtrlSpace, 0, nil}688			// handle AltGr characters689			case ctrlAlt:690				return Event{Rune, r, nil} // dropping modifiers691			// simple characters (possibly with modifier)692			case alt:693				return AltKey(r)694			default:695				return Event{Rune, r, nil}696			}697698		// section 7: Esc699		case tcell.KeyEsc:700			return Event{Esc, 0, nil}701		}702	}703704	// section 8: Invalid705	return Event{Invalid, 0, nil}706}707708func (r *FullscreenRenderer) CancelGetChar() {709	// TODO710}711712func (r *FullscreenRenderer) Pause(clear bool) {713	if clear {714		_screen.Suspend()715	}716}717718func (r *FullscreenRenderer) Resume(clear bool, sigcont bool) {719	if clear {720		_screen.Resume()721	}722}723724func (r *FullscreenRenderer) Close() {725	_screen.Fini()726	_screen = nil727}728729func (r *FullscreenRenderer) RefreshWindows(windows []Window) {730	// TODO731	for _, w := range windows {732		w.Refresh()733	}734	_screen.Show()735}736737func (r *FullscreenRenderer) NewWindow(top int, left int, width int, height int, windowType WindowType, borderStyle BorderStyle, erase bool) Window {738	width = max(0, width)739	height = max(0, height)740	normal := ColBorder741	switch windowType {742	case WindowList:743		normal = ColNormal744	case WindowHeader:745		normal = ColHeader746	case WindowFooter:747		normal = ColFooter748	case WindowInput:749		normal = ColInput750	case WindowPreview:751		normal = ColPreview752	}753	w := &TcellWindow{754		color:       r.theme.Colored,755		windowType:  windowType,756		top:         top,757		left:        left,758		width:       width,759		height:      height,760		normal:      normal,761		borderStyle: borderStyle,762		showCursor:  r.showCursor,763		tabstop:     r.tabstop}764	w.Erase()765	return w766}767768func fill(x, y, w, h int, n ColorPair, r rune) {769	for ly := 0; ly <= h; ly++ {770		for lx := 0; lx <= w; lx++ {771			_screen.SetContent(x+lx, y+ly, r, nil, n.style())772		}773	}774}775776func (w *TcellWindow) Erase() {777	fill(w.left, w.top, w.width-1, w.height-1, w.normal, ' ')778	w.drawBorder(false)779}780781func (w *TcellWindow) EraseMaybe() bool {782	w.Erase()783	return true784}785786func (w *TcellWindow) SetWrapSign(sign string, width int) {787	w.wrapSign = sign788	w.wrapSignWidth = width789}790791func (w *TcellWindow) EncloseX(x int) bool {792	return x >= w.left && x < (w.left+w.width)793}794795func (w *TcellWindow) EncloseY(y int) bool {796	return y >= w.top && y < (w.top+w.height)797}798799func (w *TcellWindow) Enclose(y int, x int) bool {800	return w.EncloseX(x) && w.EncloseY(y)801}802803func (w *TcellWindow) Move(y int, x int) {804	w.lastX = x805	w.lastY = y806	w.moveCursor = true807}808809func (w *TcellWindow) MoveAndClear(y int, x int) {810	w.Move(y, x)811	for i := w.lastX; i < w.width; i++ {812		_screen.SetContent(i+w.left, w.lastY+w.top, rune(' '), nil, w.normal.style())813	}814	w.lastX = x815}816817func (w *TcellWindow) Print(text string) {818	w.printString(text, w.normal)819}820821func (w *TcellWindow) withUrl(style tcell.Style) tcell.Style {822	if w.uri != nil {823		style = style.Url(*w.uri)824		if md := regexp.MustCompile(`id=([^:]+)`).FindStringSubmatch(*w.params); len(md) > 1 {825			style = style.UrlId(md[1])826		}827	}828	return style829}830831func underlineStyleFromAttr(a Attr) tcell.UnderlineStyle {832	switch a.UnderlineStyle() {833	case UlStyleDouble:834		return tcell.UnderlineStyleDouble835	case UlStyleCurly:836		return tcell.UnderlineStyleCurly837	case UlStyleDotted:838		return tcell.UnderlineStyleDotted839	case UlStyleDashed:840		return tcell.UnderlineStyleDashed841	default:842		return tcell.UnderlineStyleSolid843	}844}845846func (w *TcellWindow) printString(text string, pair ColorPair) {847	lx := 0848	a := pair.Attr()849850	style := pair.style()851	if a&AttrClear == 0 {852		style = style.853			Reverse(a&Attr(tcell.AttrReverse) != 0).854			StrikeThrough(a&Attr(tcell.AttrStrikeThrough) != 0).855			Italic(a&Attr(tcell.AttrItalic) != 0).856			Blink(a&Attr(tcell.AttrBlink) != 0).857			Dim(a&Attr(tcell.AttrDim) != 0)858		if a&Attr(tcell.AttrUnderline) != 0 {859			style = style.Underline(underlineStyleFromAttr(a))860			if pair.Ul() != colDefault {861				style = style.Underline(asTcellColor(pair.Ul()))862			}863		} else {864			style = style.Underline(false)865		}866	}867	style = w.withUrl(style)868869	gr := uniseg.NewGraphemes(text)870	for gr.Next() {871		st := style872		rs := gr.Runes()873874		if len(rs) == 1 {875			r := rs[0]876			if r == '\r' {877				st = style.Dim(true)878				rs[0] = '␍'879			} else if r == '\n' {880				st = style.Dim(true)881				rs[0] = '␊'882			} else if r < rune(' ') { // ignore control characters883				continue884			}885		}886		var xPos = w.left + w.lastX + lx887		var yPos = w.top + w.lastY888		if xPos < (w.left+w.width) && yPos < (w.top+w.height) {889			_screen.SetContent(xPos, yPos, rs[0], rs[1:], st)890		}891		lx += util.StringWidth(string(rs))892	}893	w.lastX += lx894}895896func (w *TcellWindow) CPrint(pair ColorPair, text string) {897	w.printString(text, pair)898}899900func (w *TcellWindow) pairStyle(pair ColorPair) tcell.Style {901	a := pair.Attr()902	var style tcell.Style903	if w.color {904		style = pair.style()905	} else {906		style = w.normal.style()907	}908	style = style.909		Blink(a&Attr(tcell.AttrBlink) != 0).910		Bold(a&Attr(tcell.AttrBold) != 0 || a&BoldForce != 0).911		Dim(a&Attr(tcell.AttrDim) != 0).912		Reverse(a&Attr(tcell.AttrReverse) != 0).913		StrikeThrough(a&Attr(tcell.AttrStrikeThrough) != 0).914		Italic(a&Attr(tcell.AttrItalic) != 0)915	if a&Attr(tcell.AttrUnderline) != 0 {916		style = style.Underline(underlineStyleFromAttr(a))917		if pair.Ul() != colDefault {918			style = style.Underline(asTcellColor(pair.Ul()))919		}920	} else {921		style = style.Underline(false)922	}923	return w.withUrl(style)924}925926func (w *TcellWindow) renderGraphemes(text string, style tcell.Style) {927	gr := uniseg.NewGraphemes(text)928	for gr.Next() {929		st := style930		rs := gr.Runes()931		if len(rs) == 1 && rs[0] == '\r' {932			st = style.Dim(true)933			rs[0] = '␍'934		}935936		xPos := w.left + w.lastX937		yPos := w.top + w.lastY938		if xPos < (w.left+w.width) && yPos < (w.top+w.height) {939			_screen.SetContent(xPos, yPos, rs[0], rs[1:], st)940		}941		w.lastX += util.StringWidth(string(rs))942	}943}944945func (w *TcellWindow) renderWrapSign(style tcell.Style) {946	sign := w.wrapSign947	if w.wrapSignWidth > w.width {948		runes, _ := util.Truncate(sign, w.width)949		sign = string(runes)950	}951	gr := uniseg.NewGraphemes(sign)952	for gr.Next() {953		rs := gr.Runes()954		_screen.SetContent(w.left+w.lastX, w.top+w.lastY, rs[0], rs[1:], style.Dim(true))955		w.lastX += uniseg.StringWidth(string(rs))956	}957}958959func (w *TcellWindow) fillString(text string, pair ColorPair) FillReturn {960	style := w.pairStyle(pair)961962	for i, segment := range strings.Split(text, "\n") {963		for j, wl := range WrapLine(segment, w.lastX, w.width, w.tabstop, w.wrapSignWidth) {964			if i > 0 || j > 0 {965				w.lastY++966				if w.lastY >= w.height {967					return FillSuspend968				}969				w.lastX = 0970				if j > 0 {971					w.renderWrapSign(style)972				}973			}974			if w.lastX < w.width {975				w.renderGraphemes(wl.Text, style)976			}977		}978	}979	if w.lastX >= w.width {980		w.lastY++981		w.lastX = 0982		return FillNextLine983	}984985	return FillContinue986}987988func (w *TcellWindow) Fill(str string) FillReturn {989	return w.fillString(str, w.normal)990}991992func (w *TcellWindow) LinkBegin(uri string, params string) {993	w.uri = &uri994	w.params = &params995}996997func (w *TcellWindow) LinkEnd() {998	w.uri = nil999	w.params = nil1000}10011002func (w *TcellWindow) CFill(fg Color, bg Color, ul Color, a Attr, str string) FillReturn {1003	if fg == colDefault {1004		fg = w.normal.Fg()1005	}1006	if bg == colDefault {1007		bg = w.normal.Bg()1008	}1009	return w.fillString(str, NewColorPair(fg, bg, a).WithUl(ul))1010}10111012func (w *TcellWindow) DrawBorder() {1013	w.drawBorder(false)1014}10151016func (w *TcellWindow) DrawHBorder() {1017	w.drawBorder(true)1018}10191020// borderStyleFor returns the tcell.Style used to draw borders for `wt`, honoring1021// whether the window is rendering with colors.1022func (w *TcellWindow) borderStyleFor(wt WindowType) tcell.Style {1023	if !w.color {1024		return w.normal.style()1025	}1026	return BorderColor(wt).style()1027}10281029// drawHLine fills row `y` with `line` between optional left/right caps.1030// A zero rune means "no cap"; caps are placed at the very edges of `w`.1031// tcell has an issue displaying two overlapping wide runes, so the line1032// stops before the cap position rather than overpainting.1033func (w *TcellWindow) drawHLine(y int, line, leftCap, rightCap rune, style tcell.Style) {1034	left := w.left1035	right := left + w.width1036	hw := runeWidth(line)1037	lw := 01038	rw := 01039	if leftCap != 0 {1040		lw = runeWidth(leftCap)1041	}1042	if rightCap != 0 {1043		rw = runeWidth(rightCap)1044	}1045	for x := left + lw; x <= right-rw-hw; x += hw {1046		_screen.SetContent(x, y, line, nil, style)1047	}1048	if leftCap != 0 {1049		_screen.SetContent(left, y, leftCap, nil, style)1050	}1051	if rightCap != 0 {1052		_screen.SetContent(right-rw, y, rightCap, nil, style)1053	}1054}10551056func (w *TcellWindow) DrawHSeparator(row int, windowType WindowType, useBottom bool) {1057	if w.height == 0 {1058		return1059	}1060	shape := w.borderStyle.shape1061	if shape == BorderNone {1062		return1063	}1064	style := w.borderStyleFor(windowType)1065	line := w.borderStyle.top1066	if useBottom {1067		line = w.borderStyle.bottom1068	}1069	var leftCap, rightCap rune1070	if shape.HasLeft() {1071		leftCap = w.borderStyle.leftMid1072	}1073	if shape.HasRight() {1074		rightCap = w.borderStyle.rightMid1075	}1076	w.drawHLine(w.top+row, line, leftCap, rightCap, style)1077}10781079func (w *TcellWindow) PaintSectionFrame(topContent, bottomContent int, windowType WindowType, edge SectionEdge) {1080	if w.height == 0 {1081		return1082	}1083	shape := w.borderStyle.shape1084	if shape == BorderNone {1085		return1086	}1087	style := w.borderStyleFor(windowType)1088	left := w.left1089	right := left + w.width1090	hasLeft := shape.HasLeft()1091	hasRight := shape.HasRight()1092	leftW := runeWidth(w.borderStyle.left)1093	rightW := runeWidth(w.borderStyle.right)1094	// Content rows: overpaint the left and right verticals (+ their 1-char margin) in1095	// the section's color. Inner margin stays at whatever bg the sub-window set.1096	for row := topContent; row <= bottomContent; row++ {1097		y := w.top + row1098		if hasLeft {1099			_screen.SetContent(left, y, w.borderStyle.left, nil, style)1100			_screen.SetContent(left+leftW, y, ' ', nil, style)1101		}1102		if hasRight {1103			_screen.SetContent(right-rightW-1, y, ' ', nil, style)1104			_screen.SetContent(right-rightW, y, w.borderStyle.right, nil, style)1105		}1106	}1107	if edge == SectionEdgeTop && shape.HasTop() {1108		var leftCap, rightCap rune1109		if hasLeft {1110			leftCap = w.borderStyle.topLeft1111		}1112		if hasRight {1113			rightCap = w.borderStyle.topRight1114		}1115		w.drawHLine(w.top, w.borderStyle.top, leftCap, rightCap, style)1116	}1117	if edge == SectionEdgeBottom && shape.HasBottom() {1118		var leftCap, rightCap rune1119		if hasLeft {1120			leftCap = w.borderStyle.bottomLeft1121		}1122		if hasRight {1123			rightCap = w.borderStyle.bottomRight1124		}1125		w.drawHLine(w.top+w.height-1, w.borderStyle.bottom, leftCap, rightCap, style)1126	}1127}11281129func (w *TcellWindow) drawBorder(onlyHorizontal bool) {1130	if w.height == 0 {1131		return1132	}1133	shape := w.borderStyle.shape1134	if shape == BorderNone {1135		return1136	}11371138	left := w.left1139	right := left + w.width1140	top := w.top1141	bot := top + w.height11421143	style := w.borderStyleFor(w.windowType)11441145	hasLeft := shape.HasLeft()1146	hasRight := shape.HasRight()11471148	if shape.HasTop() {1149		var leftCap, rightCap rune1150		if hasLeft {1151			leftCap = w.borderStyle.topLeft1152		}1153		if hasRight {1154			rightCap = w.borderStyle.topRight1155		}1156		w.drawHLine(top, w.borderStyle.top, leftCap, rightCap, style)1157	}1158	if shape.HasBottom() {1159		var leftCap, rightCap rune1160		if hasLeft {1161			leftCap = w.borderStyle.bottomLeft1162		}1163		if hasRight {1164			rightCap = w.borderStyle.bottomRight1165		}1166		w.drawHLine(bot-1, w.borderStyle.bottom, leftCap, rightCap, style)1167	}1168	if !onlyHorizontal {1169		vw := runeWidth(w.borderStyle.right)1170		for y := top; y < bot; y++ {1171			// Corner rows are already painted by drawHLine above / below.1172			if (y == top && shape.HasTop()) || (y == bot-1 && shape.HasBottom()) {1173				continue1174			}1175			if hasLeft {1176				_screen.SetContent(left, y, w.borderStyle.left, nil, style)1177			}1178			if hasRight {1179				_screen.SetContent(right-vw, y, w.borderStyle.right, nil, style)1180			}1181		}1182	}1183}