skk

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2023 License: MIT Imports: 17 Imported by: 5

README

go-readline-skk

このパッケージは Go言語製のコマンドライン向けの一行入力パッケージ go-readline-nySKK ライクな「かな漢字変換機能」を実現するアドオンです。

./demo.gif

辞書などを全てパラメータで指定する場合

package main

import (
    "context"
    "fmt"
    "os"

    "github.com/mattn/go-colorable"

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

func mains() error {
    var ed readline.Editor

    // Windows でエスケープシーケンスを有効にする
    closer := colorable.EnableColorsStdout(nil)
    defer closer()
    ed.Writer = colorable.NewColorableStdout()

    // ~/ はパッケージ側で展開されます
    skkMode, err := skk.Config{
        UserJisyoPath:    "~/.go-skk-jisyo",
        SystemJisyoPaths: []string{"SKK-JISYO.L", "SKK-JISYO.emoji"},
        CtrlJ:            keys.CtrlJ,
        KeepModeOnExit:   false,
        BindTo:           &ed,
    }.Setup()

    if err != nil {
        return err
    }
    defer func() {
        err := skkMode.SaveUserJisyo()
        if err != nil {
            fmt.Fprintln(os.Stderr, err.Error())
        }
    }()

    for {
        text, err := ed.ReadLine(context.Background())
        if err != nil {
            return err
        }
        fmt.Println("TEXT:", text)
    }
    return nil
}

func main() {
    if err := mains(); err != nil {
        fmt.Fprintln(os.Stderr, "Error:", err.Error())
        os.Exit(1)
    }
}

環境変数などの設定文字列で辞書などを指定する場合

package main

import (
    "context"
    "fmt"
    "os"

    "github.com/mattn/go-colorable"

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

func mains() error {
    var ed readline.Editor

    // Windows でエスケープシーケンスを有効にする
    closer := colorable.EnableColorsStdout(nil)
    defer closer()
    ed.Writer = colorable.NewColorableStdout()

    // for example:
    //   set "GOREADLINESKK=~/Share/Etc/SKK-JISYO.*;user=~/.go-skk-jisyo"
    //   rem ~/ はパッケージ側で展開されます
    if env := os.Getenv("GOREADLINESKK"); env != "" {
        skkMode, err := skk.Config{
            BindTo: &ed,
        }.SetupWithString(env)

        if err != nil {
            fmt.Fprintln(os.Stderr, err.Error())
        } else {
            defer func() {
                err := skkMode.SaveUserJisyo()
                if err != nil {
                    fmt.Fprintln(os.Stderr, err.Error())
                }
            }()
        }
    }
    for {
        text, err := ed.ReadLine(context.Background())
        if err != nil {
            return err
        }
        fmt.Println("TEXT:", text)
    }
    return nil
}

func main() {
    if err := mains(); err != nil {
        fmt.Fprintln(os.Stderr, "Error:", err.Error())
        os.Exit(1)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CanBindKey

type CanBindKey interface {
	BindKey(keys.Code, readline.Command)
}

type Coloring

type Coloring struct {
	Base readline.Coloring
	// contains filtered or unexported fields
}

func (*Coloring) Init

func (c *Coloring) Init() readline.ColorSequence

func (*Coloring) Next

func (c *Coloring) Next(ch rune) readline.ColorSequence

type Config

type Config struct {
	UserJisyoPath    string
	SystemJisyoPaths []string
	CtrlJ            keys.Code
	BindTo           CanBindKey
	KeepModeOnExit   bool
	MiniBuffer       MiniBuffer
}

func (Config) Setup

func (c Config) Setup() (skkMode *Mode, err error)

func (Config) SetupWithString

func (c Config) SetupWithString(env string) (*Mode, error)

type Jisyo

type Jisyo struct {
	// contains filtered or unexported fields
}

Jisyo is a dictionary that contains user or system dictionary.

func (*Jisyo) Load

func (j *Jisyo) Load(filename string) error

Load reads the contents of an dictionary from a file. It returns time-stamp and error.

func (*Jisyo) Read

func (j *Jisyo) Read(r io.Reader) error

type MiniBuffer

type MiniBuffer interface {
	Enter(io.Writer, string) (int, error)
	Leave(io.Writer) (int, error)
	Recurse() MiniBuffer
}

type MiniBufferOnCurrentLine

type MiniBufferOnCurrentLine struct {
	OriginalPrompt string
}

func (MiniBufferOnCurrentLine) Enter

func (MiniBufferOnCurrentLine) Enter(w io.Writer, prompt string) (int, error)

func (MiniBufferOnCurrentLine) Leave

func (MiniBufferOnCurrentLine) Recurse

type MiniBufferOnNextLine

type MiniBufferOnNextLine struct{}

func (MiniBufferOnNextLine) Enter

func (MiniBufferOnNextLine) Enter(w io.Writer, prompt string) (int, error)

func (MiniBufferOnNextLine) Leave

func (MiniBufferOnNextLine) Leave(w io.Writer) (int, error)

func (MiniBufferOnNextLine) Recurse

func (MiniBufferOnNextLine) Recurse() MiniBuffer

type Mode

type Mode struct {
	User       *Jisyo
	System     *Jisyo
	MiniBuffer MiniBuffer
	// contains filtered or unexported fields
}

Mode is an instance of SKK. It contains system dictionaries and user dictionaries.

func (*Mode) Call

func (M *Mode) Call(ctx context.Context, B *readline.Buffer) readline.Result

Call is readline.Command to start SKK henkan mode.

func (*Mode) SaveUserJisyo

func (M *Mode) SaveUserJisyo() error

SaveUserJisyo saves the user dictionary as filename. The file is first created with the name filename+".TMP", and replaced with the file of filename after closing. The original file is renamed to filename + ".BAK".

func (*Mode) String

func (M *Mode) String() string

String returns the name as the command starting SKK

Jump to

Keyboard shortcuts

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