multiline

package module
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2023 License: MIT Imports: 10 Imported by: 5

README

go-multiline-ny

Go Reference

This is the readline package that supports multiline input and extends go-readline-ny with new keybindings. It is compatible with Emacs.

Key Feature
Ctrl-M or Enter Insert a new line[^Y]
Ctrl-J(or Ctrl-Enter[^X]) Submit all lines
Ctrl-P or Up Move the cursor to the previous 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

[^X]: Only WindowsTerminal or Teraterm [^Y]: It is possible to give the condition to submit.

Example

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("C-m or Enter      : Insert a linefeed")
    fmt.Println("C-p or UP         : Move to the previous line.")
    fmt.Println("C-n or DOWN       : Move to the next line")
    fmt.Println("C-j               : Submit")
    fmt.Println("C-c               : Abort.")
    fmt.Println("C-D with no chars : Quit.")
    fmt.Println("C-UP   or M-P     : Move to the previous history entry")
    fmt.Println("C-DOWN or M-N     : Move to the next history entry")

    var ed multiline.Editor
    ed.SetPrompt(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)
    ed.SetWriter(colorable.NewColorableStdout())

    history := simplehistory.New()
    ed.SetHistory(history)
    ed.SetHistoryCycling(true)

    for {
        lines, err := ed.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)
    }
}

Terminate input only if you type Enter when it ends with a semicolon

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("C-m or Enter      : Submit when lines end with `;`")
    fmt.Println("                    Otherwise insert a linefeed.")
    fmt.Println("C-j               : Submit always")
    fmt.Println("C-c               : Abort.")
    fmt.Println("C-D with no chars : Quit.")

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

    ed.SubmitOnEnterWhen(func(lines []string, _ int) bool {
        return strings.HasSuffix(strings.TrimSpace(lines[len(lines)-1]), ";")
    })

    // To enable escape sequence on Windows.
    // (On other operating systems, it can be ommited)
    ed.SetWriter(colorable.NewColorableStdout())

    history := simplehistory.New()
    ed.SetHistory(history)
    ed.SetHistoryCycling(true)

    for {
        lines, err := ed.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

This section is empty.

Types

type Editor added in v0.4.0

type Editor struct {
	LineEditor readline.Editor
	Dirty      bool
	// contains filtered or unexported fields
}

func (*Editor) BindKey added in v0.8.0

func (m *Editor) BindKey(key keys.Code, f readline.Command) error

func (*Editor) CmdBackwardChar added in v0.11.2

func (m *Editor) CmdBackwardChar(ctx context.Context, b *readline.Buffer) readline.Result

func (*Editor) CmdBackwardDeleteChar added in v0.11.2

func (m *Editor) CmdBackwardDeleteChar(ctx context.Context, b *readline.Buffer) readline.Result

func (*Editor) CmdDeleteChar added in v0.11.2

func (m *Editor) CmdDeleteChar(ctx context.Context, b *readline.Buffer) readline.Result

func (*Editor) CmdForwardChar added in v0.11.2

func (m *Editor) CmdForwardChar(ctx context.Context, b *readline.Buffer) readline.Result

func (*Editor) CmdNextHistory added in v0.11.2

func (m *Editor) CmdNextHistory(_ context.Context, b *readline.Buffer) readline.Result

func (*Editor) CmdNextLine added in v0.11.2

func (m *Editor) CmdNextLine(_ context.Context, _ *readline.Buffer) readline.Result

func (*Editor) CmdPreviousHistory added in v0.11.2

func (m *Editor) CmdPreviousHistory(_ context.Context, b *readline.Buffer) readline.Result

func (*Editor) CmdPreviousLine added in v0.11.2

func (m *Editor) CmdPreviousLine(_ context.Context, _ *readline.Buffer) readline.Result

func (*Editor) CmdYank added in v0.11.2

func (m *Editor) CmdYank(_ context.Context, b *readline.Buffer) readline.Result

func (*Editor) CursorLine added in v0.11.1

func (m *Editor) CursorLine() int

func (*Editor) GotoEndLine added in v0.11.1

func (m *Editor) GotoEndLine() func()

func (*Editor) Lines added in v0.11.1

func (m *Editor) Lines() []string

func (*Editor) NewLine added in v0.8.0

func (m *Editor) NewLine(_ context.Context, b *readline.Buffer) readline.Result

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) SetDefault added in v0.9.0

func (m *Editor) SetDefault(d []string)

func (*Editor) SetHistory added in v0.6.6

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

func (*Editor) SetHistoryCycling added in v0.6.7

func (m *Editor) SetHistoryCycling(value bool)

func (*Editor) SetMoveEnd added in v0.9.0

func (m *Editor) SetMoveEnd(value bool)

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)

func (*Editor) Submit added in v0.8.0

func (m *Editor) Submit(_ context.Context, B *readline.Buffer) readline.Result

func (*Editor) SubmitOnEnterWhen added in v0.12.0

func (m *Editor) SubmitOnEnterWhen(f func([]string, int) bool)

SubmitOnEnterWhen defines the condition to submit when Enter-key is pressed.

func (*Editor) SwapEnter deprecated added in v0.6.7

func (m *Editor) SwapEnter() error

Deprecated:

func (*Editor) Sync added in v0.11.2

func (m *Editor) Sync(line string)

type PrefixCommand added in v0.11.1

type PrefixCommand struct {
	readline.KeyMap
}

func (*PrefixCommand) Call added in v0.11.1

func (cx *PrefixCommand) Call(ctx context.Context, B *readline.Buffer) readline.Result

func (*PrefixCommand) String added in v0.11.1

func (*PrefixCommand) String() string

Jump to

Keyboard shortcuts

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