8000 feat: initial version by moul · Pull Request #5 · moul/moulsay · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: initial version #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
8000
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@ orbs:
retry: moul/retry@0.6.0
docker: circleci/docker@0.5.13
#dl: moul/dl@1.7.0
tools: gotest/tools@0.0.9
tools: gotest/tools@0.0.10

jobs:
go-build:
executor: golang
steps:
- checkout
- restore_cache:
keys:
- go-mod-v1-{{ checksum "go.sum" }}
- go-mod-v1-
- retry/install
- tools/mod-download
- tools/mod-tidy-check
Expand All @@ -39,10 +35,6 @@ jobs:
- run: PATH=$PATH:$(pwd)/bin retry -m 3 make lint
- codecov/upload:
file: coverage.txt
- save_cache:
key: go-mod-v1-{{ checksum "go.sum" }}
paths:
- /go/pkg/mod

docker-build:
executor: docker
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module moul.io/moulsay

go 1.12

require (
github.com/eidolon/wordwrap v0.0.0-20161011182207-e0f54129b8bb
moul.io/asciimoul v1.1.0
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/eidolon/wordwrap v0.0.0-20161011182207-e0f54129b8bb h1:ioQwBmKdOCpMVS/bDaESqNWXIE/aw4+gsVtysCGMWZ4=
github.com/eidolon/wordwrap v0.0.0-20161011182207-e0f54129b8bb/go.mod h1:ZAPs+OyRzeVJFGvXVDVffgCzQfjg3qU9Ig8G/MU3zZ4=
moul.io/asciimoul v1.1.0 h1:eYfYiXljSe8Z6VO84N7x8rtZE1kTLK/HmU5q9eC+T+w=
moul.io/asciimoul v1.1.0/go.mod h1:uyy5fnzliF2LQ+z7+tHZTnWzaajv8g6ZyLbBoNdDiko=
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package main // import "moul.io/moulsay"

import "fmt"
import (
"fmt"
"os"
"strings"

"moul.io/moulsay/moulsay"
)

const totalMaxWidth = 72

func main() {
fmt.Println("Hello World!")
fmt.Println(moulsay.Say(strings.Join(os.Args[1:], " "), totalMaxWidth))
}
24 changes: 23 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
package main

import "os"

func Example() {
os.Args = []string{"moulsay", "hello", "world!"}
main()
// Output: Hello World!
// Output:
// ++
// ++++
// ++++
// ++++++++++
// +++ |
// ++ |
// + -== ==|
// ( <*> <*>
// | |
// | __|
// | +++
// \ =+
// \ + hello world!
// |\++++++
// | ++++ ||//
// ____| |____ _||/__
// / --- \ \| |||
// / _ _ _ / \ \ /
// | / / //_//_// | | |
}
1 change: 1 addition & 0 deletions moulsay/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package moulsay // import "moul.io/moulsay/moulsay"
39 changes: 39 additions & 0 deletions moulsay/moulsay.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package moulsay

import (
"fmt"
"strings"

"github.com/eidolon/wordwrap"
)

func Say(message string, totalMaxWidth int) string {
art := bodyLinesWithPadding()
art = trimEmptyLines(art)
artWidth := len(art[0])
maxTextWidth := totalMaxWidth - artWidth - 1
message = wordwrap.Wrapper(maxTextWidth, true)(message)
message = justifyCenter(message, maxTextWidth)
messageLines := strings.Split(message, "\n")
messageLines = trimEmptyLines(messageLines)
if len(messageLines) > len(art) {
art = append(
emptyLines(len(messageLines)-len(art), artWidth),
art...,
)
} else {
messageLines = append(
messageLines,
emptyLines((len(art)-len(messageLines))/3, 0)...,
)
messageLines = append(
emptyLines(len(art)-len(messageLines), 0),
messageLines...,
)
}
lines := make([]string, len(art))
for i := 0; i < len(art); i++ {
lines[i] = strings.TrimRight(fmt.Sprintf("%s %s", art[i], messageLines[i]), " ")
}
return strings.Join(lines, "\n")
}
27 changes: 27 additions & 0 deletions moulsay/moulsay_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package moulsay

import "fmt"

func ExampleSay() {
fmt.Println(Say("hello world!", 72))
// Output:
// ++
// ++++
// ++++
// ++++++++++
// +++ |
// ++ |
// + -== ==|
// ( <*> <*>
// | |
// | __|
// | +++
// \ =+
// \ + hello world!
// |\++++++
// | ++++ ||//
// ____| |____ _||/__
// / --- \ \| |||
// / _ _ _ / \ \ /
// | / / //_//_// | | |
}
63 changes: 63 additions & 0 deletions moulsay/util.go
AE96
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package moulsay

import (
"strings"

"moul.io/asciimoul"
)

func bodyLinesWithPadding() []string {
lines := strings.Split(asciimoul.Body, "\n")
longest := 0
for _, line := range lines {
if len(line) > longest {
longest = len(line)
}
}
for i, line := range lines {
lines[i] = line + strings.Repeat(" ", longest-len(line))
}
return lines
}

func justifyCenter(s string, n int) string {
lines := strings.Split(s, "\n")
for i, line := range lines {
if len(line) < n {
lines[i] = strings.Repeat(" ", (n-len(line))/2) + line
}
}
return strings.Join(lines, "\n")
}

func trimEmptyLines(input []string) []string {
left := 0
right := len(input)
for i := 0; i < len(input); i++ {
if strings.TrimSpace(input[i]) == "" {
left++
} else {
break
}
}
for i := right; i > left; i-- {
if strings.TrimSpace(input[i-1]) == "" {
right--
} else {
break
}
}
return input[left:right]
}

func emptyLines(amount int, width int) []string {
if amount <= 1 {
return []string{}
}
lines := make([]string, amount)
emptyLine := strings.Repeat(" ", width)
for i := 0; i < amount; i++ {
lines[i] = emptyLine
}
return lines
}
0