Skip to content

Commit 64c5819

Browse files
committed
Add "Sort Lines" and "Reverse Lines" commands
1 parent 54e5fab commit 64c5819

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

internal/application/actions.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/url"
1010
"os"
1111
"path/filepath"
12+
"slices"
1213
"strconv"
1314
"strings"
1415

@@ -54,6 +55,8 @@ const (
5455
ACTION_TO_LOWERCASE = "ToLowercase"
5556
ACTION_URL_ENCODE = "UrlEncode"
5657
ACTION_URL_DECODE = "UrlDecode"
58+
ACTION_SORT_LINES = "SortLines"
59+
ACTION_REVERSE_LINES = "ReverseLines"
5760
)
5861

5962
var dinkyActionMapping map[string]func() tview.Primitive
@@ -95,6 +98,8 @@ func init() {
9598
ACTION_TO_LOWERCASE: handleToLowercase,
9699
ACTION_URL_ENCODE: handleURLEncode,
97100
ACTION_URL_DECODE: handleURLDecode,
101+
ACTION_SORT_LINES: handleSortLines,
102+
ACTION_REVERSE_LINES: handleReverseLines,
98103
}
99104
}
100105

@@ -696,3 +701,29 @@ func handleURLDecode() tview.Primitive {
696701
})
697702
return nil
698703
}
704+
705+
func handleSortLines() tview.Primitive {
706+
if !currentFileBuffer.editor.Cursor().HasSelection() {
707+
statusBar.ShowWarning("No text selected")
708+
return nil
709+
}
710+
currentFileBuffer.editor.ActionController().TransformSelection(func(lines []string) []string {
711+
result := append([]string(nil), lines...)
712+
slices.Sort(result)
713+
return result
714+
})
715+
return nil
716+
}
717+
718+
func handleReverseLines() tview.Primitive {
719+
if !currentFileBuffer.editor.Cursor().HasSelection() {
720+
statusBar.ShowWarning("No text selected")
721+
return nil
722+
}
723+
currentFileBuffer.editor.ActionController().TransformSelection(func(lines []string) []string {
724+
result := append([]string(nil), lines...)
725+
slices.Reverse(result)
726+
return result
727+
})
728+
return nil
729+
}

internal/application/menu.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ func createMenus() []*menu.Menu {
5959
{ID: ACTION_TO_LOWERCASE, Title: "To Lowercase", Callback: handleDinkyAction},
6060
{ID: ACTION_URL_ENCODE, Title: "URL Encode", Callback: handleDinkyAction},
6161
{ID: ACTION_URL_DECODE, Title: "URL Decode", Callback: handleDinkyAction},
62+
{Title: "", Callback: nil}, // Separator
63+
{ID: ACTION_SORT_LINES, Title: "Sort Lines", Callback: handleDinkyAction},
64+
{ID: ACTION_REVERSE_LINES, Title: "Reverse Lines", Callback: handleDinkyAction},
6265
}},
6366
{Title: "[::u]V[::U]iew", Shortcut: 'v', Items: []*menu.MenuItem{
6467
{ID: smidgen.ActionToggleRuler, Title: "Line Numbers", Callback: handleSmidgenAction},

0 commit comments

Comments
 (0)