diff --git a/oviewer/action.go b/oviewer/action.go index f3ccc57..3f66350 100644 --- a/oviewer/action.go +++ b/oviewer/action.go @@ -429,6 +429,7 @@ func (root *Root) modeConfig(modeName string) (general, error) { return c, nil } +// setConverter sets the converter type. func (root *Root) setConverter(ctx context.Context, name string) { m := root.Doc if m.general.Converter == name { @@ -440,6 +441,32 @@ func (root *Root) setConverter(ctx context.Context, name string) { root.ViewSync(ctx) } +// alignFormat sets converter type to align. +func (root *Root) alignFormat(ctx context.Context) { + if root.Doc.Converter == "align" { + root.esFormat(ctx) + return + } + root.setConverter(ctx, alignConv) + root.setMessage("Set align mode") +} + +// rawFormat sets converter type to raw. +func (root *Root) rawFormat(ctx context.Context) { + if root.Doc.Converter == "raw" { + root.esFormat(ctx) + return + } + root.setConverter(ctx, rawConv) + root.setMessage("Set raw mode") +} + +// esFormat sets converter type to es. +func (root *Root) esFormat(ctx context.Context) { + root.setConverter(ctx, esConv) + root.setMessage("Set es mode") +} + // setDelimiter sets the delimiter string. func (root *Root) setDelimiter(input string) { root.Doc.setDelimiter(input) @@ -735,12 +762,24 @@ func (root *Root) bottomSectionLN(ctx context.Context) int { return lN - (root.Doc.topLN + root.Doc.firstLine() - root.Doc.SectionStartPosition) } -func (root *Root) shrinkColumn(ctx context.Context) { +// shrinkColumnToggle shrinks or expands the current cursor column. +func (root *Root) shrinkColumnToggle(ctx context.Context) { + root.ShrinkColumn(ctx, root.Doc.columnCursor) +} + +// ShrinkColumn shrinks or expands the specified column. +func (root *Root) ShrinkColumn(ctx context.Context, cursor int) bool { m := root.Doc - if m.columnCursor >= len(m.alignConv.shrink) { - return + if root.Doc.Converter != "align" { + root.setMessage("Not align mode") + return false } - m.alignConv.shrink[m.columnCursor] = !m.alignConv.shrink[m.columnCursor] - root.Doc.ClearCache() + if cursor >= len(m.alignConv.shrink) { + root.setMessage("No column selected") + return false + } + m.alignConv.shrink[cursor] = !m.alignConv.shrink[cursor] + m.ClearCache() root.ViewSync(ctx) + return true } diff --git a/oviewer/action_test.go b/oviewer/action_test.go index 6fdaa06..f98e128 100644 --- a/oviewer/action_test.go +++ b/oviewer/action_test.go @@ -1553,3 +1553,70 @@ func TestRoot_setConverter(t *testing.T) { }) } } + +func TestRoot_ShrinkColumn(t *testing.T) { + tcellNewScreen = fakeScreen + defer func() { + tcellNewScreen = tcell.NewScreen + }() + type fields struct { + fileName string + converter string + } + type args struct { + cursor int + } + tests := []struct { + name string + fields fields + args args + want bool + }{ + { + name: "testShrinkColumn", + fields: fields{ + fileName: filepath.Join(testdata, "MOCK_DATA.csv"), + converter: alignConv, + }, + args: args{ + cursor: 0, + }, + want: true, + }, + { + name: "testShrinkColumnFalse", + fields: fields{ + fileName: filepath.Join(testdata, "MOCK_DATA.csv"), + converter: esConv, + }, + args: args{ + cursor: 0, + }, + want: false, + }, + { + name: "testShrinkColumnOver", + fields: fields{ + fileName: filepath.Join(testdata, "MOCK_DATA.csv"), + converter: alignConv, + }, + args: args{ + cursor: 20, + }, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + root := rootFileReadHelper(t, tt.fields.fileName) + ctx := context.Background() + root.prepareScreen() + 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) + } + }) + } +} diff --git a/oviewer/keybind.go b/oviewer/keybind.go index 5fd24f3..b664889 100644 --- a/oviewer/keybind.go +++ b/oviewer/keybind.go @@ -83,6 +83,8 @@ const ( actionSaveBuffer = "save_buffer" actionHideOther = "hide_other" actionConvertType = "convert_type" + actionAlignFormat = "align_format" + actionRawFormat = "raw_format" actionShrinkColumn = "shrink_column" inputCaseSensitive = "input_casesensitive" @@ -169,7 +171,9 @@ func (root *Root) handlers() map[string]func(context.Context) { actionSaveBuffer: root.setSaveBuffer, actionHideOther: root.toggleHideOtherSection, actionConvertType: root.setConvertType, - actionShrinkColumn: root.shrinkColumn, + actionAlignFormat: root.alignFormat, + actionRawFormat: root.rawFormat, + actionShrinkColumn: root.shrinkColumnToggle, inputCaseSensitive: root.inputCaseSensitive, inputSmartCaseSensitive: root.inputSmartCaseSensitive, @@ -259,6 +263,8 @@ func defaultKeyBinds() KeyBind { actionSaveBuffer: {"S"}, actionHideOther: {"alt+-"}, actionConvertType: {"alt+t"}, + actionAlignFormat: {"alt+F"}, + actionRawFormat: {"alt+R"}, actionShrinkColumn: {"s"}, inputCaseSensitive: {"alt+c"}, @@ -347,6 +353,8 @@ func (k KeyBind) String() string { k.writeKeyBind(&b, actionTabWidth, "TAB width") k.writeKeyBind(&b, actionMultiColor, "multi color highlight") k.writeKeyBind(&b, actionJumpTarget, "jump target(`.n` or `n%` or `section` allowed)") + k.writeKeyBind(&b, actionAlignFormat, "align format") + k.writeKeyBind(&b, actionRawFormat, "raw format") k.writeKeyBind(&b, actionConvertType, "convert type selection") writeHeader(&b, "Section")