Rethink selection lists as intmaps

This commit is contained in:
2023-01-15 23:17:47 +00:00
parent 17734738f6
commit 048135c370
17 changed files with 245 additions and 93 deletions
+43
View File
@@ -0,0 +1,43 @@
module SelectionIntMap where
import Data.Foldable
import Data.Maybe
import Color
import Regex
import qualified Data.IntMap.Strict as IM
import Dodge.Data.SelectionList
import LensHelp
setShownIntMap :: SelectionIntMap a -> SelectionIntMap a
setShownIntMap sm = case sm ^. smRegex of
"" | not (sm ^. smRegexInput) -> sm & smShownItems .~ IM.fromAscList (zip [0..] allitms)
str -> sm & smShownItems
.~ IM.fromAscList (zip [0..] (f str : filter (regexList str . _siPictures) allitms))
where
allitms = sm ^. smItems
f str = SelectionInfo
{ _siPictures = ["FILTER: " ++ str]
, _siHeight = 1
, _siIsSelectable = False
, _siWidth = length ("FILTER: " ++ str)
, _siColor = white
, _siOffX = 0
}
-- assumes that at least one item is selectable!
-- also assumes that the integer is 1 or -1
moveSelectionMapStep :: Int -> SelectionIntMap a -> SelectionIntMap a
moveSelectionMapStep x sm = fromMaybe sm $ do
i <- _smSelPos sm
(n,_) <- IM.lookupMax (sm ^. smShownItems)
let j = (i + x) `mod` (n + 1)
case sm ^? smShownItems . ix j . siIsSelectable of
Just True -> Just $ sm & smSelPos ?~ j
_ -> Just $ moveSelectionMapStep x (sm & smSelPos ?~ j)
moveSelectionMapSelection :: Int -> SelectionIntMap a -> SelectionIntMap a
moveSelectionMapSelection i sm = foldl'
(&)
sm
(replicate (abs i) (moveSelectionMapStep (signum i)))