使用Go构建一个命令行应用程序:lolcat

喜欢CLI应用程序吗?不要错过Cowsay教程也一样!

我一直在寻找一些灵感的终端应用程序,但偶然发现洛尔卡特

原来是https://github.com/busyloop/lolcat并且已经有很多Go实现:

看起来像完全没用的东西建立,让我们开始吧!

首先,在屏幕上打印一些值,然后为它们着色,然后研究接受用户输入以充当管道。

我用https://github.com/enodata/faker生成伪造的输出。

go get -u github.com/enodata/faker

该程序输出许多短语:

package main

import ( “fmt” “strings”

<span style="color:#e6db74">"github.com/enodata/faker"</span>

)

func main() { var phrases []string

<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">i</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">1</span>; <span style="color:#a6e22e">i</span> &lt; <span style="color:#ae81ff">3</span>; <span style="color:#a6e22e">i</span><span style="color:#f92672">++</span> {
    <span style="color:#a6e22e">phrases</span> = append(<span style="color:#a6e22e">phrases</span>, <span style="color:#a6e22e">faker</span>.<span style="color:#a6e22e">Hacker</span>().<span style="color:#a6e22e">Phrases</span>()<span style="color:#f92672">...</span>)
}

<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">strings</span>.<span style="color:#a6e22e">Join</span>(<span style="color:#a6e22e">phrases</span>[:], <span style="color:#e6db74">"; "</span>))

}

不幸的是,这真是无聊的黑白。让我们添加一些颜色。我们可以通过在前面添加一个转义字符序列来做到这一点fmt.Printf。这将以金色#FFD700打印所有字符串,其颜色RGB颜色代码是(255,215,0):

package main

import ( “fmt” “strings”

<span style="color:#e6db74">"github.com/enodata/faker"</span>

)

func main() { var phrases []string

<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">i</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">1</span>; <span style="color:#a6e22e">i</span> &lt; <span style="color:#ae81ff">3</span>; <span style="color:#a6e22e">i</span><span style="color:#f92672">++</span> {
    <span style="color:#a6e22e">phrases</span> = append(<span style="color:#a6e22e">phrases</span>, <span style="color:#a6e22e">faker</span>.<span style="color:#a6e22e">Hacker</span>().<span style="color:#a6e22e">Phrases</span>()<span style="color:#f92672">...</span>)
}

<span style="color:#a6e22e">output</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">strings</span>.<span style="color:#a6e22e">Join</span>(<span style="color:#a6e22e">phrases</span>[:], <span style="color:#e6db74">"; "</span>)
<span style="color:#a6e22e">r</span>, <span style="color:#a6e22e">g</span>, <span style="color:#a6e22e">b</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">255</span>, <span style="color:#ae81ff">215</span>, <span style="color:#ae81ff">0</span> <span style="color:#75715e">//gold color

for j := 0; j < len(output); j++ { fmt.Printf("\033[38;2;%d;%d;%dm%c\033[0m", r, g, b, output[j]) } }

既然我们已经有了一个字符串,并且为使每个字符以不同的方式着色的基础,是时候介绍彩虹了。

package main

import ( “fmt” “math” “strings”

<span style="color:#e6db74">"github.com/enodata/faker"</span>

)

func rgb(i int) (int, int, int) { var f = 0.1 return int(math.Sin(ffloat64(i)+0)127 + 128), int(math.Sin(ffloat64(i)+2math.Pi/3)127 + 128), int(math.Sin(ffloat64(i)+4math.Pi/3)127 + 128) }

func main() { var phrases []string

<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">i</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">1</span>; <span style="color:#a6e22e">i</span> &lt; <span style="color:#ae81ff">3</span>; <span style="color:#a6e22e">i</span><span style="color:#f92672">++</span> {
    <span style="color:#a6e22e">phrases</span> = append(<span style="color:#a6e22e">phrases</span>, <span style="color:#a6e22e">faker</span>.<span style="color:#a6e22e">Hacker</span>().<span style="color:#a6e22e">Phrases</span>()<span style="color:#f92672">...</span>)
}

<span style="color:#a6e22e">output</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">strings</span>.<span style="color:#a6e22e">Join</span>(<span style="color:#a6e22e">phrases</span>[:], <span style="color:#e6db74">"; "</span>)

<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">j</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">0</span>; <span style="color:#a6e22e">j</span> &lt; len(<span style="color:#a6e22e">output</span>); <span style="color:#a6e22e">j</span><span style="color:#f92672">++</span> {
    <span style="color:#a6e22e">r</span>, <span style="color:#a6e22e">g</span>, <span style="color:#a6e22e">b</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">rgb</span>(<span style="color:#a6e22e">j</span>)
    <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">"\033[38;2;%d;%d;%dm%c\033[0m"</span>, <span style="color:#a6e22e">r</span>, <span style="color:#a6e22e">g</span>, <span style="color:#a6e22e">b</span>, <span style="color:#a6e22e">output</span>[<span style="color:#a6e22e">j</span>])
}
<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>()

}

那是风格!

彩虹颜色是使用rgb()函数,如原始Ruby源代码中实现的https://github.com/busyloop/lolcat/blob/master/lib/lolcat/lol.rb

现在让我们编辑程序,而不是提供自己的输出,而是让它像管道一样工作对于其他程序。它将从中读取内容os.Stdin彩虹化它。

package main

import ( “bufio” “fmt” “io” “math” “os” )

func rgb(i int) (int, int, int) { var f = 0.1 return int(math.Sin(ffloat64(i)+0)127 + 128), int(math.Sin(ffloat64(i)+2math.Pi/3)127 + 128), int(math.Sin(ffloat64(i)+4math.Pi/3)127 + 128) }

func print(output []rune) { for j := 0; j < len(output); j++ { r, g, b := rgb(j) fmt.Printf("\033[38;2;%d;%d;%dm%c\033[0m", r, g, b, output[j]) } fmt.Println() }

func main() { info, _ := os.Stdin.Stat() var output []rune

<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">info</span>.<span style="color:#a6e22e">Mode</span>()<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">os</span>.<span style="color:#a6e22e">ModeCharDevice</span> <span style="color:#f92672">!=</span> <span style="color:#ae81ff">0</span> {
    <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">"The command is intended to work with pipes."</span>)
    <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">"Usage: fortune | gorainbow"</span>)
}

<span style="color:#a6e22e">reader</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">bufio</span>.<span style="color:#a6e22e">NewReader</span>(<span style="color:#a6e22e">os</span>.<span style="color:#a6e22e">Stdin</span>)
<span style="color:#66d9ef">for</span> {
    <span style="color:#a6e22e">input</span>, <span style="color:#a6e22e">_</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">reader</span>.<span style="color:#a6e22e">ReadRune</span>()
    <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> <span style="color:#f92672">&amp;&amp;</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">==</span> <span style="color:#a6e22e">io</span>.<span style="color:#a6e22e">EOF</span> {
        <span style="color:#66d9ef">break</span>
    }
    <span style="color:#a6e22e">output</span> = append(<span style="color:#a6e22e">output</span>, <span style="color:#a6e22e">input</span>)
}

print(<span style="color:#a6e22e">output</span>)

}

它一次读取一个符文os.Stdin并将其添加到output片符文。

输出渲染已被提取到print()中,但是我们也可以在扫描每个符文时“及时”传递它:

package main

import ( “bufio” “fmt” “io” “math” “os” )

func rgb(i int) (int, int, int) { var f = 0.1 return int(math.Sin(ffloat64(i)+0)127 + 128), int(math.Sin(ffloat64(i)+2math.Pi/3)127 + 128), int(math.Sin(ffloat64(i)+4math.Pi/3)127 + 128) }

func main() { info, _ := os.Stdin.Stat()

<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">info</span>.<span style="color:#a6e22e">Mode</span>()<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">os</span>.<span style="color:#a6e22e">ModeCharDevice</span> <span style="color:#f92672">!=</span> <span style="color:#ae81ff">0</span> {
    <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">"The command is intended to work with pipes."</span>)
    <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">"Usage: fortune | gorainbow"</span>)
}

<span style="color:#a6e22e">reader</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">bufio</span>.<span style="color:#a6e22e">NewReader</span>(<span style="color:#a6e22e">os</span>.<span style="color:#a6e22e">Stdin</span>)
<span style="color:#a6e22e">j</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">0</span>
<span style="color:#66d9ef">for</span> {
    <span style="color:#a6e22e">input</span>, <span style="color:#a6e22e">_</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">reader</span>.<span style="color:#a6e22e">ReadRune</span>()
    <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> <span style="color:#f92672">&amp;&amp;</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">==</span> <span style="color:#a6e22e">io</span>.<span style="color:#a6e22e">EOF</span> {
        <span style="color:#66d9ef">break</span>
    }
    <span style="color:#a6e22e">r</span>, <span style="color:#a6e22e">g</span>, <span style="color:#a6e22e">b</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">rgb</span>(<span style="color:#a6e22e">j</span>)
    <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">"\033[38;2;%d;%d;%dm%c\033[0m"</span>, <span style="color:#a6e22e">r</span>, <span style="color:#a6e22e">g</span>, <span style="color:#a6e22e">b</span>, <span style="color:#a6e22e">input</span>)
    <span style="color:#a6e22e">j</span><span style="color:#f92672">++</span>
}

}

这和以前一样。

现在,我们可以发财致富了,Cowsay

让我们通过运行此命令使其成为系统范围的命令go buildgo install。该命令将以gololcat,因为我们使用它作为文件夹名称。


更多教程: