Skip to content

Commit 6f5b6ad

Browse files
authored
feat(spaced): Support configurable spacing (#10)
1 parent c9f8116 commit 6f5b6ad

File tree

5 files changed

+104
-13
lines changed

5 files changed

+104
-13
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ space insertion (ASI).
1414
```shell
1515
$ echo 'AESTHETIC' | spaced
1616
A E S T H E T I C
17+
18+
$ echo 'AESTHETIC' | spaced 2
19+
A E S T H E T I C
1720
```
1821

1922
### Go

cmd/spaced/main.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
package main
22

33
import (
4-
"fmt"
54
"os"
65

7-
"github.com/72636c/hyperspaced/internal/text"
8-
"github.com/72636c/hyperspaced/internal/text/transform"
6+
"github.com/72636c/hyperspaced/internal/cmd"
97
)
108

119
func main() {
12-
err := text.LineFilter(transform.Spaced)
13-
if err != nil {
14-
fmt.Fprintln(os.Stderr, "error: ", err)
15-
os.Exit(1)
16-
}
10+
cmd.Spaced(os.Stdin, os.Stdout, os.Args)
1711
}

internal/cmd/spaced.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
"strconv"
8+
9+
"github.com/72636c/hyperspaced/internal/text"
10+
"github.com/72636c/hyperspaced/internal/text/transform"
11+
)
12+
13+
var usage = `spaced [n]
14+
n int
15+
number of spaces between each character (default 1)
16+
`
17+
18+
func Spaced(reader io.Reader, writer io.Writer, args []string) {
19+
n, err := spacesFromArgs(args)
20+
if err != nil {
21+
fmt.Fprintln(os.Stderr, usage)
22+
fmt.Fprintln(os.Stderr, "error:", err)
23+
os.Exit(1)
24+
}
25+
26+
transformLine := func(str string) string {
27+
return transform.SpacedN(str, n)
28+
}
29+
30+
err = text.LineFilter(reader, writer, transformLine)
31+
if err != nil {
32+
fmt.Fprintln(os.Stderr, "error:", err)
33+
os.Exit(1)
34+
}
35+
}
36+
37+
func spacesFromArgs(args []string) (int, error) {
38+
if len(args) < 2 {
39+
return 1, nil
40+
}
41+
42+
n, err := strconv.Atoi(args[1])
43+
if err != nil {
44+
return 0, err
45+
}
46+
47+
return n, nil
48+
}

internal/cmd/spaced_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package cmd
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
func Test_Spaced(t *testing.T) {
9+
testCases := []struct {
10+
description string
11+
args []string
12+
input string
13+
expected string
14+
}{
15+
{
16+
description: "unspecified n",
17+
args: []string{"spaced"},
18+
input: "AESTHETIC",
19+
expected: "A E S T H E T I C\n",
20+
},
21+
{
22+
description: "integer n",
23+
args: []string{"spaced", "2"},
24+
input: "AESTHETIC",
25+
expected: "A E S T H E T I C\n",
26+
},
27+
}
28+
29+
for _, testCase := range testCases {
30+
t.Run(testCase.description, func(t *testing.T) {
31+
reader := strings.NewReader(testCase.input)
32+
writer := new(strings.Builder)
33+
34+
Spaced(reader, writer, testCase.args)
35+
36+
actual := writer.String()
37+
if actual != testCase.expected {
38+
t.Errorf("expected %s, received %s", testCase.expected, actual)
39+
}
40+
})
41+
}
42+
}

internal/text/lineFilter.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@ package text
33
import (
44
"bufio"
55
"fmt"
6-
"os"
6+
"io"
77
)
88

99
// TransformLine is a function that transforms a line of text.
1010
type TransformLine func(string) string
1111

12-
// LineFilter transforms lines of text as they pass from stdin to stdout.
13-
func LineFilter(transform TransformLine) error {
14-
scanner := bufio.NewScanner(os.Stdin)
12+
// LineFilter transforms lines of text as they pass from a reader to a writer.
13+
func LineFilter(
14+
reader io.Reader,
15+
writer io.Writer,
16+
transform TransformLine,
17+
) error {
18+
scanner := bufio.NewScanner(reader)
1519

1620
for scanner.Scan() {
1721
output := transform(scanner.Text())
1822

19-
_, err := fmt.Println(output)
23+
_, err := fmt.Fprintln(writer, output)
2024
if err != nil {
2125
return err
2226
}

0 commit comments

Comments
 (0)