mirror of
https://github.com/ddworken/hishtory.git
synced 2025-08-10 07:27:57 +02:00
Add bolding of matching search results for #112, currently behind the beta-mode flag
This commit is contained in:
@ -170,6 +170,21 @@ func BuildTableRow(ctx context.Context, columnNames []string, entry data.History
|
||||
return row, nil
|
||||
}
|
||||
|
||||
// Make a regex that matches the non-tokenized bits of the given query
|
||||
func MakeRegexFromQuery(query string) string {
|
||||
tokens := tokenize(strings.TrimSpace(query))
|
||||
r := ""
|
||||
for _, token := range tokens {
|
||||
if !strings.HasPrefix(token, "-") && !containsUnescaped(token, ":") {
|
||||
if r != "" {
|
||||
r += "|"
|
||||
}
|
||||
r += fmt.Sprintf("(%s)", regexp.QuoteMeta(token))
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func stringArrayToAnyArray(arr []string) []any {
|
||||
ret := make([]any, 0)
|
||||
for _, item := range arr {
|
||||
@ -711,10 +726,7 @@ func where(tx *gorm.DB, s string, v1 any, v2 any) *gorm.DB {
|
||||
}
|
||||
|
||||
func MakeWhereQueryFromSearch(ctx context.Context, db *gorm.DB, query string) (*gorm.DB, error) {
|
||||
tokens, err := tokenize(query)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to tokenize query: %w", err)
|
||||
}
|
||||
tokens := tokenize(query)
|
||||
tx := db.Model(&data.HistoryEntry{}).Where("true")
|
||||
for _, token := range tokens {
|
||||
if strings.HasPrefix(token, "-") {
|
||||
@ -891,11 +903,11 @@ func getAllCustomColumnNames(ctx context.Context) ([]string, error) {
|
||||
return ccNames, nil
|
||||
}
|
||||
|
||||
func tokenize(query string) ([]string, error) {
|
||||
func tokenize(query string) []string {
|
||||
if query == "" {
|
||||
return []string{}, nil
|
||||
return []string{}
|
||||
}
|
||||
return splitEscaped(query, ' ', -1), nil
|
||||
return splitEscaped(query, ' ', -1)
|
||||
}
|
||||
|
||||
func splitEscaped(query string, separator rune, maxSplit int) []string {
|
||||
|
Reference in New Issue
Block a user