gopkg.in/hugelgupf/u-root.v7@v7.0.0-20180831062900-6a07824681b2/cmds/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  //     ansi COMMAND
     9  //
    10  // Options:
    11  //     COMMAND must be one of:
    12  //         - clear: clear the screen and reset the cursor position
    13  //
    14  // Author:
    15  //     Manoel Vilela <manoel_vilela@engineer.com>
    16  package main
    17  
    18  import (
    19  	"fmt"
    20  	"io"
    21  	"log"
    22  	"os"
    23  )
    24  
    25  // using ansi escape codes /033 => escape code
    26  // The "\033[1;1H" part moves the cursor to position (1,1)
    27  // "\033[2J" part clears the screen.
    28  // if you wants add more escape codes, append on map below
    29  // arg:escape_code
    30  var commands = map[string]string{
    31  	"clear": "\033[1;1H\033[2J",
    32  }
    33  
    34  func ansi(w io.Writer, args []string) error {
    35  	for _, arg := range args {
    36  		_, exists := commands[arg]
    37  		if exists {
    38  			fmt.Fprintf(w, commands[arg])
    39  		} else {
    40  			return fmt.Errorf("Command ANSI '%v' don't exists", arg)
    41  		}
    42  	}
    43  	return nil
    44  }
    45  
    46  func main() {
    47  	if err := ansi(os.Stdout, os.Args[1:]); err != nil {
    48  		log.Fatalf("%v", err)
    49  	}
    50  }