From 8ca2e167f346f274fe439223a86ddf8b871e5029 Mon Sep 17 00:00:00 2001 From: Dominic Ricottone Date: Wed, 19 Aug 2020 18:36:01 -0400 Subject: [PATCH] Finishing touches on v0.0.1 --- main.go | 19 +++++++++++++------ message/format.go | 6 +++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 0e85ce4..dc082dd 100644 --- a/main.go +++ b/main.go @@ -7,10 +7,13 @@ import ( "io" "bufio" "regexp" + "flag" "git.dominic-ricottone.com/digestion/message" ) +const LINE_LENGTH = 80 + const ( ParsingPreHeader = "ParsingPreHeader" ParsingHeader = "ParsingHeader" @@ -31,7 +34,7 @@ func first_submatch(r regexp.Regexp, s string) string { } } -func parse_stream(reader io.Reader) { +func parse_stream(reader io.Reader, length int) { // Create scanner from reader input := bufio.NewScanner(reader) @@ -55,7 +58,7 @@ func parse_stream(reader io.Reader) { os.Exit(1) } - parsing := ParsingPreHeader + parsing := ParsingContent current_message := message.NewMessage() current_boundary := "" @@ -84,7 +87,7 @@ func parse_stream(reader io.Reader) { } else if parsing == ParsingContent { if re_message_break.MatchString(tline) { parsing = ParsingPreHeader - current_message.Dump() + current_message.Dump(length) current_message = message.NewMessage() current_boundary = "" } else if contains_nonempty(tline, current_boundary) { @@ -103,7 +106,7 @@ func parse_stream(reader io.Reader) { } } -func parse_file(filename string) { +func parse_file(filename string, length int) { // Check file file, err := os.Open(filename) if err != nil { @@ -113,7 +116,7 @@ func parse_file(filename string) { defer file.Close() // Parse - parse_stream(file) + parse_stream(file, length) } func main() { @@ -124,7 +127,11 @@ func main() { os.Exit(1) } + // Look for arguments + var length = flag.Int("length", LINE_LENGTH, "maximum length of lines") + flag.Parse() + // Parse - parse_stream(os.Stdin) + parse_stream(os.Stdin, *length) } diff --git a/message/format.go b/message/format.go index 4d0f713..cf3c517 100644 --- a/message/format.go +++ b/message/format.go @@ -86,9 +86,9 @@ func (m *Message) format_content(length int) ([]string, error) { return wrapped, nil } -func (m *Message) Dump() { +func (m *Message) Dump(length int) { header := m.format_header() - content, err := m.format_content(80) + content, err := m.format_content(length) if err != nil { fmt.Printf("error: %s", err) } @@ -96,6 +96,6 @@ func (m *Message) Dump() { for _, line := range append(header, content...) { fmt.Printf("%s\n", line) } - fmt.Printf("\n%s\n\n", strings.Repeat("-", 80)) + fmt.Printf("%s\n\n", strings.Repeat("-", length)) } -- 2.45.2