Skip to content

Instantly share code, notes, and snippets.

@jiro4989
Last active December 30, 2020 07:40
Show Gist options
  • Save jiro4989/90cac3e7001686d62341b8314dfd122f to your computer and use it in GitHub Desktop.
Save jiro4989/90cac3e7001686d62341b8314dfd122f to your computer and use it in GitHub Desktop.
tiny ascii art scripts on Nim language.
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
]#
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:
#[
γ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§γ¨γ†γ”γ–γ„γΎγ™οΌδ»ŠεΉ΄γ‚‚γ‚ˆγ‚γ—γγŠι‘˜γ„γ—γΎ
γΎγ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§γ¨γ†γ”γ–γ„γΎγ™οΌδ»ŠεΉ΄γ‚‚γ‚ˆγ‚γ—γγŠι‘˜γ„γ—
γ—γΎγ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§γ¨γ†γ”γ–γ„γΎγ™οΌδ»ŠεΉ΄γ‚‚γ‚ˆγ‚γ—γγŠι‘˜γ„
γ„γ—γΎγ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§γ¨γ†γ”γ–γ„γΎγ™οΌδ»ŠεΉ΄γ‚‚γ‚ˆγ‚γ—γγŠι‘˜
ι‘˜γ„γ—γΎγ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§γ¨γ†γ”γ–γ„γΎγ™οΌδ»ŠεΉ΄γ‚‚γ‚ˆγ‚γ—γγŠ
γŠι‘˜γ„γ—γΎγ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§γ¨γ†γ”γ–γ„γΎγ™οΌδ»ŠεΉ΄γ‚‚γ‚ˆγ‚γ—γ
γγŠι‘˜γ„γ—γΎγ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§γ¨γ†γ”γ–γ„γΎγ™οΌδ»ŠεΉ΄γ‚‚γ‚ˆγ‚γ—
γ—γγŠι‘˜γ„γ—γΎγ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§γ¨γ†γ”γ–γ„γΎγ™οΌδ»ŠεΉ΄γ‚‚γ‚ˆγ‚
γ‚γ—γγŠι‘˜γ„γ—γΎγ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§γ¨γ†γ”γ–γ„γΎγ™οΌδ»ŠεΉ΄γ‚‚γ‚ˆ
γ‚ˆγ‚γ—γγŠι‘˜γ„γ—γΎγ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§γ¨γ†γ”γ–γ„γΎγ™οΌδ»ŠεΉ΄γ‚‚
γ‚‚γ‚ˆγ‚γ—γγŠι‘˜γ„γ—γΎγ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§γ¨γ†γ”γ–γ„γΎγ™οΌδ»ŠεΉ΄
εΉ΄γ‚‚γ‚ˆγ‚γ—γγŠι‘˜γ„γ—γΎγ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§γ¨γ†γ”γ–γ„γΎγ™οΌδ»Š
δ»ŠεΉ΄γ‚‚γ‚ˆγ‚γ—γγŠι‘˜γ„γ—γΎγ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§γ¨γ†γ”γ–γ„γΎγ™οΌ
οΌδ»ŠεΉ΄γ‚‚γ‚ˆγ‚γ—γγŠι‘˜γ„γ—γΎγ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§γ¨γ†γ”γ–γ„γΎγ™
γ™οΌδ»ŠεΉ΄γ‚‚γ‚ˆγ‚γ—γγŠι‘˜γ„γ—γΎγ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§γ¨γ†γ”γ–γ„γΎ
γΎγ™οΌδ»ŠεΉ΄γ‚‚γ‚ˆγ‚γ—γγŠι‘˜γ„γ—γΎγ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§γ¨γ†γ”γ–γ„
γ„γΎγ™οΌδ»ŠεΉ΄γ‚‚γ‚ˆγ‚γ—γγŠι‘˜γ„γ—γΎγ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§γ¨γ†γ”γ–
γ–γ„γΎγ™οΌδ»ŠεΉ΄γ‚‚γ‚ˆγ‚γ—γγŠι‘˜γ„γ—γΎγ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§γ¨γ†γ”
γ”γ–γ„γΎγ™οΌδ»ŠεΉ΄γ‚‚γ‚ˆγ‚γ—γγŠι‘˜γ„γ—γΎγ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§γ¨γ†
γ†γ”γ–γ„γΎγ™οΌδ»ŠεΉ΄γ‚‚γ‚ˆγ‚γ—γγŠι‘˜γ„γ—γΎγ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§γ¨
γ¨γ†γ”γ–γ„γΎγ™οΌδ»ŠεΉ΄γ‚‚γ‚ˆγ‚γ—γγŠι‘˜γ„γ—γΎγ™ζ–°εΉ΄γ‚γ‘γΎγ—γ¦γŠγ‚γ§
#]#
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:
#[
こんにけは  はけにんこ
こんにけ はは けにんこ
こんに けけけけ にんこ
こん にににににに んこ
こ んんんんんんんん こ
 ここここここここここ 
 ここここここここここ 
こ んんんんんんんん こ
こん にににににに んこ
こんに けけけけ にんこ
こんにけ はは けにんこ
こんにけは  はけにんこ
]#
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