@@ -9,8 +9,6 @@
99module Graphics.Vty.Span
1010 where
1111
12−import Codec.Binary.UTF8.Width ( wcwidth )
13−
1412import Graphics.Vty.Image
1513import Graphics.Vty.Picture
1614import Graphics.Vty.DisplayRegion
@@ -25,9 +23,8 @@ import qualified Data.ByteString as B
2523import qualified Data.ByteString.Internal as BInt
2624import qualified Data.Foldable as Foldable
2725import Data.List
28−import Data.Word
29−import qualified Data.ByteString.UTF8 as BSUTF8
3026import qualified Data.String.UTF8 as UTF8
27+import Data.Word
3128
3229import Foreign.Storable ( pokeByteOff )
3330
@@ -53,6 +50,14 @@ data SpanOpSequence = SpanOpSequence
5350type RowOps = Array Word SpanOps
5451type SpanOps = [SpanOp]
5552
53+instance Show SpanOpSequence where
54+ show (SpanOpSequence _ the_row_ops)
55+ = concat $ ["{ "] ++ map (\ops -> show ops ++ "; " ) (elems the_row_ops) ++ [" }"]
56+
57+instance Show SpanOp where
58+ show (AttributeChange attr) = show attr
59+ show (TextSpan ow cw _) = "TextSpan " ++ show ow ++ " " ++ show cw
60+
5661span_ops_columns :: SpanOpSequence -> Word
5762span_ops_columns ops = region_width $ effected_region ops
5863
@@ -204,9 +209,8 @@ ops_for_row mrow_ops bg region image skip_dim@(skip_row,skip_col) y remaining_co
204209 else empty_image
205210 ops_for_row mrow_ops bg region ((i <|> hpad) <-> vpad) skip_dim y remaining_columns
206211
207−snoc_text_span :: (Foldable.Foldable t)
208− => Attr
209− -> t Char
212+snoc_text_span :: Attr
213+ -> DisplayString
210214 -> MRowOps s
211215 -> Word
212216 -> Word
@@ -215,30 +219,22 @@ snoc_text_span :: (Foldable.Foldable t)
215219snoc_text_span a text_str mrow_ops skip_col y remaining_columns = do
216220 snoc_op mrow_ops y $ AttributeChange a
217221 let (ow', dw', cw', txt) = Foldable.foldl'
218− build_cropped_txt
222+ build_text_span
219223 ( 0, 0, 0, B.empty )
220224 text_str
221225 snoc_op mrow_ops y $ TextSpan ow' cw' (UTF8.fromRep txt)
222226 return $ skip_col - dw'
223227 where
224− build_cropped_txt (ow', dw', char_count', b0) c = {-# SCC "build_cropped_txt" #-}
225− let w = wcwidth c
226− -- Characters with unknown widths occupy 1 column.
227− --
228− -- todo: Not sure if this is actually correct.
229− -- I presume there is a replacement character that is output by the terminal instead of
230− -- the character. If so then this replacement process may need to be implemented
231− -- manually for consistent behavior across terminals.
232− w' = toEnum $ if w < 0 then 1 else w
233− in if dw' == skip_col
228+ build_text_span (ow', dw', char_count', b0) (c,w) = {-# SCC "build_text_span" #-}
229+ if dw' == skip_col
234230 then if ow' == remaining_columns
235231 then ( ow', dw', char_count', b0 )
236− else if (w' + ow') > remaining_columns
232+ else if (w + ow') > remaining_columns
237233 then ( remaining_columns, dw', char_count' + ooverflow, B.append b0 $ B.pack $ encode $ genericReplicate ooverflow '…' )
238− else ( ow' + w', dw', char_count' + 1, B.append b0 $ B.pack $ encode [c] )
239− else if (w' + dw') > skip_col
234+ else ( ow' + w, dw', char_count' + 1, B.append b0 $ B.pack $ encode [c] )
235+ else if (w + dw') > skip_col
240236 then ( doverflow, skip_col, doverflow, B.append b0 $ B.pack $ encode $ genericReplicate doverflow '…' )
241− else ( ow', w' + dw', char_count', b0 )
237+ else ( ow', w + dw', char_count', b0 )
242238 where
243239 doverflow = skip_col - dw'
244240 ooverflow = remaining_columns - ow'