Skip to content

Commit

Permalink
Merge pull request #618 from noborus/converter-short
Browse files Browse the repository at this point in the history
Add a shortcut key that can easily select a converter
  • Loading branch information
noborus authored Sep 4, 2024
2 parents b0f095c + fedadf4 commit f235903
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 6 deletions.
49 changes: 44 additions & 5 deletions oviewer/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
67 changes: 67 additions & 0 deletions oviewer/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
10 changes: 9 additions & 1 deletion oviewer/keybind.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit f235903

Please sign in to comment.