示例#1
0
文件: readline.go 项目: wcgh/plum
func Readline(prompt string) (string, error) {
	c_prompt := C.CString(prompt)
	defer C.free(unsafe.Pointer(c_prompt))

	c_line := C.readline(c_prompt)
	defer C.free(unsafe.Pointer(c_line))
	line := C.GoString(c_line)

	if c_line == nil {
		return "", errors.New("C.readline call failed")
	}
	C.add_history(c_line)

	// append to file
	f, e := os.OpenFile(history_path, os.O_APPEND|os.O_WRONLY, 0600)
	if e == nil {
		defer f.Close()

		_, e = f.WriteString(line + "\n")
		if e != nil {
			fmt.Printf("error writing to history")
		}
	}

	return line, nil
}
示例#2
0
文件: console.go 项目: hycxa/gommo
func (c *console) Run() {
	defer c.Stopped()

	re := regexp.MustCompile(`[\w\:\.]+`)
	prompt := C.CString("god> ")
	defer C.free(unsafe.Pointer(prompt))

	var line string

	for !c.StopRequested() {
		cline := C.readline(prompt)
		defer C.free(unsafe.Pointer(cline))
		if cline == nil {
			fmt.Printf("\n")
			break
		}

		C.add_history(cline)
		line = C.GoString(cline)
		args := re.FindAllString(line, -1)
		if len(args) > 0 {
			f := c.funcs[args[0]]
			if f != nil {
				var ret interface{}
				ext.PCall(
					func() {
						ret = f(args[1:])
					})
				ext.LogInfo("RUN_COMMAND\t%s\t%s\t%v\n", args[0], args[1:], ret)
			}
		}
	}
}
示例#3
0
文件: cli.go 项目: alexcrichton/fargo
//export receiveLine
func receiveLine(c *C.char) {
	if c == nil {
		activeTerm.quit()
	} else {
		activeTerm.Exec(C.GoString(c))
		C.add_history(c)
	}
}
示例#4
0
// AddHistory places string at the end of the history list.
// Blank lines are discarded.
// (See add_history http://cnswww.cns.cwru.edu/php/chet/readline/history.html#IDX5)
func AddHistory(line string) {
	if len(line) == 0 || len(strings.TrimSpace(line)) == 0 {
		return
	}
	if unicode.IsSpace(rune(line[0])) { // ignorespace
		return
	}
	if prev, err := GetHistory(-1); err == nil && prev == line { // ignore consecutive dups
		return
	}
	cline := C.CString(line)
	C.add_history(cline)
	C.free(unsafe.Pointer(cline))
}
示例#5
0
// Rlwrap prompts the user with the given prompt string and calls the
// underlying readline function. If the input stream closes, Rlwrap returns an
// EOF error.
func Rlwrap(prompt string, record bool) (string, error) {
	p := C.CString(prompt)
	defer C.free(unsafe.Pointer(p))

	ret := C.readline(p)
	if ret == nil {
		return "", errors.New("EOF")
	}
	defer C.free(unsafe.Pointer(ret))

	if record {
		C.add_history(ret)
	}

	return C.GoString(ret), nil
}
示例#6
0
文件: readline.go 项目: wcgh/plum
func loadHistory(filename string) error {
	content, err := ioutil.ReadFile(history_path)
	if err != nil {
		return err
	}

	for _, add_line := range strings.Split(string(content), "\n") {
		if add_line == "" {
			continue
		}
		c_add_line := C.CString(add_line)
		C.add_history(c_add_line)
		C.free(unsafe.Pointer(c_add_line))
	}

	return nil
}
示例#7
0
func AddHistory(s string) {
	p := C.CString(s)
	C.add_history(p)
	C.free(unsafe.Pointer(p))
}
示例#8
0
// Add an item to the history.
func AddHistory(s string) {
	n := HistorySize()
	if n == 0 || s != GetHistory(n-1) {
		C.add_history(C.CString(s))
	}
}
示例#9
0
func AddHistory(line string) {
	cLine := C.CString(line)
	C.add_history(cLine)
	C.free(unsafe.Pointer(cLine))
}