Skip to content

Commit

Permalink
Use lazy text to read files
Browse files Browse the repository at this point in the history
The migration to strict text made `vgrep` hang whenever a large file was
loaded to the pager. Strict text is suitable for representing individual
lines, but not for representing a huge file. The solution is to read the
file using lazy text, split thc content into lines, and then convert the
lines to strict text.

Fixes #30.
  • Loading branch information
fmthoma committed Dec 27, 2016
1 parent 700b709 commit 421be02
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import Data.Sequence (Seq)
import qualified Data.Sequence as S
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.IO as TL
import Distribution.PackageDescription.TH
import qualified Graphics.Vty as Vty
import Graphics.Vty.Input.Events hiding (Event)
Expand Down Expand Up @@ -192,13 +193,19 @@ loadSelectedFileToPager = do
whenJust maybeFileName $ \fileName -> do
fileExists <- liftIO (doesFileExist fileName)
fileContent <- if fileExists
then liftIO (fmap (S.fromList . T.lines) (T.readFile fileName))
then readLinesFrom fileName
else use inputLines
displayContent <- expandForDisplay fileContent
highlightLineNumbers <- use (results . currentFileResultLineNumbers)
zoom pager (replaceBufferContents displayContent highlightLineNumbers)
moveToSelectedLineNumber
zoom widgetState (splitView FocusRight (1 % 3))
where
readLinesFrom file = liftIO $ do
content <- TL.readFile file
pure (fileLines content)
fileLines = S.fromList . map TL.toStrict . TL.lines


moveToSelectedLineNumber :: Monad m => VgrepT AppState m ()
moveToSelectedLineNumber =
Expand Down

0 comments on commit 421be02

Please sign in to comment.