devdotlog avatar

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

ssh0702

Published: 25 Mar 2019 › Updated: 25 Mar 2019golang으로 console 환경에서 UI 처리하기

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

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

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

Written by

Read more #go posts


Best Posts From devdotlog

We have not curated any of ssh0702's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From devdotlog