Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module System.Terminal.Emulator.Parsing.Internal where
import Control.Applicative ((<|>))
import Data.Attoparsec.Text
import Data.Char (isDigit)
import Data.Ix (inRange)
import Data.List.NonEmpty (NonEmpty ((:|)))
import qualified Data.List.NonEmpty as NE
import Data.Maybe (mapMaybe)
Expand Down Expand Up @@ -141,7 +142,7 @@ data ControlSequenceIntroducerComponents
-- This parser always succeeds
parseControlSequenceIntroducer :: Parser ControlSequenceIntroducerInput
parseControlSequenceIntroducer = do
str <- takeTill ((`between` (0x40, 0x7E)) . fromEnum)
str <- takeTill ((inRange (0x40, 0x7E)) . fromEnum)
c <- anyChar
pure (ControlSequenceIntroducerInput ((str) <> T.singleton c))

Expand Down Expand Up @@ -387,14 +388,11 @@ singleCharacterEscapeSequence c =
-- Helper functions
-----------------------------------------------------------------------

between :: Ord a => a -> (a, a) -> Bool
between val (low, high) = val >= low && val <= high

isControlC0 :: Char -> Bool
isControlC0 c = fromEnum c `between` (0, 0x1F) || c == '\DEL'
isControlC0 c = (0, 0x1F) `inRange` fromEnum c || c == '\DEL'

isControlC1 :: Char -> Bool
isControlC1 c = fromEnum c `between` (0x80, 0x9f)
isControlC1 c = (0x80, 0x9f) `inRange` fromEnum c

isControl :: Char -> Bool
isControl c = isControlC0 c || isControlC1 c
12 changes: 5 additions & 7 deletions hs-term-emulator/src/System/Terminal/Emulator/Parsing/Types.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module System.Terminal.Emulator.Parsing.Types where

import Data.Ix (inRange)
import Data.Text (Text)
import Data.Vector (Vector)
import System.Console.ANSI.Types (SGR)
Expand Down Expand Up @@ -203,16 +204,16 @@ codeToSGR 24 = Just $ SGR.SetUnderlining SGR.NoUnderline
codeToSGR 39 = Just $ SGR.SetDefaultColor SGR.Foreground
codeToSGR 49 = Just $ SGR.SetDefaultColor SGR.Background
codeToSGR code
| code `between` (30, 37) = do
| inRange (30, 37) code = do
color <- codeToColor (code - 30)
Just $ SGR.SetColor SGR.Foreground SGR.Dull color
| code `between` (90, 97) = do
| inRange (90, 97) code = do
color <- codeToColor (code - 90)
Just $ SGR.SetColor SGR.Foreground SGR.Vivid color
| code `between` (40, 47) = do
| inRange (40, 47) code = do
color <- codeToColor (code - 40)
Just $ SGR.SetColor SGR.Background SGR.Dull color
| code `between` (100, 107) = do
| inRange (100, 107) code = do
color <- codeToColor (code - 100)
Just $ SGR.SetColor SGR.Background SGR.Vivid color
| otherwise = Nothing
Expand All @@ -227,6 +228,3 @@ codeToColor 5 = Just SGR.Magenta
codeToColor 6 = Just SGR.Cyan
codeToColor 7 = Just SGR.White
codeToColor _ = Nothing

between :: Ord a => a -> (a, a) -> Bool
between val (low, high) = val >= low && val <= high
8 changes: 3 additions & 5 deletions hs-term-emulator/src/System/Terminal/Emulator/Term/Process.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC8
import Data.Foldable (foldl')
import Data.Ix (inRange)
import Data.List (iterate')
import Data.Text (Text)
import qualified Data.Text as T
Expand Down Expand Up @@ -226,7 +227,7 @@ insertBlankChars n term = (cursorLine %~ updateLine) term

insertBlankLines :: Int -> Term -> Term
insertBlankLines n term
| between (term ^. scrollTop, term ^. scrollBottom) (term ^. cursorPos . _1) = scrollDown (term ^. cursorPos . _1) n term
| inRange (term ^. scrollTop, term ^. scrollBottom) (term ^. cursorPos . _1) = scrollDown (term ^. cursorPos . _1) n term
| otherwise = term

deleteChars :: Int -> Term -> Term
Expand All @@ -245,7 +246,7 @@ deleteChars n term = (cursorLine %~ updateLine) term

deleteLines :: Int -> Term -> Term
deleteLines n term
| between (term ^. scrollTop, term ^. scrollBottom) (term ^. cursorPos . _1) = scrollUp (term ^. cursorPos . _1) n term
| inRange (term ^. scrollTop, term ^. scrollBottom) (term ^. cursorPos . _1) = scrollUp (term ^. cursorPos . _1) n term
| otherwise = term

setScrollingRegion :: Maybe Int -> Maybe Int -> Term -> Term
Expand Down Expand Up @@ -457,6 +458,3 @@ limit minVal maxVal val
| val < minVal = minVal
| val > maxVal = maxVal
| otherwise = val

between :: Ord a => (a, a) -> a -> Bool
between (low, high) val = val >= low && val <= high