fixed
Go to file
c-bata 2921d1d259 Enter when exit 2017-08-09 22:47:08 +09:00
_example Fix EAGAIN error 2017-08-09 14:04:10 +09:00
_tools Rename package 2017-08-08 00:19:51 +09:00
.gitignore Initial commit 2017-07-03 23:36:56 +09:00
LICENSE Update LICENSE 2017-07-16 13:51:31 +09:00
Makefile Rename package 2017-08-08 00:19:51 +09:00
README.md Update README 2017-08-09 15:35:11 +09:00
bisect.go Move go files to project root 2017-07-18 05:27:57 +09:00
buffer.go Replace panic to log.Print 2017-08-06 15:21:14 +09:00
buffer_test.go Move go files to project root 2017-07-18 05:27:57 +09:00
completion.go Refactor completions 2017-08-09 21:33:47 +09:00
console_interface.go Add refactor change 2017-08-04 16:23:18 +09:00
document.go Move go files to project root 2017-07-18 05:27:57 +09:00
document_test.go Move go files to project root 2017-07-18 05:27:57 +09:00
filter.go Rename Completion to Suggest 2017-08-04 20:30:50 +09:00
filter_test.go Rename Completion to Suggest 2017-08-04 20:30:50 +09:00
history.go Fix history clear 2017-08-04 20:33:28 +09:00
key.go Add refactor change 2017-08-04 16:23:18 +09:00
key_string.go Move go files to project root 2017-07-18 05:27:57 +09:00
option.go Refactor completions 2017-08-09 21:33:47 +09:00
prompt.go Enter when exit 2017-08-09 22:47:08 +09:00
render.go Refactor completions 2017-08-09 21:33:47 +09:00
render_test.go Rename Completion to Suggest 2017-08-04 20:30:50 +09:00
vt100_input.go Refactor completions 2017-08-09 21:33:47 +09:00
vt100_output.go Set bold option 2017-07-19 01:16:57 +09:00

go-prompt

demo

Library for building powerful interactive prompt in Go, inspired by python-prompt-toolkit.

Similar Projects

Projects using go-prompt

Getting Started

package main

import (
	"fmt"

	"github.com/c-bata/go-prompt"
)

// executor executes command and print the output.
func executor(in string) {
	fmt.Println("Your input: " + in)
}

// completer returns the completion items from user input.
func completer(in string) []prompt.Suggest {
	s := []prompt.Suggest{
		{Text: "users", Description: "user table"},
		{Text: "sites", Description: "sites table"},
		{Text: "articles", Description: "articles table"},
		{Text: "comments", Description: "comments table"},
	}
	return prompt.FilterHasPrefix(s, in, true)
}

func main() {
	p := prompt.New(
		executor,
		completer,
		prompt.OptionPrefix(">>> "),
		prompt.OptionTitle("sql-prompt"),
	)
	p.Run()
}

Color Options

go-prompt has many color options. It is difficult to describe by text. So please see below:

options

  • OptionPrefixTextColor(prompt.Color) : default prompt.Blue
  • OptionPrefixBackgroundColor(prompt.Color) : default prompt.DefaultColor
  • OptionInputTextColor(prompt.Color) : default prompt.DefaultColor
  • OptionInputBGColor(prompt.Color) : default prompt.DefaultColor
  • OptionPreviewSuggestionTextColor(prompt.Color) : default prompt.Green
  • OptionPreviewSuggestionBGColor(prompt.Color) : default prompt.DefaultColor
  • OptionSuggestionTextColor(prompt.Color) : default prompt.White
  • OptionSuggestionBGColor(prompt.Color) : default prompt.Cyan
  • OptionSelectedSuggestionTextColor(prompt.Color) : default prompt.Black
  • OptionSelectedSuggestionBGColor(prompt.Color) : default prompt.DefaultColor
  • OptionDescriptionTextColor(prompt.Color) : default prompt.Black
  • OptionDescriptionBGColor(prompt.Color) : default prompt.Turquoise
  • OptionSelectedDescriptionTextColor(prompt.Color) : default prompt.White
  • OptionSelectedDescriptionBGColor(prompt.Color) : default prompt.Cyan

Other Options

OptionTitle(string) : default ""

Option to set title displayed at the header bar of terminal.

OptionHistory([]string) : default []string{}

Option to set history.

OptionPrefix(string) : default "> "

Option to set prefix string.

OptionMaxSuggestions(x uint16) : default 6

The max number of displayed suggestions.

OptionParser(prompt.ConsoleParser) : default VT100Parser

To set a custom ConsoleParser object. An argument should implement ConsoleParser interface.

OptionWriter(prompt.ConsoleWriter) : default VT100Writer

To set a custom ConsoleWriter object. An argument should implement ConsoleWriter interace.

go-prompt internals

This is a short description of go-prompt implementation. go-prompt consists of three parts.

  1. Input parser
  2. Emulate user input with Buffer object.
  3. Render buffer object.

Input Parser

input-parser animation

Input Parser only supports only vt100 compatible console now.

  • Set raw mode.
  • Read standard input.
  • Parse byte array

Emulate user input

go-prompt contains Buffer class. It represents input state by handling user input key.

Buffer object has text and cursor position.

package main

import "github.com/c-bata/go-prompt"

func main() {
  b := prompt.NewBuffer()
  ... wip
}

Render Buffer object.

Renderer object renders a buffer object.

package main

the output is below:

TODO this is screen shot

LICENSE

This software is licensed under the MIT License (See LICENSE ).