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