From f707a8510f52c1b9f4e0653d8f14c16a06492f40 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Mon, 26 Aug 2024 18:25:07 -0400 Subject: formatter --- .gitignore | 1 + fmt.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 fmt.go diff --git a/.gitignore b/.gitignore index c94b1c4..ba9e3e6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ markov +fmt diff --git a/fmt.go b/fmt.go new file mode 100644 index 0000000..e54a1d1 --- /dev/null +++ b/fmt.go @@ -0,0 +1,33 @@ +// This program wraps its input into short lines. +package main + +import ( + "bufio" + "fmt" + "os" +) + +const maxWidth = 80 // maximum number of characters per line + +func main() { + input := bufio.NewScanner(os.Stdin) + input.Split(bufio.ScanWords) + width := 0 + for input.Scan() { + word := input.Text() + + if width+1+len(word) > maxWidth { + fmt.Println() + width = 0 + } + + if width == 0 { + fmt.Print(word) + width += len(word) + } else { + fmt.Printf(" %s", word) + width += 1 + len(word) + } + } + fmt.Println() +} -- cgit v1.2.3