Skip to content

Commit

Permalink
populate robot details view
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Sep 11, 2024
1 parent 326003e commit 05a4bc4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 45 deletions.
11 changes: 6 additions & 5 deletions src/swarm-tui/Swarm/TUI/Controller/UpdateUI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ module Swarm.TUI.Controller.UpdateUI (
updateAndRedrawUI,
) where

import Brick hiding (Direction, Location)
import Brick hiding (Direction, Location, on)
import Brick.Focus
import Data.Function (on)

-- See Note [liftA2 re-export from Prelude]
import Brick.Widgets.List qualified as BL
Expand Down Expand Up @@ -44,7 +45,7 @@ import Swarm.TUI.Model.Repl
import Swarm.TUI.Model.UI
import Swarm.TUI.View.Objective qualified as GR
import Swarm.TUI.View.Robot
import Swarm.TUI.View.Robot.Type (libList, robID, robotListContent)
import Swarm.TUI.View.Robot.Type (libList, rob, robotListContent)
import Witch (into)
import Prelude hiding (Applicative (..))

Expand Down Expand Up @@ -192,11 +193,11 @@ doRobotListUpdate g = do
, _uiDbg = dOps
}
oldList = getList $ gp ^. uiDialogs . uiRobot . robotListContent . libList
maybeOldSelectedRID = robID . snd <$> BL.listSelectedElement oldList
maybeOldSelected = snd <$> BL.listSelectedElement oldList

modificationFunc = case maybeOldSelectedRID of
modificationFunc = case maybeOldSelected of
Nothing -> id
Just oldSelectedRID -> updateList (BL.listFindBy ((== oldSelectedRID) . robID))
Just oldSelected -> updateList (BL.listFindBy (((==) `on` (view robotID . rob)) oldSelected))

uiState . uiGameplay . uiDialogs . uiRobot . robotListContent . libList .= modificationFunc rd

Expand Down
6 changes: 3 additions & 3 deletions src/swarm-tui/Swarm/TUI/View.hs
Original file line number Diff line number Diff line change
Expand Up @@ -619,12 +619,12 @@ drawModal s = \case
HelpModal -> helpWidget (s ^. gameState . randomness . seed) (s ^. runtimeState . webPort) (s ^. keyEventHandling)
RobotsModal -> case s ^. uiState . uiGameplay . uiDialogs . uiRobot . robotsDisplayMode of
RobotList -> renderRobotsList $ s ^. uiState . uiGameplay . uiDialogs . uiRobot . robotListContent
SingleRobotDetails -> case maybeSelectedRID of
SingleRobotDetails -> case maybeSelectedRobot of
Nothing -> str "No selection"
Just selectedRID -> renderRobotDetails selectedRID
Just r -> renderRobotDetails r
where
oldList = getList $ s ^. uiState . uiGameplay . uiDialogs . uiRobot . robotListContent . libList
maybeSelectedRID = robID . snd <$> BL.listSelectedElement oldList
maybeSelectedRobot = rob . snd <$> BL.listSelectedElement oldList
RecipesModal -> availableListWidget (s ^. gameState) RecipeList
CommandsModal -> commandsListWidget (s ^. gameState)
MessagesModal -> availableListWidget (s ^. gameState) MessageList
Expand Down
2 changes: 1 addition & 1 deletion src/swarm-tui/Swarm/TUI/View/Robot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ mkLibraryEntries c =
mkRobotRow <$> S.fromList robots
where
mkRobotRow robot =
RobotRowPayload (robot ^. robotID) $
RobotRowPayload robot $
LibRobotRow
{ _fID = strWidget $ show $ robot ^. robotID
, _fName = nameWidget
Expand Down
100 changes: 65 additions & 35 deletions src/swarm-tui/Swarm/TUI/View/Robot/Details.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,75 @@
module Swarm.TUI.View.Robot.Details (renderRobotDetails) where

import Brick
import Brick.Widgets.Border
import Brick.Widgets.List qualified as BL
import Brick.Widgets.TabularList.Mixed
import Brick.Widgets.Center (hCenter)
import Brick.Widgets.Table qualified as BT
import Control.Lens hiding (from, (<.>))
import Control.Lens as Lens hiding (Const, from)
import Data.IntMap qualified as IM
import Data.List (transpose)
import Data.Foldable (Foldable (toList))
import Data.Map qualified as M
import Data.Maybe (fromMaybe)
import Data.Sequence (Seq)
import Data.Sequence qualified as S
import Data.Set (Set)
import Data.Text qualified as T
import Data.Vector (Vector)
import Data.Vector qualified as V
import Linear (V2 (..), distance)
import Numeric (showFFloat)
import Swarm.Game.CESK (CESK (..))
import Swarm.Game.Entity as E
import Swarm.Game.Location
import Prettyprinter (pretty)
import Swarm.Game.Robot
import Swarm.Game.Robot.Activity
import Swarm.Game.Robot.Concrete
import Swarm.Game.State
import Swarm.Game.State.Robot
import Swarm.Game.State.Substate
import Swarm.Game.Tick (addTicks)
import Swarm.Game.Universe
import Swarm.Game.World.Coords
import Swarm.TUI.Model.DebugOption
import Swarm.Language.Pretty (prettyText)
import Swarm.Log
import Swarm.TUI.Model.Name
import Swarm.TUI.Model.UI.Gameplay
import Swarm.TUI.View.Attribute.Attr
import Swarm.TUI.View.CellDisplay
import Swarm.TUI.View.Robot.Type
import Swarm.Util (applyWhen, maximum0)
import Swarm.Util.UnitInterval
import Swarm.Util.WindowedCounter qualified as WC
import System.Clock (TimeSpec (..))
import Swarm.TUI.View.Attribute.Attr (boldAttr)

renderRobotDetails selectedRID =
str $ unwords ["Selected robot", show selectedRID]
renderRobotDetails :: Robot -> Widget Name
renderRobotDetails robot =
vBox
[ hCenter $
str $
unwords
[ "Selected robot"
, show $ view robotID robot
]
, str "-------------------"
, logsTable
, str " "
, hBox [commandsTable, padLeft (Pad 2) inventorySummary]
]
where
inventorySummary =
vBox $
map
str
[ "6 things"
, "7 things"
]

logsTable =
BT.renderTable
. BT.columnBorders True
. BT.rowBorders False
. BT.surroundingBorder False
. BT.setDefaultColAlignment BT.AlignLeft
. BT.setColAlignment BT.AlignRight 0
. BT.table
$ (map (withAttr boldAttr . str) ["Time", "Entry"] : logsTableEntries)

mkLogTableEntry x =
[ str . show . pretty . view leTime $ x
, txt . view leText $ x
]
logsTableEntries = map mkLogTableEntry . toList . view robotLog $ robot

commandsTable =
BT.renderTable
. BT.columnBorders True
. BT.rowBorders False
. BT.surroundingBorder False
. BT.setDefaultColAlignment BT.AlignLeft
. BT.setColAlignment BT.AlignRight 0
. BT.table
$ map (withAttr boldAttr . str) ["Command", "Count"] : commandHistogramEntries

mkHistogramEntry (k, v) =
[ txt $ prettyText k
, str $ show v
]

commandHistogramEntries =
map mkHistogramEntry $
M.toList $
robot ^. activityCounts . commandsHistogram
2 changes: 1 addition & 1 deletion src/swarm-tui/Swarm/TUI/View/Robot/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ newtype Widths = Widths
deriving (Generic)

data RobotRowPayload a = RobotRowPayload
{ robID :: RID
{ rob :: Robot
, rPayload :: LibRobotRow a
}
deriving (Functor)
Expand Down
1 change: 1 addition & 0 deletions swarm.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,7 @@ library swarm-tui
brick-tabular-list,
bytestring,
clock,
prettyprinter,
colour,
containers,
extra,
Expand Down

0 comments on commit 05a4bc4

Please sign in to comment.