mirror of
https://github.com/Gu1llaum-3/sshm.git
synced 2025-10-19 01:17:20 +02:00
Merge pull request #12 from ldreux/support-subsearch
feat: support multiple words search
This commit is contained in:
commit
abbda54125
@ -37,35 +37,64 @@ func sortHostsByName(hosts []config.SSHHost) []config.SSHHost {
|
||||
|
||||
// filterHosts filters hosts according to the search query (name or tags)
|
||||
func (m Model) filterHosts(query string) []config.SSHHost {
|
||||
subqueries := strings.Split(query, " ")
|
||||
subqueriesLength := len(subqueries)
|
||||
subfilteredHosts := make([][]config.SSHHost, subqueriesLength)
|
||||
for i, subquery := range subqueries {
|
||||
subfilteredHosts[i] = m.filterHostsByWord(subquery)
|
||||
}
|
||||
|
||||
// return the intersection of search results
|
||||
result := make([]config.SSHHost, 0)
|
||||
tempMap := map[string]int{}
|
||||
for _, hosts := range subfilteredHosts {
|
||||
for _, host := range hosts {
|
||||
if _, ok := tempMap[host.Name]; !ok {
|
||||
tempMap[host.Name] = 1
|
||||
} else {
|
||||
tempMap[host.Name] = tempMap[host.Name] + 1
|
||||
}
|
||||
|
||||
if tempMap[host.Name] == subqueriesLength {
|
||||
result = append(result, host)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// filterHostsByWord filters hosts according to a single word
|
||||
func (m Model) filterHostsByWord(word string) []config.SSHHost {
|
||||
var filtered []config.SSHHost
|
||||
|
||||
if query == "" {
|
||||
if word == "" {
|
||||
filtered = m.hosts
|
||||
} else {
|
||||
query = strings.ToLower(query)
|
||||
word = strings.ToLower(word)
|
||||
|
||||
for _, host := range m.hosts {
|
||||
// Check the hostname
|
||||
if strings.Contains(strings.ToLower(host.Name), query) {
|
||||
if strings.Contains(strings.ToLower(host.Name), word) {
|
||||
filtered = append(filtered, host)
|
||||
continue
|
||||
}
|
||||
|
||||
// Check the hostname
|
||||
if strings.Contains(strings.ToLower(host.Hostname), query) {
|
||||
if strings.Contains(strings.ToLower(host.Hostname), word) {
|
||||
filtered = append(filtered, host)
|
||||
continue
|
||||
}
|
||||
|
||||
// Check the user
|
||||
if strings.Contains(strings.ToLower(host.User), query) {
|
||||
if strings.Contains(strings.ToLower(host.User), word) {
|
||||
filtered = append(filtered, host)
|
||||
continue
|
||||
}
|
||||
|
||||
// Check the tags
|
||||
for _, tag := range host.Tags {
|
||||
if strings.Contains(strings.ToLower(tag), query) {
|
||||
if strings.Contains(strings.ToLower(tag), word) {
|
||||
filtered = append(filtered, host)
|
||||
break
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user