Add search history preference (#193)
This commit is contained in:
1 parent
91be37fd8a
commit
4c32243e6e
13 files changed
+78
-19
No files matched your search
@@ -0,0 +1,64 @@
|
||||
import "pkg:/source/utils/ArrayUtils.bs"
|
||||
import "pkg:/source/utils/RegistryUtils.bs"
|
||||
import "pkg:/source/utils/StringUtils.bs"
|
||||
|
||||
namespace SearchHistoryUtils
|
||||
function Save(preferencesNode as object, q as string, maxItems = 10 as integer) as void
|
||||
seachHistoryEnabled = preferencesNode["search_history.enabled"]
|
||||
if not seachHistoryEnabled
|
||||
return
|
||||
end if
|
||||
|
||||
history = GetAllSaved(preferencesNode)
|
||||
index = ArrayUtils.IndexOfString(history, q)
|
||||
if index <> -1
|
||||
history.Delete(index)
|
||||
end if
|
||||
|
||||
history.Unshift(q)
|
||||
|
||||
if history.Count() > maxItems
|
||||
history.Pop()
|
||||
end if
|
||||
|
||||
history = FormatJson(history)
|
||||
|
||||
RegistryUtils.Write(RegistryUtils.SEARCH_HISTORY, history)
|
||||
end function
|
||||
|
||||
function GetSaved(preferencesNode as object, q as string) as object
|
||||
history = GetAllSaved(preferencesNode)
|
||||
if history.count() = 0
|
||||
return []
|
||||
end if
|
||||
|
||||
if StringUtils.IsNullOrEmpty(q)
|
||||
return history
|
||||
end if
|
||||
result = []
|
||||
for each keyword in history
|
||||
if keyword.InStr(q) = 0
|
||||
result.push(keyword)
|
||||
end if
|
||||
end for
|
||||
return result
|
||||
end function
|
||||
|
||||
function GetAllSaved(preferencesNode as object) as object
|
||||
seachHistoryEnabled = preferencesNode["search_history.enabled"]
|
||||
if not seachHistoryEnabled
|
||||
return []
|
||||
end if
|
||||
|
||||
history = RegistryUtils.Read(RegistryUtils.SEARCH_HISTORY)
|
||||
if history = invalid
|
||||
return []
|
||||
end if
|
||||
return ParseJson(history)
|
||||
end function
|
||||
|
||||
function Clear()
|
||||
RegistryUtils.Delete(RegistryUtils.SEARCH_HISTORY)
|
||||
end function
|
||||
|
||||
end namespace
|
||||
Reference in new issue
Block a user