Beispiel #1
0
func main() {
	if len(os.Args) > 1 {
		input := strings.Replace(strings.Join(os.Args[1:], ""), " ", "", -1)
		res, err := compute.Evaluate(input)
		if err != nil {
			fmt.Println("Error: " + err.Error())
			return
		}
		fmt.Printf("%s\n", strconv.FormatFloat(res, 'G', -1, 64))
		return
	}

	var err error
	regularState, err = terminal.MakeRaw(0)
	if err != nil {
		panic(err)
	}
	defer terminal.Restore(0, regularState)

	term := terminal.NewTerminal(os.Stdin, "> ")
	term.AutoCompleteCallback = handleKey
	for {
		text, err := term.ReadLine()
		if err != nil {
			if err == io.EOF {
				// Quit without error on Ctrl^D
				fmt.Println()
				break
			}
			panic(err)
		}

		text = strings.Replace(text, " ", "", -1)
		if text == "exit" || text == "quit" {
			break
		}

		res, err := compute.Evaluate(text)
		if err != nil {
			fmt.Println("Error: " + err.Error())
			continue
		}
		fmt.Printf("%s\n", strconv.FormatFloat(res, 'G', -1, 64))
	}
}
Beispiel #2
0
func (plugin *pluginMath) doMath(handler foxbot.Handler) error {
	res, err := compute.Evaluate(strings.Join(handler.Args, ""))

	if len(handler.Args) == 0 || strings.Trim(handler.Args[0], " ") == "" {
		keyboard := &tgbotapi.ForceReply{
			ForceReply: true,
			Selective:  true,
		}

		msg := tgbotapi.NewMessage(handler.Update.Message.Chat.ID, "Please enter your equation")
		msg.ReplyToMessageID = handler.Update.Message.MessageID
		msg.ReplyMarkup = keyboard

		plugin.waitingEquation = &foxbot.WaitingForText{
			IsWaiting: true,
			ChatID:    handler.Update.Message.Chat.ID,
			UserID:    handler.Update.Message.From.ID,
			AnyInChat: false,
			Handler:   plugin.doMath,
		}

		handler.API.SendMessage(msg)

		return nil
	}

	if err != nil {
		return err
	}

	msg := tgbotapi.NewMessage(handler.Update.Message.Chat.ID, strconv.FormatFloat(res, 'f', -1, 64))
	msg.ReplyToMessageID = handler.Update.Message.MessageID
	handler.API.SendMessage(msg)

	return nil
}