Is there a Go function similar to C\'s getchar
able to handle tab press in console? I want to make some sort of completion in my console app.
C's getchar()
example:
#include <stdio.h>
void main()
{
char ch;
ch = getchar();
printf("Input Char Is :%c",ch);
}
Go equivalent:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
fmt.Printf("Input Char Is : %v", string([]byte(input)[0]))
// fmt.Printf("You entered: %v", []byte(input))
}
The last commented line just shows that when you press tab
the first element is U+0009 ('CHARACTER TABULATION').
However for your needs (detecting tab) C's getchar()
is not suitable as it requires the user to hit enter. What you need is something like ncurses' getch()/ readline/ jLine as mentioned by @miku. With these, you actually wait for a single keystroke.
So you have multiple options:
Use ncurses
/ readline
binding, for example https://code.google.com/p/goncurses/ or equivalent like https://github.com/nsf/termbox
Roll your own see http://play.golang.org/p/plwBIIYiqG for starting point
use os.Exec
to run stty or jLine.
refs:
https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/zhBE5MH4n-Q
https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/S9AO_kHktiY
https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/icMfYF8wJCk
Assuming that you want unbuffered input (without having to hit enter), this does the job on UNIX systems:
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
// disable input buffering
exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
// do not display entered characters on the screen
exec.Command("stty", "-F", "/dev/tty", "-echo").Run()
// restore the echoing state when exiting
defer exec.Command("stty", "-F", "/dev/tty", "echo").Run()
var b []byte = make([]byte, 1)
for {
os.Stdin.Read(b)
fmt.Println("I got the byte", b, "("+string(b)+")")
}
}
Thanks goes to Paul Rademacher - this works (at least on Mac):
package main
import (
"bytes"
"fmt"
"github.com/pkg/term"
)
func getch() []byte {
t, _ := term.Open("/dev/tty")
term.RawMode(t)
bytes := make([]byte, 3)
numRead, err := t.Read(bytes)
t.Restore()
t.Close()
if err != nil {
return nil
}
return bytes[0:numRead]
}
func main() {
for {
c := getch()
switch {
case bytes.Equal(c, []byte{3}):
return
case bytes.Equal(c, []byte{27, 91, 68}): // left
fmt.Println("LEFT pressed")
default:
fmt.Println("Unknown pressed", c)
}
}
return
}
Other answers here suggest such things as:
Using cgo
os.Exec
of stty
using code that uses /dev/tty
using a GNU readline package
However, for the simple case this is easy just using a package from the Go Project's Sub-repositories.
Basically, use terminal.MakeRaw and terminal.Restore to set standard input to raw mode (checking for errors, e.g. if stdin is not a terminal); then you can either read bytes directly from os.Stdin
, or more likely, via a bufio.Reader
(for better efficiency).
For example, something like this:
package main
import (
"bufio"
"fmt"
"log"
"os"
"golang.org/x/crypto/ssh/terminal"
)
func main() {
// fd 0 is stdin
state, err := terminal.MakeRaw(0)
if err != nil {
log.Fatalln("setting stdin to raw:", err)
}
defer func() {
if err := terminal.Restore(0, state); err != nil {
log.Println("warning, failed to restore terminal:", err)
}
}()
in := bufio.NewReader(os.Stdin)
for {
r, _, err := in.ReadRune()
if err != nil {
log.Println("stdin:", err)
break
}
fmt.Printf("read rune %q\r\n", r)
if r == 'q' {
break
}
}
}
1- You may use C.getch()
:
This works in Windows command line, Reads only one character without Enter:
(Run output binary file inside shell (terminal), not inside pipe or Editor.)
package main
//#include<conio.h>
import "C"
import "fmt"
func main() {
c := C.getch()
fmt.Println(c)
}
2- For Linux ( tested on Ubuntu ):
package main
/*
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
char getch(){
char ch = 0;
struct termios old = {0};
fflush(stdout);
if( tcgetattr(0, &old) < 0 ) perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if( tcsetattr(0, TCSANOW, &old) < 0 ) perror("tcsetattr ICANON");
if( read(0, &ch,1) < 0 ) perror("read()");
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if(tcsetattr(0, TCSADRAIN, &old) < 0) perror("tcsetattr ~ICANON");
return ch;
}
*/
import "C"
import "fmt"
func main() {
fmt.Println(C.getch())
fmt.Println()
}
See:
What is Equivalent to getch() & getche() in Linux?
Why can't I find <conio.h> on Linux?
3- Also this works, but needs "Enter":
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
r := bufio.NewReader(os.Stdin)
c, err := r.ReadByte()
if err != nil {
panic(err)
}
fmt.Println(c)
}
You can also use ReadRune:
reader := bufio.NewReader(os.Stdin)
// ...
char, _, err := reader.ReadRune()
if err != nil {
fmt.Println("Error reading key...", err)
}
A rune is similar to a character, as GoLang does not really have characters, in order to try and support multiple languages/unicode/etc.