golang으로 console 환경에서 UI 처리하기

in #go6 years ago

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)
        }
    }
}
Sort:  

Hello @ssh0702! This is a friendly reminder that you can download Partiko today and start earning Steem easier than ever before!

Partiko is a fast and beautiful mobile app for Steem. You can login using your Steem account, browse, post, comment and upvote easily on your phone!

You can even earn up to 3,000 Partiko Points per day, and easily convert them into Steem token!

Download Partiko now using the link below to receive 1000 Points as bonus right away!

https://partiko.app/referral/partiko

Congratulations @ssh0702! You received a personal award!

Happy Steem Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

Downvote challenge - Add up to 3 funny badges to your board
Vote for @Steemitboard as a witness to get one more award and increased upvotes!