Skip to content

Commit

Permalink
feat: Add sort subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Allaman committed May 20, 2024
1 parent 84120b8 commit e4bb656
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Commands:
pwgen Password generator (letters and numbers)
reverse Reverse the input
rng Random number generator
sort Sort the input by line
split Split a string
tail Returns the last n lines
url encode Encode string to valid URL
Expand Down
16 changes: 16 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type CLI struct {
PwGen pwGenCmd `cmd:"" name:"pwgen" help:"Password generator (letters and numbers)"`
Reverse reverseCmd `cmd:"" help:"Reverse the input"`
Rng rngCmd `cmd:"" help:"Random number generator"`
Sort sortCmd `cmd:"" help:"Sort the input by line"`
Split splitCmd `cmd:"" help:"Split a string"`
Tail tailCmd `cmd:"" help:"Returns the last n lines"`
URL struct {
Expand Down Expand Up @@ -405,3 +406,18 @@ func (c *pwGenCmd) Run(globals *Globals) error {
printOutput(passwords, globals.Trim)
return nil
}

type sortCmd struct {
Desc bool `default:"false" short:"d" help:"Descending"`
IgnoreEmptyLines bool `default:"true" help:"Ignore empty lines"`
Unique bool `default:"false" short:"u" help:"Only unique lines"`
}

func (c *sortCmd) Run(globals *Globals) error {
in, err := readFromSTDIN()
if err != nil {
return err
}
printOutput(sortLines(in, c.Desc, c.IgnoreEmptyLines, c.Unique), globals.Trim)
return nil
}
42 changes: 42 additions & 0 deletions sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"sort"
"strings"
)

func sortLines(input string, desc, ignoreEmptyLines, unique bool) string {
lines := strings.Split(input, "\n")
if ignoreEmptyLines {
nonEmptyLines := make([]string, 0, len(lines))
for _, line := range lines {
if strings.TrimSpace(line) != "" {
nonEmptyLines = append(nonEmptyLines, line)
}
}
lines = nonEmptyLines
}

if unique {
lines = uniqueLines(lines)
}

if desc {
sort.Sort(sort.Reverse(sort.StringSlice(lines)))
} else {
sort.Strings(lines)
}
return strings.Join(lines, "\n")
}

func uniqueLines(lines []string) []string {
lineMap := make(map[string]struct{})
uniqueLines := make([]string, 0, len(lines))
for _, line := range lines {
if _, exists := lineMap[line]; !exists {
lineMap[line] = struct{}{}
uniqueLines = append(uniqueLines, line)
}
}
return uniqueLines
}
103 changes: 103 additions & 0 deletions sort_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package main

import (
"strings"
"testing"
)

func TestSortLines(t *testing.T) {
tests := []struct {
input string
desc bool
ignoreEmptyLines bool
unique bool
expected string
}{
{
input: `banana
apple
cherry
banana
apple
date`,
desc: false,
ignoreEmptyLines: true,
unique: true,
expected: `apple
banana
cherry
date`,
},
{
input: `banana
apple
cherry
banana
apple
date`,
desc: true,
ignoreEmptyLines: true,
unique: true,
expected: `date
cherry
banana
apple`,
},
{
input: `a
b
a
b`,
desc: false,
ignoreEmptyLines: true,
unique: true,
expected: `a
b`,
},
{
input: `a
b
a
b`,
desc: true,
ignoreEmptyLines: true,
unique: true,
expected: `b
a`,
},
{
input: `a
b
a
b`,
desc: false,
ignoreEmptyLines: false,
unique: false,
expected: `
a
a
b
b`,
},
{
input: `one line`,
desc: false,
ignoreEmptyLines: true,
unique: true,
expected: `one line`,
},
}

for _, tt := range tests {
t.Run("", func(t *testing.T) {
result := sortLines(tt.input, tt.desc, tt.ignoreEmptyLines, tt.unique)
if strings.TrimSpace(result) != strings.TrimSpace(tt.expected) {
t.Errorf("sortLines(%q, %t, %t, %t) = %q; want %q", tt.input, tt.desc, tt.ignoreEmptyLines, tt.unique, result, tt.expected)
}
})
}
}

0 comments on commit e4bb656

Please sign in to comment.