diff --git a/oviewer/convert_align.go b/oviewer/convert_align.go index 8e90267..d0dc0f6 100644 --- a/oviewer/convert_align.go +++ b/oviewer/convert_align.go @@ -73,13 +73,20 @@ func (a *align) convertDelm(st *parseState) bool { lc := make(contents, 0, len(st.lc)) for c := 0; c < len(indexes); c++ { e := pos.x(indexes[c][0]) - lc = append(lc, st.lc[s:e]...) width := e - s + if width <= 0 { + break + } + lc = append(lc, st.lc[s:e]...) + s = e + + if c >= len(a.maxWidths) { + continue + } // Add space to align columns. for ; width < a.maxWidths[c]+1; width++ { lc = append(lc, SpaceContent) } - s = e } lc = append(lc, st.lc[s:]...) st.lc = lc @@ -91,19 +98,23 @@ func (a *align) convertDelm(st *parseState) bool { func (a *align) convertWidth(st *parseState) bool { s := 0 lc := make(contents, 0, len(st.lc)) - for i := 0; i < len(a.orgWidths); i++ { - e := findColumnEnd(st.lc, a.orgWidths, i) + 1 + for c := 0; c < len(a.orgWidths); c++ { + e := findColumnEnd(st.lc, a.orgWidths, c) + 1 e = min(e, len(st.lc)) width := e - s - if s >= e { + if width <= 0 { break } lc = append(lc, st.lc[s:e]...) + s = e + + if c >= len(a.maxWidths) { + continue + } // Add space to align columns. - for ; width <= a.maxWidths[i]; width++ { + for ; width <= a.maxWidths[c]; width++ { lc = append(lc, SpaceContent) } - s = e } lc = append(lc, st.lc[s:]...) st.lc = lc diff --git a/oviewer/convert_align_test.go b/oviewer/convert_align_test.go index 4dbb086..d804061 100644 --- a/oviewer/convert_align_test.go +++ b/oviewer/convert_align_test.go @@ -115,7 +115,6 @@ func Test_align_convert(t *testing.T) { want: false, wantStr: "a,b,", }, - { name: "convertAlignWidth", fields: fields{ @@ -156,3 +155,125 @@ func Test_align_convert(t *testing.T) { }) } } + +func Test_align_convertDelm(t *testing.T) { + type fields struct { + es *escapeSequence + maxWidths []int + orgWidths []int + WidthF bool + delimiter string + delimiterReg *regexp.Regexp + count int + } + type args struct { + st *parseState + } + tests := []struct { + name string + fields fields + args args + want bool + wantStr string + }{ + { + name: "convertAlignDelm", + fields: fields{ + es: newESConverter(), + maxWidths: []int{1, 2}, + WidthF: false, + delimiter: ",", + count: 0, + }, + args: args{ + st: &parseState{ + lc: StrToContents("a,b,c,d,e,f", 8), + mainc: '\n', + }, + }, + want: false, + wantStr: "a ,b ,c,d,e,f", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + a := &align{ + es: tt.fields.es, + maxWidths: tt.fields.maxWidths, + orgWidths: tt.fields.orgWidths, + WidthF: tt.fields.WidthF, + delimiter: tt.fields.delimiter, + delimiterReg: tt.fields.delimiterReg, + count: tt.fields.count, + } + if got := a.convertDelm(tt.args.st); got != tt.want { + t.Errorf("align.convertDelm() = %v, want %v", got, tt.want) + } + goStr, _ := ContentsToStr(tt.args.st.lc) + if goStr != tt.wantStr { + t.Errorf("align.convertDelm() = %v, want %v", goStr, tt.wantStr) + } + }) + } +} + +func Test_align_convertWidth(t *testing.T) { + type fields struct { + es *escapeSequence + maxWidths []int + orgWidths []int + WidthF bool + delimiter string + delimiterReg *regexp.Regexp + count int + } + type args struct { + st *parseState + } + tests := []struct { + name string + fields fields + args args + want bool + wantStr string + }{ + { + name: "convertAlignWidth", + fields: fields{ + es: newESConverter(), + maxWidths: []int{3, 3}, + orgWidths: []int{2, 5, 8, 11, 14, 17}, + WidthF: true, + count: 0, + }, + args: args{ + st: &parseState{ + lc: StrToContents("a b c d e f", 8), + mainc: '\n', + }, + }, + want: false, + wantStr: "a b c d e f", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + a := &align{ + es: tt.fields.es, + maxWidths: tt.fields.maxWidths, + orgWidths: tt.fields.orgWidths, + WidthF: tt.fields.WidthF, + delimiter: tt.fields.delimiter, + delimiterReg: tt.fields.delimiterReg, + count: tt.fields.count, + } + if got := a.convertWidth(tt.args.st); got != tt.want { + t.Errorf("align.convertWidth() = %v, want %v", got, tt.want) + } + goStr, _ := ContentsToStr(tt.args.st.lc) + if goStr != tt.wantStr { + t.Errorf("align.convertWidth() = %v, want %v", goStr, tt.wantStr) + } + }) + } +}