Créez une application de ligne de commande avec Go: lolcat

Vous aimez les applications CLI?Ne manquez pas levachetutoriel aussi!

Je cherchais des applications terminales pour m'inspirer et je suis tombé surlol chat.

L'original esthttps://github.com/busyloop/lolcatet il y a déjà pas mal d'implémentations Go:

Ressemble àchose complètement inutileà construire, alors faisons-le!

Commençons par imprimer certaines valeurs à l'écran, puis nous passerons à leur coloration, puis nous examinerons l'acceptation des entrées de l'utilisateur pour qu'elles fonctionnent comme un tube.

j'utilisehttps://github.com/enodata/fakerpour générer une fausse sortie.

go get -u github.com/enodata/faker

Ce programme produit un certain nombre de phrases:

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>))

}

Malheureusement, tout cela est ennuyeux en N / B. Ajoutons de la couleur. Nous pouvons le faire en ajoutant une séquence de caractères d'échappement dansfmt.Printf. Ceci imprime toutes les chaînes dans la couleur or # FFD700, dontCode couleur RVBest (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]) } }

Maintenant que nous avons une chaîne et les bases pour rendre chaque personnage coloré d'une manière différente, il est temps d'introduire l'arc-en-ciel.

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>()

}

C'est du style!

La couleur arc-en-ciel est générée à l'aide durgb()fonction, telle qu'implémentée dans le code source original de Ruby danshttps://github.com/busyloop/lolcat/blob/master/lib/lolcat/lol.rb

Éditons maintenant le programme et au lieu de fournir sa propre sortie, laissez-letravailler comme un tuyaupour d'autres programmes. Il lira le contenu deos.Stdinet arc-en-ciel.

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>)

}

Il lit une rune à la fois depuisos.Stdinet l'ajoute auoutputtranche de runes.

Le rendu de sortie a été extrait dans print (), mais nous pourrions également diriger «juste à temps» chaque rune au fur et à mesure de son analyse:

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>
}

}

Cela fonctionne comme avant.

Nous pouvons maintenant nous divertir avec la fortune etvache

Faisons une commande à l'échelle du système en exécutantgo buildetgo install. La commande sera exécutée commegololcat, puisque nous l'avons utilisé comme nom de dossier.


Plus de tutoriels go: