// MTGCommand is a command for getting information about MTG cards.. func MTGCommand(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message, command string, parts []string) { cardNames := fuzzy.RankFindFold(command, MTGCardNames) if len(cardNames) == 0 { service.SendMessage(message.Channel(), "Could not find a card with that name, sorry.") return } sort.Sort(cardNames) card := MTGCardMap[cardNames[0].Target] rest := "" if card.Text != "" { rest += "\n" } if card.Power != nil { rest += MTGRestReplacer.Replace(fmt.Sprintf("%s/%s", *card.Power, *card.Toughness)) } if card.Loyalty != nil { rest += MTGRestReplacer.Replace(fmt.Sprintf("%d", *card.Loyalty)) } if card.ID != nil { if rest != "" && rest != "\n" { rest += "\n" } rest += fmt.Sprintf("(http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=%d&type=card)", *card.ID) } if service.Name() == bruxism.DiscordServiceName { service.SendMessage(message.Channel(), fmt.Sprintf("**%s** %s\n*%s*\n%s%s", card.Name, card.ManaCost, card.Type, MTGTextReplacer.Replace(card.Text), rest)) } else { service.SendMessage(message.Channel(), strings.Replace(fmt.Sprintf("%s. %s. %s. %s%s", card.Name, card.Type, card.ManaCost, card.Text, rest), "\n", " ", -1)) } }
func (R *Runner) update() { R.Selected = -1 T := R.App.Widget line := strings.Split(T.Content[0].Content, " ")[0] items := fuzzy.RankFindFold(line, R.Items) end := 12 if end > len(items) { end = len(items) } if len(items) > 0 { sort.Sort(items) R.Suggests = func() []string { r := []string{} for _, s := range items[:end] { r = append(r, s.Target) } return r }() if line == R.Suggests[0] { T.SetRules(0, []HighlightRule{HighlightRule{0, len(line), GREEN, "default"}}) } else { if len(R.Suggests) == 1 { T.SetRules(0, []HighlightRule{HighlightRule{0, len(line), ACCENT, "default"}}) } else { T.SetRules(0, []HighlightRule{HighlightRule{0, -1, "foreground", "default"}}) } } } tmp := make([]Line, len(T.Content)) copy(tmp, T.Content) newContent := tmp[:1] for _, item := range R.Suggests { newContent = append(newContent, Line{item, []HighlightRule{}}) } if len(items) > len(R.Suggests) { newContent = append(newContent, Line{fmt.Sprintf("%d more…", len(items)-12), []HighlightRule{HighlightRule{0, -1, "gray", "default"}}}) } T.SetContent(newContent) first := R.Suggests[0] if line != first && len(first) > len(line) && line == first[:len(line)] && len(strings.Split(T.Content[0].Content, " ")) == 1 { suggest := first[len(line):] ll, _, _ := T.Fonts["default"].SizeUTF8(line) sl, _, _ := T.Fonts["default"].SizeUTF8(suggest) r := sdl.Rect{ X: T.Padding.Left + int32(ll), Y: T.Padding.Top, W: int32(sl), H: int32(T.LineHeight), } T.DrawColoredText(suggest, &r, "gray", "default", []HighlightRule{}) } }
func (R *Runner) autocomplete() { T := R.App.Widget tokens := strings.Split(T.Content[0].Content, " ") line := tokens[0] items := fuzzy.RankFindFold(line, R.Items) if len(items) > 0 { sort.Sort(items) line = items[0].Target if len(tokens) > 1 { line += " " + strings.Join(tokens[1:], " ") } T.ChangeLine(0, Line{line, []HighlightRule{HighlightRule{0, len(line), GREEN, "default"}}}) T.MoveCursor(0, len(line)) R.update() } }
func fuzzySimple(conf *types.Configuration, searchFor string) *types.Connection { words := listWords(conf.AllConnections) reader := bufio.NewReader(os.Stdin) var found string for { color.Yellow("Search for: ") var input string var err error if searchFor != "" { input = searchFor searchFor = "" } else { input, err = reader.ReadString('\n') p(err, "reading stdin") input = strings.Trim(input, "\r\n ") } if input == "" { for _, v := range words { sort.Stable(StringList(words)) fmt.Println(v) } continue } suggs := fuzzy.RankFindFold(input, words) if len(suggs) > 1 { sort.Stable(suggs) color.Yellowln("Suggestions:") for _, v := range suggs { fmt.Println(v.Target) } color.Redln("Please try again.") } else if len(suggs) == 0 { color.Redln("Nothing found for", input+". Please try again.") } else { found = suggs[0].Target break } } conn := conf.AllConnections[found] return conn }
func filter(conf *types.Configuration, input string) (map[string]int, string) { input = strings.TrimSpace(input) if input == "" { return nil, "" } connections := listConnections(conf, true) words := listWords(connections) suggs := fuzzy.RankFindFold(input, words) res := make(map[string]int) minDist := math.MaxInt minPath := "" for _, s := range suggs { if s.Distance < minDist { minDist = s.Distance minPath = s.Target } res[s.Target] = s.Distance } return res, minPath }