Skip to content

Commit

Permalink
Merge pull request #496 from noborus/preventive-fixes
Browse files Browse the repository at this point in the history
Added proactive fixes and tests
  • Loading branch information
noborus authored Feb 18, 2024
2 parents 335fec2 + 5551e9e commit a6b3e1e
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 67 deletions.
31 changes: 17 additions & 14 deletions oviewer/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,37 +495,40 @@ func ContentsToStr(lc contents) (string, widthPos) {
for ; i <= bn; i++ {
pos = append(pos, n)
}
mn, err := buff.WriteRune(c.mainc)
if err != nil {
log.Println(err)
}
bn += mn
bn += writeRune(&buff, c.mainc)
for _, r := range c.combc {
cn, err := buff.WriteRune(r)
if err != nil {
log.Println(err)
}
bn += cn
bn += writeRune(&buff, r)
}
}

str := buff.String()
for ; i <= bn; i++ {
pos = append(pos, len(lc))
}
return str, pos
}

func writeRune(w *strings.Builder, r rune) int {
n, err := w.WriteRune(r)
if err != nil {
log.Fatal(err)
}
return n
}

// x returns the x position on the screen.
func (pos widthPos) x(x int) int {
if x < len(pos) {
return pos[x]
// [n]byte -> x.
func (pos widthPos) x(n int) int {
if n < len(pos) {
return pos[n]
}
return pos[len(pos)-1]
}

// n return string position from content.
// x -> [n]byte.
func (pos widthPos) n(w int) int {
var x int
x := w
for _, c := range pos {
if c >= w {
x = c
Expand Down
62 changes: 57 additions & 5 deletions oviewer/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ func Test_parseStringOverlapping(t *testing.T) {
})
}
}

func Test_parseStringStyle(t *testing.T) {
func Test_parseStringStyle1(t *testing.T) {
t.Parallel()
type args struct {
line string
Expand Down Expand Up @@ -355,6 +354,47 @@ func Test_parseStringStyle(t *testing.T) {
}
}

func Test_parseStringStyle2(t *testing.T) {
type args struct {
str string
tabWidth int
}
tests := []struct {
name string
args args
want contents
}{
{
name: "OverLine",
args: args{
str: "\x1B[53mol\x1B[m", tabWidth: 8,
},
want: contents{
{width: 1, style: tcell.StyleDefault.Underline(true), mainc: rune('o'), combc: nil},
{width: 1, style: tcell.StyleDefault.Underline(true), mainc: rune('l'), combc: nil},
},
},
{
name: "UnOverLine",
args: args{
str: "\x1B[53mo\x1B[m\x1B[55mu\x1B[m\x1B[53ml\x1B[m", tabWidth: 8,
},
want: contents{
{width: 1, style: tcell.StyleDefault.Underline(true), mainc: rune('o'), combc: nil},
{width: 1, style: tcell.StyleDefault.Underline(false), mainc: rune('u'), combc: nil},
{width: 1, style: tcell.StyleDefault.Underline(true), mainc: rune('l'), combc: nil},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := parseString(tt.args.str, tt.args.tabWidth); !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseString() = %#v, want %#v", got, tt.want)
}
})
}
}

func Test_parseStringUnStyle(t *testing.T) {
t.Parallel()
type args struct {
Expand Down Expand Up @@ -780,11 +820,17 @@ func Test_widthPos_x(t *testing.T) {
want: 8,
},
{
name: "testあいうえお",
name: "あいうえお",
pos: widthPos{0, 2, 2, 2, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10, 10, 10},
args: args{12},
want: 8,
},
{
name: "あいうえお2",
pos: widthPos{0, 2, 2, 2, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10, 10, 10},
args: args{20},
want: 10,
},
}
for _, tt := range tests {
tt := tt
Expand Down Expand Up @@ -815,17 +861,23 @@ func Test_widthPos_n(t *testing.T) {
want: 2,
},
{
name: "testあいうえお",
name: "あいうえお",
pos: widthPos{0, 2, 2, 2, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10, 10, 10},
args: args{8},
want: 12,
},
{
name: "test2あいうえお",
name: "あいうえお2",
pos: widthPos{0, 2, 2, 2, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10, 10, 10},
args: args{9},
want: 15,
},
{
name: "あいうえお3",
pos: widthPos{0, 2, 2, 2, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10, 10, 10},
args: args{20},
want: 15,
},
}
for _, tt := range tests {
tt := tt
Expand Down
49 changes: 14 additions & 35 deletions oviewer/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ func (root *Root) sendGoto(num int) {

// MoveLine fires an eventGoto event that moves to the specified line.
func (root *Root) MoveLine(num int) {
if !root.checkScreen() {
return
}
root.sendGoto(num)
}

Expand Down Expand Up @@ -164,16 +161,6 @@ func (root *Root) keyEvent(ctx context.Context, ev *tcell.EventKey) {
}
}

// checkScreen returns true if the screen is ready.
// checkScreen is used in case it is called directly from the outside.
// True if called from the event loop.
func (root *Root) checkScreen() bool {
if root == nil {
return false
}
return root.Screen != nil
}

// eventAppQuit represents a quit event.
type eventAppQuit struct {
tcell.EventTime
Expand All @@ -185,9 +172,6 @@ func (root *Root) Quit() {
}

func (root *Root) sendQuit() {
if !root.checkScreen() {
return
}
ev := &eventAppQuit{}
ev.SetEventNow()
root.postEvent(ev)
Expand All @@ -204,9 +188,6 @@ func (root *Root) Suspend() {
}

func (root *Root) sendSuspend() {
if !root.checkScreen() {
return
}
ev := &eventAppSuspend{}
ev.SetEventNow()
root.postEvent(ev)
Expand Down Expand Up @@ -236,9 +217,6 @@ func (root *Root) regularUpdate() {
}

func (root *Root) sendUpdateEndNum() {
if !root.checkScreen() {
return
}
if !root.hasDocChanged() {
return
}
Expand All @@ -258,10 +236,6 @@ type eventDocument struct {

// SetDocument fires the eventDocument event.
func (root *Root) SetDocument(docNum int) {
if !root.checkScreen() {
return
}

if docNum < 0 || docNum < root.DocumentLen() {
return
}
Expand All @@ -287,9 +261,6 @@ func (root *Root) AddDocument(m *Document) {
}

func (root *Root) sendAddDocument(m *Document) {
if !root.checkScreen() {
return
}
ev := &eventAddDocument{}
ev.m = m
ev.SetEventNow()
Expand All @@ -307,9 +278,6 @@ func (root *Root) CloseDocument(m *Document) {
}

func (root *Root) sendCloseDocument() {
if !root.checkScreen() {
return
}
ev := &eventCloseDocument{}
ev.SetEventNow()
root.postEvent(ev)
Expand All @@ -327,9 +295,6 @@ func (root *Root) Reload() {
}

func (root *Root) sendReload(m *Document) {
if !root.checkScreen() {
return
}
ev := &eventReload{}
ev.m = m
ev.SetEventNow()
Expand All @@ -345,8 +310,22 @@ func (root *Root) releaseEventBuffer() {

// postEvent is a wrapper for tcell.Event.
func (root *Root) postEvent(ev tcell.Event) {
if !root.checkScreen() {
return
}

if err := root.Screen.PostEvent(ev); err != nil {
log.Printf("postEvent %s", err)
root.releaseEventBuffer()
}
}

// checkScreen returns true if the screen is ready.
// checkScreen is used in case it is called directly from the outside.
// True if called from the event loop.
func (root *Root) checkScreen() bool {
if root == nil {
return false
}
return root.Screen != nil
}
8 changes: 1 addition & 7 deletions oviewer/mouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,6 @@ func (root *Root) CopySelect() {
}

func (root *Root) sendCopySelect() {
if !root.checkScreen() {
return
}
ev := &eventCopySelect{}
ev.SetEventNow()
root.postEvent(ev)
Expand Down Expand Up @@ -193,9 +190,6 @@ func (root *Root) Paste() {
}

func (root *Root) sendPaste() {
if !root.checkScreen() {
return
}
ev := &eventPaste{}
ev.SetEventNow()
root.postEvent(ev)
Expand Down Expand Up @@ -415,7 +409,7 @@ func (scr SCR) selectLine(line LineC, x1 int, x2 int) string {
start := line.pos.n(x1)
end := line.pos.n(x2)

if start > len(line.str) || end > len(line.str) {
if start > len(line.str) || end > len(line.str) || start > end {
log.Printf("selectLine:len(%d):start(%d):end(%d)", len(line.str), start, end)
return ""
}
Expand Down
10 changes: 10 additions & 0 deletions oviewer/oviewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,16 @@ func applyStyle(style tcell.Style, s OVStyle) tcell.Style {
if s.UnStrikeThrough {
style = style.StrikeThrough(false)
}
if s.OverLine {
// tcell does not support overline.
//style = style.OverLine(true)
style = style.Underline(true)
}
if s.UnOverLine {
// tcell does not support unOverline.
//style = style.UnOverLine(false)
style = style.Underline(false)
}
return style
}

Expand Down
6 changes: 0 additions & 6 deletions oviewer/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,9 +639,6 @@ func (root *Root) Search(str string) {
}

func (root *Root) sendForwardSearch(str string) {
if !root.checkScreen() {
return
}
ev := &eventNextSearch{}
ev.str = str
ev.SetEventNow()
Expand All @@ -656,9 +653,6 @@ func (root *Root) BackSearch(str string) {
}

func (root *Root) sendBackSearch(str string) {
if !root.checkScreen() {
return
}
ev := &eventNextBackSearch{}
ev.str = str
ev.SetEventNow()
Expand Down

0 comments on commit a6b3e1e

Please sign in to comment.