David's Blog

Building Interactive Terminal Applications with Golang and Gocui

By David Li on 2023-05-30T19:37:46.109Z

Golang, also known as Go, has become a popular programming language for building high-performance applications. One of the strong points of Go is its simplicity and ease of use, which makes it an excellent choice for creating command-line tools and terminal applications. In this article, we will explore how to build an interactive terminal application using Gocui, a minimalistic and flexible library for building intuitive console user interfaces in Go.

Introduction to Gocui

Gocui is an open-source library designed specifically for building terminal-based applications that require a user interface. It provides a simple and efficient way to manage console UI components, such as windows, input fields, and menus. It also provides several features like mouse support, flexible layout management, and custom keybindings.

To get started with Gocui, you’ll need to have Go installed on your system. You can download and install it from the official website. Once you have Go installed, you can install Gocui by running the following command:

go get -u github.com/jroimartin/gocui

Building a Basic Gocui Application

Let’s start by building a simple Gocui application that displays “Hello, Gocui!” in a terminal window. Create a new Go file, `main., and add the following code:

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.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, Gocui!")
	}
	return nil
}

This code sets up a basic Gocui application with a single “hello” view that displays “Hello, Gocui!” at the center of the terminal window. The layout function is responsible for creating and positioning UI elements within the terminal.

To run the application, execute the following command:

go run main.go

You should see the “Hello, Gocui!” message displayed at the center of your terminal window.

Adding Interactivity

Let’s extend our example to make it more interactive. We will add the ability to quit the application by pressing the q key. Update the main function to include the following keybinding:

func main() {
	// ...
	g.SetManagerFunc(layout)

	if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
		log.Panicln(err)
	}

	if err := g.SetKeybinding("", 'q', gocui.ModNone, quit); err != nil {
		log.Panicln(err)
	}

	if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
		log.Panicln(err)
	}
}

func quit(g *gocui.Gui, v *gocui.View) error {
	return gocui.ErrQuit
}

Now, when you run the application and press either q or Ctrl+C, the application will exit gracefully.

Conclusion

Gocui is a powerful library for building interactive terminal applications in Go. It provides a simple and efficient way to manage console UI components and supports a variety of features, including custom keybindings, mouse support, and flexible layout management. In this article, we covered the basics of creating a Gocui application, but there are many more features and possibilities to explore.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-04-22.