Cursor key terminal input in Go - go

I am creating a Go application for usage in a terminal. The following code asks a user to input text to the terminal.
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
for {
fmt.Println("Please input something and use arrows to move along the text left and right")
in := bufio.NewReader(os.Stdin)
_, err := in.ReadString('\n')
if err != nil {
fmt.Println(err)
}
}
}
The problem is that a user cannot use left and right arrows to go along the just inputted text in order to modify it. When he presses arrows, the console prints ^[[D^[[C^[[A^[[B signs.
The output:
Please input something and use arrows to move along the text left and right
hello^[[D^[[C^[[A^[[B
How to make arrow keys behave more user-friendly and let a human navigate along the just inputted text, using left and right arrows?
I guess, I should pay attention to libraries like termbox-go or gocui but how to use them exactly for this purpose, I do not know.

A simpler example would be carmark/pseudo-terminal-go, where you can put a terminal in raw mode and benefit from the full up-down-left-right cursor moves.
From terminal.go#NewTerminal()
// NewTerminal runs a VT100 terminal on the given ReadWriter. If the ReadWriter is
// a local terminal, that terminal must first have been put into raw mode.
// prompt is a string that is written at the start of each input line (i.e.
// "> ").
func NewTerminal(c io.ReadWriter, prompt string) *Terminal
See terminal/terminal.go and terminal/terminal_test.go, as well as MakeRaw()

Related

Create a verbose console inside Golang app using Fyne

Comming from Python with PyQt gui, I was used to add kind of console in my programm. The purpose was to indicate to the user information on the processes in progress, on the execution errors encountered, etc.
In Python/PyQt, I was using QLineEdit to do that. It was pretty easy to use. Just create and insert the widget in my gui and add a row for each information by calling appen().
For example, the console could say "esedb loading" when loading an esedb file, then "esedb file loaded" when finished, then "esedb parsing" for the next step, etc...
Now, I'm learning Golang with Fyne and I'm looking for a way to do something similar.
I found widget.NewTextGrid() but it doesn't work as I expect.
I can't just append new line. If I understand well, I have to store text in a string variable
Could you advice me about the way to do that ?
Thanks!
package main
import (
//"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
func main() {
myapp := app.New()
myappGui := myapp.NewWindow("Example")
myappGui.Resize(fyne.NewSize(400, 600))
textConsole := widget.NewTextGrid()
TextGrid is a complex component designed for managing character specific font styles in a monospace arrangement (like a terminal etc).
For performance I would recommend a VBox in a Scroll widget where each line is another appended Label (you can set them to monospace text style as well). If you want the text to be interactive then as other answers have said the NewMultiLineEntry is likely for you.
Text is complex and we are working hard to optimise more of the complex usages and large file handling, so it will get smoother in later releases…
widget.TextGrid does not have a method to append a line, but it does support querying its current content using TextGrid.Text(). So what you may do is set a new text that is its current content and the new line concatenated, e.g.:
textConsole.SetText(textConsole.Text() + "\n" + line)
But know that widget.TextGrid does not support scrolling: its size will be dictated by its string content. You can make it scrollable of course by using a container.Scroll.
For example:
func main() {
myapp := app.New()
w := myapp.NewWindow("Example")
w.Resize(fyne.NewSize(500, 300))
textConsole := widget.NewTextGrid()
scrollPane := container.NewScroll(textConsole)
w.SetContent(scrollPane)
go func() {
for {
textConsole.SetText(textConsole.Text() + time.Now().String() + "\n")
scrollPane.ScrollToBottom()
time.Sleep(time.Second)
}
}()
w.ShowAndRun()
}
Alternatively you may use a multiline widget.Entry. It also supports selecting any part of it, and by default it's also editable. You may disable editing of course. It supports scrolling by default.
See this example:
func main() {
myapp := app.New()
w := myapp.NewWindow("Example")
w.Resize(fyne.NewSize(500, 300))
textConsole := widget.NewMultiLineEntry()
textConsole.Disable() // Disable editing
w.SetContent(textConsole)
go func() {
for {
textConsole.SetText(textConsole.Text + time.Now().String() + "\n")
time.Sleep(time.Second)
}
}()
w.ShowAndRun()
}

Automatically add New Line(s) after 80th character in a string containing ANSI Escape Codes [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I want a Go program to display a CP437-encoded 'ANSI art file', 80 columns wide, that contains ANSI escape codes -- color, cursor placement, etc. and extended ANSI (CP437) box drawing codes.
When viewing the ANSI file in a linux terminal (see Playground link below) with a 80 col CP437-capable terminal, the file displays properly, e.g. line breaks are implicitly read; however, when viewing with a wider terminal (which is the goal), new lines are not implicitly processed/added and the file may display improperly, without required line breaks.
How do I iterate through the .ans file and manually add new lines after the 80th character, counting only the actual characters displayed (not the escape codes)?
I've tried go libraries like ansiwrap and reflow. Ansiwrap is really just for text wrapping, but Reflow gets the closest to the goal, but the line breaks are not quite right.
Playground link of my test code with Reflow.
How it renders (in a CP437 terminal, 132x37):
How it should look (from art program):
To solve this, first I pulled visualLength function from the
golang.org/x/term package [1], then I wrote a bufio.SplitFunc [2] for this
use case.
package main
func ansi(data []byte, eof bool) (int, []byte, error) {
var s []rune
for i, r := range string(data) {
if s = append(s, r); visualLength(s) == 80 {
width := len(string(r))
return i+width, data[:i+width], nil
}
}
return 0, nil, nil
}
Result:
package main
import (
"bufio"
"golang.org/x/text/encoding/charmap"
"os"
)
func main() {
f, err := os.Open("LDA-PHASE90.ANS")
if err != nil {
panic(err)
}
defer f.Close()
s := bufio.NewScanner(charmap.CodePage437.NewDecoder().Reader(f))
s.Split(ansi)
for s.Scan() {
println(s.Text())
}
}
https://github.com/golang/term/blob/6886f2df/terminal.go#L431-L450
https://golang.org/pkg/bufio#SplitFunc

Go TUI programming using TCell API

I am trying to learn TUI programming in Go using the TCell API.
It is a simple app that print word "hello". However, when I run the program below, nothing happens. Please tell me what I am doing wrong.
package main
import (
"fmt"
"github.com/gdamore/tcell"
"os"
)
func main() {
scn, err := tcell.NewScreen()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
hhh := []rune("hello")
scn.SetContent(10, 10, rune(' '), hhh, tcell.StyleDefault)
scn.Show()
}
The creator of this api (https://github.com/gdamore/tcell.git) provided the solution. Here is his respond:
There are three potential issues.
First, you need to initialize the screen. Call scn.Init() after creating the screen.
The second is that your call to SetContent is misguided. The string you are passing is to accommodate combining characters. Instead you need to call SetContent 5 times (one for each letter of "hello") with a different offset, and the appropriate letter of "hello". You probably want to just pass "" for the 4th argument (the string), since none of this is combining characters..
The third problem is that your program just exits. On most terminals this will cause the reset of the terminal to occur, losing your output. (On xterm, for example, tcell uses the alternate screen buffer by default, which leads to exit causing the contents of that screen to be lost, when it switches back to the primary screen buffer at program termination.) The simplest way to prove this is to add a time.Sleep(time.Second * 10) or similar as the last line of your program.
Here is the modified code:
import (
"fmt"
"github.com/gdamore/tcell"
"github.com/gdamore/tcell/encoding"
"os"
"time"
)
func main() {
encoding.Register()
scn, err := tcell.NewScreen()
scn.Init()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
scn.Clear()
scn.SetContent(10, 10, rune('h'), []rune(""), tcell.StyleDefault)
scn.Show()
time.Sleep(time.Second * 2)
}
Hope this help.

Making a full screen Terminal application with Go

I'm trying to build a full screen terminal application. I'm using Go as my language of choice. I've figured out how to read from os.Stdin, but I'm unclear on how to clear the terminal window and manipulate the cursor position. I also want to capture the terminal input without it being printed (echoed back).
My questions are:
How can I effectively clear and print to the terminal with column/row coordinates?
How do I stop the terminal from printing keys pressed
My intent:
I want to create a full screen terminal application that renders it's own UI and handles input internally (hot keys/navigation/etc...).
If there are any libraries that cover this sort of use case please feel free to suggest them.
The easiest way to clear the terminal and set position is via ansi escape codes. However, this may not be the ideal way as variation in terminals may come back to bite you.
fmt.Print("\033[2J") //Clear screen
fmt.Printf("\033[%d;%dH", line, col) // Set cursor position
A better alternative would be to use a library like goncurses or termbox-go (credit: second is from Tim Cooper's comment).
With such a library you can do things like this:
import (
gc "code.google.com/p/goncurses"
)
func main() {
s, err := gc.Init()
if err != nil {
panic(err)
}
defer gc.End()
s.Move(5, 2)
s.Println("Hello")
s.GetChar()
}
Code above copied from Rosetta Code
As of December 2019, I would recommend using rivo/tview library.
(goncurses mentioned by #vastlysuperiorman has not been updated since June 2019 and termbox-go is explicitly declared unmaintained).
Here's the "hello world" app, taken from the project's README (reformatted for readability):
package main
import (
"github.com/rivo/tview"
)
func main() {
box := tview.NewBox().
SetBorder(true).
SetTitle("Hello, world!")
if err := tview.NewApplication().SetRoot(box, true).Run(); err != nil {
panic(err)
}
}
tview provides screenshots and example code as well as the standard godoc reference.
To stop the terminal from printing keys pressed you can use the below code:
import (
"fmt"
"syscall"
"golang.org/x/crypto/ssh/terminal"
)
func main(){
fmt.Print("Enter Value: ")
byteInput, _ := terminal.ReadPassword(int(syscall.Stdin))
input:= string(byteInput)
fmt.Println() // it's necessary to add a new line after user's input
fmt.Printf("Your input is '%s'", input)
}

How to have an in-place string that updates on stdout

I want to output to stdout and have the output "overwrite" the previous output.
For example; if I output On 1/10, I want the next output On 2/10 to overwrite On 1/10. How can I do this?
stdout is a stream (io.Writer). You cannot modify what was already written to it. What can be changed is how that stream's represented in case it is printed to a terminal. Note that there's no good reason to assume this scenario. For example, a user could redirect stdout to a pipe or to a file at will.
So the proper approach is to first check:
if the stdout is going to a terminal
what is that terminal's procedure to overwrite a line/screen
Both of the above are out of this question's scope, but let's assume that a terminal is our device. Then usually, printing:
fmt.Printf("\rOn %d/10", i)
will overwrite the previous line in the terminal. \r stands for carriage return, implemented by many terminals as moving the cursor to the beginning of the current line, hence providing the "overwrite line" facility.
As an example of "other" terminal with a differently supported 'overwriting', here is an example at the playground.
Use this solution if you want to rewrite multiple lines to the output. For instance, I made a decent Conway's "Game of Life" output using this method.
DISCLAIMER: this only works on ANSI Terminals, and besides using fmt this isn't a Go-specific answer either.
fmt.Printf("\033[0;0H")
// put your other fmt.Printf(...) here
Brief Explanation: this is an escape sequence which tells the ANSI terminal to move the cursor to a particular spot on the screen. The \033[ is the so-called escape sequence, and the 0;0H is the type of code telling the terminal move the cursor to row 0, column 0 of the terminal.
Source: https://en.wikipedia.org/wiki/ANSI_escape_code#Sequence_elements
The solution for one string which will replace whole string
fmt.Printf("\033[2K\r%d", i)
For example, it correctly prints from 10 to 0:
for i:= 10; i>=0; i-- {
fmt.Printf("\033[2K\r%d", i)
time.Sleep(1 * time.Second)
}
fmt.Println()
which previous answers don't solve.
Found something worth sharing for problems like this.
Sharing for people who might be facing same problem in future
Check if output is being written to terminal. If so, use \r (carriage return) defined by terminal to move cursor to the beginning of line
package main
import (
"flag"
"fmt"
"os"
"time"
)
var spinChars = `|/-\`
type Spinner struct {
message string
i int
}
func NewSpinner(message string) *Spinner {
return &Spinner{message: message}
}
func (s *Spinner) Tick() {
fmt.Printf("%s %c \r", s.message, spinChars[s.i])
s.i = (s.i + 1) % len(spinChars)
}
func isTTY() bool {
fi, err := os.Stdout.Stat()
if err != nil {
return false
}
return fi.Mode()&os.ModeCharDevice != 0
}
func main() {
flag.Parse()
s := NewSpinner("working...")
isTTY := isTTY()
for i := 0; i < 100; i++ {
if isTTY {
s.Tick()
}
time.Sleep(100 * time.Millisecond)
}
}
Example code taken from

Resources