Skip to content

Instantly share code, notes, and snippets.

@alex-ant
Last active April 6, 2018 13:16
Show Gist options
  • Save alex-ant/c050a6bc3633d90d76f7fd7c50890c1e to your computer and use it in GitHub Desktop.
Save alex-ant/c050a6bc3633d90d76f7fd7c50890c1e to your computer and use it in GitHub Desktop.
String to vertical text
package main
import (
"fmt"
"strings"
"unicode/utf8"
)
const (
delimiter string = "_"
centerText bool = true
)
func extendString(str *string, minLen int) {
tmpStr := *str
for i := 0; i < minLen-utf8.RuneCountInString(*str); i++ {
if i%2 == 0 || !centerText {
tmpStr += delimiter
continue
}
tmpStr = delimiter + tmpStr
}
*str = tmpStr
}
func getSlice(str string) (strSl []string, longestWordLen int) {
strSl = strings.Split(str, " ")
for _, word := range strSl {
wLen := utf8.RuneCountInString(word)
if longestWordLen < wLen {
longestWordLen = wLen
}
}
for i := 0; i < len(strSl); i++ {
extendString(&strSl[i], longestWordLen)
}
return
}
func main() {
str := "Some text here to be converted into vertical"
strSl, lw := getSlice(str)
fmt.Println("```")
for i := 0; i < lw; i++ {
for _, word := range strSl {
fmt.Printf("%s ", string([]rune(word)[i]))
}
fmt.Print("\n")
}
fmt.Println("```")
// Output with centerText=false:
// ```
// S t h t b c i v
// o e e o e o n e
// m x r _ _ n t r
// e t e _ _ v o t
// _ _ _ _ _ e _ i
// _ _ _ _ _ r _ c
// _ _ _ _ _ t _ a
// _ _ _ _ _ e _ l
// _ _ _ _ _ d _ _
// ```
// Output with centerText=true:
// ```
// _ _ _ _ _ c _ v
// _ _ _ _ _ o _ e
// S t h _ _ n i r
// o e e t b v n t
// m x r o e e t i
// e t e _ _ r o c
// _ _ _ _ _ t _ a
// _ _ _ _ _ e _ l
// _ _ _ _ _ d _ _
// ```
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment