Skip to content

Commit

Permalink
feat(new-debugger): Add scrolling to the new sequence-diagram
Browse files Browse the repository at this point in the history
  • Loading branch information
symbiont-daniel-gustafsson committed Mar 28, 2022
1 parent 1c58023 commit 7b6ca0d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/new-debugger/detsys-debugger.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ library
, aeson
, base ^>=4.14
, brick
, microlens
, text
, vector
, word-wrap
Expand Down
38 changes: 30 additions & 8 deletions src/new-debugger/src/Debugger/SequenceDia.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE BangPatterns #-}
module Debugger.SequenceDia where

type Name = String
Expand All @@ -8,10 +9,18 @@ hLine = '─'
vLine :: Char
vLine = ''

mkBoxLines :: Bool -> [(Int, Name)] -> [String]
mkBoxLines isTop allBoxes = let (x,y,z) = go allBoxes in [x,y,z]
mkBoxLines :: Bool -> Bool -> [(Int, Name)] -> [String]
mkBoxLines isTop dotted allBoxes = let (x,y,z) = go allBoxes in if isTop then [x,y,z, extraLine] else [extraLine, x,y,z]
where
emptySpace i = (replicate i ' ', replicate i ' ', replicate i ' ')
extraLine = mygo allBoxes
where
mygo [] = mempty
mygo ((pre,name):xs)
= replicate (pre + 2 + length name `div` 2) ' ' ++
[if dotted then '' else vLine] ++
replicate (length name `div` 2 + 2) ' ' ++
mygo xs
go :: [(Int, Name)] -> (String, String, String)
go [] = mempty
go ((pre,name):xs) =
Expand Down Expand Up @@ -73,17 +82,30 @@ data Arrow msg = Arrow
, aMessage :: msg
}

generate :: [Arrow String] -> Int -> String
generate originalArrs current = unlines $
mkBoxLines True names <>
generate :: [Arrow String] -> Int -> Int -> String
generate originalArrs current height = unlines $
mkBoxLines True (dropBeginning > 0) names <>
concat [ mkArrow names fromIndex toIndex msg
| arr <- arrsMsg
| arr <- take howManyElements $ drop dropBeginning arrsMsg
, let fromIndex = index (aFrom arr) names
toIndex = index (aTo arr) names
msg = aMessage arr
] <>
mkBoxLines False names
mkBoxLines False (howManyElements < length originalArrs - dropBeginning) names
where

indexOfCurrent = mygo 0 originalArrs -- TODO this is wrong
where
mygo !_ [] = error "INTERNAL ERROR! can't find current index"
mygo n (arr : arrs)
| aAt arr == current = n
| otherwise = mygo (succ n) arrs

howManyElements = (height - 2*4) `div` 2 -- number of arrows we should display
atLeastHowManyAfter = min 5 howManyElements

dropBeginning = max (min (length originalArrs) (indexOfCurrent+atLeastHowManyAfter) - howManyElements) 0

arrsMsg = fmap (\arr -> if aAt arr == current
then arr {aMessage = markedMessage (aMessage arr)}
else arr {aMessage = simpleMessage (aMessage arr)})
Expand Down Expand Up @@ -119,7 +141,7 @@ generate originalArrs current = unlines $
| otherwise = fromIndex

example :: Int -> String
example = generate arrs
example = generate arrs 1000
where
arrs =
[ Arrow "Client" "Dumblog" 0 "Append"
Expand Down
2 changes: 1 addition & 1 deletion src/new-debugger/src/Debugger/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ data InstanceState = InstanceState
{ isState :: String -- Should probably be per reactor
, isCurrentEvent :: DebEvent
, isRunningVersion :: Int64
, isSeqDia :: String
, isSeqDia :: Int -> String -- the current height
, isLogs :: [String]
, isSent :: [DebEvent]
}
Expand Down
15 changes: 13 additions & 2 deletions src/new-debugger/src/Debugger/UI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Brick.Widgets.Border (borderWithLabel)
import Brick.Widgets.Border.Style (unicode)
import Brick.Widgets.Center (center, vCenter)
import qualified Brick.Widgets.List as L
import Lens.Micro ((^.))
import Control.Monad (void)
import Data.Maybe (fromMaybe)
import qualified Data.Text as Text
Expand Down Expand Up @@ -62,10 +63,20 @@ renderReactorState as = fillWidth $ vCenter $ myWrap
myStr x = hBox [ raw $ V.text' a c
| Segment a c <- parseANSI (Text.pack x)
]
-- only works for greedy sizes
withHeight :: (Int -> Widget n) -> Widget n
withHeight c = Widget
{ hSize = Greedy
, vSize = Greedy
, render = do
ctx <- getContext
render (c $ ctx ^. availHeightL)
}

renderSeqDia :: AppState -> Widget ()
renderSeqDia as = fillWidth $ vCenter $ myWrap
(fromMaybe "?" . fmap (isSeqDia . snd) $ L.listSelectedElement $ asLog as)
renderSeqDia as = withHeight $ \h ->
fillWidth $ vCenter $ myWrap
(fromMaybe "?" . fmap (($ h) . isSeqDia . snd) $ L.listSelectedElement $ asLog as)
where
fillWidth x = hBox [x, center $ str " "]
myWrap = vBox . map myStr . lines
Expand Down

0 comments on commit 7b6ca0d

Please sign in to comment.