本文整理汇总了Golang中github.com/peterh/liner.NewLiner函数的典型用法代码示例。如果您正苦于以下问题:Golang NewLiner函数的具体用法?Golang NewLiner怎么用?Golang NewLiner使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewLiner函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewICli
// NewICli creates a new ICli object and initializes it.
func NewICli(name, prompt, version, history, desc string) (*ICli, error) {
// Initialize the clif interfaces
//clif.DefaultStyles = clif.SunburnStyles
icli := &ICli{
Cli: clif.New(name, version, desc),
term: liner.NewLiner(),
prompt: prompt,
history: history,
optionSet: make(map[string]string),
}
f, err := os.Open(history)
if err != nil {
f, err = os.Create(history)
if err != nil {
return nil, fmt.Errorf("could not open or create history file %s :: %v",
history, err)
}
}
defer f.Close()
_, err = icli.term.ReadHistory(f)
if err != nil {
return nil, fmt.Errorf("could not read history file %s :: %v", history, err)
}
// set ctrl-c to abort the input.
icli.term.SetCtrlCAborts(true)
return icli, nil
}
示例2: NewShell
func NewShell() *Shell {
sh := &Shell{
shell: liner.NewLiner(),
prompt: "mbus> ",
hist: filepath.Join(".", ".fcs_lpc_motor_history"),
motor: m702.New("134.158.125.223:502"),
}
sh.shell.SetCtrlCAborts(true)
sh.shell.SetCompleter(func(line string) (c []string) {
for n := range sh.cmds {
if strings.HasPrefix(n, strings.ToLower(line)) {
c = append(c, n)
}
}
return
})
if f, err := os.Open(sh.hist); err == nil {
sh.shell.ReadHistory(f)
f.Close()
}
sh.cmds = map[string]shellCmd{
"dump": sh.cmdDump,
"get": sh.cmdGet,
"motor": sh.cmdMotor,
"quit": sh.cmdQuit,
"set": sh.cmdSet,
}
return sh
}
示例3: New
func New(args []string) *cli {
if len(args) > 1 {
return nil
}
// We assume the terminal starts in cooked mode.
cooked, _ = liner.TerminalMode()
if cooked == nil {
return nil
}
i := &cli{liner.NewLiner()}
if history_path, err := task.GetHistoryFilePath(); err == nil {
if f, err := os.Open(history_path); err == nil {
i.ReadHistory(f)
f.Close()
}
}
uncooked, _ = liner.TerminalMode()
i.SetCtrlCAborts(true)
i.SetTabCompletionStyle(liner.TabPrints)
i.SetWordCompleter(complete)
return i
}
示例4: NewCLI
// NewCLI create CLI from Config.
func NewCLI(conf *Config) (*CLI, error) {
origin := "http://127.0.0.1"
if conf.Origin != "" {
origin = conf.Origin
}
conn, err := websocket.Dial(conf.URL, "", origin)
if err != nil {
return nil, err
}
template := EmptyTemplate
if _, err := os.Stat(conf.Template); err == nil {
template, err = NewTemplateFile(conf.Template)
if err != nil {
return nil, err
}
}
cli := &CLI{
conn,
liner.NewLiner(),
template,
conf.Debug,
}
cli.liner.SetCtrlCAborts(true)
cli.liner.SetCompleter(Completer(template))
return cli, nil
}
示例5: terminal
func terminal(path string) (*liner.State, error) {
term := liner.NewLiner()
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
<-c
err := persist(term, history)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to properly clean up terminal: %v\n", err)
os.Exit(1)
}
os.Exit(0)
}()
f, err := os.Open(path)
if err != nil {
return term, err
}
defer f.Close()
_, err = term.ReadHistory(f)
return term, err
}
示例6: main
func main() {
flag.Parse()
line := liner.NewLiner()
line.SetCtrlCAborts(true)
defer line.Close()
for {
cmd, err := line.Prompt("> ")
if err == liner.ErrPromptAborted || err == io.EOF {
break
} else if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
args := shlex.Parse(cmd)
switch strings.ToLower(args[0]) {
case "search":
if err := search(args[1:]); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
case "magnet":
if err := magnet(args[1:]); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
}
}
}
示例7: TestParseCommand_HistoryWithBlankCommand
func TestParseCommand_HistoryWithBlankCommand(t *testing.T) {
t.Parallel()
c := cli.CommandLine{Line: liner.NewLiner()}
defer c.Line.Close()
// append one entry to history
c.Line.AppendHistory("x")
tests := []struct {
cmd string
}{
{cmd: "history"},
{cmd: " history"},
{cmd: "history "},
{cmd: "History "},
{cmd: ""}, // shouldn't be persisted in history
{cmd: " "}, // shouldn't be persisted in history
}
// don't validate because blank commands are never executed
for _, test := range tests {
c.ParseCommand(test.cmd)
}
// buf shall not contain empty commands
var buf bytes.Buffer
c.Line.WriteHistory(&buf)
scanner := bufio.NewScanner(&buf)
for scanner.Scan() {
if scanner.Text() == "" || scanner.Text() == " " {
t.Fatal("Empty commands should not be persisted in history.")
}
}
}
示例8: PromptConfirm
func PromptConfirm(prompt string) (bool, error) {
var (
input string
err error
)
prompt = prompt + " [y/N] "
if liner.TerminalSupported() {
lr := liner.NewLiner()
defer lr.Close()
input, err = lr.Prompt(prompt)
} else {
fmt.Print(prompt)
input, err = bufio.NewReader(os.Stdin).ReadString('\n')
fmt.Println()
}
if len(input) > 0 && strings.ToUpper(input[:1]) == "Y" {
return true, nil
} else {
return false, nil
}
return false, err
}
示例9: TestParseCommand_History
func TestParseCommand_History(t *testing.T) {
t.Parallel()
c := cli.CommandLine{Line: liner.NewLiner()}
defer c.Line.Close()
// append one entry to history
c.Line.AppendHistory("abc")
tests := []struct {
cmd string
}{
{cmd: "history"},
{cmd: " history"},
{cmd: "history "},
{cmd: "History "},
}
for _, test := range tests {
if err := c.ParseCommand(test.cmd); err != nil {
t.Fatalf(`Got error %v for command %q, expected nil.`, err, test.cmd)
}
}
// buf size should be at least 1
var buf bytes.Buffer
c.Line.WriteHistory(&buf)
if buf.Len() < 1 {
t.Fatal("History is borked")
}
}
示例10: New
func New(client service.Client) *Term {
return &Term{
prompt: "(dlv) ",
line: liner.NewLiner(),
client: client,
}
}
示例11: RunRepl
func RunRepl(store *Secstore) {
var sections []string
var line string
var err error
liner := liner.NewLiner()
defer liner.Close()
liner.SetCompleter(completer)
for {
line, err = liner.Prompt("> ")
if err != nil {
break
}
liner.AppendHistory(line)
sections = splitSections(line)
if len(sections) == 0 {
} else if strings.HasPrefix("quit", sections[0]) {
break
} else {
err = EvalCommand(store, sections)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}
fmt.Fprintln(os.Stderr, "Exiting...")
}
示例12: commandLoop
func commandLoop() error {
in := liner.NewLiner()
defer in.Close()
in.SetCtrlCAborts(true)
f := NewForther()
in.SetCompleter(f.Complete)
for {
line, err := in.Prompt(f.Prompt())
if err == io.EOF || err == liner.ErrPromptAborted {
return nil
} else if err != nil {
return err
}
in.AppendHistory(line)
for i, cmd := range strings.Fields(line) {
if err := f.Process(cmd); err == io.EOF {
return nil
} else if err != nil {
fmt.Printf("Cannot run op %d (%q): %v\n", i+1, cmd, err)
break
}
}
}
}
示例13: runTerm
func runTerm(cmd *cobra.Command, args []string) {
if len(args) != 0 {
cmd.Usage()
return
}
db := makeSQLClient()
liner := liner.NewLiner()
defer func() {
_ = liner.Close()
}()
for {
l, err := liner.Prompt("> ")
if err != nil {
if err != io.EOF {
fmt.Fprintf(os.Stderr, "Input error: %s\n", err)
}
break
}
if len(l) == 0 {
continue
}
liner.AppendHistory(l)
if err := processOneLine(db, l); err != nil {
fmt.Printf("Error: %s\n", err)
}
}
}
示例14: uiLoop
func uiLoop() {
line := liner.NewLiner()
line.SetCtrlCAborts(true)
fmt.Print("\n\nFlow v0.1.0\n\nPress Ctrl+C to exit\n\n")
for {
if input, err := line.Prompt("flow> "); err == nil {
inputs := strings.SplitN(input, " ", 2)
if len(inputs) > 1 {
cmd := inputs[0]
args := inputs[1]
checkCmd(cmd, args)
} else {
checkCmd(input, "")
}
} else if err == liner.ErrPromptAborted {
break
} else {
log.Printf("error de terminal: %s\n", err.Error())
}
}
line.Close()
out <- Event{
Type: UserExit,
Data: nil,
}
}
示例15: newLightweightJSRE
func newLightweightJSRE(libPath string, client comms.EthereumClient, interactive bool) *jsre {
js := &jsre{ps1: "> "}
js.wait = make(chan *big.Int)
js.client = client
js.ds = docserver.New("/")
// update state in separare forever blocks
js.re = re.New(libPath)
if err := js.apiBindings(js); err != nil {
utils.Fatalf("Unable to initialize console - %v", err)
}
if !liner.TerminalSupported() || !interactive {
js.prompter = dumbterm{bufio.NewReader(os.Stdin)}
} else {
lr := liner.NewLiner()
js.withHistory(func(hist *os.File) { lr.ReadHistory(hist) })
lr.SetCtrlCAborts(true)
js.loadAutoCompletion()
lr.SetWordCompleter(apiWordCompleter)
lr.SetTabCompletionStyle(liner.TabPrints)
js.prompter = lr
js.atexit = func() {
js.withHistory(func(hist *os.File) { hist.Truncate(0); lr.WriteHistory(hist) })
lr.Close()
close(js.wait)
}
}
return js
}