コード例 #1
0
ファイル: goreadline.go プロジェクト: npe9/minimega
//export minicliCompletion
//
// From readline documentation:
// Returns an array of (char *) which is a list of completions for text. If
// there are no completions, returns (char **)NULL. The first entry in the
// returned array is the substitution for text. The remaining entries are the
// possible completions. The array is terminated with a NULL pointer.
func minicliCompletion(text *C.char, start, end C.int) **C.char {
	// Determine the size of a pointer on the current system
	var b *C.char
	ptrSize := unsafe.Sizeof(b)

	// Copy the buffer containing the line so far
	line := C.GoString(C.rl_line_buffer)

	// Default is to not change the string
	vals := []string{C.GoString(text)}

	// Generate suggestions
	suggest := minicli.Suggest(line)

	if len(suggest) == 1 {
		// Use only suggestion as substitution for text
		vals[0] = suggest[0]
	} else if len(suggest) == 0 {
		// No suggestions.. fall back on default behavior (filename completion)
		return C.rl_completion_matches(text,
			(*C.rl_compentry_func_t)(C.rl_filename_completion_function))
	}
	vals = append(vals, suggest...)

	// Copy suggestions into char**
	ptr := C.malloc(C.size_t(len(vals)+1) * C.size_t(ptrSize))
	for i, v := range vals {
		element := (**C.char)(unsafe.Pointer(uintptr(ptr) + uintptr(i)*ptrSize))
		*element = C.CString(v)
	}
	element := (**C.char)(unsafe.Pointer(uintptr(ptr) + uintptr(len(vals))*ptrSize))
	*element = nil

	return (**C.char)(ptr)
}
コード例 #2
0
ファイル: goreadline.go プロジェクト: jenareljam/minimega
//export minicliCompletion
//
// From readline documentation:
// Returns an array of (char *) which is a list of completions for text. If
// there are no completions, returns (char **)NULL. The first entry in the
// returned array is the substitution for text. The remaining entries are the
// possible completions. The array is terminated with a NULL pointer.
func minicliCompletion(text *C.char, start, end C.int) **C.char {
	// Determine the size of a pointer on the current system
	var b *C.char
	ptrSize := unsafe.Sizeof(b)

	// Copy the buffer containing the line so far
	line := C.GoString(C.rl_line_buffer)

	// Generate suggestions
	var suggest []string
	suggest = minicli.Suggest(line)

	if len(suggest) == 0 {
		// No suggestions.. fall back on default behavior (filename completion)
		if FilenameCompleter == nil {
			return C.rl_completion_matches(text,
				(*C.rl_compentry_func_t)(C.rl_filename_completion_function))
		}

		suggest = FilenameCompleter(line)
		if len(suggest) == 0 {
			// no dice, use the builtin
			return C.rl_completion_matches(text,
				(*C.rl_compentry_func_t)(C.rl_filename_completion_function))
		}
	}
	vals := append([]string{lcp(suggest)}, suggest...)

	// Copy suggestions into char**
	ptr := C.malloc(C.size_t(len(vals)+1) * C.size_t(ptrSize))
	for i, v := range vals {
		element := (**C.char)(unsafe.Pointer(uintptr(ptr) + uintptr(i)*ptrSize))
		*element = C.CString(v)
	}
	element := (**C.char)(unsafe.Pointer(uintptr(ptr) + uintptr(len(vals))*ptrSize))
	*element = nil

	return (**C.char)(ptr)
}