DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,503 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for The birth of potato
DagdrΓΆm
DagdrΓΆm

Posted on

The birth of potato

#go

Potato is the code-name for a small open source project that these days I am working on. I think an average programmer should be able to write this program in a day, however a more realistic estimate for me will be something between 5 days to 3 months...

One more thing: there are programs that already do what potato aims to do, however I am writing Potato will in Go, a native language that produces binaries with low startup latency -- a good fit for small CLI programs.

Anyway, as a wise man once said:

A journey of a thousand miles begins with a single step.


Writing a string in color using Go

This is a feature that Potato will gonna need. The code snippet below shows how we can print a string in color using Go:

package main;

import (
    "github.com/fatih/color"
)

func main() {
    color.Red("Hello world")
}
Enter fullscreen mode Exit fullscreen mode

More examples at github.com/fatih/color.


And a bit more work: splitting a string into its lines, prepending a colored prefix to each line, and printing it on the screen:

package main

import (
    "fmt"
    "strings"

    "github.com/fatih/color"
)

func main() {
    input := `
    This is a
    multiline
    string
    `

    for _, line := range strings.Split(input, "\n") {
        fmt.Println(color.RedString("[TOMATO] ") + line)
    }

    for _, line := range strings.Split(input, "\n") {
        fmt.Println(color.GreenString("[LETTUCE] ") + line)
    }
}
Enter fullscreen mode Exit fullscreen mode

Another example: an inefficient way of prepending a colorful prefix to every line of a text file:

package main

import (
    "fmt"
    "log"
    "os"
    "strings"

    "github.com/fatih/color"
)

func main() {
    body, err := os.ReadFile("/workspaces/lorem.txt")
    if err != nil {
        log.Fatal(err)
    }

    for _, line := range strings.Split(string(body), "\n") {
        fmt.Println(color.RedString("[PREFIX] ") + line)
    }
}
Enter fullscreen mode Exit fullscreen mode

Possible call syntax:

$ potato --std-out-color green \
         --std-out-prefix "[STDOUT] " \
         --std-err-color red \
         --std-err-prefix "[STDERR] " \
         --command build --project ./proj1 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

DEV has this feature:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. πŸ›