Skip to content

Commit

Permalink
Merge pull request #624 from noborus/refactoring-again-variable-name
Browse files Browse the repository at this point in the history
(Re -revision)Change the name of the variable name
  • Loading branch information
noborus authored Sep 10, 2024
2 parents 1bbf743 + 039d5e3 commit f5e315b
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 114 deletions.
66 changes: 45 additions & 21 deletions oviewer/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func (root *Root) setViewMode(ctx context.Context, modeName string) {

// modeConfig returns the configuration of the specified mode.
func (root *Root) modeConfig(modeName string) (general, error) {
if modeName == generalName {
if modeName == nameGeneral {
return root.General, nil
}

Expand All @@ -450,27 +450,27 @@ func (root *Root) setConverter(ctx context.Context, name string) {

// alignFormat sets converter type to align.
func (root *Root) alignFormat(ctx context.Context) {
if root.Doc.Converter == alignConv {
if root.Doc.Converter == convAlign {
root.esFormat(ctx)
return
}
root.setConverter(ctx, alignConv)
root.setConverter(ctx, convAlign)
root.setMessage("Set align mode")
}

// rawFormat sets converter type to raw.
func (root *Root) rawFormat(ctx context.Context) {
if root.Doc.Converter == rawConv {
if root.Doc.Converter == convRaw {
root.esFormat(ctx)
return
}
root.setConverter(ctx, rawConv)
root.setConverter(ctx, convRaw)
root.setMessage("Set raw mode")
}

// esFormat sets converter type to es.
func (root *Root) esFormat(ctx context.Context) {
root.setConverter(ctx, esConv)
root.setConverter(ctx, convEscaped)
root.setMessage("Set es mode")
}

Expand Down Expand Up @@ -769,24 +769,48 @@ func (root *Root) bottomSectionLN(ctx context.Context) int {
return lN - (root.Doc.topLN + root.Doc.firstLine() - root.Doc.SectionStartPosition)
}

// shrinkColumnToggle shrinks or expands the current cursor column.
func (root *Root) shrinkColumnToggle(ctx context.Context) {
root.ShrinkColumn(ctx, root.Doc.columnCursor)
// ShrinkColumn shrinks the specified column.
func (root *Root) ShrinkColumn(ctx context.Context, cursor int) error {
return root.Doc.shrinkColumn(cursor, true)
}

// ShrinkColumn shrinks or expands the specified column.
func (root *Root) ShrinkColumn(ctx context.Context, cursor int) bool {
m := root.Doc
if root.Doc.Converter != alignConv {
root.setMessage("Not align mode")
return false
// ExpandColumn expands the specified column.
func (root *Root) ExpandColumn(ctx context.Context, cursor int) error {
return root.Doc.shrinkColumn(cursor, false)
}

// toggleColumnShrink shrinks or expands the current cursor column.
func (root *Root) toggleColumnShrink(ctx context.Context) {
cursor := root.Doc.columnCursor
shrink, err := root.Doc.isColumnShrink(cursor)
if err != nil {
root.setMessage(err.Error())
}
if err := root.Doc.shrinkColumn(cursor, !shrink); err != nil {
root.setMessage(err.Error())
}
}

// isColumnShrink returns whether the specified column is shrink.
func (m *Document) isColumnShrink(cursor int) (bool, error) {
if m.Converter != convAlign {
return false, ErrNotAlignMode
}
if cursor >= len(m.alignConv.columnAttrs) {
return false, ErrNoColumnSelected
}
return m.alignConv.columnAttrs[cursor].shrink, nil
}

// shinkColumn shrinks or expands the specified column.
func (m *Document) shrinkColumn(cursor int, shrink bool) error {
if m.Converter != convAlign {
return ErrNotAlignMode
}
if cursor >= len(m.alignConv.shrink) {
root.setMessage("No column selected")
return false
if cursor < 0 || cursor >= len(m.alignConv.columnAttrs) {
return ErrNoColumnSelected
}
m.alignConv.shrink[cursor] = !m.alignConv.shrink[cursor]
m.alignConv.columnAttrs[cursor].shrink = shrink
m.ClearCache()
root.ViewSync(ctx)
return true
return nil
}
36 changes: 20 additions & 16 deletions oviewer/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ func TestRoot_modeConfig(t *testing.T) {
{
name: "testModeConfig general",
args: args{
modeName: generalName,
modeName: nameGeneral,
},
want: general{},
wantErr: false,
Expand Down Expand Up @@ -1527,21 +1527,21 @@ func TestRoot_setConverter(t *testing.T) {
{
name: "testSetConverterEscape",
args: args{
name: esConv,
name: convEscaped,
},
want: newESConverter(),
},
{
name: "testSetConverterRaw",
args: args{
name: rawConv,
name: convRaw,
},
want: newRawConverter(),
},
{
name: "testSetConverterAlign",
args: args{
name: alignConv,
name: convAlign,
},
want: newAlignConverter(false),
},
Expand Down Expand Up @@ -1570,45 +1570,49 @@ func TestRoot_ShrinkColumn(t *testing.T) {
}
type args struct {
cursor int
shrink bool
}
tests := []struct {
name string
fields fields
args args
want bool
name string
fields fields
args args
wantErr bool
}{
{
name: "testShrinkColumn",
fields: fields{
fileName: filepath.Join(testdata, "MOCK_DATA.csv"),
converter: alignConv,
converter: convAlign,
},
args: args{
cursor: 0,
shrink: true,
},
want: true,
wantErr: false,
},
{
name: "testShrinkColumnFalse",
fields: fields{
fileName: filepath.Join(testdata, "MOCK_DATA.csv"),
converter: esConv,
converter: convEscaped,
},
args: args{
cursor: 0,
shrink: true,
},
want: false,
wantErr: true,
},
{
name: "testShrinkColumnOver",
fields: fields{
fileName: filepath.Join(testdata, "MOCK_DATA.csv"),
converter: alignConv,
converter: convAlign,
},
args: args{
cursor: 20,
shrink: true,
},
want: false,
wantErr: true,
},
}
for _, tt := range tests {
Expand All @@ -1619,8 +1623,8 @@ func TestRoot_ShrinkColumn(t *testing.T) {
root.Doc.Converter = tt.fields.converter
root.Doc.ColumnDelimiter = ","
root.prepareDraw(ctx)
if got := root.ShrinkColumn(ctx, tt.args.cursor); got != tt.want {
t.Errorf("Root.ShrinkColumn() = %v, want %v", got, tt.want)
if err := root.Doc.shrinkColumn(tt.args.cursor, tt.args.shrink); (err != nil) != tt.wantErr {
t.Errorf("Root.ShrinkColumn() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
Expand Down
34 changes: 19 additions & 15 deletions oviewer/convert_align.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,28 @@ import (
// It is used to align columns when the delimiter is reached or to align columns by adding spaces to the end of the line.
type align struct {
es *escapeSequence
orgWidths []int // Original width of each column. This is the width determined by Guesswidth.
maxWidths []int // Maximum width of each column.
orgWidths []int
shrink []bool // Shrink column.
rightAlign []bool // Right align column.
columnAttrs []columnAttribute
WidthF bool
delimiter string
delimiterReg *regexp.Regexp
count int
}

// columnAttribute is a structure that holds the attributes of a column.
type columnAttribute struct {
shrink bool // Shrink column.
rightAlign bool // Right align column.
}

func newAlignConverter(widthF bool) *align {
return &align{
es: newESConverter(),
maxWidths: []int{},
shrink: []bool{},
rightAlign: []bool{},
count: 0,
WidthF: widthF,
es: newESConverter(),
maxWidths: []int{},
columnAttrs: []columnAttribute{},
count: 0,
WidthF: widthF,
}
}

Expand Down Expand Up @@ -185,17 +189,17 @@ func appendSpaces(lc contents, num int) contents {
}

func (a *align) isShrink(col int) bool {
if len(a.shrink) <= col {
return false
if col >= 0 && col < len(a.columnAttrs) {
return a.columnAttrs[col].shrink
}
return a.shrink[col]
return false
}

func (a *align) isRightAlign(col int) bool {
if len(a.rightAlign) <= col {
return false
if col >= 0 && col < len(a.columnAttrs) {
return a.columnAttrs[col].rightAlign
}
return a.rightAlign[col]
return false
}

func countLeftSpaces(lc contents, s int) int {
Expand Down
Loading

0 comments on commit f5e315b

Please sign in to comment.