From 0a9924e96cba33b70343642c6994aed425ec4a98 Mon Sep 17 00:00:00 2001 From: BraydonKains Date: Fri, 19 May 2023 22:22:44 -0400 Subject: [PATCH] started muld --- cmd/mld/main.go | 7 ---- cmd/muld/main.go | 70 +++++++++++++++++++++++++++++++++++++ internal/experiment/main.go | 24 ------------- 3 files changed, 70 insertions(+), 31 deletions(-) delete mode 100644 cmd/mld/main.go create mode 100644 cmd/muld/main.go delete mode 100644 internal/experiment/main.go diff --git a/cmd/mld/main.go b/cmd/mld/main.go deleted file mode 100644 index e64ea4a..0000000 --- a/cmd/mld/main.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "fmt" - -func main() { - fmt.Println("not ready yet") -} diff --git a/cmd/muld/main.go b/cmd/muld/main.go new file mode 100644 index 0000000..d885868 --- /dev/null +++ b/cmd/muld/main.go @@ -0,0 +1,70 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "log" + "os" + "runtime" + + "github.com/RageCage64/multilinediff" +) + +func main() { + if err := run(); err != nil { + log.Fatal(err) + } +} + +func run() error { + if (len(os.Args) - 1) != 2 { + return fmt.Errorf("requires exactly two arguments, got %d", len(os.Args)) + } + + leftPath := os.Args[1] + rightPath := os.Args[2] + + leftContent, err := readContent(leftPath) + if err != nil { + return err + } + rightContent, err := readContent(rightPath) + if err != nil { + return err + } + + lineSep := "\n" + if runtime.GOOS == "windows" { + lineSep = "\r\n" + } + + fmt.Println(multilinediff.Diff(leftContent, rightContent, lineSep)) + + return nil +} + +func readContent(path string) (string, error) { + if path == "-" { + return readFromStdin() + } + content, err := os.ReadFile(path) + return string(content), err +} + +func readFromStdin() (string, error) { + stdin := bufio.NewReader(os.Stdin) + data := []byte{} + for { + b, err := stdin.ReadByte() + if err != nil { + if err == io.EOF { + break + } else { + return "", err + } + } + data = append(data, b) + } + return string(data), nil +} diff --git a/internal/experiment/main.go b/internal/experiment/main.go deleted file mode 100644 index e725dd6..0000000 --- a/internal/experiment/main.go +++ /dev/null @@ -1,24 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/RageCage64/multilinediff" -) - -func main() { - a := `a -b -c -diff` - b := `a b -c -f -f -f -diff` - diff, count := multilinediff.Diff(a, b, "\n") - fmt.Println(count) - fmt.Println("--------") - fmt.Println(diff) -}