diff options
| author | tv <tv@krebsco.de> | 2026-03-09 02:25:18 +0100 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-03-09 02:26:50 +0100 |
| commit | 230e538e41360f2018db9a8b5274402d0b3200b6 (patch) | |
| tree | 3c7cb9e360850d0a404087a66bfb860441905a9b /src/TextViewport/Viewport/Instance.hs | |
| parent | fdf2c5436dfea4a30af445059e77a54e14b64752 (diff) | |
generalize Item from Text to Textual
Diffstat (limited to 'src/TextViewport/Viewport/Instance.hs')
| -rw-r--r-- | src/TextViewport/Viewport/Instance.hs | 34 |
1 files changed, 18 insertions, 16 deletions
diff --git a/src/TextViewport/Viewport/Instance.hs b/src/TextViewport/Viewport/Instance.hs index 1bafcbd..c3ce338 100644 --- a/src/TextViewport/Viewport/Instance.hs +++ b/src/TextViewport/Viewport/Instance.hs @@ -1,5 +1,7 @@ module TextViewport.Viewport.Instance where +import Data.Hashable (Hashable) +import Data.Sequences (Index, Textual) import TextViewport.Buffer.Item import TextViewport.Buffer.Buffer (Buffer) import TextViewport.Buffer.Buffer qualified as Buffer @@ -12,64 +14,64 @@ import TextViewport.Viewport.Viewport (Viewport, clampViewport, mkViewport) import TextViewport.Viewport.Viewport qualified as Viewport -data Instance = Instance - { viRender :: RenderState +data Instance a = Instance + { viRender :: RenderState a , viView :: Viewport } deriving (Show) -mkInstance :: Int -> Int -> Buffer -> Instance +mkInstance :: (Hashable a, Textual a, Index a ~ Int) => Int -> Int -> Buffer a -> Instance a mkInstance width height buf = let rs = mkRenderState width buf vp = mkViewport width height rs in Instance rs vp -visibleLines :: Instance -> [RenderedLine] +visibleLines :: Instance a -> [RenderedLine a] visibleLines (Instance rs vp) = take (Viewport.vpHeight vp) . drop (Viewport.vpOffset vp) . RenderedBuffer.flatten $ RenderState.rsRendered rs -applyToInstance :: (Viewport -> Viewport) -> Instance -> Instance +applyToInstance :: (Viewport -> Viewport) -> Instance a -> Instance a applyToInstance f (Instance rs vp) = let vp' = f vp in Instance rs (clampViewport rs vp') -applyToInstanceRS :: (RenderState -> Viewport -> Viewport) -> Instance -> Instance +applyToInstanceRS :: (RenderState a -> Viewport -> Viewport) -> Instance a -> Instance a applyToInstanceRS f (Instance rs vp) = let vp' = f rs vp in Instance rs (clampViewport rs vp') -scrollByI :: Int -> Instance -> Instance +scrollByI :: Int -> Instance a -> Instance a scrollByI delta = applyToInstance (Viewport.scrollBy delta) -scrollUpI :: Int -> Instance -> Instance +scrollUpI :: Int -> Instance a -> Instance a scrollUpI delta = applyToInstance (Viewport.scrollUp delta) -scrollDownI :: Int -> Instance -> Instance +scrollDownI :: Int -> Instance a -> Instance a scrollDownI delta = applyToInstance (Viewport.scrollDown delta) -pageUpI :: Instance -> Instance +pageUpI :: Instance a -> Instance a pageUpI = applyToInstance Viewport.pageUp -pageDownI :: Instance -> Instance +pageDownI :: Instance a -> Instance a pageDownI = applyToInstance Viewport.pageDown -alignTopI :: Instance -> Instance +alignTopI :: Instance a -> Instance a alignTopI = applyToInstance Viewport.alignTop -alignBottomI :: Instance -> Instance +alignBottomI :: Instance a -> Instance a alignBottomI = applyToInstanceRS Viewport.alignBottom -modifyItemI :: Int -> (Item -> Item) -> Instance -> Instance +modifyItemI :: (Hashable a, Textual a, Index a ~ Int) => Int -> (Item a -> Item a) -> Instance a -> Instance a modifyItemI ix f (Instance rs vp) = let buf' = Buffer.modifyItem ix f (RenderState.rsBuffer rs) rs' = mkRenderState (RenderState.rsWidth rs) buf' vp' = clampViewport rs' vp in Instance rs' vp' -lookupPositionI :: Int -> Int -> Instance -> Maybe (Int, Int) +lookupPositionI :: Int -> Int -> Instance a -> Maybe (Int, Int) lookupPositionI x y (Instance rs vp) = lookupPosition x y vp (RenderState.rsRendered rs) ---debugVI :: Instance -> IO () +--debugVI :: Instance a -> IO () --debugVI (Instance rs vp) = do -- putStrLn ("offset = " ++ show (Viewport.vpOffset vp)) -- putStrLn ("height = " ++ show (Viewport.vpHeight vp)) |
