Skip to content

Commit a6b3e1e

Browse files
authored
Merge pull request #496 from noborus/preventive-fixes
Added proactive fixes and tests
2 parents 335fec2 + 5551e9e commit a6b3e1e

File tree

6 files changed

+99
-67
lines changed

6 files changed

+99
-67
lines changed

oviewer/content.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -495,37 +495,40 @@ func ContentsToStr(lc contents) (string, widthPos) {
495495
for ; i <= bn; i++ {
496496
pos = append(pos, n)
497497
}
498-
mn, err := buff.WriteRune(c.mainc)
499-
if err != nil {
500-
log.Println(err)
501-
}
502-
bn += mn
498+
bn += writeRune(&buff, c.mainc)
503499
for _, r := range c.combc {
504-
cn, err := buff.WriteRune(r)
505-
if err != nil {
506-
log.Println(err)
507-
}
508-
bn += cn
500+
bn += writeRune(&buff, r)
509501
}
510502
}
503+
511504
str := buff.String()
512505
for ; i <= bn; i++ {
513506
pos = append(pos, len(lc))
514507
}
515508
return str, pos
516509
}
517510

511+
func writeRune(w *strings.Builder, r rune) int {
512+
n, err := w.WriteRune(r)
513+
if err != nil {
514+
log.Fatal(err)
515+
}
516+
return n
517+
}
518+
518519
// x returns the x position on the screen.
519-
func (pos widthPos) x(x int) int {
520-
if x < len(pos) {
521-
return pos[x]
520+
// [n]byte -> x.
521+
func (pos widthPos) x(n int) int {
522+
if n < len(pos) {
523+
return pos[n]
522524
}
523525
return pos[len(pos)-1]
524526
}
525527

526528
// n return string position from content.
529+
// x -> [n]byte.
527530
func (pos widthPos) n(w int) int {
528-
var x int
531+
x := w
529532
for _, c := range pos {
530533
if c >= w {
531534
x = c

oviewer/content_test.go

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,7 @@ func Test_parseStringOverlapping(t *testing.T) {
196196
})
197197
}
198198
}
199-
200-
func Test_parseStringStyle(t *testing.T) {
199+
func Test_parseStringStyle1(t *testing.T) {
201200
t.Parallel()
202201
type args struct {
203202
line string
@@ -355,6 +354,47 @@ func Test_parseStringStyle(t *testing.T) {
355354
}
356355
}
357356

357+
func Test_parseStringStyle2(t *testing.T) {
358+
type args struct {
359+
str string
360+
tabWidth int
361+
}
362+
tests := []struct {
363+
name string
364+
args args
365+
want contents
366+
}{
367+
{
368+
name: "OverLine",
369+
args: args{
370+
str: "\x1B[53mol\x1B[m", tabWidth: 8,
371+
},
372+
want: contents{
373+
{width: 1, style: tcell.StyleDefault.Underline(true), mainc: rune('o'), combc: nil},
374+
{width: 1, style: tcell.StyleDefault.Underline(true), mainc: rune('l'), combc: nil},
375+
},
376+
},
377+
{
378+
name: "UnOverLine",
379+
args: args{
380+
str: "\x1B[53mo\x1B[m\x1B[55mu\x1B[m\x1B[53ml\x1B[m", tabWidth: 8,
381+
},
382+
want: contents{
383+
{width: 1, style: tcell.StyleDefault.Underline(true), mainc: rune('o'), combc: nil},
384+
{width: 1, style: tcell.StyleDefault.Underline(false), mainc: rune('u'), combc: nil},
385+
{width: 1, style: tcell.StyleDefault.Underline(true), mainc: rune('l'), combc: nil},
386+
},
387+
},
388+
}
389+
for _, tt := range tests {
390+
t.Run(tt.name, func(t *testing.T) {
391+
if got := parseString(tt.args.str, tt.args.tabWidth); !reflect.DeepEqual(got, tt.want) {
392+
t.Errorf("parseString() = %#v, want %#v", got, tt.want)
393+
}
394+
})
395+
}
396+
}
397+
358398
func Test_parseStringUnStyle(t *testing.T) {
359399
t.Parallel()
360400
type args struct {
@@ -780,11 +820,17 @@ func Test_widthPos_x(t *testing.T) {
780820
want: 8,
781821
},
782822
{
783-
name: "testあいうえお",
823+
name: "あいうえお",
784824
pos: widthPos{0, 2, 2, 2, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10, 10, 10},
785825
args: args{12},
786826
want: 8,
787827
},
828+
{
829+
name: "あいうえお2",
830+
pos: widthPos{0, 2, 2, 2, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10, 10, 10},
831+
args: args{20},
832+
want: 10,
833+
},
788834
}
789835
for _, tt := range tests {
790836
tt := tt
@@ -815,17 +861,23 @@ func Test_widthPos_n(t *testing.T) {
815861
want: 2,
816862
},
817863
{
818-
name: "testあいうえお",
864+
name: "あいうえお",
819865
pos: widthPos{0, 2, 2, 2, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10, 10, 10},
820866
args: args{8},
821867
want: 12,
822868
},
823869
{
824-
name: "test2あいうえお",
870+
name: "あいうえお2",
825871
pos: widthPos{0, 2, 2, 2, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10, 10, 10},
826872
args: args{9},
827873
want: 15,
828874
},
875+
{
876+
name: "あいうえお3",
877+
pos: widthPos{0, 2, 2, 2, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10, 10, 10},
878+
args: args{20},
879+
want: 15,
880+
},
829881
}
830882
for _, tt := range tests {
831883
tt := tt

oviewer/event.go

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,6 @@ func (root *Root) sendGoto(num int) {
111111

112112
// MoveLine fires an eventGoto event that moves to the specified line.
113113
func (root *Root) MoveLine(num int) {
114-
if !root.checkScreen() {
115-
return
116-
}
117114
root.sendGoto(num)
118115
}
119116

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

167-
// checkScreen returns true if the screen is ready.
168-
// checkScreen is used in case it is called directly from the outside.
169-
// True if called from the event loop.
170-
func (root *Root) checkScreen() bool {
171-
if root == nil {
172-
return false
173-
}
174-
return root.Screen != nil
175-
}
176-
177164
// eventAppQuit represents a quit event.
178165
type eventAppQuit struct {
179166
tcell.EventTime
@@ -185,9 +172,6 @@ func (root *Root) Quit() {
185172
}
186173

187174
func (root *Root) sendQuit() {
188-
if !root.checkScreen() {
189-
return
190-
}
191175
ev := &eventAppQuit{}
192176
ev.SetEventNow()
193177
root.postEvent(ev)
@@ -204,9 +188,6 @@ func (root *Root) Suspend() {
204188
}
205189

206190
func (root *Root) sendSuspend() {
207-
if !root.checkScreen() {
208-
return
209-
}
210191
ev := &eventAppSuspend{}
211192
ev.SetEventNow()
212193
root.postEvent(ev)
@@ -236,9 +217,6 @@ func (root *Root) regularUpdate() {
236217
}
237218

238219
func (root *Root) sendUpdateEndNum() {
239-
if !root.checkScreen() {
240-
return
241-
}
242220
if !root.hasDocChanged() {
243221
return
244222
}
@@ -258,10 +236,6 @@ type eventDocument struct {
258236

259237
// SetDocument fires the eventDocument event.
260238
func (root *Root) SetDocument(docNum int) {
261-
if !root.checkScreen() {
262-
return
263-
}
264-
265239
if docNum < 0 || docNum < root.DocumentLen() {
266240
return
267241
}
@@ -287,9 +261,6 @@ func (root *Root) AddDocument(m *Document) {
287261
}
288262

289263
func (root *Root) sendAddDocument(m *Document) {
290-
if !root.checkScreen() {
291-
return
292-
}
293264
ev := &eventAddDocument{}
294265
ev.m = m
295266
ev.SetEventNow()
@@ -307,9 +278,6 @@ func (root *Root) CloseDocument(m *Document) {
307278
}
308279

309280
func (root *Root) sendCloseDocument() {
310-
if !root.checkScreen() {
311-
return
312-
}
313281
ev := &eventCloseDocument{}
314282
ev.SetEventNow()
315283
root.postEvent(ev)
@@ -327,9 +295,6 @@ func (root *Root) Reload() {
327295
}
328296

329297
func (root *Root) sendReload(m *Document) {
330-
if !root.checkScreen() {
331-
return
332-
}
333298
ev := &eventReload{}
334299
ev.m = m
335300
ev.SetEventNow()
@@ -345,8 +310,22 @@ func (root *Root) releaseEventBuffer() {
345310

346311
// postEvent is a wrapper for tcell.Event.
347312
func (root *Root) postEvent(ev tcell.Event) {
313+
if !root.checkScreen() {
314+
return
315+
}
316+
348317
if err := root.Screen.PostEvent(ev); err != nil {
349318
log.Printf("postEvent %s", err)
350319
root.releaseEventBuffer()
351320
}
352321
}
322+
323+
// checkScreen returns true if the screen is ready.
324+
// checkScreen is used in case it is called directly from the outside.
325+
// True if called from the event loop.
326+
func (root *Root) checkScreen() bool {
327+
if root == nil {
328+
return false
329+
}
330+
return root.Screen != nil
331+
}

oviewer/mouse.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,6 @@ func (root *Root) CopySelect() {
144144
}
145145

146146
func (root *Root) sendCopySelect() {
147-
if !root.checkScreen() {
148-
return
149-
}
150147
ev := &eventCopySelect{}
151148
ev.SetEventNow()
152149
root.postEvent(ev)
@@ -193,9 +190,6 @@ func (root *Root) Paste() {
193190
}
194191

195192
func (root *Root) sendPaste() {
196-
if !root.checkScreen() {
197-
return
198-
}
199193
ev := &eventPaste{}
200194
ev.SetEventNow()
201195
root.postEvent(ev)
@@ -415,7 +409,7 @@ func (scr SCR) selectLine(line LineC, x1 int, x2 int) string {
415409
start := line.pos.n(x1)
416410
end := line.pos.n(x2)
417411

418-
if start > len(line.str) || end > len(line.str) {
412+
if start > len(line.str) || end > len(line.str) || start > end {
419413
log.Printf("selectLine:len(%d):start(%d):end(%d)", len(line.str), start, end)
420414
return ""
421415
}

oviewer/oviewer.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,16 @@ func applyStyle(style tcell.Style, s OVStyle) tcell.Style {
813813
if s.UnStrikeThrough {
814814
style = style.StrikeThrough(false)
815815
}
816+
if s.OverLine {
817+
// tcell does not support overline.
818+
//style = style.OverLine(true)
819+
style = style.Underline(true)
820+
}
821+
if s.UnOverLine {
822+
// tcell does not support unOverline.
823+
//style = style.UnOverLine(false)
824+
style = style.Underline(false)
825+
}
816826
return style
817827
}
818828

oviewer/search.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -639,9 +639,6 @@ func (root *Root) Search(str string) {
639639
}
640640

641641
func (root *Root) sendForwardSearch(str string) {
642-
if !root.checkScreen() {
643-
return
644-
}
645642
ev := &eventNextSearch{}
646643
ev.str = str
647644
ev.SetEventNow()
@@ -656,9 +653,6 @@ func (root *Root) BackSearch(str string) {
656653
}
657654

658655
func (root *Root) sendBackSearch(str string) {
659-
if !root.checkScreen() {
660-
return
661-
}
662656
ev := &eventNextBackSearch{}
663657
ev.str = str
664658
ev.SetEventNow()

0 commit comments

Comments
 (0)