Last active
December 30, 2020 07:40
-
-
Save jiro4989/90cac3e7001686d62341b8314dfd122f to your computer and use it in GitHub Desktop.
tiny ascii art scripts on Nim language.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import strutils, sequtils, algorithm | |
proc corner(text: string): seq[string] = | |
for i, v in text.pairs: | |
result.add(text[0..i] & " ".repeat(text.len-i)) | |
proc diamond(text: string): seq[string] = | |
let rightTop = corner(text) | |
let leftTop = rightTop.mapIt(it.reversed.join) | |
let rightBottom = rightTop.reversed | |
let leftBottom = leftTop.reversed | |
for i, v in rightTop: | |
result.add(leftTop[i] & v) | |
for i, v in rightBottom: | |
result.add(leftBottom[i] & v) | |
for line in diamond("hello world"): | |
echo line | |
# $ nim c -r ascii_dia.nim | |
# | |
# result: | |
#[ | |
hh | |
ehhe | |
lehhel | |
llehhell | |
ollehhello | |
ollehhello | |
w ollehhello w | |
ow ollehhello wo | |
row ollehhello wor | |
lrow ollehhello worl | |
dlrow ollehhello world | |
dlrow ollehhello world | |
lrow ollehhello worl | |
row ollehhello wor | |
ow ollehhello wo | |
w ollehhello w | |
ollehhello | |
ollehhello | |
llehhell | |
lehhel | |
ehhe | |
hh | |
]# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unicode | |
proc flow(text: string, limit: int): seq[string] = | |
var tmp = text.toRunes | |
for i in 0..limit: | |
tmp = tmp[^1] & tmp[0..^2] | |
let line = $tmp | |
result.add(line) | |
for line in flow("ζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎγοΌδ»εΉ΄γγγγγγι‘γγγΎγ", 20): | |
echo line | |
# $ nim c -r ascii_flow.nim | |
# | |
# output: | |
#[ | |
γζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎγοΌδ»εΉ΄γγγγγγι‘γγγΎ | |
γΎγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎγοΌδ»εΉ΄γγγγγγι‘γγ | |
γγΎγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎγοΌδ»εΉ΄γγγγγγι‘γ | |
γγγΎγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎγοΌδ»εΉ΄γγγγγγι‘ | |
ι‘γγγΎγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎγοΌδ»εΉ΄γγγγγγ | |
γι‘γγγΎγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎγοΌδ»εΉ΄γγγγγ | |
γγι‘γγγΎγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎγοΌδ»εΉ΄γγγγ | |
γγγι‘γγγΎγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎγοΌδ»εΉ΄γγγ | |
γγγγι‘γγγΎγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎγοΌδ»εΉ΄γγ | |
γγγγγι‘γγγΎγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎγοΌδ»εΉ΄γ | |
γγγγγγι‘γγγΎγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎγοΌδ»εΉ΄ | |
εΉ΄γγγγγγι‘γγγΎγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎγοΌδ» | |
δ»εΉ΄γγγγγγι‘γγγΎγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎγοΌ | |
οΌδ»εΉ΄γγγγγγι‘γγγΎγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎγ | |
γοΌδ»εΉ΄γγγγγγι‘γγγΎγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎ | |
γΎγοΌδ»εΉ΄γγγγγγι‘γγγΎγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγ | |
γγΎγοΌδ»εΉ΄γγγγγγι‘γγγΎγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγ | |
γγγΎγοΌδ»εΉ΄γγγγγγι‘γγγΎγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγ | |
γγγγΎγοΌδ»εΉ΄γγγγγγι‘γγγΎγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γ | |
γγγγγΎγοΌδ»εΉ΄γγγγγγι‘γγγΎγζ°εΉ΄γγγΎγγ¦γγγ§γ¨ | |
γ¨γγγγγΎγοΌδ»εΉ΄γγγγγγι‘γγγΎγζ°εΉ΄γγγΎγγ¦γγγ§ | |
#]# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unicode, strutils, algorithm, sequtils | |
iterator corner(text: string): string = | |
var tmp = text.toRunes | |
for i in 0..tmp.len: | |
var line = $tmp[0..^(i+1)] & "γ" | |
if i != 0: | |
let ch = tmp[^i..^i][0] | |
for i in 0..<i: | |
line.add ch | |
yield line | |
iterator square(text: string): string = | |
for line in corner(text): | |
echo line & line.toRunes.reversed.join | |
for line in toSeq(corner(text)).reversed: | |
echo line & line.toRunes.reversed.join | |
for line in square("γγγ«γ‘γ―"): | |
echo line | |
# $ nim c -r ascii_square.nim | |
# | |
# output: | |
#[ | |
γγγ«γ‘γ―γγγ―γ‘γ«γγ | |
γγγ«γ‘γγ―γ―γγ‘γ«γγ | |
γγγ«γγ‘γ‘γ‘γ‘γγ«γγ | |
γγγγ«γ«γ«γ«γ«γ«γγγ | |
γγγγγγγγγγγγ | |
γγγγγγγγγγγγ | |
γγγγγγγγγγγγ | |
γγγγγγγγγγγγ | |
γγγγ«γ«γ«γ«γ«γ«γγγ | |
γγγ«γγ‘γ‘γ‘γ‘γγ«γγ | |
γγγ«γ‘γγ―γ―γγ‘γ«γγ | |
γγγ«γ‘γ―γγγ―γ‘γ«γγ | |
]# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unicode, sequtils, algorithm, strutils | |
func corner(text: string): seq[string] = | |
var tmp = text.toRunes | |
for i in 1..tmp.len: | |
result.add $tmp[0..<i] | |
func maxLen(lines: seq[string]): int = | |
let lens = lines.mapIt(it.toRunes.len) | |
let zipped = zip(lens, lines) | |
let max = zipped.sortedByIt(it[0])[^1][0] | |
return max | |
func middle(lines: seq[string]): seq[string] = | |
let max = lines.maxLen | |
for line in lines: | |
let padLen = (max - line.toRunes.len) div 2 | |
let pad = "γ".repeat(padLen).join | |
result.add pad & line | |
func right(lines: seq[string]): seq[string] = | |
let max = lines.maxLen | |
for line in lines: | |
let padLen = (max - line.toRunes.len) | |
let pad = "γ".repeat(padLen).join | |
result.add pad & line | |
const text = "ζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎγ" | |
for line in corner(text): | |
echo line | |
for line in corner(text).middle: | |
echo line | |
for line in corner(text).right: | |
echo line | |
# $ nim c -r ascii_triangle.nim | |
# | |
# output: | |
#[ | |
ζ° | |
ζ°εΉ΄ | |
ζ°εΉ΄γ | |
ζ°εΉ΄γγ | |
ζ°εΉ΄γγγΎ | |
ζ°εΉ΄γγγΎγ | |
ζ°εΉ΄γγγΎγγ¦ | |
ζ°εΉ΄γγγΎγγ¦γ | |
ζ°εΉ΄γγγΎγγ¦γγ | |
ζ°εΉ΄γγγΎγγ¦γγγ§ | |
ζ°εΉ΄γγγΎγγ¦γγγ§γ¨ | |
ζ°εΉ΄γγγΎγγ¦γγγ§γ¨γ | |
ζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγ | |
ζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγ | |
ζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγ | |
ζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎ | |
ζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎγ | |
γγγγγγγγζ° | |
γγγγγγγζ°εΉ΄ | |
γγγγγγγζ°εΉ΄γ | |
γγγγγγζ°εΉ΄γγ | |
γγγγγγζ°εΉ΄γγγΎ | |
γγγγγζ°εΉ΄γγγΎγ | |
γγγγγζ°εΉ΄γγγΎγγ¦ | |
γγγγζ°εΉ΄γγγΎγγ¦γ | |
γγγγζ°εΉ΄γγγΎγγ¦γγ | |
γγγζ°εΉ΄γγγΎγγ¦γγγ§ | |
γγγζ°εΉ΄γγγΎγγ¦γγγ§γ¨ | |
γγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γ | |
γγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγ | |
γζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγ | |
γζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγ | |
ζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎ | |
ζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎγ | |
γγγγγγγγγγγγγγγγζ° | |
γγγγγγγγγγγγγγγζ°εΉ΄ | |
γγγγγγγγγγγγγγζ°εΉ΄γ | |
γγγγγγγγγγγγγζ°εΉ΄γγ | |
γγγγγγγγγγγγζ°εΉ΄γγγΎ | |
γγγγγγγγγγγζ°εΉ΄γγγΎγ | |
γγγγγγγγγγζ°εΉ΄γγγΎγγ¦ | |
γγγγγγγγγζ°εΉ΄γγγΎγγ¦γ | |
γγγγγγγγζ°εΉ΄γγγΎγγ¦γγ | |
γγγγγγγζ°εΉ΄γγγΎγγ¦γγγ§ | |
γγγγγγζ°εΉ΄γγγΎγγ¦γγγ§γ¨ | |
γγγγγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γ | |
γγγγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγ | |
γγγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγ | |
γγζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγ | |
γζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎ | |
ζ°εΉ΄γγγΎγγ¦γγγ§γ¨γγγγγΎγ | |
]# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment