Skip to content

Commit

Permalink
Merge pull request #40 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 3.6.0.
  • Loading branch information
andyone authored Apr 27, 2024
2 parents f3d741f + 305735f commit abab7ad
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 36 deletions.
93 changes: 57 additions & 36 deletions linenoise.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
//go:build cgo || !windows
// +build cgo !windows

// Package linenoise wraps the linenoise library (https://github.com/antirez/linenoise).
//
// The package is imported with "go-" prefix
// import "github.com/essentialkaos/go-linenoise"
//
// Simple readline usage:
// linenoise.Line("prompt> ")
//
// Adding lines to history, you could simply do this for every line you read.
// linenoise.AddHistory("This will be displayed in prompt when arrow-up is pressed")
// Package linenoise wraps the linenoise library (https://github.com/yhirose/linenoise).
package linenoise

// ///////////////////////////////////////////////////////////////////////////////// //
Expand All @@ -25,6 +16,7 @@ import "C"

import (
"errors"
"fmt"
"unsafe"
)

Expand All @@ -45,24 +37,23 @@ var ErrKillSignal = errors.New("Prompt was quited with a kill signal")
// ///////////////////////////////////////////////////////////////////////////////// //

// complHandler is completion handler function
var complHandler = func(input string) []string {
return nil
}
var complHandler CompletionHandler = emptyCompomplHandler

// hintHandler is hint handler function
var hintHandler = func(input string) string {
return ""
}
var hintHandler HintHandler = emptyHintHandler

// hintColor contains hint color ANSI code (dark grey by default)
var hintColor = 90
var hintColor uint8 = 90

// hintBold contains flag for bold hints
var hintBold bool = false

// ///////////////////////////////////////////////////////////////////////////////// //

func init() {
C.linenoiseSetupCompletionCallbackHook()
C.linenoiseSetupHintCallbackHook()
}
var emptyCompomplHandler = func(input string) []string { return nil }
var emptyHintHandler = func(input string) string { return "" }

// ///////////////////////////////////////////////////////////////////////////////// //

// Line displays given string and returns line from user input
func Line(prompt string) (string, error) {
Expand Down Expand Up @@ -162,23 +153,41 @@ func SetMaskMode(enable bool) {
}
}

// SetCompletionHandler sets the CompletionHandler to be used for completion
// SetCompletionHandler sets the CompletionHandler to be used for completion (using Tab key)
//
// You can pass nil as the handler to remove the previously set handler
func SetCompletionHandler(h CompletionHandler) {
if h == nil {
complHandler = emptyCompomplHandler
return
}

complHandler = h
}

// SetHintHandler sets the HintHandler to be used for input hints
//
// You can pass nil as the handler to remove the previously set handler
func SetHintHandler(h HintHandler) {
if h == nil {
hintHandler = emptyHintHandler
return
}

hintHandler = h
}

// SetHintColor sets hint text color
func SetHintColor(color int) {
if color < 0 {
color = 0
// SetHintColor sets hint text color to color with given ANSI code
//
// Color codes: https://github.com/essentialkaos/fmtc/wiki#816-colors
func SetHintColor(color uint8, bold bool) error {
if color < 30 || color > 97 || (color > 37 && color < 90) {
return fmt.Errorf("\"%d\" is not valid ANSI color code", color)
}

hintColor = color
hintColor, hintBold = color, bold

return nil
}

// PrintKeyCodes puts linenoise in key codes debugging mode.
Expand All @@ -190,23 +199,30 @@ func PrintKeyCodes() {

// ///////////////////////////////////////////////////////////////////////////////// //

// init inits callbacks
func init() {
C.linenoiseSetupCompletionCallbackHook()
C.linenoiseSetupHintCallbackHook()
}

//export linenoiseGoCompletionCallbackHook
func linenoiseGoCompletionCallbackHook(input *C.char, completions *C.linenoiseCompletions) {
completionsSlice := complHandler(C.GoString(input))

completionsLen := len(completionsSlice)
completions.len = C.size_t(completionsLen)

if completionsLen > 0 {
cvec := C.malloc(C.size_t(int(unsafe.Sizeof(*(**C.char)(nil))) * completionsLen))
cvecSlice := (*(*[999999]*C.char)(cvec))[:completionsLen]
if completionsLen == 0 {
return
}

for i, str := range completionsSlice {
cvecSlice[i] = C.CString(str)
}
cvec := C.malloc(C.size_t(int(unsafe.Sizeof(*(**C.char)(nil))) * completionsLen))
cvecSlice := (*(*[999999]*C.char)(cvec))[:completionsLen]

completions.cvec = (**C.char)(cvec)
for i, str := range completionsSlice {
cvecSlice[i] = C.CString(str)
}

completions.cvec = (**C.char)(cvec)
}

//export linenoiseGoHintCallbackHook
Expand All @@ -218,7 +234,12 @@ func linenoiseGoHintCallbackHook(input *C.char, color *C.int, bold *C.int) *C.ch
}

*color = C.int(hintColor)
*bold = C.int(0)

if hintBold {
*bold = C.int(1)
} else {
*bold = C.int(0)
}

return C.CString(hintText)
}
17 changes: 17 additions & 0 deletions linenoise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,20 @@ var _ = Suite(&LinenoiseSuite{})
func (s *LinenoiseSuite) TestSimple(c *C) {
c.Assert(ErrKillSignal, NotNil)
}

// This is not a real test, is just basic compilation test
func (s *LinenoiseSuite) TestSetHintColor(c *C) {
c.Assert(SetHintColor(1, false), NotNil)
c.Assert(SetHintColor(38, false), NotNil)
c.Assert(SetHintColor(120, false), NotNil)
c.Assert(SetHintColor(31, false), IsNil)
c.Assert(SetHintColor(92, false), IsNil)
}

func (s *LinenoiseSuite) TestNilHandler(c *C) {
SetCompletionHandler(nil)
c.Assert(complHandler, NotNil)

SetHintHandler(nil)
c.Assert(hintHandler, NotNil)
}

0 comments on commit abab7ad

Please sign in to comment.