-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(spaced): Support configurable spacing (#10)
- Loading branch information
Showing
5 changed files
with
104 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters