Coder Social home page Coder Social logo

readline's Introduction

Readline Bindings

This is a set of bindings to the GNU Readline Library.

The existing readline bindings for Go are more limited than this library, if you can believe it.

Note that the return type of String() has changed.

It was

func String(prompt string) string

and it's now

func String(prompt string) (string, error)

Installing the library

To install the library in order to use it, type:

go get github.com/bobappleyard/readline

You may need to be root.

For Mac OS X users, you may see errors like rl_catch_sigwinch undeclared. If so, you need to install GNU Readline via Homebrew:

brew install readline

On Mac OS X, if you encounter errors like these:

> go get github.com/bobappleyard/readline
# github.com/bobappleyard/readline
readline.go:38:19: error: stdio.h: No such file or directory
readline.go:39:20: error: stdlib.h: No such file or directory
In file included from /usr/local/opt/readline/include/readline/keymaps.h:35,
    	         from /usr/local/opt/readline/include/readline/readline.h:37,
            	 from readline.go:40:
/usr/local/opt/readline/include/readline/chardefs.h:25:19: error: ctype.h: No such file or directory
/usr/local/opt/readline/include/readline/chardefs.h:38:22: error: string.h: No such file or directory
In file included from readline.go:41:
/usr/local/opt/readline/include/readline/history.h:29:58: error: time.h: No such file or directory

You need to install "Command Line Tools" which can be done in XCode > Preferences > Downloads.

To install the library in order to hack on it, type

git clone git://github.com/bobappleyard/readline.git

Using the library

import "github.com/bobappleyard/readline"

These bindings provide access to three basic features of Readline:

  • Getting text from a prompt (via the String() and Reader() functions).
  • Managing the prompt's history (via the AddHistory(), GetHistory(), ClearHistory() and HistorySize() functions).
  • Controlling tab completion (via the Completer variable).

An example of the library's use:

package main

import (
	"io"
	"fmt"
	"github.com/bobappleyard/readline"
)

func main() {
	for {
		l, err := readline.String("> ")
		if err == io.EOF {
			break
		}
		if err != nil {
			fmt.Println("error: ", err)
			break
		}
		fmt.Println(l)
		readline.AddHistory(l)
	}
}

readline's People

Contributors

bobappleyard avatar chzyer avatar djcas9 avatar knuesel avatar mcuadros avatar nathany avatar peterbourgon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

readline's Issues

print thousands of ^C when press ^C

When I press ^C intend to exit, sometime it cause follows in ubuntu.

^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^CSegmentation fault (core dumped)

But I can't reproduce it everytime.

I try to set CatchSigint to false and catch os.Interrupt myself and call os.Exit immediately, but still not working.

Doesn't goinstall on Darwin (OS X 10.7)

$ goinstall -u -v -clean github.com/bobappleyard/readline
goinstall: github.com/bobappleyard/readline: download
goinstall: /: /usr/bin/git clone http://github.com/bobappleyard/readline.git /Users/peter/src/go/src/pkg/github.com/bobappleyard/readline
goinstall: selecting tip
goinstall: /Users/peter/src/go/src/pkg/github.com/bobappleyard/readline: /usr/bin/git pull
goinstall: io: skipping standard library
goinstall: os: skipping standard library
goinstall: /Users/peter/src/go/src/pkg/github.com/bobappleyard/readline: /bin/bash gomake -f- clean install
/bin/bash: === cd /Users/peter/src/go/src/pkg/github.com/bobappleyard/readline; bash gomake -f- clean install
rm -rf _.o *.a *.[568vq] [568vq].out *.so _obj _test _testmain.go *.exe cgo test.out build.out
CGOPKGPATH=github.com/bobappleyard cgo -- readline.go
cc1: warnings being treated as errors
readline.go: In function 'register_readline':
readline.go:14: warning: assignment from incompatible pointer type
make: *** [_obj/_cgo_run] Error 2
--- exit status 2
goinstall: github.com/bobappleyard/readline: install: running bash: exit status 2 ($GOPATH not set)

$ uname -a
Darwin sophia 11.0.0 Darwin Kernel Version 11.0.0: Sat Jun 18 12:56:35 PDT 2011; root:xnu-1699.22.73~1/RELEASE_X86_64 x86_64

$ cd $GOROOT && hg ident
c77997547d54+ weekly/weekly.2011-09-07

Catch CTRL-D

Hello, i would catch ctrl-d to make a line return, because now, when i type CTRL-D the program exit but my prompt is near the readline prompt like this :

Go> nurza@g74sx: ~

Thank you.

Completion

Hello, i don't understand how to use completion.

It should be nice to have an example in example_test.go.

Thank you.

readline with go 1.5 on OS X

Hi,

Go 1.5 is not released yet, but I just gave the latest version a spin on OS X and it does not seem to work together with readline. When running go test I get:

# testmain
/Users/alexander/go1.5/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
ld: illegal text-relocation to '_history_length' in /usr/local/opt/readline/lib/libreadline.dylib from 'github.com/bobappleyard/readline.init' in /var/folders/nf/ybp4s8z13wsdfjy0_4pcly_w0000gn/T/go-link-871618495/go.o for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

FAIL    github.com/bobappleyard/readline [build failed]

This is with:

go version devel +31aa6f2 Fri Jun 26 05:16:38 2015 +0000 darwin/amd64

Apple LLVM version 7.0.0 (clang-700.0.53)
Target: x86_64-apple-darwin14.4.0
Thread model: posix

Using the latest git commit of readline: a1f23ef

When adding -v to LDFLAGS, this is the output:

# github.com/bobappleyard/readline
Apple LLVM version 7.0.0 (clang-700.0.53)
Target: x86_64-apple-darwin14.4.0
Thread model: posix
 "/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.10.0 -o $WORK/github.com/bobappleyard/readline/_obj/_cgo_.o -L/usr/local/opt/readline/lib $WORK/github.com/bobappleyard/readline/_obj/_cgo_main.o $WORK/github.com/bobappleyard/readline/_obj/_cgo_export.o $WORK/github.com/bobappleyard/readline/_obj/readline.cgo2.o -lreadline -lhistory -lSystem /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.0/lib/darwin/libclang_rt.osx.a
# github.com/bobappleyard/readline
Apple LLVM version 7.0.0 (clang-700.0.53)
Target: x86_64-apple-darwin14.4.0
Thread model: posix
 "/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.10.0 -o $WORK/github.com/bobappleyard/readline/_obj/_all.o -L/usr/local/opt/readline/lib $WORK/github.com/bobappleyard/readline/_obj/_cgo_export.o $WORK/github.com/bobappleyard/readline/_obj/readline.cgo2.o -r
# testmain
/Users/alexander/go1.5/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
Apple LLVM version 7.0.0 (clang-700.0.53)
Target: x86_64-apple-darwin14.4.0
Thread model: posix
 "/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.10.0 -o $WORK/github.com/bobappleyard/readline/_test/readline.test -L/usr/local/opt/readline/lib -no_pie -headerpad 1144 -pagezero_size 4000000 /var/folders/nf/ybp4s8z13wsdfjy0_4pcly_w0000gn/T/go-link-001374678/000000.o /var/folders/nf/ybp4s8z13wsdfjy0_4pcly_w0000gn/T/go-link-001374678/000001.o /var/folders/nf/ybp4s8z13wsdfjy0_4pcly_w0000gn/T/go-link-001374678/go.o -lreadline -lhistory -lpthread -lSystem /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.0/lib/darwin/libclang_rt.osx.a
ld: illegal text-relocation to '_history_length' in /usr/local/opt/readline/lib/libreadline.dylib from 'github.com/bobappleyard/readline.init' in /var/folders/nf/ybp4s8z13wsdfjy0_4pcly_w0000gn/T/go-link-001374678/go.o for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

FAIL    github.com/bobappleyard/readline [build failed]

Thanks for creating this package! I'm using it in algernon.

Sigpanic when i resize the window

Hello, i found a bug:

Take your example script, make it as a main package.

package main

import (
    "fmt"
    "github.com/bobappleyard/readline"
    "os"
    "os/signal"
    "syscall"
    "time"
)

func main() {
    time.Sleep(time.Minute)
    sigint := make(chan os.Signal, 1)
    signal.Notify(sigint, syscall.SIGINT)
    readline.CatchSigint = false
    var line string
    var err error
    done := make(chan struct{})
    go func() {
        line, err = readline.String("> ")
        close(done)
    }()
    select {
    case <-sigint:
        fmt.Println("\nInterrupted")
        // Restore terminal attributes
        readline.Cleanup()
        // Note that we still have a goroutine reading from Stdin that
        // will terminate when we exit.
        os.Exit(1)
    case <-done:
        fmt.Printf("Read line %s, error %v\n", line, err)
    }
}

Run the program. When you resize the console: sigpanic

fatal error: unexpected signal during runtime execution

[signal 0xb code=0x1 addr=0x0 pc=0x7fc8d9370070]

runtime stack:
runtime.gothrow(0x54e470, 0x2a)
    /usr/local/go/src/runtime/panic.go:503 +0x8e
runtime.sigpanic()
    /usr/local/go/src/runtime/sigpanic_unix.go:14 +0x5e

goroutine 6 [syscall, locked to thread]:
runtime.cgocall_errno(0x401c60, 0xc208018710, 0xc200000000)
    /usr/local/go/src/runtime/cgocall.go:130 +0xf5 fp=0xc2080186f0 sp=0xc2080186c8
github.com/bobappleyard/readline._Cfunc_rl_resize_terminal()
    /home/manager/gopath/src/github.com/bobappleyard/readline/:116 +0x45 fp=0xc208018710 sp=0xc2080186f0
github.com/bobappleyard/readline.handleSignals()
    /home/manager/gopath/src/github.com/bobappleyard/readline/readline.go:321 +0x2be fp=0xc2080187e0 sp=0xc208018710
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:2232 +0x1 fp=0xc2080187e8 sp=0xc2080187e0
created by github.com/bobappleyard/readline.init·1
    /home/manager/gopath/src/github.com/bobappleyard/readline/readline.go:333 +0x25

goroutine 1 [sleep]:
main.main()
    /home/manager/readline/main.go:13 +0x38

goroutine 5 [syscall]:
os/signal.loop()
    /usr/local/go/src/os/signal/signal_unix.go:21 +0x1f
created by os/signal.init·1
    /usr/local/go/src/os/signal/signal_unix.go:27 +0x35

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
exit status 2

I am using Go1.4.1 on a Debian Linux Jessie.
I have tested the bug with Gnome Terminal and Terminator

Problem after exiting by ^C

If I run a test example and exit from it by ^C parent shell does not echo characters back to terminal

Probably related to #22

clang compiler errors on OSX mavericks

I'm getting the following error when running go build for the project

clang: error: argument unused during compilation: '-fno-eliminate-unused-debug-types'

Here's the output for go build -n

#
# github.com/bobappleyard/readline
#

mkdir -p $WORK/github.com/bobappleyard/readline/_obj/
mkdir -p $WORK/github.com/bobappleyard/
cd /Users/danott/Code/go/src/github.com/bobappleyard/readline
/usr/local/Cellar/go/1.1.2/libexec/pkg/tool/darwin_amd64/cgo -objdir $WORK/github.com/bobappleyard/readline/_obj/ -- -I/usr/local/opt/readline/include -I $WORK/github.com/bobappleyard/readline/_obj/ readline.go
/usr/local/Cellar/go/1.1.2/libexec/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/github.com/bobappleyard/readline/_obj/ -I /usr/local/Cellar/go/1.1.2/libexec/pkg/darwin_amd64 -o $WORK/github.com/bobappleyard/readline/_obj/_cgo_defun.6 -D GOOS_darwin -D GOARCH_amd64 $WORK/github.com/bobappleyard/readline/_obj/_cgo_defun.c
LIBGCC=$(gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -print-libgcc-file-name)
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I/usr/local/opt/readline/include -I $WORK/github.com/bobappleyard/readline/_obj/ -o $WORK/github.com/bobappleyard/readline/_obj/_cgo_main.o -c $WORK/github.com/bobappleyard/readline/_obj/_cgo_main.c
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I/usr/local/opt/readline/include -I $WORK/github.com/bobappleyard/readline/_obj/ -o $WORK/github.com/bobappleyard/readline/_obj/_cgo_export.o -c $WORK/github.com/bobappleyard/readline/_obj/_cgo_export.c
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I/usr/local/opt/readline/include -I $WORK/github.com/bobappleyard/readline/_obj/ -o $WORK/github.com/bobappleyard/readline/_obj/readline.cgo2.o -c $WORK/github.com/bobappleyard/readline/_obj/readline.cgo2.c
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -o $WORK/github.com/bobappleyard/readline/_obj/_cgo_.o $WORK/github.com/bobappleyard/readline/_obj/_cgo_main.o $WORK/github.com/bobappleyard/readline/_obj/_cgo_export.o $WORK/github.com/bobappleyard/readline/_obj/readline.cgo2.o -L/usr/local/opt/readline/lib -lreadline -lhistory
/usr/local/Cellar/go/1.1.2/libexec/pkg/tool/darwin_amd64/cgo -objdir $WORK/github.com/bobappleyard/readline/_obj/ -dynimport $WORK/github.com/bobappleyard/readline/_obj/_cgo_.o -dynout $WORK/github.com/bobappleyard/readline/_obj/_cgo_import.c
/usr/local/Cellar/go/1.1.2/libexec/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/github.com/bobappleyard/readline/_obj/ -I /usr/local/Cellar/go/1.1.2/libexec/pkg/darwin_amd64 -o $WORK/github.com/bobappleyard/readline/_obj/_cgo_import.6 -D GOOS_darwin -D GOARCH_amd64 $WORK/github.com/bobappleyard/readline/_obj/_cgo_import.c
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -o $WORK/github.com/bobappleyard/readline/_obj/_all.o $WORK/github.com/bobappleyard/readline/_obj/_cgo_export.o $WORK/github.com/bobappleyard/readline/_obj/readline.cgo2.o -L/usr/local/opt/readline/lib -Wl,-r -nostdlib $LIBGCC
/usr/local/Cellar/go/1.1.2/libexec/pkg/tool/darwin_amd64/6g -o $WORK/github.com/bobappleyard/readline/_obj/_go_.6 -p github.com/bobappleyard/readline -D _/Users/danott/Code/go/src/github.com/bobappleyard/readline -I $WORK $WORK/github.com/bobappleyard/readline/_obj/_cgo_gotypes.go $WORK/github.com/bobappleyard/readline/_obj/readline.cgo1.go
/usr/local/Cellar/go/1.1.2/libexec/pkg/tool/darwin_amd64/pack grcP $WORK $WORK/github.com/bobappleyard/readline.a $WORK/github.com/bobappleyard/readline/_obj/_go_.6 $WORK/github.com/bobappleyard/readline/_obj/_cgo_import.6 $WORK/github.com/bobappleyard/readline/_obj/_cgo_defun.6 $WORK/github.com/bobappleyard/readline/_obj/_all.o

I'm on OSX Mavericks, using brew install readline. If there's any more information I can provide to help, let me know!

"Illegal instruction: 4" when ^C in an empty prompt

Ran this code with OS X 10.10 Terminal, full screen, bash, Command Line Tools, and readline (via Homebrew):

for {
    l, err := readline.String("> ")
    if err == io.EOF {
        break
    }
    if err != nil {
        fmt.Println("error: ", err)
        break
    }
    if l != "" {
        fmt.Println(l)
    }
    readline.AddHistory(l)
}

I entered this:

$ myprogram
> 
> 
> 
> 
> 
> 
> 
> ls
ls
> hello
hello
> ^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C[...]C^C^C^C^C^C^C^C^C^C^CIllegal instruction: 4

and it printed thousands of "^C" followed by "Illegal instruction: 4".

Can't reproduce it.

Feature request: context-aware Complete

Not sure if this is the right avenue for feature requests; please correct me if it's not. Right now, tab-completion works great, but there is no way to identify a difference between

> ha[tab]

and

> the cat in the ha[tab]

It would be great if I could get the context string (eg. "the cat in the") or slice of strings (eg. []string{"the","cat","in","the"}) as an argument to the Complete function, in addition to the latest input token ("ha").

need to trim return value from readline.String()

need to trim return value from readline.String() to remove "\n". I do not understand why. This piece of code is OK for me.

package main

import (
    "fmt"
    "strings"
    "github.com/bobappleyard/readline"
)

func main() {
    for {
        l := strings.Trim(readline.String("> "), "\n")
        if l == "exit" {
            break
        }
        fmt.Println(l)
        readline.AddHistory(l)
    }
}

I am under Ubuntu 12.04 with go 1.02.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.