Terminal control/Cursor movement: Difference between revisions
Content added Content deleted
(added autohotkey implementation) |
(Go solution) |
||
Line 248: | Line 248: | ||
</lang> |
</lang> |
||
=={{header|Go}}== |
|||
===External commands=== |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"time" |
|||
"os" |
|||
"os/exec" |
|||
"strconv" |
|||
) |
|||
func main() { |
|||
tput("clear") // clear screen |
|||
tput("cup", "6", "3") // an initial position |
|||
time.Sleep(1 * time.Second) |
|||
tput("cub1") // left |
|||
time.Sleep(1 * time.Second) |
|||
tput("cuf1") // right |
|||
time.Sleep(1 * time.Second) |
|||
tput("cuu1") // up |
|||
time.Sleep(1 * time.Second) |
|||
// cud1 seems broken for me. cud 1 works fine though. |
|||
tput("cud", "1") // down |
|||
time.Sleep(1 * time.Second) |
|||
tput("cr") // begining of line |
|||
time.Sleep(1 * time.Second) |
|||
// get screen size here |
|||
var h, w int |
|||
cmd := exec.Command("stty", "size") |
|||
cmd.Stdin = os.Stdin |
|||
d, _ := cmd.Output() |
|||
fmt.Sscan(string(d), &h, &w) |
|||
// end of line |
|||
tput("hpa", strconv.Itoa(w-1)) |
|||
time.Sleep(2 * time.Second) |
|||
// top left |
|||
tput("home") |
|||
time.Sleep(2 * time.Second) |
|||
// bottom right |
|||
tput("cup", strconv.Itoa(h-1), strconv.Itoa(w-1)) |
|||
time.Sleep(3 * time.Second) |
|||
} |
|||
func tput(args ...string) error { |
|||
cmd := exec.Command("tput", args...) |
|||
cmd.Stdout = os.Stdout |
|||
return cmd.Run() |
|||
}</lang> |
|||
===ANSI escape codes=== |
|||
Not meeting all task requirements. Some of the movements are awkward with ANSI escape codes alone and are best done with one of the other techniques shown. |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"time" |
|||
) |
|||
func main() { |
|||
fmt.Print("\033[2J\033[6;3H") // clear screen, move to an initial position |
|||
time.Sleep(1 * time.Second) // pause to let cursor blink |
|||
fmt.Print("\033[D") // left |
|||
time.Sleep(1 * time.Second) |
|||
fmt.Print("\033[C") // right |
|||
time.Sleep(1 * time.Second) |
|||
fmt.Print("\033[A") // up |
|||
time.Sleep(1 * time.Second) |
|||
fmt.Print("\033[B") // down |
|||
time.Sleep(1 * time.Second) |
|||
fmt.Print("\033[;H") // top left |
|||
time.Sleep(1 * time.Second) |
|||
}</lang> |
|||
===Ncurses=== |
|||
{{libheader|curses}} |
|||
<lang go>package main |
|||
import ( |
|||
"log" |
|||
"time" |
|||
gc "code.google.com/p/goncurses" |
|||
) |
|||
func main() { |
|||
s, err := gc.Init() |
|||
if err != nil { |
|||
log.Fatal("init:", err) |
|||
} |
|||
defer gc.End() |
|||
// an initial position |
|||
s.Move(6, 3) |
|||
s.Refresh() // update screen |
|||
time.Sleep(1 * time.Second) // allow time for cursor to blink |
|||
// left |
|||
y, x := s.CursorYX() |
|||
s.Move(y, x-1) |
|||
s.Refresh() |
|||
time.Sleep(1 * time.Second) |
|||
// right |
|||
y, x = s.CursorYX() |
|||
s.Move(y, x+1) |
|||
s.Refresh() |
|||
time.Sleep(1 * time.Second) |
|||
// up |
|||
y, x = s.CursorYX() |
|||
s.Move(y-1, x) |
|||
s.Refresh() |
|||
time.Sleep(1 * time.Second) |
|||
// down |
|||
y, x = s.CursorYX() |
|||
s.Move(y+1, x) |
|||
s.Refresh() |
|||
time.Sleep(1 * time.Second) |
|||
// beginning of line |
|||
y, x = s.CursorYX() |
|||
s.Move(y, 0) |
|||
s.Refresh() |
|||
time.Sleep(1 * time.Second) |
|||
// get window size for moves to edges |
|||
my, mx := s.MaxYX() |
|||
// end of line |
|||
y, x = s.CursorYX() |
|||
s.Move(y, mx-1) |
|||
s.Refresh() |
|||
time.Sleep(2 * time.Second) |
|||
// top left |
|||
s.Move(0, 0) |
|||
s.Refresh() |
|||
time.Sleep(2 * time.Second) |
|||
// bottom right |
|||
s.Move(my-1, mx-1) |
|||
s.Refresh() |
|||
s.GetChar() |
|||
}</lang> |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |