Giter Site home page Giter Site logo

nyaosorg / go-readline-ny Goto Github PK

View Code? Open in Web Editor NEW
20.0 3.0 3.0 356 KB

Readline library for golang , used in nyagos

Home Page: https://pkg.go.dev/github.com/nyaosorg/go-readline-ny

License: MIT License

Go 99.11% Makefile 0.89%
go golang nyagos command-line readline readline-library

go-readline-ny's Introduction

Go Reference Go Report Card Wiki License

The New Yet another Readline for Go (go-readline-ny)

The New Yet another Readline for Go (go-readline-ny) is a one-line input library for CUI applications written in Go that is extensible for different needs. It has been running inside the command line shell "NYAGOS" for a long time.

  • Emacs-like key-bindings
  • History
  • Word Completion (file names, command names, or any names in given array)
  • Syntax Highlighting
  • Supported OS: Windows and Linux
  • Suported Unicode (UTF8)
    • Surrogate-pair
    • Emoji (via clipboard)
    • Zero-Width-Joiner (via clipboard)
    • Variation Selector (via clipboard pasted by Ctrl-Y)
  • Add-Ons:
  • MIT License

Zero-Width-Joiner sample on Windows-Terminal

The most simple sample.

package main

import (
    "context"
    "fmt"

    "github.com/nyaosorg/go-readline-ny"
)

func main() {
    var editor readline.Editor
    text, err := editor.ReadLine(context.Background())
    if err != nil {
        fmt.Printf("ERR=%s\n", err.Error())
    } else {
        fmt.Printf("TEXT=%s\n", text)
    }
}

If the target platform includes Windows, you have to import and use go-colorable like example2.go .

Tiny Shell. This is a sample of prompt change, colorization, filename completion and history browsing.

package main

import (
    "context"
    "fmt"
    "io"
    "os"
    "os/exec"
    "strings"

    "github.com/mattn/go-colorable"

    "github.com/nyaosorg/go-readline-ny"
    "github.com/nyaosorg/go-readline-ny/coloring"
    "github.com/nyaosorg/go-readline-ny/completion"
    "github.com/nyaosorg/go-readline-ny/keys"
    "github.com/nyaosorg/go-readline-ny/simplehistory"
)

func main() {
    history := simplehistory.New()

    editor := &readline.Editor{
        PromptWriter: func(w io.Writer) (int, error) {
            return io.WriteString(w, "\x1B[1;36m$ \x1B[0m") // print `$ ` with cyan
        },
        Writer:         colorable.NewColorableStdout(),
        History:        history,
        Coloring:       &coloring.VimBatch{},
        HistoryCycling: true,
    }

    editor.BindKey(keys.CtrlI, completion.CmdCompletionOrList{
        Completion: completion.File{},
        Postfix:    " ",
    })
    // If you do not want to list files with double-tab-key,
    // use `CmdCompletion` instead of `CmdCompletionOrList`

    fmt.Println("Tiny Shell. Type Ctrl-D to quit.")
    for {
        text, err := editor.ReadLine(context.Background())

        if err != nil {
            fmt.Printf("ERR=%s\n", err.Error())
            return
        }

        fields := strings.Fields(text)
        if len(fields) <= 0 {
            continue
        }
        cmd := exec.Command(fields[0], fields[1:]...)
        cmd.Stdout = os.Stdout
        cmd.Stderr = os.Stderr
        cmd.Stdin = os.Stdin

        cmd.Run()

        history.Add(text)
    }
}

example3.go

This is a sample to change key-bindings to diamond cursor.

example4.go

This is a sample that implements the function to start the text editor defined by the environment variable EDITOR and import the edited contents when the ESCAPE key is pressed.

go-readline-ny's People

Contributors

hymkor avatar masamitsu-murase avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

go-readline-ny's Issues

wezterm support

I saw this tweet: https://twitter.com/NyaosOrg/status/1497486187279175681
and found:

go-readline-ny/moji.go

Lines 15 to 33 in 48b319a

var isWezTerm = os.Getenv("WEZTERM_EXECUTABLE") != ""
var (
// SurrogatePairOk is true when the surrogated pair unicode is supported
// If it is false, <NNNN> is displayed instead.
SurrogatePairOk = isWindowsTerminal || isWezTerm
// ZeroWidthJoinSequenceOk is true when ZWJ(U+200D) is supported.
// If it is false, <NNNN> is displayed instead.
ZeroWidthJoinSequenceOk = isWindowsTerminal
// VariationSequenceOk is true when Variation Sequences are supported.
// If it is false, <NNNN> is displayed instead.
VariationSequenceOk = isWindowsTerminal
// ModifierSequenceOk is false, SkinTone sequence are treated as two
// character
ModifierSequenceOk = isWindowsTerminal
)

I'd like to make some suggestions and invite you to please open issues with wezterm if things aren't working as you expect!

Detecting wezterm

Firstly, I would suggest looking at these two environment variables if you'd like to detect when you are running inside a local wezterm:

; env | egrep '^TERM_'
TERM_PROGRAM=WezTerm
TERM_PROGRAM_VERSION=20220213-203140-4a1c4b55

If you'd like to support the case where you are running in a remote ssh session, you could use a sequence like XTVERSION to probe the terminal version:

  1. Emit \x1b[>q
  2. Read the response string, which for wezterm looks like: ^[P>|WezTerm 20220213-203140-4a1c4b55^[\
  3. That read operation should have a timeout in case the remote terminal doesn't respond to this sequence.

Variation Sequences

These are supported by wezterm when the unicode version is set to enable them.

https://wezfurlong.org/wezterm/config/lua/config/unicode_version.html#unicode-version-escape-sequence describes escape sequences that you can use to enable them for your application

Modifier Sequence

wezterm should support all skin tone and other modifiers out of the box: we use harfbuzz for this and have explicit unit tests to verify that these are shaped correctly.

If you find examples where this isn't true, please file an issue!

ZWJ

These should be supported, but I'm not sure what your criteria for conformance is for these. I would love to hear more!

Feature Request: Disable history cycling

Thank you for this project.

Feature request: assume that a user has inputted the following through editor.ReadLine() function:

  1. Hello
  2. There
  3. Morning

If the user presses the UP arrow key for 4 times, than "Morning" will be redisplayed.

Most probably this is due to the following code within history.go

	if this.historyPointer <= 0 {
		this.historyPointer = this.History.Len()
	}

Would it be possible to implement a flag such that, when enabled, if the user presses the UP key >= 4, then only the first input (i.e. "Hello") is displayed?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.