Add some terminal utils

This commit is contained in:
Alessio 2021-07-23 19:59:33 -07:00
parent 6c111ab976
commit 1fa6e50b7e
4 changed files with 144 additions and 0 deletions

View File

@ -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

15
terminal_utils/colors.go Normal file
View File

@ -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"

View File

@ -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 ")
}

View File

@ -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, doesnt 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, doesnt 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)
}
}
}