Skip to content

Commit

Permalink
wip: table rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Sep 7, 2024
1 parent ad352a5 commit 77a20b5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 48 deletions.
1 change: 1 addition & 0 deletions src/swarm-tui/Swarm/TUI/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ import Swarm.TUI.Launch.Prep (prepareLaunchDialog)
import Swarm.TUI.List
import Swarm.TUI.Model
import Swarm.TUI.Model.Dialog
import Swarm.TUI.Model.Dialog.RobotDisplay
import Swarm.TUI.Model.Name
import Swarm.TUI.Model.Repl
import Swarm.TUI.Model.StateUpdate
Expand Down
74 changes: 36 additions & 38 deletions src/swarm-tui/Swarm/TUI/Model/Dialog/Robot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@
-- SPDX-License-Identifier: BSD-3-Clause
module Swarm.TUI.Model.Dialog.Robot where

import Data.Maybe (fromMaybe)
import Swarm.Game.Robot.Activity
import Swarm.Game.Robot.Concrete
import Swarm.Game.State.Robot
import Swarm.Game.World.Coords
import Swarm.TUI.View.CellDisplay
import Swarm.TUI.View.Robot

-- import Swarm.TUI.View.Util

import Brick
import Brick.AttrMap
import Brick.Widgets.Border
Expand All @@ -26,6 +16,7 @@ import Control.Lens hiding (from, (<.>))
import Control.Lens as Lens hiding (Const, from)
import Data.IntMap qualified as IM
import Data.Map qualified as M
import Data.Maybe (fromMaybe)
import Data.Sequence (Seq)
import Data.Sequence qualified as S
import Data.Set (Set)
Expand All @@ -37,13 +28,19 @@ import Swarm.Game.CESK (CESK (..))
import Swarm.Game.Entity as E
import Swarm.Game.Location
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.Universe
import Swarm.Game.World.Coords
import Swarm.TUI.Model.DebugOption
import Swarm.TUI.Model.Dialog.RobotDisplay
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
import Swarm.Util (applyWhen)
import System.Clock (TimeSpec (..))

Expand All @@ -56,8 +53,8 @@ data RobotRenderingContext = RobotRenderingContext

makeLenses ''RobotRenderingContext

emptyRobotDisplay :: RobotDisplay
emptyRobotDisplay =
emptyRobotDisplay :: Set DebugOption -> RobotDisplay
emptyRobotDisplay uiDbg =
RobotDisplay
{ _robotsDisplayMode = RobotList
, _lastFocusedRobotId = Nothing
Expand All @@ -66,29 +63,32 @@ emptyRobotDisplay =
MixedRenderers
{ cell = dc
, rowHdr = Just rowHdr
, colHdr = Just colHdr
, colHdr = Just $ colHdr uiDbg
, colHdrRowHdr = Just $ ColHdrRowHdr $ \_ _ -> vLimit 1 (fill ' ') <=> hBorder
}
}

renderTheRobots :: RobotDisplay -> Widget Name
renderTheRobots rd = renderMixedTabularList (rd ^. libRenderers) (LstFcs True) (rd ^. libList)
renderTheRobots rd =
renderMixedTabularList (rd ^. libRenderers) (LstFcs True) (rd ^. libList)

columnHdrAttr :: AttrName
columnHdrAttr = attrName "columnHeader"

rowHdrAttr :: AttrName
rowHdrAttr = attrName "rowHeader"

colHdr :: MixedColHdr Name Widths
colHdr =
colHdr :: Set DebugOption -> MixedColHdr Name Widths
colHdr uiDbg =
MixedColHdr
{ draw = \_ (MColC (Ix ci)) -> case colHdrs V.!? ci of
{ draw = \_ (MColC (Ix ci)) -> case hdrs V.!? ci of
Just ch -> withAttr columnHdrAttr (padRight Max (str ch) <+> str " ") <=> hBorder
Nothing -> emptyWidget
, widths = \(Widths song) -> song
, height = ColHdrH 2
}
where
hdrs = colHdrs uiDbg

rowHdr :: RowHdr Name LibRobotRow
rowHdr =
Expand All @@ -103,8 +103,23 @@ rowHdr =
, toRH = \_ (Ix i) -> i + 1
}

colHdrs :: Vector String
colHdrs = V.fromList ["Artist", "Title", "Album"]
colHdrs :: Set DebugOption -> Vector String
colHdrs uiDbg = V.fromList augmentedHeadings
where
headings =
[ "Name"
, "Age"
, "Pos"
, "Items"
, "Status"
, "Actns"
, "Cmds"
, "Cycles"
, "Activity"
, "Log"
]
augmentedHeadings = applyWhen debugRID ("ID" :) headings
debugRID = uiDbg ^. Lens.contains ListRobotIDs

wpr :: WidthsPerRow LibRobotRow Widths
wpr = WsPerR $ \(Widths song) e -> case e of
Expand Down Expand Up @@ -136,29 +151,12 @@ libraryEntries =

wprk :: WidthsPerRowKind LibRobotRow Widths
wprk = WsPerRK $ \(AvlW aW) _ ->
let artist = max 7 $ (aW * 30) `div` 100
title = max 6 $ (aW * 30) `div` 100
album = aW - artist - title
in Widths {robotRowWidths = fmap ColW [artist, title, album]}
Widths {robotRowWidths = fmap ColW [4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6]}

robotsTable :: RobotRenderingContext -> BT.Table Name
robotsTable c =
BT.table $
map (padLeftRight 1) <$> (headers : robotRows)
BT.table []
where
headings =
[ "Name"
, "Age"
, "Pos"
, "Items"
, "Status"
, "Actns"
, "Cmds"
, "Cycles"
, "Activity"
, "Log"
]
headers = withAttr robotAttr . txt <$> applyWhen debugRID ("ID" :) headings
robotRows = mkRobotRow <$> robots
mkRobotRow robot =
applyWhen debugRID (idWidget :) cells
Expand Down
13 changes: 3 additions & 10 deletions src/swarm-tui/Swarm/TUI/Model/UI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,42 +62,35 @@ module Swarm.TUI.Model.UI (

import Brick (AttrMap)
import Brick.Focus
import Brick.Widgets.List qualified as BL
import Control.Arrow ((&&&))
import Control.Effect.Accum
import Control.Effect.Lift
import Control.Lens hiding (from, (<.>))
import Data.Bits (FiniteBits (finiteBitSize))
import Data.List.Extra (enumerate)
import Data.Map (Map)
import Data.Map qualified as M
import Data.Sequence (Seq)
import Data.Set (Set)
import Data.Text (Text)
import Data.Text qualified as T
import Swarm.Game.Achievement.Attainment
import Swarm.Game.Achievement.Definitions
import Swarm.Game.Achievement.Persistence
import Swarm.Game.Failure (SystemFailure)
import Swarm.Game.ResourceLoading (getSwarmHistoryPath)
import Swarm.Game.ScenarioInfo (
ScenarioInfoPair,
)
import Swarm.Game.Universe
import Swarm.Game.World.Coords
import Swarm.TUI.Editor.Model
import Swarm.TUI.Inventory.Sorting
import Swarm.TUI.Launch.Model
import Swarm.TUI.Launch.Prep
import Swarm.TUI.Model.DebugOption (DebugOption)
import Swarm.TUI.Model.Dialog
import Swarm.TUI.Model.Dialog.Robot
import Swarm.TUI.Model.Menu
import Swarm.TUI.Model.Name
import Swarm.TUI.Model.Repl
import Swarm.TUI.Model.UI.Gameplay
import Swarm.TUI.View.Attribute.Attr (swarmAttrMap)
import Swarm.Util
import Swarm.Util.Lens (makeLensesExcluding, makeLensesNoSigs)
import Swarm.Util.Lens (makeLensesNoSigs)
import System.Clock

-- * Toplevel UIState definition
Expand Down Expand Up @@ -207,7 +200,7 @@ initUIState speedFactor showMainMenu debug = do
{ _uiModal = Nothing
, _uiGoal = emptyGoalDisplay
, _uiStructure = emptyStructureDisplay
, _uiRobot = emptyRobotDisplay
, _uiRobot = emptyRobotDisplay debug
}
, _uiIsAutoPlay = False
, _uiTiming =
Expand Down

0 comments on commit 77a20b5

Please sign in to comment.