Documentation ¶
Overview ¶
Provides an API binding for the TUI-Library libncurses. The binding is designed to be used in multithreaded environments and therefore uses an command channel to communicate with the ncurses library.
Example (GoTMDialog) ¶
w, _ := ncurses.Initscr() // Ensure, that ncurses will be properly exited defer ncurses.Endwin() defer func() { if r := recover(); r != nil { ncurses.Endwin() fmt.Printf("panic:\n%s\n", r) os.Exit(-1) } }() // Enable color mode ncurses.StartColor() // Define color pairs ncurses.AddColorPair("bw", ncurses.ColorGreen, ncurses.ColorBlack) ncurses.AddColorPair("wb", ncurses.ColorWhite, ncurses.ColorBlue) // Set cursor visiblity to hidden ncurses.SetCursor(ncurses.CURSOR_HIDDEN) // Automatically refresh after each command w.AutoRefresh = true // Set color for stdscr-window to system defaults. w.Wbkgd("std") // Draw a border around main window (stdscr) w.Box() // Create a new window for greeting-text at cell (x=20,y=5) with a size of 25 x 5 cells. w2, err := ncurses.NewWindow("dialog", ncurses.Position{20, 5}, ncurses.Size{25, 5}) // This can fail if the terminal is too small. if err != nil { panic(err) } w2.AutoRefresh = true // Use color pair wb (2) w2.Wbkgd("wb") // Draw a border around our "Greeting Window". w2.Box() // Move cursor relative to the window borders of w2 w2.Move(2, 3) // Output our greeting text fmt.Fprintf(w2, "Hello from Go\u2122-Lang!") // Move cursor relative to the beginning of our main window w.Move(17, 19) // Output exit instruction for the user fmt.Fprintf(w, " => Press a key to exit <=") // Wait for user input (e.g. keypress) w.Getch()
Output:
Index ¶
- func AddColorPair(name string, fg, bg Color)
- func Endwin() error
- func GetComChannel() chan<- Command
- func SetColor(name string) error
- func SetCursor(choice CursorVisibility) error
- func SetEcho(on bool) error
- func StartColor() error
- type Attribute
- type Color
- type Command
- type CommandName
- type CommandScope
- type CommandValue
- type CursorVisibility
- type Position
- type Size
- type Window
- func (w *Window) Box()
- func (w *Window) Clear()
- func (w *Window) GetMaxYX() (Size, error)
- func (w *Window) GetName() string
- func (w *Window) Getch() rune
- func (w *Window) Insert(format string, val ...interface{})
- func (w *Window) Move(y, x uint16)
- func (w *Window) Refresh()
- func (w *Window) Scroll(n int)
- func (w *Window) SetAttribute(att ...Attribute)
- func (w *Window) SetScrolling(enable bool)
- func (w *Window) String() string
- func (w *Window) Wbkgd(pairName string) error
- func (w *Window) Write(p []byte) (n int, err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddColorPair ¶
Adds a new pair of colors, which can be used to manipulate the color of terminal outputs. Choose one color of type Color.
Example ¶
w, _ := Initscr() defer Endwin() AddColorPair("mypair", ColorWhite, ColorBlue) SetColor("mypair") fmt.Fprintf(w, "Hello in White and Blue")
Output: Hello in White and Blue
func Endwin ¶
func Endwin() error
Closes an initialized terminal. Must be called before the program is about to exit.
func GetComChannel ¶
func GetComChannel() chan<- Command
Returns the command channel associated with stdscr (see ncurses documentation for information about stdscr)
func SetColor ¶
Selects a font foreground/background color pair. Every write uses the new color pair.
Use AddColorPair to create ncurses Color-Pairs. There is one default color-pair which defaults to the terminal color-set for foreground and background.
func SetCursor ¶
func SetCursor(choice CursorVisibility) error
Sets visiblity of the terminal cursor.
func StartColor ¶
func StartColor() error
Changes terminal to color mode. If the terminal does not support colors. StartColor returns an error.
Types ¶
type Attribute ¶
type Attribute int
const ( // Normal text AttrNormal Attribute = C.A_NORMAL // Hightlighted text AttrHighlighted Attribute = C.A_STANDOUT // Underlined text AttrUnderline Attribute = C.A_UNDERLINE AttrReversed Attribute = C.A_REVERSE // Blinking text AttrBlink Attribute = C.A_BLINK // Dimmed text AttrDim Attribute = C.A_DIM // Bold text AttrBold Attribute = C.A_BOLD // Protected text AttrProtect Attribute = C.A_PROTECT // Hidden text AttrInvisible Attribute = C.A_INVIS // Alternative Charset AttrAltcharset Attribute = C.A_ALTCHARSET )
type Command ¶
type Command struct { // Name of the command Name CommandName // Window on which the command should be executed Window *Window // Scope of command Scope CommandScope // Data which should be passed along with the command Value CommandValue }
Ncurse related commands
type CommandName ¶
type CommandName uint8
Name of a ncurses command
const ( MOVE CommandName = iota // Move the terminal cursor ADD // Add string at current cursor location INSERT // Insert string at current cursor location DELETE // Delete string at current cursor locatio REFRESH // Flush buffer to video memory CLEAR // Clear the entire window SCROLLOK // Enables scrolling SCROLL // Scrolls current window SETCOLOR // Sets the color for text and background WBKGD // Sets fg and bg of entire window ATTRSET // Sets font attributes for follwing output START_TA // UNIMPL(sebi2020) Initiates a transaction END_TA // UNIMPL(sebi2020) Finalizes a transaction )
All available commands, which can be passed to the command channel
func (CommandName) String ¶
func (cn CommandName) String() string
TODO(sebi2020): Use generator for this
type CommandScope ¶
type CommandScope int
Scope of Command
const ( GLOBAL CommandScope = iota // not a window specific command LOCAL // a window specific command )
type CursorVisibility ¶
type CursorVisibility uint8
Defines visibility of the terminal cursor.
const ( // Cursor is hidden CURSOR_HIDDEN CursorVisibility = iota // Cursor is visible CURSOR_VISIBLE // Cursor is visible and highlighted (not supported on all terminals) CURSOR_HIGHTLIGHTED )
type Window ¶
type Window struct { // Set to true, if you want to automatically refresh the window after it recieved a command AutoRefresh bool // contains filtered or unexported fields }
Holds information about the currently used Terminal and works as a handle for all ncurses related function calls.
func Initscr ¶
Initializes the ncurses library and install cleanup functions. The function returns a new Term struct. After calling this method no outputs should be done with fmt.Print*. Make sure to call Term.Endwin() before making any output with fmt.Print*.
Example (DoThis) ¶
fmt.Println("Before: This is okay!") w, _ := Initscr() // Normally, you could use defer Endwin() fmt.Fprintf(w, "While: This is also okay!") Endwin() fmt.Printf("After: This is also okay!") // do some things
Output:
Example (DontDoThis) ¶
w, _ := Initscr() // Make sure, to call Endwin() before exiting defer Endwin() fmt.Println("Don't do this after you've called ncurses.InitScr()") // do some things
Output:
func NewWindow ¶
Creates a new window. Make sure the windows do not overlap.
Notice: Fields for position and Size are swapped. The first parameter sets the column count, not the line count.
TODO(sebi2020): Add DelWindow method
Example ¶
w, _ := Initscr() defer Endwin() w2, err := NewWindow("MyWindow", Position{4, 4}, Size{20, 4}) if err != nil { panic(err) } fmt.Fprintf(w2, "Hello from MyWindow")
Output:
func (*Window) GetMaxYX ¶
Retrieves the terminal height and width (gathered through Terminfo & Termcap DB)
func (*Window) Refresh ¶
func (w *Window) Refresh()
Refreshes terminal screen. Outputs all content written since last call to Refresh()
func (*Window) SetAttribute ¶
Sets char attributes for following output in Window w.
func (*Window) SetScrolling ¶
Allow you to enable window scrolling. Must be called before calling Scroll(n int)