Skip to content

Commit

Permalink
(Re -revision)Change the name of the variable name
Browse files Browse the repository at this point in the history
Change the general name forward for variables.
Add ColumnAttribute and put shink and right align inside.
Added ShrinkColumn and ExpandColumn.
  • Loading branch information
noborus committed Sep 8, 2024
1 parent 1bbf743 commit e2d9c06
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 112 deletions.
51 changes: 30 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,33 @@ 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(ctx, 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(ctx, cursor, false)
}

// toggleColumnShrink shrinks or expands the current cursor column.
func (root *Root) toggleColumnShrink(ctx context.Context) {
cursor := root.Doc.columnCursor
if err := root.Doc.shrinkColumn(ctx, cursor, !root.Doc.alignConv.columnAttrs[cursor].shrink); err != nil {
root.setMessage(err.Error())
}
if cursor >= len(m.alignConv.shrink) {
root.setMessage("No column selected")
return false
}

// shinkColumn shrinks or expands the specified column.
func (m *Document) shrinkColumn(ctx context.Context, cursor int, shrink bool) error {
if m.Converter != convAlign {
return ErrNotAlignMode
}
if 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(ctx, tt.args.cursor, tt.args.shrink); (err != nil) != tt.wantErr {
t.Errorf("Root.ShrinkColumn() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
Expand Down
30 changes: 17 additions & 13 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 {
if len(a.columnAttrs) <= col {
return false
}
return a.shrink[col]
return a.columnAttrs[col].shrink
}

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

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

0 comments on commit e2d9c06

Please sign in to comment.