英文:
function similar to getchar
问题
有没有类似于C语言中的getchar
函数的Go函数可以处理控制台中的Tab键按下?我想在我的控制台应用程序中实现一些自动补全的功能。
英文:
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.
答案1
得分: 25
C的getchar()
示例:
#include <stdio.h>
void main()
{
char ch;
ch = getchar();
printf("输入的字符是:%c",ch);
}
Go的等效代码:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
fmt.Printf("输入的字符是:%v", string([]byte(input)[0]))
// fmt.Printf("你输入的是:%v", []byte(input))
}
最后被注释的那行代码只是展示了当你按下tab
键时,第一个元素是U+0009('CHARACTER TABULATION')。
然而,对于你的需求(检测tab键),C的getchar()
并不适用,因为它需要用户按下回车键。你需要的是像@miku提到的ncurses的getch()
/ readline/ jLine这样的东西。使用这些工具,你实际上等待一个单个的按键。
所以你有多种选择:
-
使用
ncurses
/readline
绑定,例如https://code.google.com/p/goncurses/或类似的https://github.com/nsf/termbox -
自己编写,参考http://play.golang.org/p/plwBIIYiqG
-
使用
os.Exec
运行stty或jLine。
参考资料:
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
英文:
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
答案2
得分: 17
假设您想要无缓冲输入(无需按回车键),在UNIX系统上可以使用以下代码实现:
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
// 禁用输入缓冲
exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
// 不在屏幕上显示输入的字符
exec.Command("stty", "-F", "/dev/tty", "-echo").Run()
// 退出时恢复回显状态
defer exec.Command("stty", "-F", "/dev/tty", "echo").Run()
var b []byte = make([]byte, 1)
for {
os.Stdin.Read(b)
fmt.Println("我得到了字节", b, "("+string(b)+")")
}
}
英文:
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)+")")
}
}
答案3
得分: 9
其他答案建议使用以下方法:
-
使用cgo
- 效率低下
- "cgo不是Go"(https://dave.cheney.net/2016/01/18/cgo-is-not-go)
-
使用
os.Exec
的stty
- 不可移植
- 效率低下
- 容易出错
-
使用使用
/dev/tty
的代码- 不可移植
-
使用GNU readline包
- 如果它是C readline的包装器或者使用上述技术之一实现的,则效率低下
- 否则可以
然而,对于简单的情况,可以直接使用Go项目的子存储库中的包。
[编辑:之前的答案使用了golang.org/x/crypto/ssh/terminal
包,但该包已被弃用;它已移至golang.org/x/term
。代码/链接已相应更新。]
基本上,使用term.MakeRaw
和term.Restore
将标准输入设置为原始模式(检查错误,例如如果stdin不是终端);然后可以直接从os.Stdin
读取字节,或者更可能是通过bufio.Reader
(以提高效率)。
例如,像这样的代码:
package main
import (
"bufio"
"fmt"
"log"
"os"
"golang.org/x/term"
)
func main() {
// fd 0 is stdin
state, err := term.MakeRaw(0)
if err != nil {
log.Fatalln("setting stdin to raw:", err)
}
defer func() {
if err := term.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
}
}
}
英文:
Other answers here suggest such things as:
-
Using cgo
- inefficient
- "cgo is not Go"
-
os.Exec
ofstty
- not portable
- inefficient
- error prone
-
using code that uses
/dev/tty
- not portable
-
using a GNU readline package
- inefficient if it's a wrapper to C readline or if implemented using one of the above techniques
- otherwise okay
However, for the simple case this is easy just using a package from the Go Project's Sub-repositories.
[Edit: Previously this answer used the golang.org/x/crypto/ssh/terminal
package which has since been deprecated; it was moved to golang.org/x/term
. Code/links updated appropriately.]
Basically, use
term.MakeRaw
and
term.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/term"
)
func main() {
// fd 0 is stdin
state, err := term.MakeRaw(0)
if err != nil {
log.Fatalln("setting stdin to raw:", err)
}
defer func() {
if err := term.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
}
}
}
答案4
得分: 8
感谢Paul Rademacher - 这在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
}
英文:
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
}
答案5
得分: 1
1- 你可以使用C.getch()
:
这在Windows命令行中有效,在不输入回车的情况下只读取一个字符:
(在shell(终端)中运行输出的二进制文件,而不是在管道或编辑器中运行。)
package main
//#include<conio.h>
import "C"
import "fmt"
func main() {
c := C.getch()
fmt.Println(c)
}
2- 对于Linux(在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()
}
参考:
https://stackoverflow.com/questions/7469139/what-is-equivalent-to-getch-getche-in-linux
https://stackoverflow.com/questions/8792317/why-cant-i-find-conio-h-on-linux
3- 这种方法也可以,但需要“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)
}
英文:
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.)
<!-- language: lang-golang -->
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:
https://stackoverflow.com/questions/7469139/what-is-equivalent-to-getch-getche-in-linux
https://stackoverflow.com/questions/8792317/why-cant-i-find-conio-h-on-linux
3- Also this works, but needs "Enter":
<!-- language: lang-golang -->
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)
}
答案6
得分: -5
你也可以使用ReadRune:
reader := bufio.NewReader(os.Stdin)
// ...
char, _, err := reader.ReadRune()
if err != nil {
fmt.Println("读取键盘输入错误...", err)
}
Rune(符文)类似于字符,因为Go语言实际上没有字符,为了尝试支持多种语言/Unicode等。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论