Skip to content

Commit

Permalink
Merge pull request #6 from diamondburned/f-cli
Browse files Browse the repository at this point in the history
Change CLI to files
  • Loading branch information
nihaals authored Feb 12, 2021
2 parents f931a75 + a9b62e8 commit 2950ed5
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 33 deletions.
23 changes: 20 additions & 3 deletions bottom/bottom.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,36 @@ func EncodedLen(s string) int {
}

// EncodeTo encodes the given string into the writer.
func EncodeTo(out io.StringWriter, s string) (int, error) {
func EncodeTo(out io.StringWriter, s string) error {
var sum int

for _, sChar := range []byte(s) {
n, err := out.WriteString(valueCharacters[sChar])
if err != nil {
return sum, err
return err
}

sum += n
}

return sum, nil
return nil
}

// EncodeFrom encodes from the given src reader to out.
func EncodeFrom(out io.StringWriter, src io.ByteReader) error {
for {
b, err := src.ReadByte()
if err != nil {
if errors.Is(err, io.EOF) {
return nil
}
return err
}

if _, err = out.WriteString(valueCharacters[b]); err != nil {
return err
}
}
}

// Validate validates a bottom string.
Expand Down
2 changes: 1 addition & 1 deletion bottom/bottom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func BenchmarkEncodeTo(b *testing.B) {

var w noopWriter
for i := 0; i < b.N; i++ {
_, _ = EncodeTo(w, testCase.out)
_ = EncodeTo(w, testCase.out)
}
}

Expand Down
74 changes: 74 additions & 0 deletions cmd/bottom/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

import (
"bufio"
"flag"
"fmt"
"log"
"os"
"path/filepath"

"github.com/nihaals/bottom-go/bottom"
)

func init() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(),
"Usage: %s [flags...] [input]\n\n", filepath.Base(os.Args[0]))
fmt.Fprintf(flag.CommandLine.Output(),
"Flags:\n")
flag.PrintDefaults()
}
}

func main() {
var (
decode bool
output string = "-"
)

flag.BoolVar(&decode, "d", decode, "Decode instead of encode")
flag.StringVar(&output, "o", output, "Output path, - for stdout")
flag.Parse()

var err error

var src = os.Stdin
if input := flag.Arg(0); input != "" && input != "-" {
src, err = os.Open(input)
if err != nil {
log.Fatalln("failed to open input:", err)
}
defer src.Close()
}

var dst = os.Stdout
if output != "" && output != "-" {
dst, err = os.Create(output)
if err != nil {
log.Fatalln("failed to create output:", err)
}
defer dst.Close()
}

// Make a new buffered writer of size 1024.
bufdst := bufio.NewWriterSize(dst, 1024)
defer bufdst.Flush()

// Make a new buffered reader also of size 1024.
bufsrc := bufio.NewReaderSize(src, 1024)

if decode {
if err := bottom.DecodeFrom(bufdst, bufsrc); err != nil {
log.Fatalln("failed to decode:", err)
}
}

if err = bottom.EncodeFrom(bufdst, bufsrc); err != nil {
log.Fatalln("failed to encode:", err)
}

if err != nil {
log.Fatalln(err)
}
}
29 changes: 0 additions & 29 deletions cmd/main.go

This file was deleted.

0 comments on commit 2950ed5

Please sign in to comment.