summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/Scanner.hs107
1 files changed, 52 insertions, 55 deletions
diff --git a/src/Scanner.hs b/src/Scanner.hs
index 24546be..3383920 100644
--- a/src/Scanner.hs
+++ b/src/Scanner.hs
@@ -4,7 +4,8 @@ module Scanner
) where
import Data.Bits ((.&.), testBit)
-import Data.Char (ord)
+import Data.Char (isDigit, ord)
+import Data.List (unsnoc)
import System.IO (Handle, hGetChar, hLookAhead, hWaitForInput)
@@ -27,6 +28,11 @@ data Scan =
+-- | Read one key, mouse event, or terminal reply.
+--
+-- Escape sequences that end early or contain unexpected characters are not
+-- an error: what was read of them is returned as a 'ScanKey', and the
+-- character that did not fit is left for the next 'scan'.
scan :: Handle -> IO Scan
scan h =
hGetChar h >>= \case
@@ -46,67 +52,62 @@ scanESC h =
-- XXX M-F1 and other crazy chords may cause
-- \ESC\ESC... on wu, so we just recurse here...
scanESC h
- | c == 'O' -> do -- 04/15
+ | c == 'O' -> -- 04/15
-- XXX Non-CSI SS3
-- XXX finalByte is maybe calles somehow else here, but it's
-- the same range
- final <- expectChar h finalByte
- return (ScanKey (reverse (final : ['O','\ESC'])))
+ ScanKey . reverse <$> zeroOrOne h finalByte ['O','\ESC']
| otherwise ->
return $ ScanKey ['\ESC',c]
scanCS :: Handle -> IO Scan
-scanCS h =
- hWaitLookAhead timeout h >>= \case
- Nothing -> return $ ScanKey "\ESC" -- TODO move this to scanESC
- Just c
- | c == '<' -> hGetChar h >> scanSGRMousePayload h
- | c == 'M' -> hGetChar h >> scanX10MousePayload h
- | otherwise -> scanOtherCSISequence h
+scanCS h = do
+ params <- zeroOrMore h parameterByte ['[', '\ESC']
+ inters <- zeroOrMore h intermediateByte params
+ s <- reverse <$> zeroOrOne h finalByte inters
+ case s of
+ "\ESC[" -> return $ ScanKey "\ESC" -- TODO move this to scanESC
+ "\ESC[M" -> scanX10MousePayload h
+ '\ESC' : '[' : '<' : payload | Just m <- sgrMouse payload -> return m
+ _ -> return $ ScanKey s
scanX10MousePayload :: Handle -> IO Scan
-scanX10MousePayload h = do
- b <- ord <$> hGetChar h
- x <- ord <$> hGetChar h
- y <- ord <$> hGetChar h
- let pressed = (b .&. 3) /= 3
- return (decodeMouseEvent (b - 32) (x - 32) (y - 32) pressed)
+scanX10MousePayload h =
+ upTo 3 >>= \payload ->
+ case map ord payload of
+ [b, x, y] ->
+ let pressed = (b .&. 3) /= 3 in
+ return (decodeMouseEvent (b - 32) (x - 32) (y - 32) pressed)
+ _ ->
+ return $ ScanKey ("\ESC[M" <> payload)
+ where
+ upTo :: Int -> IO [Char]
+ upTo 0 = return []
+ upTo n =
+ hWaitGetChar timeout h >>= \case
+ Just c -> (c :) <$> upTo (n - 1)
+ Nothing -> return []
-scanSGRMousePayload :: Handle -> IO Scan
-scanSGRMousePayload h = do
- b <- scanNumber h
- _ <- expectChar h (==';')
- x <- scanNumber h
- _ <- expectChar h (==';')
- y <- scanNumber h
- m <- hGetChar h -- 'M' or 'm'
- let pressed = (m == 'M')
+-- | Decode the part of an SGR mouse report after @\\ESC[<@, e.g. @0;10;20M@.
+sgrMouse :: String -> Maybe Scan
+sgrMouse payload = do
+ (params, final) <- unsnoc payload
+ pressed <- case final of
+ 'M' -> Just True
+ 'm' -> Just False
+ _ -> Nothing
+ [b, x, y] <- traverse number (split ';' params)
return (decodeMouseEvent b x y pressed)
-
-
-scanOtherCSISequence :: Handle -> IO Scan
-scanOtherCSISequence h = do
- params <- zeroOrMore h parameterByte ['[', '\ESC']
- inters <- zeroOrMore h intermediateByte params
- final <- expectChar h finalByte
- return (ScanKey (reverse (final : inters)))
-
-
-scanNumber :: Handle -> IO Int
-scanNumber h = go 0
where
- go acc =
- hWaitLookAhead timeout h >>= \case
- Just c ->
- if '0' <= c && c <= '9'
- then hGetChar h >> go (acc * 10 + (ord c - ord '0'))
- else return acc
+ number s | not (null s), all isDigit s = Just (read s)
+ number _ = Nothing
- Nothing -> fail "timeout"
-
+ split c s = case break (== c) s of
+ (a, _ : rest) -> a : split c rest
+ (a, []) -> [a]
zeroOrMore :: Handle -> (Char -> Bool) -> [Char] -> IO [Char]
@@ -120,15 +121,11 @@ zeroOrMore h p buf =
return buf
-expectChar :: Handle -> (Char -> Bool) -> IO Char
-expectChar h p =
- hWaitGetChar timeout h >>= \case
- Just c ->
- if p c
- then return c
- else fail ("unexpected char: " ++ show c)
- Nothing ->
- fail "timeout"
+zeroOrOne :: Handle -> (Char -> Bool) -> [Char] -> IO [Char]
+zeroOrOne h p buf =
+ hWaitLookAhead timeout h >>= \case
+ Just c | p c -> hGetChar h >> return (c:buf)
+ _ -> return buf
parameterByte :: Char -> Bool