本文整理匯總了Golang中github.com/peterh/liner.State類的典型用法代碼示例。如果您正苦於以下問題:Golang State類的具體用法?Golang State怎麽用?Golang State使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了State類的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: configureLiner
func configureLiner(linerState *liner.State) {
linerState.SetCtrlCAborts(true)
linerState.SetCompleter(func(line string) (c []string) {
for _, n := range commandCompletions {
if strings.HasPrefix(n, strings.ToLower(line)) {
c = append(c, n)
}
}
return
})
/* TODO
// WordCompleter takes the currently edited line with the cursor position and
// returns the completion candidates for the partial word to be completed. If
// the line is "Hello, wo!!!" and the cursor is before the first '!',
// ("Hello, wo!!!", 9) is passed to the completer which may returns
// ("Hello, ", {"world", "Word"}, "!!!") to have "Hello, world!!!".
linerState.SetWordCompleter(func(line string, pos int) (head string, completions []string, tail string) {
for _, n := range wordCompletions {
if strings.HasPrefix(n, strings.ToLower(line)) {
c = append(c, n)
}
}
return
})
*/
}
示例2: writeLinerHistory
func writeLinerHistory(line *liner.State) {
if f, err := os.Create(historyFilename); err != nil {
log.Print("Error writing history file: ", err)
} else {
line.WriteHistory(f)
f.Close()
}
}
示例3: persist
// from google/cayley
func persist(term *liner.State, path string) error {
f, err := os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
return fmt.Errorf("could not open %q to append history: %v", path, err)
}
defer f.Close()
_, err = term.WriteHistory(f)
if err != nil {
return fmt.Errorf("could not write history to %q: %v", path, err)
}
return term.Close()
}
示例4: msgAdd
func (ce *CtrlEngine) msgAdd(
c *cli.Context,
from, to, file string,
mailInput, permanentSignature bool,
attachments []string,
minDelay, maxDelay int32,
line *liner.State,
r io.Reader,
) error {
fromMapped, err := identity.Map(from)
if err != nil {
return err
}
prev, _, err := ce.msgDB.GetNym(fromMapped)
if err != nil {
return err
}
if prev == "" {
return log.Errorf("user ID %s not found", from)
}
// TODO: handle attachments
var msg []byte
if file != "" {
// read message from file
msg, err = ioutil.ReadFile(file)
if err != nil {
return log.Error(err)
}
} else if line != nil {
// read message from terminal
fmt.Fprintln(ce.fileTable.StatusFP,
"type message (end with Ctrl-D on empty line):")
var inbuf bytes.Buffer
for {
ln, err := line.Prompt("")
if err != nil {
if err == io.EOF {
break
}
return log.Error(err)
}
inbuf.WriteString(ln + "\n")
}
msg = inbuf.Bytes()
} else {
// read message from stdin
msg, err = ioutil.ReadAll(r)
if err != nil {
return log.Error(err)
}
}
if mailInput {
recipient, message, err := mail.Parse(bytes.NewBuffer(msg))
if err != nil {
return err
}
to = recipient
msg = []byte(message)
}
toMapped, err := identity.Map(to)
if err != nil {
return err
}
prev, _, contactType, err := ce.msgDB.GetContact(fromMapped, toMapped)
if err != nil {
return err
}
if prev == "" || contactType == msgdb.GrayList || contactType == msgdb.BlackList {
return log.Errorf("contact %s not found (for user ID %s)", to, from)
}
// store message in message DB
now := times.Now()
err = ce.msgDB.AddMessage(fromMapped, toMapped, now, true, string(msg),
permanentSignature, minDelay, maxDelay)
if err != nil {
return err
}
log.Info("message added")
if line != nil {
fmt.Fprintln(ce.fileTable.StatusFP, "message added")
}
return nil
}
示例5: openLinerHistory
func openLinerHistory(line *liner.State) {
if f, err := os.Open(historyFilename); err == nil {
line.ReadHistory(f)
f.Close()
}
}
示例6: saveHistory
func (r *REPL) saveHistory(prompt *liner.State) {
if f, err := os.Create(r.historyPath); err == nil {
prompt.WriteHistory(f)
f.Close()
}
}
示例7: loadHistory
func (r *REPL) loadHistory(prompt *liner.State) {
if f, err := os.Open(r.historyPath); err == nil {
prompt.ReadHistory(f)
f.Close()
}
}