diff --git a/.build.yml b/.build.yml index 16b8faa..37183ee 100644 --- a/.build.yml +++ b/.build.yml @@ -23,6 +23,12 @@ tasks: duration=$SECONDS echo "Task completed in $(($duration / 60))m$(($duration % 60))s." + - test_terminal_utils: | + cd twitter_offline_engine/terminal_utils + + go get . + go test -bench=. -cover + - test_scraper: | cd twitter_offline_engine/scraper diff --git a/terminal_utils/colors.go b/terminal_utils/colors.go new file mode 100644 index 0000000..8bda0c1 --- /dev/null +++ b/terminal_utils/colors.go @@ -0,0 +1,15 @@ +package terminal_utils + + +/** + * Colors for terminal output + */ +const COLOR_RESET = "\033[0m" +const COLOR_RED = "\033[31m" +const COLOR_GREEN = "\033[32m" +const COLOR_YELLOW = "\033[33m" +const COLOR_BLUE = "\033[34m" +const COLOR_PURPLE = "\033[35m" +const COLOR_CYAN = "\033[36m" +const COLOR_GRAY = "\033[37m" +const COLOR_WHITE = "\033[97m" diff --git a/terminal_utils/formatting.go b/terminal_utils/formatting.go new file mode 100644 index 0000000..0f1a99b --- /dev/null +++ b/terminal_utils/formatting.go @@ -0,0 +1,46 @@ +package terminal_utils + +import ( + "time" + "strings" +) + +/** + * Format a timestamp in human-readable form. + */ +func FormatDate(t time.Time) string { + return t.Format("Jan 2, 2006 15:04:05") +} + + +/** + * Wrap lines to fixed width, while respecting word breaks + */ +func WrapParagraph(paragraph string, width int) []string { + var lines []string + i := 0 + for i < len(paragraph) - width { + // Find a word break at the end of the line to avoid splitting up words + end := i + width + for end > i && paragraph[end] != ' ' { // Look for a space, starting at the end + end -= 1 + } + lines = append(lines, paragraph[i:end]) + i = end + 1 + } + lines = append(lines, paragraph[i:]) + return lines +} + + +/** + * Return the text as a wrapped, indented block + */ +func WrapText(text string, width int) string { + paragraphs := strings.Split(text, "\n") + var lines []string + for _, paragraph := range paragraphs { + lines = append(lines, WrapParagraph(paragraph, width)...) + } + return strings.Join(lines, "\n ") +} diff --git a/terminal_utils/formatting_test.go b/terminal_utils/formatting_test.go new file mode 100644 index 0000000..cd4e659 --- /dev/null +++ b/terminal_utils/formatting_test.go @@ -0,0 +1,77 @@ +package terminal_utils_test + +import ( + "testing" + "reflect" + + "offline_twitter/terminal_utils" +) + + +func TestWrapParagraph(t *testing.T) { + test_cases := []struct{ + Text string + Expected []string + } { + { + "These are public health officials who are making decisions about your lifestyle because they know more about health, fitness and well-being than you do", + []string{ + "These are public health officials who are making decisions", + "about your lifestyle because they know more about health,", + "fitness and well-being than you do", + }, + }, + { + `Things I learned in law school:`, + []string{`Things I learned in law school:`}, + }, + { + `Every student is smarter than you except the ones in your group project.`, + []string{ + `Every student is smarter than you except the ones in your`, + `group project.`, + }, + }, + } + for _, testcase := range test_cases { + result := terminal_utils.WrapParagraph(testcase.Text, 60) + if !reflect.DeepEqual(result, testcase.Expected) { + t.Errorf("Expected:\n%s\nGot:\n%s\n", testcase.Expected, result) + } + } +} + + + +func TestWrapText(t *testing.T) { + test_cases := []struct{ + Text string + Expected string + } { + { + "These are public health officials who are making decisions about your lifestyle because they know more about health, fitness and well-being than you do", +`These are public health officials who are making decisions + about your lifestyle because they know more about health, + fitness and well-being than you do`, + }, + { +`Things I learned in law school: +Falling behind early gives you more time to catch up. +Never use a long word when a diminutive one will suffice. +Every student is smarter than you except the ones in your group project. +If you try & fail, doesn’t matter. Try again & fail better`, +`Things I learned in law school: + Falling behind early gives you more time to catch up. + Never use a long word when a diminutive one will suffice. + Every student is smarter than you except the ones in your + group project. + If you try & fail, doesn’t matter. Try again & fail better`, + }, + } + for _, testcase := range test_cases { + result := terminal_utils.WrapText(testcase.Text, 60) + if result != testcase.Expected { + t.Errorf("Expected:\n%s\nGot:\n%s\n", testcase.Expected, result) + } + } +}