multiline

package module
v0.6.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 16, 2023 License: MIT Imports: 7 Imported by: 5

README

go-multiline-ny

This is the readline package supporting multi-lines that extends go-readline-ny

The new key-bindings. It has compatibility with Emacs.

Key Feature
Ctrl-M or Enter Insert a new line
Ctrl-J or Ctrl-Enter Submit all lines
Ctrl-P or Up Move the cursor to the next line
Ctrl-N or Down Move the cursor to the next line
Alt-P or Ctrl-Up Fetch the previous lines-set of the history
Alt-N or Ctrl-Down Fetch the next lines-set of the history
Ctrl-Y Paste the string in the clipboard

image

package main

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

    "github.com/hymkor/go-multiline-ny"
    "github.com/mattn/go-colorable"
    "github.com/nyaosorg/go-readline-ny/simplehistory"
)

func main() {
    ctx := context.Background()
    fmt.Println("Ctrl-M or Enter      : Insert a linefeed")
    fmt.Println("Ctrl-P or UP         : Move to the previous line.")
    fmt.Println("Ctrl-N or DOWN       : Move to the next line")
    fmt.Println("Ctrl-J or Ctrl-Enter : Submit")
    fmt.Println("Ctrl-C               : Abort.")
    fmt.Println("Ctrl-D with no chars : Quit.")
    fmt.Println("Ctrl-UP   or ALT-P   : Move to the previous history entry")
    fmt.Println("Ctrl-DOWN or ALT-N   : Move to the next history entry")

    var editor multiline.Editor
    editor.Prompt = func(w io.Writer, lnum int) (int, error) {
        return fmt.Fprintf(w, "[%d] ", lnum+1)
    }

    // To enable escape sequence on Windows.
    // (On other operating systems, it can be ommited)
    editor.LineEditor.Writer = colorable.NewColorableStdout()

    history := simplehistory.New()
    editor.LineEditor.History = history
    editor.LineEditor.HistoryCycling = true

    for {
        lines, err := editor.Read(ctx)
        if err != nil {
            fmt.Fprintln(os.Stderr, err.Error())
            return
        }
        L := strings.Join(lines, "\n")
        fmt.Println("-----")
        fmt.Println(L)
        fmt.Println("-----")
        history.Add(L)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Read

func Read(ctx context.Context) ([]string, error)

Read (The function version) is same as `var m multiline.Editor ; m.Read(context)`. It Exists only for compatibility.

Types

type Editor added in v0.4.0

type Editor struct {
	LineEditor readline.Editor

	Prompt func(w io.Writer, i int) (int, error)
	// contains filtered or unexported fields
}

func New added in v0.3.0

func New() *Editor

New is same as `&multiline.Editor{}`. It exists only for compatibility. You can use `var m multiline.Editor`.

func (*Editor) Read added in v0.4.0

func (m *Editor) Read(ctx context.Context) ([]string, error)

func (*Editor) SetColoring added in v0.6.6

func (m *Editor) SetColoring(c readline.Coloring)

func (*Editor) SetHistory added in v0.6.6

func (m *Editor) SetHistory(h readline.IHistory)

func (*Editor) SetPrompt added in v0.6.6

func (m *Editor) SetPrompt(f func(io.Writer, int) (int, error))

func (*Editor) SetWriter added in v0.6.6

func (m *Editor) SetWriter(w io.Writer)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL