From: Evgenii Akentev Date: Sun, 9 Jul 2023 14:56:12 +0000 (+0400) Subject: Fix goToLine when line number is negative X-Git-Url: https://git.ak3n.com/?a=commitdiff_plain;h=166cd97553e918c5bdf67d38ab86dc1a9326d0da;p=line-indexed-cursor.git Fix goToLine when line number is negative --- diff --git a/src/System/IO/LineIndexedCursor.hs b/src/System/IO/LineIndexedCursor.hs index 14d62cd..10e2819 100644 --- a/src/System/IO/LineIndexedCursor.hs +++ b/src/System/IO/LineIndexedCursor.hs @@ -83,8 +83,9 @@ getCurrentLineNumber' CursorHandle{..} = do pure cln goToLine' :: CursorHandle -> Integer -> IO Integer -goToLine' CursorHandle{..} ln = do - modifyMVar linesIdx $ \(idx, size, _) -> do +goToLine' ch@CursorHandle{..} ln = + if (ln < 0) then getCurrentLineNumber' ch + else modifyMVar linesIdx $ \(idx, size, _) -> do if ln > size then do hSeek fileHandle AbsoluteSeek (idx !! 0) -- try to read until the requested line number diff --git a/test/Main.hs b/test/Main.hs index 8e002ec..9e74031 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -29,6 +29,13 @@ main = hspec $ do l <- getCurrentLine c l `shouldBe` Just "Sed elementum velit sit amet orci mollis tincidunt." + it "goToLine is negative" $ \(_, c) -> do + ln <- goToLine c (-10) + ln `shouldBe` 0 + + l <- getCurrentLine c + l `shouldBe` Just "Lorem ipsum dolor sit amet, consectetur adipiscing elit." + it "goToLine is too big" $ \(_, c) -> do l <- getCurrentLine c l `shouldBe` Just "Lorem ipsum dolor sit amet, consectetur adipiscing elit."