Skip to content

Commit

Permalink
Document changes in the README
Browse files Browse the repository at this point in the history
  • Loading branch information
Verseth committed Jul 25, 2023
1 parent fd2ed9c commit ee5ed49
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 5 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ It's a great library but it's been abandoned
for quite a while.
This project aims to continue its development.

The library has been rewritten in many aspects, fixing existing bugs and adding new essential functionality.

Most notable changes include:
- Support for custom syntax highlighting with a lexer
- Multiline editing
- A scrolling buffer is used for displaying the current content which makes it possible to edit text of arbitrary length (only the visible part of the text is rendered)
- Support for automatic indentation when pressing <kbd>Enter</kbd> and the input is incomplete or for executing the input when it is complete. This is determined by a custom callback function.

I highly encourage you to see the [changelog](CHANGELOG.md) which fully documents the changes that have been made.

---

A library for building powerful interactive prompts inspired by [python-prompt-toolkit](https://github.com/jonathanslenders/python-prompt-toolkit),
Expand Down Expand Up @@ -49,9 +59,21 @@ func main() {

## Features

### Automatic indentation with a custom callback

![automatic indentation](readme/automatic-indentation.gif)

### Multiline editing with scrolling

![multiline editing](readme/multiline-editing.gif)

### Custom syntax highlighting

![syntax highlighting](readme/syntax-highlighting.gif)

### Powerful auto-completion

[![demo](https://github.com/c-bata/assets/raw/master/go-prompt/kube-prompt.gif)](https://github.com/c-bata/kube-prompt)
[![autocompletion](https://github.com/c-bata/assets/raw/master/go-prompt/kube-prompt.gif)](https://github.com/c-bata/kube-prompt)

(This is a GIF animation of kube-prompt.)

Expand Down
6 changes: 6 additions & 0 deletions _example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Uses a custom lexer that colours every character with an even index green.

Shows you how to hook up a custom lexer for syntax highlighting.

## automatic indenter

Inserts a newline and indentation when the <kbd>Enter</kbd> key is pressed unless the input ends with a curly brace `}` and the amount of opening and ending braces is the same (then it gets printed).

Shows you how to define a custom callback which determines whether the input is complete and should be executed or a newline with indentation should be inserted (after <kbd>Enter</kbd> has been pressed).

## bang-executor

Inserts a newline when the <kbd>Enter</kbd> key is pressed unless the input ends with an exclamation point `!` (then it gets printed).
Expand Down
44 changes: 44 additions & 0 deletions _example/automatic-indenter/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"fmt"
"strings"
"unicode/utf8"

"github.com/elk-language/go-prompt"
)

func main() {
p := prompt.New(
executor,
prompt.WithPrefix(">>> "),
prompt.WithExecuteOnEnterCallback(ExecuteOnEnter),
)

p.Run()
}

func ExecuteOnEnter(input string, indentSize int) (int, bool) {
lines := strings.SplitAfter(input, "\n")
var spaces int
if len(lines) > 0 {
lastLine := lines[len(lines)-1]
for _, char := range lastLine {
if char == '}' {
spaces -= 2 * indentSize
break
}
if char != ' ' {
break
}
spaces++
}
}

char, _ := utf8.DecodeLastRuneInString(input)
return 1 + spaces/indentSize, char == '}' && strings.Count(input, "}") == strings.Count(input, "{")
}

func executor(s string) {
fmt.Println("Your input: " + s)
}
2 changes: 1 addition & 1 deletion _example/bang-executor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ func ExecuteOnEnter(input string, indentSize int) (int, bool) {
}

func executor(s string) {
fmt.Println("You printed: " + s)
fmt.Println("Your input: " + s)
}
1 change: 1 addition & 0 deletions _example/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ go build -o ${BIN_DIR}/simple-echo ${DIR}/simple-echo/main.go
go build -o ${BIN_DIR}/simple-echo-cjk-cyrillic ${DIR}/simple-echo/cjk-cyrillic/main.go
go build -o ${BIN_DIR}/even-lexer ${DIR}/even-lexer/main.go
go build -o ${BIN_DIR}/bang-executor ${DIR}/bang-executor/main.go
go build -o ${BIN_DIR}/automatic-indenter ${DIR}/automatic-indenter/main.go
50 changes: 47 additions & 3 deletions _example/even-lexer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"unicode"
"unicode/utf8"

"github.com/elk-language/go-prompt"
Expand All @@ -11,13 +12,15 @@ import (
func main() {
p := prompt.New(
executor,
prompt.WithLexer(prompt.NewEagerLexer(lexer)),
prompt.WithLexer(prompt.NewEagerLexer(wordLexer)),
prompt.WithLexer(prompt.NewEagerLexer(charLexer)), // the last one overrides the other
)

p.Run()
}

func lexer(line string) []prompt.Token {
// colors every other character green
func charLexer(line string) []prompt.Token {
var elements []prompt.Token

for i, value := range line {
Expand All @@ -37,6 +40,47 @@ func lexer(line string) []prompt.Token {
return elements
}

// colors every other word green
func wordLexer(line string) []prompt.Token {
if len(line) == 0 {
return nil
}

var elements []prompt.Token
var currentByte strings.ByteNumber
var wordIndex int
var lastChar rune

var color prompt.Color
for i, char := range line {
currentByte = strings.ByteNumber(i)
if unicode.IsSpace(char) {
if wordIndex%2 == 0 {
color = prompt.Green
} else {
color = prompt.White
}

element := prompt.NewSimpleToken(color, currentByte)
elements = append(elements, element)
wordIndex++
continue
}
lastChar = char
}
if !unicode.IsSpace(lastChar) {
if wordIndex%2 == 0 {
color = prompt.Green
} else {
color = prompt.White
}
element := prompt.NewSimpleToken(color, currentByte)
elements = append(elements, element)
}

return elements
}

func executor(s string) {
fmt.Println("You printed: " + s)
fmt.Println("Your input: " + s)
}
Binary file added readme/automatic-indentation.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme/multiline-editing.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme/syntax-highlighting.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ee5ed49

Please sign in to comment.