Rethink selection lists as intmaps
This commit is contained in:
@@ -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)))
|
||||
|
||||
Reference in New Issue
Block a user