Skip to content

Commit

Permalink
feat(spaced): Support configurable spacing (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
72636c authored Jan 1, 2019
1 parent c9f8116 commit 6f5b6ad
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 13 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ space insertion (ASI).
```shell
$ echo 'AESTHETIC' | spaced
A E S T H E T I C

$ echo 'AESTHETIC' | spaced 2
A E S T H E T I C
```

### Go
Expand Down
10 changes: 2 additions & 8 deletions cmd/spaced/main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package main

import (
"fmt"
"os"

"github.com/72636c/hyperspaced/internal/text"
"github.com/72636c/hyperspaced/internal/text/transform"
"github.com/72636c/hyperspaced/internal/cmd"
)

func main() {
err := text.LineFilter(transform.Spaced)
if err != nil {
fmt.Fprintln(os.Stderr, "error: ", err)
os.Exit(1)
}
cmd.Spaced(os.Stdin, os.Stdout, os.Args)
}
48 changes: 48 additions & 0 deletions internal/cmd/spaced.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cmd

import (
"fmt"
"io"
"os"
"strconv"

"github.com/72636c/hyperspaced/internal/text"
"github.com/72636c/hyperspaced/internal/text/transform"
)

var usage = `spaced [n]
n int
number of spaces between each character (default 1)
`

func Spaced(reader io.Reader, writer io.Writer, args []string) {
n, err := spacesFromArgs(args)
if err != nil {
fmt.Fprintln(os.Stderr, usage)
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}

transformLine := func(str string) string {
return transform.SpacedN(str, n)
}

err = text.LineFilter(reader, writer, transformLine)
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
}

func spacesFromArgs(args []string) (int, error) {
if len(args) < 2 {
return 1, nil
}

n, err := strconv.Atoi(args[1])
if err != nil {
return 0, err
}

return n, nil
}
42 changes: 42 additions & 0 deletions internal/cmd/spaced_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cmd

import (
"strings"
"testing"
)

func Test_Spaced(t *testing.T) {
testCases := []struct {
description string
args []string
input string
expected string
}{
{
description: "unspecified n",
args: []string{"spaced"},
input: "AESTHETIC",
expected: "A E S T H E T I C\n",
},
{
description: "integer n",
args: []string{"spaced", "2"},
input: "AESTHETIC",
expected: "A E S T H E T I C\n",
},
}

for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
reader := strings.NewReader(testCase.input)
writer := new(strings.Builder)

Spaced(reader, writer, testCase.args)

actual := writer.String()
if actual != testCase.expected {
t.Errorf("expected %s, received %s", testCase.expected, actual)
}
})
}
}
14 changes: 9 additions & 5 deletions internal/text/lineFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ package text
import (
"bufio"
"fmt"
"os"
"io"
)

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

// LineFilter transforms lines of text as they pass from stdin to stdout.
func LineFilter(transform TransformLine) error {
scanner := bufio.NewScanner(os.Stdin)
// LineFilter transforms lines of text as they pass from a reader to a writer.
func LineFilter(
reader io.Reader,
writer io.Writer,
transform TransformLine,
) error {
scanner := bufio.NewScanner(reader)

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

_, err := fmt.Println(output)
_, err := fmt.Fprintln(writer, output)
if err != nil {
return err
}
Expand Down

0 comments on commit 6f5b6ad

Please sign in to comment.