Skip to content

Instantly share code, notes, and snippets.

@jaytaylor
Created October 31, 2018 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaytaylor/da22e042be9253a148095fe47a664aa4 to your computer and use it in GitHub Desktop.
Save jaytaylor/da22e042be9253a148095fe47a664aa4 to your computer and use it in GitHub Desktop.
ANSI terminal color-codes cleaner for Go.
package main
import (
"fmt"
"io/ioutil"
"regexp"
"github.com/spf13/cobra"
)
var (
doClean bool
rootCmd = &cobra.Command{
Use: "a-test-by-jay",
Short: "just a test",
Long: "just a test",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
expr := regexp.MustCompile(`\x1B\[[0-?]*[ -/]*[@-~]`)
data, err := ioutil.ReadFile(args[0])
if err != nil {
panic(err)
}
if doClean {
data = expr.ReplaceAll(data, []byte{})
}
fmt.Println(string(data))
},
}
)
func init() {
rootCmd.PersistentFlags().BoolVarP(&doClean, "clean", "c", false, "Activate regexp ANSI color cleaning")
}
func main() {
if err := rootCmd.Execute(); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment