### Installing GOCUI Package with Go Get Source: https://github.com/jroimartin/gocui/blob/master/README.md This shell command fetches and installs the GOCUI package from GitHub. Requires Go environment to be set up. Outputs the package to GOPATH; no specific inputs or limitations beyond network access. ```sh go get github.com/jroimartin/gocui ``` -------------------------------- ### Creating Basic Console UI with GOCUI in Go Source: https://github.com/jroimartin/gocui/blob/master/README.md This Go example initializes a GUI, sets up a layout with a centered 'Hello world!' view, and binds Ctrl+C to quit. Requires the gocui package import and runs in a terminal supporting ANSI. Handles errors via logging; supports normal output mode with no major limitations for basic use. ```go package main import ( "fmt" "log" "github.com/jroimartin/gocui" ) func main() { g, err := gocui.NewGui(gocui.OutputNormal) if err != nil { log.Panicln(err) } defer g.Close() g.SetManagerFunc(layout) if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { log.Panicln(err) } if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { log.Panicln(err) } } func layout(g *gocui.Gui) error { maxX, maxY := g.Size() if v, err := g.SetView("hello", maxX/2-7, maxY/2, maxX/2+7, maxY/2+2); err != nil { if err != gocui.ErrUnknownView { return err } fmt.Fprintln(v, "Hello world!") } return nil } func quit(g *gocui.Gui, v *gocui.View) error { return gocui.ErrQuit } ``` -------------------------------- ### Initialize GUI with Event Loop in Go Source: https://context7.com/jroimartin/gocui/llms.txt Creates a new GUI instance with a specified output mode and starts the main event loop. Requires gocui library import. Sets up layout manager and keybindings. Handles cleanup with defer statement. ```Go package main import ( "log" "github.com/jroimartin/gocui" ) func main() { // Create GUI with normal output mode (8 colors) g, err := gocui.NewGui(gocui.OutputNormal) if err != nil { log.Panicln(err) } defer g.Close() // Set the layout manager function g.SetManagerFunc(layout) // Set up keybindings if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { log.Panicln(err) } // Start the main loop if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { log.Panicln(err) } } func layout(g *gocui.Gui) error { maxX, maxY := g.Size() if v, err := g.SetView("main", 0, 0, maxX-1, maxY-1); err != nil { if err != gocui.ErrUnknownView { return err } v.Title = "My Application" v.Wrap = true } return nil } func quit(g *gocui.Gui, v *gocui.View) error { return gocui.ErrQuit } ``` -------------------------------- ### Generating Local Documentation for GOCUI Source: https://github.com/jroimartin/gocui/blob/master/README.md This shell command runs Go's documentation tool to generate docs for the GOCUI package locally. Depends on the package being installed. Displays documentation in terminal; useful for offline reference without limitations. ```sh go doc github.com/jroimartin/gocui ``` -------------------------------- ### Go: Update GUI from Goroutines Source: https://context7.com/jroimartin/gocui/llms.txt This snippet demonstrates how to safely update the gocui GUI from goroutines using the Update method. It provides examples of updating a counter, monitoring a log file, and fetching data periodically. ```go func startBackgroundTask(g *gocui.Gui) { go func() { ticker := time.NewTicker(1 * time.Second) defer ticker.Stop() counter := 0 for range ticker.C { counter++ // Safely update GUI from goroutine g.Update(func(g *gocui.Gui) error { v, err := g.View("status") if err != nil { return err } v.Clear() fmt.Fprintf(v, "Counter: %d", counter) return nil }) } }() } func monitorLogFile(g *gocui.Gui, filename string) { go func() { file, err := os.Open(filename) if err != nil { return } defer file.Close() scanner := bufio.NewScanner(file) for scanner.Scan() { line := scanner.Text() // Update log view from goroutine g.Update(func(g *gocui.Gui) error { v, err := g.View("logs") if err != nil { return err } fmt.Fprintln(v, line) return nil }) } }() } func fetchDataPeriodically(g *gocui.Gui) { go func() { for { time.Sleep(5 * time.Second) // Simulate fetching data data := fetchRemoteData() g.Update(func(g *gocui.Gui) error { v, err := g.View("data") if err != nil { return err } v.Clear() fmt.Fprintf(v, "Updated: %s\n", time.Now().Format(time.RFC3339)) fmt.Fprintf(v, "Data: %v\n", data) return nil }) } }() } ``` -------------------------------- ### Create and Manage Multiple Views in Go Source: https://context7.com/jroimartin/gocui/llms.txt Creates multiple views with different positioning strategies including absolute coordinates, relative coordinates, and centered layouts. Demonstrates view customization including titles, highlighting, colors, wrapping, and frames. Uses fmt.Fprintln for content writing. ```Go func layout(g *gocui.Gui) error { maxX, maxY := g.Size() // Create a sidebar view with absolute coordinates if v, err := g.SetView("sidebar", -1, -1, 30, maxY); err != nil { if err != gocui.ErrUnknownView { return err } v.Title = "Menu" v.Highlight = true v.SelBgColor = gocui.ColorGreen v.SelFgColor = gocui.ColorBlack fmt.Fprintln(v, "Option 1") fmt.Fprintln(v, "Option 2") fmt.Fprintln(v, "Option 3") fmt.Fprintln(v, "Option 4") } // Create main content view with relative coordinates if v, err := g.SetView("content", 30, -1, maxX, maxY); err != nil { if err != gocui.ErrUnknownView { return err } v.Title = "Content" v.Wrap = true v.Autoscroll = true fmt.Fprintln(v, "Welcome to the application!") fmt.Fprintln(v, "This view wraps text automatically.") } // Create a centered popup view if v, err := g.SetView("popup", maxX/2-20, maxY/2-5, maxX/2+20, maxY/2+5); err != nil { if err != gocui.ErrUnknownView { return err } v.Title = "Popup Window" v.Frame = true fmt.Fprintln(v, "This is a centered popup!") } return nil } ``` -------------------------------- ### Configure colors and styles for gocui views in Go Source: https://context7.com/jroimartin/gocui/llms.txt This snippet shows how to customize view appearance, including background, foreground, selection colors, and text attributes in gocui. It sets global colors, creates a styled view with colored text, and demonstrates 256-color mode using a separate Gui. Requires the gocui library and basic knowledge of terminal escape sequences. ```go func setupStyledViews(g *gocui.Gui) error { maxX, maxY := g.Size() // Configure global colors g.BgColor = gocui.ColorBlack g.FgColor = gocui.ColorWhite g.SelBgColor = gocui.ColorBlue g.SelFgColor = gocui.ColorYellow g.Highlight = true // Create view with custom colors if v, err := g.SetView("styled", 5, 5, maxX-5, 15); err != nil { if err != gocui.ErrUnknownView { return err } v.Title = "Styled View" v.BgColor = gocui.ColorBlue v.FgColor = gocui.ColorWhite v.SelBgColor = gocui.ColorGreen v.SelFgColor = gocui.ColorBlack v.Highlight = true // Write colored text fmt.Fprintln(v, "\x1b[31mRed text\x1b[0m") fmt.Fprintln(v, "\x1b[32mGreen text\x1b[0m") fmt.Fprintln(v, "\x1b[33mYellow text\x1b[0m") fmt.Fprintln(v, "\x1b[34mBlue text\x1b[0m") fmt.Fprintln(v, "\x1b[35mMagenta text\x1b[0m") fmt.Fprintln(v, "\x1b[36mCyan text\x1b[0m") fmt.Fprintln(v, "\x1b[1;37mBold white text\x1b[0m") fmt.Fprintln(v, "\x1b[4;37mUnderlined text\x1b[0m") } // Create view with 256-color mode g256, err := gocui.NewGui(gocui.Output256) if err != nil { return err } if, err := g256.SetView("colors256", 5, 5, 50, 20); err != nil { if != gocui.ErrUnknownView { return err } v.Title = "256 Colors" for i := 0; i < 256; i++ { fmt.Fprintf(v, "\x1b[38;5;%dmColor %d\x1b[0m\n", i, i) } } return nil } ``` -------------------------------- ### Manage view layers using gocui in Go Source: https://context7.com/jroimartin/gocui/llms.txt This snippet demonstrates how to create multiple overlapping views and control their stacking order with gocui. It includes functions to add base, middle, and top layers, bring a view to the front, send a view to the back, and toggle a popup view. Requires the gocui library and a running Gui instance. ```go func manageViewLayers(g *gocui.Gui) error { maxX, maxY := g.Size() // Create base view if v, err := g.SetView("base", 0, 0, maxX-1, maxY-1); err != nil { if err != gocui.ErrUnknownView { return err } v.Title = "Base Layer" fmt.Fprintln(v, "This is the bottom layer") } // Create middle overlay if v, err := g.SetView("middle", 10, 5, 50, 15); err != nil { if err != gocui.ErrUnknownView { return err } v.Title = "Middle Layer" v.Frame = true fmt.Fprintln(v, "Middle") } // Create top overlay if v, err := g.SetView("top", 20, 10, 60, 20); err != nil { if err != gocui.ErrUnknownView { return err } v.Title = "Top Layer" v.Frame = true fmt.Fprintln(v, "Top overlay") } return nil } func bringToFront(g *gocui, viewName string) error { _, err := g.SetViewOnTop(viewName) return err } func sendToBack(g *gocui.Gui, viewName string) error { _, err := g.SetViewOnBottom(viewName) return err } func togglePopup(g *gocui.Gui, v *gocui.View) error { _, err := g.View("popup") if err == gocui.ErrUnknownView { // Create popup maxX, maxY := g.Size() if v, err := g.SetView("popup", maxX/2-25, maxY/2-5, maxX/2+25, maxY/2+5); err != nil { if err != gocui.ErrUnknownView { return err } v.Title = "Popup" fmt.Fprintln(v, "Press ESC to close") } g.SetViewOnTop("popup") g.SetCurrentView("popup") } else { // Delete popup if err := g.DeleteView("popup"); err != nil { return err } } return nil } ``` -------------------------------- ### Configure Keyboard Shortcuts and Navigation in gocui Source: https://context7.com/jroimartin/gocui/llms.txt Sets up global and view-specific keybindings for a gocui application. Includes global quit handler (Ctrl+C), view-specific cursor navigation (arrow keys, enter), and tab-based view switching between sidebar and content views. Functions handle cursor movement with automatic origin scrolling when reaching view boundaries. ```go func setupKeybindings(g *gocui.Gui) error { // Global keybinding (empty string "" means all views) if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { return err } // View-specific keybindings if err := g.SetKeybinding("sidebar", gocui.KeyArrowDown, gocui.ModNone, cursorDown); err != nil { return err } if err := g.SetKeybinding("sidebar", gocui.KeyArrowUp, gocui.ModNone, cursorUp); err != nil { return err } if err := g.SetKeybinding("sidebar", gocui.KeyEnter, gocui.ModNone, selectItem); err != nil { return err } // Tab to switch between views if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, switchView); err != nil { return err } return nil } func cursorDown(g *gocui.Gui, v *gocui.View) error { if v != nil { cx, cy := v.Cursor() if err := v.SetCursor(cx, cy+1); err != nil { ox, oy := v.Origin() if err := v.SetOrigin(ox, oy+1); err != nil { return err } } } return nil } func cursorUp(g *gocui.Gui, v *gocui.View) error { if v != nil { ox, oy := v.Origin() cx, cy := v.Cursor() if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 { if err := v.SetOrigin(ox, oy-1); err != nil { return err } } } return nil } func switchView(g *gocui.Gui, v *gocui.View) error { if v == nil || v.Name() == "sidebar" { _, err := g.SetCurrentView("content") return err } _, err := g.SetCurrentView("sidebar") return err } func quit(g *gocui.Gui, v *gocui.View) error { return gocui.ErrQuit } ``` -------------------------------- ### Create Editable Text Views with gocui Source: https://context7.com/jroimartin/gocui/llms.txt Implements an editable text view in gocui with both default and custom editor functionality. Sets up a bordered editor view with text editing capabilities, line wrapping, and global cursor visibility. Includes a custom editor implementation that handles character input, backspace, delete, newline, arrow key navigation, and Ctrl+A for moving to line beginning. ```go func setupEditableView(g *gocui.Gui) error { maxX, maxY := g.Size() if v, err := g.SetView("editor", 5, 5, maxX-5, maxY-5); err != nil { if err != gocui.ErrUnknownView { return err } v.Title = "Text Editor" v.Editable = true v.Wrap = true v.Editor = gocui.DefaultEditor fmt.Fprintln(v, "Start typing here...") // Set this view as the current view with cursor if _, err := g.SetCurrentView("editor"); err != nil { return err } } // Enable cursor globally g.Cursor = true return nil } // Custom editor implementation type customEditor struct{} func (e *customEditor) Edit(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) { switch { case ch != 0 && mod == 0: v.EditWrite(ch) case key == gocui.KeySpace: v.EditWrite(' ') case key == gocui.KeyBackspace || key == gocui.KeyBackspace2: v.EditDelete(true) case key == gocui.KeyDelete: v.EditDelete(false) case key == gocui.KeyEnter: v.EditNewLine() case key == gocui.KeyArrowDown: v.MoveCursor(0, 1, false) case key == gocui.KeyArrowUp: v.MoveCursor(0, -1, false) case key == gocui.KeyArrowLeft: v.MoveCursor(-1, 0, false) case key == gocui.KeyArrowRight: v.MoveCursor(1, 0, false) case key == gocui.KeyCtrlA: // Custom: Move to beginning of line v.SetCursor(0, v.Cursor()) } } ``` -------------------------------- ### Create Masked Password Input and Handle Login in GoCUI Source: https://context7.com/jroimartin/gocui/llms.txt This snippet demonstrates setting up editable views for username and password input in a GoCUI terminal application, with password masking using the Mask property to hide sensitive data. It relies on the gocui package for GUI management, along with strings and fmt for text handling. Inputs include a Gui instance; outputs are configured views and login processing via an authenticate function, with limitations on view positioning based on terminal size. ```go func createPasswordInput(g *gocui.Gui) error { maxX, maxY := g.Size() // Create username input if v, err := g.SetView("username", maxX/2-20, maxY/2-5, maxX/2+20, maxY/2-3); err != nil { if err != gocui.ErrUnknownView { return err } v.Title = "Username" v.Editable = true v.Editor = gocui.DefaultEditor } // Create password input with masking if v, err := g.SetView("password", maxX/2-20, maxY/2-2, maxX/2+20, maxY/2); err != nil { if err != gocui.ErrUnknownView { return err } v.Title = "Password" v.Editable = true v.Editor = gocui.DefaultEditor v.Mask = '*' // Mask characters with asterisks } // Create submit button if v, err := g.SetView("submit", maxX/2-10, maxY/2+2, maxX/2+10, maxY/2+4); err != nil { if err != gocui.ErrUnknownView { return err } fmt.Fprintln(v, " [Enter to Submit]") } g.Cursor = true g.SetCurrentView("username") return nil } func handleLogin(g *gocui.Gui, v *gocui.View) error { usernameView, err := g.View("username") if err != nil { return err } passwordView, err := g.View("password") if err != nil { return err } username := strings.TrimSpace(usernameView.Buffer()) password := strings.TrimSpace(passwordView.Buffer()) // Process login if authenticate(username, password) { return showMainMenu(g) } return showError(g, "Invalid credentials") } ``` -------------------------------- ### Go: Handle Mouse Events Source: https://context7.com/jroimartin/gocui/llms.txt This snippet demonstrates enabling mouse support in gocui and handling mouse clicks and wheel events within a view. It sets keybindings and defines event handlers for mouse interactions. ```go func setupMouseSupport(g *gocui.Gui) error { // Enable mouse support g.Mouse = true // Handle left mouse click if err := g.SetKeybinding("content", gocui.MouseLeft, gocui.ModNone, handleMouseClick); err != nil { return err } // Handle mouse wheel if err := g.SetKeybinding("content", gocui.MouseWheelUp, gocui.ModNone, scrollUp); err != nil { return err } if err := g.SetKeybinding("content", gocui.MouseWheelDown, gocui.ModNone, scrollDown); err != nil { return err } return nil } func handleMouseClick(g *gocui.Gui, v *gocui.View) error { if v == nil { return nil } cx, cy := v.Cursor() line, err := v.Line(cy) if err != nil { return nil } // Show clicked line in status view status, err := g.View("status") if err != nil { return err } status.Clear() fmt.Fprintf(status, "Clicked: %s", line) return nil } func scrollUp(g *gocui.Gui, v *gocui.View) error { if v != nil { ox, oy := v.Origin() if oy > 0 { if err := v.SetOrigin(ox, oy-1); err != nil { return err } } } return nil } func scrollDown(g *gocui.Gui, v *gocui.View) error { if v != nil { ox, oy := v.Origin() if err := v.SetOrigin(ox, oy+1); err != nil { return err } } return nil } ``` -------------------------------- ### Go: Save View Content Source: https://context7.com/jroimartin/gocui/llms.txt This snippet demonstrates saving the contents of a gocui view buffer to a file. It uses the io.Reader interface to read the buffer and ioutil.WriteFile to save it. ```go func saveViewContent(g *gocui.Gui, v *gocui.View) error { if v == nil { return nil } // Read view content using io.Reader interface v.Rewind() // Reset read offset buf := make([]byte, 4096) content := "" for { n, err := v.Read(buf) if n > 0 { content += string(buf[:n]) } if err == io.EOF { break } if err != nil { return err } } // Save to file return ioutil.WriteFile("output.txt", []byte(content), 0644) } ``` -------------------------------- ### Go: Manipulate View Content Source: https://context7.com/jroimartin/gocui/llms.txt This snippet demonstrates how to interact with the view buffers in gocui using the io.ReadWriter interface. It includes writing text, clearing the view, reading content, and iterating through lines. ```go func manipulateViewContent(g *gocui.Gui) error { v, err := g.View("content") if err != nil { return err } // Write to view using standard fmt functions fmt.Fprintln(v, "New line of text") fmt.Fprintf(v, "Formatted text: %d items\n", 42) // Write colored text using ANSI codes fmt.Fprintln(v, "\x1b[0;31mRed text\x1b[0m") fmt.Fprintln(v, "\x1b[0;32mGreen text\x1b[0m") fmt.Fprintln(v, "\x1b[1;34mBold blue text\x1b[0m") // Clear view content v.Clear() // Get current line _, cy := v.Cursor() line, err := v.Line(cy) if err != nil { return err } // Read all buffer content content := v.Buffer() // Read visible buffer content visibleContent := v.ViewBuffer() // Get buffer as lines lines := v.BufferLines() for i, line := range lines { fmt.Printf("Line %d: %s\n", i, line) } return nil } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.