Skip to content

Commit

Permalink
cursorcolor start
Browse files Browse the repository at this point in the history
  • Loading branch information
gdamore committed Mar 7, 2024
1 parent 652ba11 commit 1bdce1e
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 18 deletions.
15 changes: 7 additions & 8 deletions _demos/cursors.go
Expand Up @@ -15,7 +15,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// beep makes a beep every second until you press ESC
package main

import (
Expand Down Expand Up @@ -60,25 +59,25 @@ func main() {
switch ev.Rune() {
case '0':
s.SetContent(2, 2, '0', nil, style)
s.SetCursorStyle(tcell.CursorStyleDefault)
s.SetCursorStyle(tcell.CursorStyleDefault, tcell.ColorReset)
case '1':
s.SetContent(2, 2, '1', nil, style)
s.SetCursorStyle(tcell.CursorStyleBlinkingBlock)
s.SetCursorStyle(tcell.CursorStyleBlinkingBlock, tcell.ColorGreen)
case '2':
s.SetCell(2, 2, tcell.StyleDefault, '2')
s.SetCursorStyle(tcell.CursorStyleSteadyBlock)
s.SetCursorStyle(tcell.CursorStyleSteadyBlock, tcell.ColorBlue)
case '3':
s.SetCell(2, 2, tcell.StyleDefault, '3')
s.SetCursorStyle(tcell.CursorStyleBlinkingUnderline)
s.SetCursorStyle(tcell.CursorStyleBlinkingUnderline, tcell.ColorRed)
case '4':
s.SetCell(2, 2, tcell.StyleDefault, '4')
s.SetCursorStyle(tcell.CursorStyleSteadyUnderline)
s.SetCursorStyle(tcell.CursorStyleSteadyUnderline, tcell.ColorOrange)
case '5':
s.SetCell(2, 2, tcell.StyleDefault, '5')
s.SetCursorStyle(tcell.CursorStyleBlinkingBar)
s.SetCursorStyle(tcell.CursorStyleBlinkingBar, tcell.ColorYellow)
case '6':
s.SetCell(2, 2, tcell.StyleDefault, '6')
s.SetCursorStyle(tcell.CursorStyleSteadyBar)
s.SetCursorStyle(tcell.CursorStyleSteadyBar, tcell.ColorPink)
}
s.Show()

Expand Down
13 changes: 11 additions & 2 deletions console_win.go
Expand Up @@ -49,6 +49,7 @@ type cScreen struct {
oscreen consoleInfo
ocursor cursorInfo
cursorStyle CursorStyle
cursorColor Color
oimode uint32
oomode uint32
cells CellBuffer
Expand Down Expand Up @@ -173,6 +174,8 @@ const (
vtUnderColorReset = "\x1b[59m"
vtEnterUrl = "\x1b]8;%s;%s\x1b\\" // NB arg 1 is id, arg 2 is url
vtExitUrl = "\x1b]8;;\x1b\\"
vtCursorColorRGB = "\x1b]12;#%02x%02x%02x\007"
vtCursorColorReset = "\x1b]112\007"
)

var vtCursorStyles = map[CursorStyle]string{
Expand Down Expand Up @@ -344,6 +347,7 @@ func (s *cScreen) disengage() {

if s.vten {
s.emitVtString(vtCursorStyles[CursorStyleDefault])
s.emitVtString(vtCursorColorReset)
s.emitVtString(vtEnableAm)
if !s.disableAlt {
s.emitVtString(vtExitCA)
Expand Down Expand Up @@ -435,6 +439,12 @@ func (s *cScreen) showCursor() {
if s.vten {
s.emitVtString(vtShowCursor)
s.emitVtString(vtCursorStyles[s.cursorStyle])
if (s.cursorColor == ColorReset) {
s.emitVtString(vtCursorColorReset)
} else if s.cursorColor.Valid() {
r, g, b := s.cursorColor.TrueColor().RGB()
s.emitVtString(vtCursorColorRGB, r, g, b)

Check failure on line 446 in console_win.go

View workflow job for this annotation

GitHub Actions / build

too many arguments in call to s.emitVtString

Check failure on line 446 in console_win.go

View workflow job for this annotation

GitHub Actions / build (windows-latest)

too many arguments in call to s.emitVtString
}
} else {
s.setCursorInfo(&cursorInfo{size: 100, visible: 1})
}
Expand All @@ -458,7 +468,7 @@ func (s *cScreen) ShowCursor(x, y int) {
s.Unlock()
}

func (s *cScreen) SetCursorStyle(cs CursorStyle) {
func (s *cScreen) SetCursor(cs CursorStyle, _ Color) {
s.Lock()
if !s.fini {
if _, ok := vtCursorStyles[cs]; ok {
Expand Down Expand Up @@ -1100,7 +1110,6 @@ func (s *cScreen) setCursorInfo(info *cursorInfo) {
_, _, _ = procSetConsoleCursorInfo.Call(
uintptr(s.out),
uintptr(unsafe.Pointer(info)))

}

func (s *cScreen) setCursorPos(x, y int, vtEnable bool) {
Expand Down
17 changes: 13 additions & 4 deletions screen.go
@@ -1,4 +1,4 @@
// Copyright 2023 The TCell Authors
// Copyright 2024 The TCell Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
Expand Down Expand Up @@ -79,8 +79,9 @@ type Screen interface {

// SetCursorStyle is used to set the cursor style. If the style
// is not supported (or cursor styles are not supported at all),
// then this will have no effect.
SetCursorStyle(CursorStyle)
// then this will have no effect. Color will be changed if supplied,
// and the terminal supports doing so.
SetCursorStyle(CursorStyle, ...Color)

// Size returns the screen size as width, height. This changes in
// response to a call to Clear or Flush.
Expand Down Expand Up @@ -312,7 +313,7 @@ type screenImpl interface {
SetStyle(style Style)
ShowCursor(x int, y int)
HideCursor()
SetCursorStyle(CursorStyle)
SetCursor(CursorStyle, Color)
Size() (width, height int)
EnableMouse(...MouseFlags)
DisableMouse()
Expand Down Expand Up @@ -464,3 +465,11 @@ func (b *baseScreen) PostEvent(ev Event) error {
return ErrEventQFull
}
}

func (b *baseScreen) SetCursorStyle(cs CursorStyle, ccs ...Color) {
if len(ccs) > 0 {
b.SetCursor(cs, ccs[0])
} else {
b.SetCursor(cs, ColorNone)
}
}
4 changes: 2 additions & 2 deletions simulation.go
@@ -1,4 +1,4 @@
// Copyright 2023 The TCell Authors
// Copyright 2024 The TCell Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
Expand Down Expand Up @@ -239,7 +239,7 @@ func (s *simscreen) hideCursor() {
s.cursorvis = false
}

func (s *simscreen) SetCursorStyle(CursorStyle) {}
func (s *simscreen) SetCursor(CursorStyle, Color) {}

func (s *simscreen) Show() {
s.Lock()
Expand Down
3 changes: 3 additions & 0 deletions terminfo/terminfo.go
Expand Up @@ -227,6 +227,9 @@ type Terminfo struct {
CursorSteadyUnderline string
CursorBlinkingBar string
CursorSteadyBar string
CursorColor string // nothing uses it yet
CursorColorRGB string // Cs (but not really because Cs uses X11 color string)
CursorColorReset string // Cr
EnterUrl string
ExitUrl string
SetWindowSize string
Expand Down
30 changes: 29 additions & 1 deletion tscreen.go
Expand Up @@ -160,6 +160,9 @@ type tScreen struct {
underFg string
cursorStyles map[CursorStyle]string
cursorStyle CursorStyle
cursorColor Color
cursorRGB string
cursorFg string
saved *term.State
stopQ chan struct{}
eventQ chan Event
Expand Down Expand Up @@ -460,7 +463,20 @@ func (t *tScreen) prepareCursorStyles() {
CursorStyleSteadyBar: "\x1b[6 q",
}
}
if t.ti.CursorColorRGB != "" {
// if it was X11 style with just a single %p1%s, then convert
t.cursorRGB = t.ti.CursorColorRGB
}
if t.ti.CursorColorReset != "" {
t.cursorFg = t.ti.CursorColorReset
}
if t.cursorRGB == "" {
t.cursorRGB = "\x1b]12;%p1%s\007"
t.cursorFg = "\x1b]112\007"
}

// convert XTERM style color names to RGB color code. We have no way to do palette colors
t.cursorRGB = strings.Replace(t.cursorRGB, "%p1%s", "#%p1%02x%p2%02x%p3%02x", 1)
}

func (t *tScreen) prepareKey(key Key, val string) {
Expand Down Expand Up @@ -912,9 +928,10 @@ func (t *tScreen) ShowCursor(x, y int) {
t.Unlock()
}

func (t *tScreen) SetCursorStyle(cs CursorStyle) {
func (t *tScreen) SetCursor(cs CursorStyle, cc Color) {
t.Lock()
t.cursorStyle = cs
t.cursorColor = cc
t.Unlock()
}

Expand All @@ -937,6 +954,14 @@ func (t *tScreen) showCursor() {
t.TPuts(esc)
}
}
if t.cursorRGB != "" {
if t.cursorColor == ColorReset {
t.TPuts(t.cursorFg)
} else if t.cursorColor.Valid() {
r, g, b := t.cursorColor.RGB()
t.TPuts(t.ti.TParm(t.cursorRGB, int(r), int(g), int(b)))
}
}
t.cx = x
t.cy = y
}
Expand Down Expand Up @@ -1954,6 +1979,9 @@ func (t *tScreen) disengage() {
if t.cursorStyles != nil && t.cursorStyle != CursorStyleDefault {
t.TPuts(t.cursorStyles[CursorStyleDefault])
}
if t.cursorFg != "" && t.cursorColor.Valid() {
t.TPuts(t.cursorFg)
}
t.TPuts(ti.ResetFgBg)
t.TPuts(ti.AttrOff)
t.TPuts(ti.ExitKeypad)
Expand Down
2 changes: 1 addition & 1 deletion wscreen.go
Expand Up @@ -158,7 +158,7 @@ func (t *wScreen) ShowCursor(x, y int) {
t.Unlock()
}

func (t *wScreen) SetCursorStyle(cs CursorStyle) {
func (t *wScreen) SetCursor(cs CursorStyle, _ Color) {
t.Lock()
js.Global().Call("setCursorStyle", curStyleClasses[cs])
t.Unlock()
Expand Down

0 comments on commit 1bdce1e

Please sign in to comment.