@@ -5,6 +5,7 @@
55module Graphics.Vty.Input.Classify
66 ( classify
77 , KClass(..)
8+ , ClassifierState(..)
89 )
910where
1011
@@ -27,6 +28,17 @@ import qualified Data.ByteString as BS
2728import qualified Data.ByteString.Char8 as BS8
2829import Data.ByteString.Char8 (ByteString)
2930
31+-- | Whether the classifier is currently processing a chunked format.
32+-- Currently, only bracketed pastes use this.
33+data ClassifierState
34+ = ClassifierStart
35+ -- ^ Not processing a chunked format.
36+ | ClassifierInChunk ByteString [ByteString]
37+ -- ^ Currently processing a chunked format. The initial chunk is in the
38+ -- first argument and a reversed remainder of the chunks is collected in
39+ -- the second argument. At the end of the processing, the chunks are
40+ -- reversed and concatenated with the final chunk.
41+
3042compile :: ClassifyMap -> ByteString -> KClass
3143compile table = cl' where
3244 -- take all prefixes and create a set of these
@@ -54,18 +66,27 @@ compile table = cl' where
5466 -- neither a prefix or a full event.
5567 [] -> Invalid
5668
57−classify :: ClassifyMap -> ByteString -> KClass
58−classify table =
59− let standardClassifier = compile table
60− in \s -> case BS.uncons s of
61− _ | bracketedPasteStarted s ->
69+classify :: ClassifyMap -> ClassifierState -> ByteString -> KClass
70+classify table = process
71+ where
72+ standardClassifier = compile table
73+
74+ process ClassifierStart s =
75+ case BS.uncons s of
76+ _ | bracketedPasteStarted s ->
77+ if bracketedPasteFinished s
78+ then parseBracketedPaste s
79+ else Chunk
80+ _ | isMouseEvent s -> classifyMouseEvent s
81+ _ | isFocusEvent s -> classifyFocusEvent s
82+ Just (c,cs) | c >= 0xC2 -> classifyUtf8 c cs
83+ _ -> standardClassifier s
84+
85+ process (ClassifierInChunk p ps) s | bracketedPasteStarted p =
6286 if bracketedPasteFinished s
63− then parseBracketedPaste s
64− else Prefix
65− _ | isMouseEvent s -> classifyMouseEvent s
66− _ | isFocusEvent s -> classifyFocusEvent s
67− Just (c,cs) | c >= 0xC2 -> classifyUtf8 c cs
68− _ -> standardClassifier s
87+ then parseBracketedPaste $ BS.concat $ p:reverse (s:ps)
88+ else Chunk
89+ process ClassifierInChunk{} _ = Invalid
6990
7091classifyUtf8 :: Word8 -> ByteString -> KClass
7192classifyUtf8 c cs =