Skip to content

Commit

Permalink
More refactoring, common cell buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
gdamore committed Dec 4, 2023
1 parent 602a473 commit d82c270
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 89 deletions.
23 changes: 4 additions & 19 deletions console_win.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,21 +894,6 @@ func (s *cScreen) mapStyle(style Style) uint16 {
return attr
}

func (s *cScreen) SetContent(x, y int, primary rune, combining []rune, style Style) {
s.Lock()
if !s.fini {
s.cells.SetContent(x, y, primary, combining, style)
}
s.Unlock()
}

func (s *cScreen) GetContent(x, y int) (rune, []rune, Style, int) {
s.Lock()
primary, combining, style, width := s.cells.GetContent(x, y)
s.Unlock()
return primary, combining, style, width
}

func (s *cScreen) sendVtStyle(style Style) {
esc := &strings.Builder{}

Expand Down Expand Up @@ -1146,10 +1131,6 @@ func (s *cScreen) resize() {
_ = s.PostEvent(NewEventResize(w, h))
}

func (s *cScreen) Fill(r rune, style Style) {
s.cells.Fill(r, style)
}

func (s *cScreen) clearScreen(style Style, vtEnable bool) {
if vtEnable {
s.sendVtStyle(style)
Expand Down Expand Up @@ -1328,3 +1309,7 @@ func (s *cScreen) LockRegion(x, y, width, height int, lock bool) {
func (s *cScreen) Tty() (Tty, bool) {
return nil, false
}

func (s *cScreen) GetCells() *CellBuffer {
return &s.cells

Check warning on line 1314 in console_win.go

View check run for this annotation

Codecov / codecov/patch

console_win.go#L1313-L1314

Added lines #L1313 - L1314 were not covered by tests
}
49 changes: 46 additions & 3 deletions screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package tcell

import "sync"

// Screen represents the physical (or emulated) screen.
// This can be a terminal window or a physical console. Platforms implement
// this differently.
Expand Down Expand Up @@ -307,9 +309,6 @@ const (
type screenImpl interface {
Init() error
Fini()
Fill(rune, Style)
GetContent(x, y int) (primary rune, combining []rune, style Style, width int)
SetContent(x int, y int, primary rune, combining []rune, style Style)
SetStyle(style Style)
ShowCursor(x int, y int)
HideCursor()
Expand Down Expand Up @@ -342,6 +341,17 @@ type screenImpl interface {
SetSize(int, int)
LockRegion(x, y, width, height int, lock bool)
Tty() (Tty, bool)

// Following methods are not part of the Screen api, but are used for interaction with
// the common layer code.

// Locker locks the underlying data structures so that we can access them
// in a thread-safe way.
sync.Locker

// GetCells returns a pointer to the underlying CellBuffer that the implementation uses.
// Various methods will write to these for performance, but will use the lock to do so.
GetCells() *CellBuffer
}

type baseScreen struct {
Expand All @@ -359,3 +369,36 @@ func (b *baseScreen) SetCell(x int, y int, style Style, ch ...rune) {
func (b *baseScreen) Clear() {
b.Fill(' ', StyleDefault)
}

func (b *baseScreen) Fill(r rune, style Style) {
cb := b.GetCells()
b.Lock()
for i := range cb.cells {
c := &cb.cells[i]
c.currMain = r
c.currComb = nil
c.currStyle = style
c.width = 1
}
b.Unlock()
}

func (b *baseScreen) SetContent(x, y int, mainc rune, combc []rune, st Style) {

cells := b.GetCells()
b.Lock()
cells.SetContent(x, y, mainc, combc, st)
b.Unlock()
}

func (b *baseScreen) GetContent(x, y int) (rune, []rune, Style, int) {
var primary rune
var combining []rune
var style Style
var width int
cells := b.GetCells()
b.Lock()
primary, combining, style, width = cells.GetContent(x, y)
b.Unlock()
return primary, combining, style, width

Check warning on line 403 in screen.go

View check run for this annotation

Codecov / codecov/patch

screen.go#L394-L403

Added lines #L394 - L403 were not covered by tests
}
30 changes: 5 additions & 25 deletions simulation.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 The TCell Authors
// Copyright 2023 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 @@ -149,30 +149,6 @@ func (s *simscreen) SetStyle(style Style) {
s.Unlock()
}

func (s *simscreen) Fill(r rune, style Style) {
s.Lock()
s.back.Fill(r, style)
s.Unlock()
}

func (s *simscreen) SetContent(x, y int, mainc rune, combc []rune, st Style) {

s.Lock()
s.back.SetContent(x, y, mainc, combc, st)
s.Unlock()
}

func (s *simscreen) GetContent(x, y int) (rune, []rune, Style, int) {
var mainc rune
var combc []rune
var style Style
var width int
s.Lock()
mainc, combc, style, width = s.back.GetContent(x, y)
s.Unlock()
return mainc, combc, style, width
}

func (s *simscreen) drawCell(x, y int) int {

mainc, combc, style, width := s.back.GetContent(x, y)
Expand Down Expand Up @@ -558,3 +534,7 @@ func (s *simscreen) LockRegion(x, y, width, height int, lock bool) {
func (s *simscreen) Tty() (Tty, bool) {
return nil, false
}

func (s *simscreen) GetCells() *CellBuffer {
return &s.back
}
27 changes: 4 additions & 23 deletions tscreen.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,29 +595,6 @@ func (t *tScreen) SetStyle(style Style) {
t.Unlock()
}

func (t *tScreen) Fill(r rune, style Style) {
t.Lock()
if !t.fini {
t.cells.Fill(r, style)
}
t.Unlock()
}

func (t *tScreen) SetContent(x, y int, mainc rune, combc []rune, style Style) {
t.Lock()
if !t.fini {
t.cells.SetContent(x, y, mainc, combc, style)
}
t.Unlock()
}

func (t *tScreen) GetContent(x, y int) (rune, []rune, Style, int) {
t.Lock()
mainc, combc, style, width := t.cells.GetContent(x, y)
t.Unlock()
return mainc, combc, style, width
}

func (t *tScreen) encodeRune(r rune, buf []byte) []byte {

nb := make([]byte, 6)
Expand Down Expand Up @@ -1960,3 +1937,7 @@ func (t *tScreen) finalize() {
t.disengage()
_ = t.tty.Close()
}

func (t *tScreen) GetCells() *CellBuffer {
return &t.cells

Check warning on line 1942 in tscreen.go

View check run for this annotation

Codecov / codecov/patch

tscreen.go#L1941-L1942

Added lines #L1941 - L1942 were not covered by tests
}
23 changes: 4 additions & 19 deletions wscreen.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,6 @@ func (t *wScreen) SetStyle(style Style) {
t.Unlock()
}

func (t *wScreen) Fill(r rune, style Style) {
t.Lock()
t.cells.Fill(r, style)
t.Unlock()
}

func (t *wScreen) SetContent(x, y int, mainc rune, combc []rune, style Style) {
t.Lock()
t.cells.SetContent(x, y, mainc, combc, style)
t.Unlock()
}

func (t *wScreen) GetContent(x, y int) (rune, []rune, Style, int) {
t.Lock()
mainc, combc, style, width := t.cells.GetContent(x, y)
t.Unlock()
return mainc, combc, style, width
}

// paletteColor gives a more natural palette color actually matching
// typical XTerm. We might in the future want to permit styling these
// via CSS.
Expand Down Expand Up @@ -569,6 +550,10 @@ func (t *wScreen) Tty() (Tty, bool) {
return nil, false
}

func (t *wScreen) GetCells() *CellBuffer {
return &t.cells
}

// WebKeyNames maps string names reported from HTML
// (KeyboardEvent.key) to tcell accepted keys.
var WebKeyNames = map[string]Key{
Expand Down

0 comments on commit d82c270

Please sign in to comment.