This post is from a low-reputation account and contains an unverified outbound link. Be cautious before clicking external links.
console 환경에서 GUI 처럼 구성하는 것을 TUI라고 한다. 아래의 예제처럼 console 환경에서도 다양한 처리가 가능한다.
golang으로 단순한 console을 넘어서는 application을 위해서는 이런 TUI 가 필요한데, 많이 사용되는 라이브러리로 termbox(https://github.com/nsf/termbox-go)가 있다.
다음 예제는 CTRL + V, CTRL + X의 키보드 이벤트를 처리하는 예제이다. 단순하게 키보드 이벤트 처리만 할 수도 있다.
package main
import (
"fmt"
termbox "github.com/nsf/termbox-go"
)
func main() {
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
// clear
termbox.Flush()
loop:
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
// program exit
if ev.Key == termbox.KeyCtrlX {
break loop
}
// past clipboard
if ev.Key == termbox.KeyCtrlV {
fmt.Println("Keyboard Ctrl + V")
}
termbox.Flush()
case termbox.EventError:
panic(ev.Err)
}
}
}