Commit 03b21775 authored by Eric's avatar Eric

[Update] improve search result

parent 1a883bef
...@@ -63,7 +63,7 @@ func (h *interactiveHandler) Initial() { ...@@ -63,7 +63,7 @@ func (h *interactiveHandler) Initial() {
h.displayBanner() h.displayBanner()
h.loadUserAssets() h.loadUserAssets()
h.loadUserAssetNodes() h.loadUserAssetNodes()
h.searchResult = h.assets h.searchResult = h.assets[:0]
h.winWatchChan = make(chan bool) h.winWatchChan = make(chan bool)
} }
...@@ -135,7 +135,6 @@ func (h *interactiveHandler) Dispatch(ctx cctx.Context) { ...@@ -135,7 +135,6 @@ func (h *interactiveHandler) Dispatch(ctx cctx.Context) {
return return
default: default:
assets := h.searchAsset(line) assets := h.searchAsset(line)
h.searchResult = assets
h.displayAssetsOrProxy(assets) h.displayAssetsOrProxy(assets)
} }
default: default:
...@@ -143,7 +142,6 @@ func (h *interactiveHandler) Dispatch(ctx cctx.Context) { ...@@ -143,7 +142,6 @@ func (h *interactiveHandler) Dispatch(ctx cctx.Context) {
case strings.Index(line, "/") == 0: case strings.Index(line, "/") == 0:
searchWord := strings.TrimSpace(line[1:]) searchWord := strings.TrimSpace(line[1:])
assets := h.searchAsset(searchWord) assets := h.searchAsset(searchWord)
h.searchResult = assets
h.displayAssets(assets) h.displayAssets(assets)
case strings.Index(line, "g") == 0: case strings.Index(line, "g") == 0:
searchWord := strings.TrimSpace(strings.TrimPrefix(line, "g")) searchWord := strings.TrimSpace(strings.TrimPrefix(line, "g"))
...@@ -151,13 +149,11 @@ func (h *interactiveHandler) Dispatch(ctx cctx.Context) { ...@@ -151,13 +149,11 @@ func (h *interactiveHandler) Dispatch(ctx cctx.Context) {
if num >= 0 { if num >= 0 {
assets := h.searchNodeAssets(num) assets := h.searchNodeAssets(num)
h.displayAssets(assets) h.displayAssets(assets)
h.searchResult = assets
continue continue
} }
} }
default: default:
assets := h.searchAsset(line) assets := h.searchAsset(line)
h.searchResult = assets
h.displayAssetsOrProxy(assets) h.displayAssetsOrProxy(assets)
} }
} }
...@@ -229,6 +225,11 @@ func (h *interactiveHandler) displayAssets(assets model.AssetList) { ...@@ -229,6 +225,11 @@ func (h *interactiveHandler) displayAssets(assets model.AssetList) {
h.systemUserSelect = &systemUser h.systemUserSelect = &systemUser
h.Proxy(context.TODO()) h.Proxy(context.TODO())
} }
if pag.page.GetPageSize() >= pag.page.TotalCount() {
h.searchResult = assets
} else {
h.searchResult = h.searchResult[:0]
}
} }
} }
...@@ -263,14 +264,22 @@ func (h *interactiveHandler) loadUserAssetNodes() { ...@@ -263,14 +264,22 @@ func (h *interactiveHandler) loadUserAssetNodes() {
} }
func (h *interactiveHandler) searchAsset(key string) (assets []model.Asset) { func (h *interactiveHandler) searchAsset(key string) (assets []model.Asset) {
if indexNum, err := strconv.Atoi(key); err == nil { if indexNum, err := strconv.Atoi(key); err == nil && len(h.searchResult) > 0 {
if indexNum > 0 && indexNum <= len(h.searchResult) { if indexNum > 0 && indexNum <= len(h.searchResult) {
assets = []model.Asset{h.searchResult[indexNum-1]} assets = []model.Asset{h.searchResult[indexNum-1]}
return return
} }
} }
for _, assetValue := range h.searchResult { var searchData []model.Asset
contents := []string{assetValue.Ip, assetValue.Hostname, assetValue.Comment} switch len(h.searchResult) {
case 0:
searchData = h.assets
default:
searchData = h.searchResult
}
for _, assetValue := range searchData {
contents := []string{assetValue.Hostname, assetValue.Ip, assetValue.Comment}
if isSubstring(contents, key) { if isSubstring(contents, key) {
assets = append(assets, assetValue) assets = append(assets, assetValue)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment