package main import ( "fmt" "github.com/jroimartin/gocui" ) var ( viewWidth = 30 viewHeight = 3 ) func layout(g *gocui.Gui) error { if v, err := g.SetView("input", 2, 2, 28, 5); err != nil { if err != gocui.ErrUnknownView { return err } v.Title = "Calculator" v.Editable = true v.Wrap = false if _, err := g.SetCurrentView("input"); err != nil { return err } } return nil } func calculate(g *gocui.Gui, v *gocui.View) error { val := v.Buffer() result, err := eval(val) if err != nil { result = err.Error() } if _, err := g.SetView("result", 2, 9, 28, 12); err != nil { if err != gocui.ErrUnknownView { return err } fmt.Fprintf(v, "\n= %s\n", result) } return nil } func main() { g, err := gocui.NewGui(gocui.OutputNormal) if err != nil { fmt.Println(err) return } defer g.Close() g.SetManagerFunc(layout) if err := g.SetKeybinding("input", gocui.KeyEnter, gocui.ModNone, calculate); err != nil { fmt.Println(err) return } if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { fmt.Println(err) return } }
package main import ( "fmt" "io/ioutil" "log" "os" "github.com/jroimartin/gocui" ) var ( title = "File Explorer" maxX = 0 maxY = 0 cursorX = 0 cursorY = 0 currentDir = "." ) func layout(g *gocui.Gui) error { if v, err := g.SetView("header", 0, 0, maxX, 2); err != nil { if err != gocui.ErrUnknownView { return err } v.Title = title v.Editable = false v.Wrap = false } if v, err := g.SetView("list", 0, 2, maxX, maxY); err != nil { if err != gocui.ErrUnknownView { return err } v.Title = "Current Directory" v.Editable = false v.Wrap = false v.Highlight = true g.SetCurrentView("list") } return nil } func updateList(g *gocui.Gui) { v, err := g.View("list") if err == nil { v.Clear() files, err := ioutil.ReadDir(currentDir) if err != nil { log.Println(err) return } for i, file := range files { if i == cursorY { fmt.Fprintf(v, "\033[0;34m%s\033[0m\n", file.Name()) } else { fmt.Fprintf(v, "%s\n", file.Name()) } } } } func keybindings(g *gocui.Gui) error { if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { return err } if err := g.SetKeybinding("list", gocui.KeyArrowDown, gocui.ModNone, moveCursorDown); err != nil { return err } if err := g.SetKeybinding("list", gocui.KeyArrowUp, gocui.ModNone, moveCursorUp); err != nil { return err } if err := g.SetKeybinding("list", gocui.KeyEnter, gocui.ModNone, openSelectedFile); err != nil { return err } if err := g.SetKeybinding("header", gocui.KeyEnter, gocui.ModNone, openSelectedFile); err != nil { return err } return nil } func moveCursorUp(g *gocui.Gui, v *gocui.View) error { if cursorY > 0 { cursorY -= 1 updateList(g) } return nil } func moveCursorDown(g *gocui.Gui, v *gocui.View) error { v, _ = g.View("list") if files, _ := ioutil.ReadDir(currentDir); cursorY < len(files)-1 { cursorY += 1 updateList(g) } return nil } func openSelectedFile(g *gocui.Gui, v *gocui.View) error { v, _ = g.View("list") if files, _ := ioutil.ReadDir(currentDir); len(files) > 0 { if fileInfo, _ := os.Stat(currentDir + "/" + files[cursorY].Name()); fileInfo.IsDir() && files[cursorY].Name() != ".." { currentDir = currentDir + "/" + files[cursorY].Name() cursorY = 0 updateList(g) g.Update(func(g *gocui.Gui) error { g.View("list").Title = currentDir return nil }) } else { // Handle file opened event here log.Printf("Opening file %s\n", files[cursorY].Name()) } } return nil } func quit(g *gocui.Gui, v *gocui.View) error { return gocui.ErrQuit } func main() { g, err := gocui.NewGui(gocui.OutputNormal) if err != nil { log.Panicln(err) } defer g.Close() maxX, maxY = g.Size() g.SetManagerFunc(layout) if err := keybindings(g); err != nil { log.Panicln(err) } updateList(g) if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { log.Panicln(err) } }This code sets up a simple layout with a header and a list view. It then handles user inputs by listing the files and folders in the current directory, allowing the user to navigate the directory and open files. The list view uses colors to highlight the currently selected item. In conclusion, the gocui package library provides a simple and lightweight way to create GUI views in Go programming language. It allows for creating custom layouts and handling user inputs with ease. This package library is useful for creating simple terminal applications with interactive interfaces.