Skip to content

Commit

Permalink
Version 2.1
Browse files Browse the repository at this point in the history
Merge pull request #16 from muhammadmuzzammil1998/version-2.1
  • Loading branch information
muhammadmuzzammil1998 authored Apr 5, 2019
2 parents 754bbf7 + 1c60dbf commit f8d6e98
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 8 deletions.
5 changes: 3 additions & 2 deletions .dist/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ cd ..
rm -rf bin deb
mkdir bin

version=2.0
version=2.1
maintainer="Muhammad Muzzammil <email@muzzammil.xyz>"
url="https://github.com/muhammadmuzzammil1998/catsay/"
desc="catsay is a program that generates pictures of a cat holding a sign with a message."
Expand Down Expand Up @@ -91,5 +91,6 @@ done

echo -e "\n\nPacking binaries..."
tar -zcvf .dist/catsay-binaries.tar.gz bin > /dev/null

file .dist/catsay-binaries.tar.gz >> bin/BIN_INFO
mv bin/BIN_INFO .dist/BIN_INFO
rm -rf bin
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ catsay

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

.dist/BIN_INFO
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down Expand Up @@ -57,3 +58,6 @@ Start PowerShell as an admin
```ps
$ del C:\Windows\catsay.exe
```
## Contributors

- **[@mattn](https://github.com/mattn)** - Fixed width for multi-byte strings *([#14](https://github.com/muhammadmuzzammil1998/catsay/pull/14))*
14 changes: 8 additions & 6 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import (
"io"
"os"
"strings"
"unicode/utf8"

"github.com/mattn/go-runewidth"
)

//Defining a main cat
Expand All @@ -47,7 +48,7 @@ func main() {

//Checking flags
if version {
data = buildMessage("CatSay", "\tby Muhammad Muzzammil", "", "Version 2.0", "", "http://bit.ly/CATSAY")
data = buildMessage("CatSay", "\tby Muhammad Muzzammil", "", "Version 2.1", "", "http://bit.ly/CATSAY")
} else if info, _ := os.Stdin.Stat(); info.Mode()&os.ModeCharDevice != 0 {
data = buildMessage("oh hai kittehs!", "use pipez... i doan knoe how 2 werk otherwize.", "example: echo \"y halo thar, im kat\" | catsay", "", "or try \"catsay -help\"", "btw, i hatz dis sign. lulz")
} else {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
16 changes: 16 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,22 @@ func TestReadLines(t *testing.T) {
t.Fatalf("Couldn't parse bufio.Reader")
}
}

func TestGetWidth(t *testing.T) {
setup()
r := getWidth(message)
if r < 9 {
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)
Expand All @@ -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)
Expand All @@ -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)
}
}

0 comments on commit f8d6e98

Please sign in to comment.