github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/cmds/exp/ansi/ansi.go (about)

     1  // Copyright 2015-2017 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Print ansi escape sequences.
     6  //
     7  // Synopsis:
     8  //
     9  //	ansi COMMAND
    10  //
    11  // Options:
    12  //
    13  //	COMMAND must be one of:
    14  //	    - clear: clear the screen and reset the cursor position
    15  //
    16  // Author:
    17  //
    18  //	Manoel Vilela <manoel_vilela@engineer.com>
    19  package main
    20  
    21  import (
    22  	"fmt"
    23  	"io"
    24  	"log"
    25  	"os"
    26  )
    27  
    28  // using ansi escape codes /033 => escape code
    29  // The "\033[1;1H" part moves the cursor to position (1,1)
    30  // "\033[2J" part clears the screen.
    31  // if you wants add more escape codes, append on map below
    32  // arg:escape_code
    33  var commands = map[string]string{
    34  	"clear": "\033[1;1H\033[2J",
    35  }
    36  
    37  func ansi(w io.Writer, args []string) error {
    38  	for _, arg := range args {
    39  		_, exists := commands[arg]
    40  		if exists {
    41  			fmt.Fprintf(w, commands[arg])
    42  		} else {
    43  			return fmt.Errorf("Command ANSI '%v' don't exists", arg)
    44  		}
    45  	}
    46  	return nil
    47  }
    48  
    49  func main() {
    50  	if err := ansi(os.Stdout, os.Args[1:]); err != nil {
    51  		log.Fatalf("%v", err)
    52  	}
    53  }