Categorygithub.com/yetsing/startprompt
modulepackage
0.0.19
Repository: https://github.com/yetsing/startprompt.git
Documentation: pkg.go.dev

# README

startprompt

start build prompt commandline application

开始构建提示符命令行应用

复刻划掉参考 python-prompt-toolkit

支持 Linux MacOS

hello world

CommandLine

package main

/*
读取用户输入并将其打印出来
*/

import (
	"fmt"

	"github.com/yetsing/startprompt"
)

func main() {
	c, err := startprompt.NewCommandLine(nil)
	if err != nil {
		fmt.Printf("failed to startprompt.NewCommandLine: %v\n", err)
		return
	}
	line, err := c.ReadInput()
	if err != nil {
		fmt.Printf("ReadInput error: %v\n", err)
	}
	fmt.Println("echo:", line)
}

TCommandLine

package main

/*
helloworld 例子,读取用户输入并将其打印出来
*/

import (
	"fmt"

	"github.com/yetsing/startprompt"
)

func main() {
	c, err := startprompt.NewTCommandLine(nil)
	if err != nil {
		fmt.Printf("failed to startprompt.NewTCommandLine: %v\n", err)
		return
	}
	defer c.Close()
	c.Println("Type some text. Press Enter confirm or Ctrl-D exit")
	line, err := c.ReadInput()
	if err != nil {
		return
	}
	c.Println("echo:", line)
}

实现特性

  • 支持常用快捷键操作,可看 keybinding
  • 支持输入历史(提供内存和文件两种实现)
  • 支持语法高亮(通过自定义分词器实现)
  • 支持鼠标操作,可看 mouse (TCommandLine 支持)

有两个实现 CommandLine TCommandLine这两个在细节行为上有差异TCommandLine 基于 tcell 实现,增加鼠标支持

更多例子

examples 文件夹有一些简单的例子,其中以 t 开头的是使用 TCommandLine 的例子。

开启 Debug 日志

日志内容会输出到当前目录下的 startprompt.log 文件中

c, err := startprompt.NewTCommandLine(&startprompt.CommandLineOption{
    EnableDebug: true,
})

参考

python-prompt-toolkit

pygments

词法分析主要参考下面

Let's Build A Simple Interpreter

《用Go语言自制解释器》

# Packages

No description provided by the author
No description provided by the author
Package lexer 实现各种分词器.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

No description provided by the author
No description provided by the author
NewCommandLine 传入配置,新建命令行对象.
NewDocument 新建 Document 对象.
goland:noinspection GoUnusedExportedFunction.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NewTCommandLine 新建命令行对象.
RepeatString 将 n 个字符串 s 拼接在一起.
StringLjustWidth 返回指定宽度左对齐的字符串,不够的右边补空格.

# Constants

AbortActionIgnore 忽略此次操作.
AbortActionRetry 让用户重新输入.
AbortActionReturnError 返回错误,一般是 AbortError 或 ExitError.
AbortActionReturnNone 返回空.
AbortActionUnspecific 空值.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
EventTypeBackspace (127) Backspace.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
EventTypeCtrlBackslash Both Control-\ and Ctrl-|.
goland:noinspection GoUnusedConst.
EventTypeCtrlCircumflex Control-^.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
EventTypeCtrlH Control-H (8) (Identical to '\b').
EventTypeCtrlI Control-I (9) (Identical to '\t').
EventTypeCtrlJ Control-J (10) (Identical to '\n').
goland:noinspection GoUnusedConst.
EventTypeCtrlL Control-L (Clear; form feed).
EventTypeCtrlM Control-M (13) (Identical to '\r').
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
EventTypeCtrlSpace Control-Space (Also for Ctrl-@).
EventTypeCtrlSquareClose Control-].
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
EventTypeCtrlUnderscore Control-underscore (Also for Ctrl-hypen.).
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
EventTypeMouseDblclick 鼠标左键双击.
EventTypeMouseDown 按下鼠标左键.
EventTypeMouseMove 按下鼠标左键并且移动.
EventTypeMouseTripleClick 鼠标左键三击.
EventTypeMouseUp 松开鼠标左键.
goland:noinspection GoUnusedConst.
EventTypeMouseWheelUp EventTypeMouseWheelDown 鼠标滚轮.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
goland:noinspection GoUnusedConst.
PollEventInput 输入事件,用户按下键盘输入 PollEventRedraw 重画事件,重画当前输入 PollEventTimeout 超时事件,一段时间内没有其他事件触发.
No description provided by the author
No description provided by the author

# Variables

AbortError 用户中断,一般是 Ctrl-C 触发 ExitError 用户停止,一般是 Ctrl-D 触发.
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
CommandLineOption 命令行选项.
No description provided by the author
No description provided by the author
Document 一个不可修改的数据结构,用于保存文本和光标位置.
No description provided by the author
EventKey 代表键盘事件.
EventMouse 代表鼠标事件.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
MouseInfoOfInput 在当前输入中的一些鼠标信息.
No description provided by the author
No description provided by the author
Screen 以坐标维度缓冲输出字符.
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author