Пример #1
0
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
0
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
0
// 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
0
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
0
func openLinerHistory(line *liner.State) {
	if f, err := os.Open(historyFilename); err == nil {
		line.ReadHistory(f)
		f.Close()
	}
}
Пример #6
0
func (r *REPL) saveHistory(prompt *liner.State) {
	if f, err := os.Create(r.historyPath); err == nil {
		prompt.WriteHistory(f)
		f.Close()
	}
}
Пример #7
0
func (r *REPL) loadHistory(prompt *liner.State) {
	if f, err := os.Open(r.historyPath); err == nil {
		prompt.ReadHistory(f)
		f.Close()
	}
}