From d56c8a0c33be1c201b788075cd6cad421869877c Mon Sep 17 00:00:00 2001 From: Marcus Kok Date: Fri, 1 Mar 2024 13:52:01 -0500 Subject: [PATCH] format lines --- cmd/pretty.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/cmd/pretty.go b/cmd/pretty.go index 88d603a..d94c9a1 100644 --- a/cmd/pretty.go +++ b/cmd/pretty.go @@ -1,7 +1,11 @@ package cmd import ( + "bufio" "fmt" + "log" + "os" + "strings" "github.com/spf13/cobra" ) @@ -14,6 +18,24 @@ var prettyCmd = &cobra.Command { Use: "pretty", Run: func(cmd *cobra.Command, args []string) { var filename = args[0] - fmt.Printf("Run the pretty command with filename %s", filename) + f, err := os.Open(filename) + if err != nil { + log.Fatal(err) + } + + defer f.Close() + + scanner := bufio.NewScanner(f) + + for scanner.Scan() { + line := scanner.Text() + formattedLine := strings.Replace(line, "\\n", "\n", -1) + formattedLine = strings.Replace(formattedLine, "\\t", "\t", -1) + fmt.Printf(formattedLine) + } + + if err := scanner.Err(); err != nil { + log.Fatal(err) + } }, }