package cjlookup import ( "fmt" "strconv" "strings" query "github.com/tgckpg/golifehk/query" utils "github.com/tgckpg/golifehk/utils" ) type QueryResult struct { Lang string Error error Query *query.QueryObject } func writeCCharInfo(sb *strings.Builder, cc *CChar) { sb.WriteString(getCUHARTSUrlForChar(cc.Face)) sb.WriteString(" ") sb.WriteString(getCUHARTSUrlForPronounce(cc.JyutPing.Roman)) if 0 < len(*cc.JiDukJam()) { sb.WriteString("\n") utils.WriteMDv2Text(sb, "異讀: ") sb.WriteString(strings.Join(getCUHARTSUrlsForPronounce(*cc.JiDukJam()), " ")) } sb.WriteString("\n") utils.WriteMDv2Text(sb, "倉: ") utils.WriteMDv2Text(sb, cc.CangJie) sb.WriteString("\n") utils.WriteMDv2Text(sb, "同音字:") for i, cchar := range *cc.JyutPing.SortedTungJamZi() { sb.WriteString(" ") utils.WriteMDv2Text(sb, cchar.Face) utils.WriteMDv2Text(sb, strconv.Itoa(i+1)) } } func (this QueryResult) Message() (string, error) { if this.Error != nil { return "", this.Error } if this.Query == nil { panic("Query is nil") } sb := strings.Builder{} q := *this.Query argv := *q.SearchTerms args := len(argv) if len(*q.Results) == 0 { terms := make([]string, args, len(*q.SearchTerms)) for i, term := range *q.SearchTerms { terms[i] = term.Org } return "", fmt.Errorf("Not Found: \"%s\"", strings.Join(terms, "\", \"")) } if utils.IsASCIIAlnum(argv[0].Value) { // Roman Search cjp := any((*q.Results)[0]).(*CJyutPing) if args == 1 { if len(*q.Results) == 1 { if len(*cjp.SortedTungJamZi()) == 1 { for _, cchar := range *cjp.SortedTungJamZi() { writeCCharInfo(&sb, cchar) } } else { for i, cchar := range *cjp.SortedTungJamZi() { sb.WriteString(" ") utils.WriteMDv2Text(&sb, cchar.Face) utils.WriteMDv2Text(&sb, strconv.Itoa(i+1)) } } } else { return "", fmt.Errorf("Multiple results found: %s", *q.Results) } } else if args == 2 { var err error x, err := strconv.Atoi(argv[1].Value) if err != nil { return "", fmt.Errorf("Invalid index: %s", argv[1].Org) } writeCCharInfo(&sb, (*cjp.SortedTungJamZi())[x-1]) } } else { // Char Search cchar := any((*q.Results)[0]).(*CChar) if args == 1 { writeCCharInfo(&sb, cchar) } else if args == 2 { var err error x, err := strconv.Atoi(argv[1].Value) if err != nil { return "", fmt.Errorf("Invalid index: %s", argv[1].Org) } tjz := (*(cchar.JyutPing).SortedTungJamZi())[x-1] if tjz != nil { writeCCharInfo(&sb, tjz) } } } return sb.String(), nil }