From 500bfcba3cb4398054a961ee7204041e68c222e7 Mon Sep 17 00:00:00 2001 From: Thomas Sindt Date: Mon, 6 Apr 2026 08:52:10 +0200 Subject: [PATCH] ncurses fuer go --- cursestest/go.mod | 5 +++ cursestest/go.sum | 2 + cursestest/nctest.go | 95 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 cursestest/go.mod create mode 100644 cursestest/go.sum create mode 100644 cursestest/nctest.go diff --git a/cursestest/go.mod b/cursestest/go.mod new file mode 100644 index 0000000..41ab6db --- /dev/null +++ b/cursestest/go.mod @@ -0,0 +1,5 @@ +module nctest + +go 1.26.1 + +require github.com/rthornton128/goncurses v0.0.0-20251206192105-7e6d2302acca // indirect diff --git a/cursestest/go.sum b/cursestest/go.sum new file mode 100644 index 0000000..8e1e36f --- /dev/null +++ b/cursestest/go.sum @@ -0,0 +1,2 @@ +github.com/rthornton128/goncurses v0.0.0-20251206192105-7e6d2302acca h1:kFR/H0Y6K2vP5EU0lYLoRWXIiLPztK1r6DTUeSkNo08= +github.com/rthornton128/goncurses v0.0.0-20251206192105-7e6d2302acca/go.mod h1:AHlKFomPTwmO7H2vL8d7VNrQNQmhMi/DBhDnHRhjbCo= diff --git a/cursestest/nctest.go b/cursestest/nctest.go new file mode 100644 index 0000000..5b16b0a --- /dev/null +++ b/cursestest/nctest.go @@ -0,0 +1,95 @@ +// goncurses - ncurses library for Go. +// Copyright 2011 Rob Thornton. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +/* This example show a basic menu similar to that found in the ncurses + * examples from TLDP */ +package main + +import ( + "log" + + . "github.com/rthornton128/goncurses" +) + +const ( + HEIGHT = 10 + WIDTH = 30 +) + +func main() { + var active int + menu := []string{"Choice 1", "Choice 2", "Choice 3", "Choice 4", "Exit"} + + stdscr, err := Init() + if err != nil { + log.Fatal(err) + } + defer End() + + Raw(true) + Echo(false) + Cursor(0) + stdscr.Clear() + stdscr.Keypad(true) + + my, mx := stdscr.MaxYX() + y, x := 2, (mx/2)-(WIDTH/2) + + win, _ := NewWindow(HEIGHT, WIDTH, y, x) + win.Keypad(true) + + stdscr.Print("Use arrow keys to go up and down, Press enter to select") + stdscr.Refresh() + + printmenu(win, menu, active) + + for { + ch := stdscr.GetChar() + switch Key(ch) { + case 'q': + return + case KEY_UP: + if active == 0 { + active = len(menu) - 1 + } else { + active -= 1 + } + case KEY_DOWN: + if active == len(menu)-1 { + active = 0 + } else { + active += 1 + } + case KEY_RETURN, KEY_ENTER, Key('\r'): + stdscr.MovePrintf(my-2, 0, "Choice #%d: %s selected", + active, + menu[active]) + stdscr.ClearToEOL() + stdscr.Refresh() + default: + stdscr.MovePrintf(my-2, 0, "Character pressed = %3d/%c", + ch, ch) + stdscr.ClearToEOL() + stdscr.Refresh() + } + + printmenu(win, menu, active) + } +} + +func printmenu(w *Window, menu []string, active int) { + y, x := 2, 2 + w.Box(0, 0) + for i, s := range menu { + if i == active { + w.AttrOn(A_REVERSE) + w.MovePrint(y+i, x, s) + w.AttrOff(A_REVERSE) + } else { + w.MovePrint(y+i, x, s) + } + } + w.Refresh() +}