diff --git a/.travis.yml b/.travis.yml index 98a5fe2..06896c1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,6 +8,7 @@ go: - master before_script: + - go get -d - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter - chmod +x ./cc-test-reporter - ./cc-test-reporter before-build diff --git a/README.md b/README.md index 29c96db..0b3f1b0 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ Requires [git](https://git-scm.com/download/win) to clone and [Go](https://golan ```bash $ git clone https://github.com/muhammadmuzzammil1998/catsay.git $ cd catsay +$ go get -d $ go build ``` diff --git a/app.go b/app.go index 1f3e61e..07d1906 100644 --- a/app.go +++ b/app.go @@ -31,7 +31,8 @@ import ( "io" "os" "strings" - "unicode/utf8" + + "github.com/mattn/go-runewidth" ) //Defining a main cat @@ -87,9 +88,9 @@ func readLines(reader *bufio.Reader) []string { func getWidth(message []string) int { ret := cat.MinLen for _, l := range message { - len := utf8.RuneCountInString(l) - if len > ret { - ret = len + width := runewidth.StringWidth(l) + if width > ret { + ret = width } } return ret @@ -131,7 +132,8 @@ func removeTabs(message []string) []string { func formatMessage(message []string, width int) []string { var ret []string for _, l := range message { - ret = append(ret, l+strings.Repeat(" ", width-utf8.RuneCountInString(l))) + w := runewidth.StringWidth(l) + ret = append(ret, l+strings.Repeat(" ", width-w)) } return ret } diff --git a/app_test.go b/app_test.go index ab63ba9..a437990 100644 --- a/app_test.go +++ b/app_test.go @@ -39,6 +39,7 @@ func TestReadLines(t *testing.T) { t.Fatalf("Couldn't parse bufio.Reader") } } + func TestGetWidth(t *testing.T) { setup() r := getWidth(message) @@ -46,12 +47,14 @@ func TestGetWidth(t *testing.T) { t.Fatalf("Expected to be greater or equal to 9 but got %d", r) } } + func TestRemoveTabs(t *testing.T) { r := removeTabs(message) if r[4] != "this one has tabs!" { t.Fatalf("Tabs were not replaced with spaces") } } + func TestCreateMessage(t *testing.T) { setup() data := removeTabs(message) @@ -68,6 +71,7 @@ func TestCreateMessage(t *testing.T) { t.Fatalf("Expected\n%s\nbut got\n%s", e, r) } } + func TestFormatMessage(t *testing.T) { setup() data := removeTabs(message) @@ -81,3 +85,15 @@ func TestFormatMessage(t *testing.T) { t.Fatalf("Length of each line should be equal. Expected %d but got %d", eLen, rLen) } } + +func TestMultiByteString(t *testing.T) { + setup() + data := removeTabs(buildMessage("こんにちわ世界")) + got := createMessage(data, getWidth(data)) + var want = ` ________________ +< こんにちわ世界 > + ----------------` + if got != want { + t.Fatalf("Expected\n%s\nbut got\n%s", want, got) + } +}