From d4826a25968f057867f799df13277c8e940ef5b5 Mon Sep 17 00:00:00 2001 From: Noboru Saito Date: Mon, 9 Sep 2024 06:24:23 +0900 Subject: [PATCH] Add a check to shrink in Align mode Add a shrinking column or judgment function. Add functions other than the shrink and expansion tooggle. --- oviewer/action.go | 23 +++++++++++++++++++---- oviewer/action_test.go | 2 +- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/oviewer/action.go b/oviewer/action.go index f1767db..3241d04 100644 --- a/oviewer/action.go +++ b/oviewer/action.go @@ -771,24 +771,39 @@ func (root *Root) bottomSectionLN(ctx context.Context) int { // ShrinkColumn shrinks the specified column. func (root *Root) ShrinkColumn(ctx context.Context, cursor int) error { - return root.Doc.shrinkColumn(ctx, cursor, true) + return root.Doc.shrinkColumn(cursor, true) } // ExpandColumn expands the specified column. func (root *Root) ExpandColumn(ctx context.Context, cursor int) error { - return root.Doc.shrinkColumn(ctx, cursor, false) + 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 - if err := root.Doc.shrinkColumn(ctx, cursor, !root.Doc.alignConv.columnAttrs[cursor].shrink); err != nil { + shrink, err := root.Doc.isColumnShink(cursor) + if err != nil { + root.setMessage(err.Error()) + } + if err := root.Doc.shrinkColumn(cursor, !shrink); err != nil { root.setMessage(err.Error()) } } +// isColumnShink returns whether the specified column is shrink. +func (m *Document) isColumnShink(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(ctx context.Context, cursor int, shrink bool) error { +func (m *Document) shrinkColumn(cursor int, shrink bool) error { if m.Converter != convAlign { return ErrNotAlignMode } diff --git a/oviewer/action_test.go b/oviewer/action_test.go index ecfe83c..d732d27 100644 --- a/oviewer/action_test.go +++ b/oviewer/action_test.go @@ -1623,7 +1623,7 @@ func TestRoot_ShrinkColumn(t *testing.T) { root.Doc.Converter = tt.fields.converter root.Doc.ColumnDelimiter = "," root.prepareDraw(ctx) - if err := root.Doc.shrinkColumn(ctx, tt.args.cursor, tt.args.shrink); (err != nil) != tt.wantErr { + 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) } })