internal hamming widget generator should return AttrNames

This commit is contained in:
Jonathan Lamothe 2024-07-23 16:18:40 -04:00
parent 4ea2185243
commit 120d60bc32
3 changed files with 141 additions and 258 deletions

View File

@ -24,9 +24,17 @@ License along with this program. If not, see
|-} |-}
module Hamming.App.Widgets (hammingW) where module Hamming.App.Widgets (
-- * Hamming Widget
hammingW,
-- ** Attribute Names
hammingAttr,
hammingMarginAttr,
hammingCheckAttr,
hammingZeroAttr,
) where
import Brick.AttrMap (attrName) import Brick.AttrMap (AttrName, attrName)
import Brick.Types (Widget) import Brick.Types (Widget)
import Brick.Widgets.Core (hBox, str, vBox, withAttr) import Brick.Widgets.Core (hBox, str, vBox, withAttr)
@ -39,15 +47,19 @@ hammingW
-> Widget ResName -> Widget ResName
hammingW = withAttr (attrName "hamming") hammingW = withAttr (attrName "hamming")
. vBox . vBox
. map . map (hBox . map (\(a, w) -> withAttr a $ str [w]))
( hBox
. map
( \(ns, v) -> foldr
(withAttr . attrName)
(str [v])
ns
)
)
. hammingW' . hammingW'
hammingAttr :: AttrName
hammingAttr = attrName "hamming"
hammingMarginAttr :: AttrName
hammingMarginAttr = hammingAttr <> marginAttr
hammingCheckAttr :: AttrName
hammingCheckAttr = hammingAttr <> checkAttr
hammingZeroAttr :: AttrName
hammingZeroAttr = hammingAttr <> zeroAttr
--jl --jl

View File

@ -22,27 +22,48 @@ You should have received a copy of the GNU Affero General Public
License along with this program. If not, see License along with this program. If not, see
<https://www.gnu.org/licenses/>. <https://www.gnu.org/licenses/>.
**IMPORTANT NOTE:** This module is for internal use *only* and should be
considered subject to change at any time.
|-} |-}
{-# LANGUAGE TupleSections #-} {-# LANGUAGE TupleSections #-}
module Hamming.App.Widgets.Internal (hammingW') where module Hamming.App.Widgets.Internal (
-- * Hamming Code Widget
hammingW',
-- ** Attribute Names
marginAttr,
checkAttr,
zeroAttr,
) where
import Data.Bits ((.&.)) import Data.Bits ((.&.))
import Lens.Micro (_2, (^.), (%~)) import Lens.Micro (_2, (^.), (%~))
import Brick.AttrMap (AttrName, attrName)
import Hamming.App.Types import Hamming.App.Types
hammingW' :: AppState -> [[([String], Char)]] hammingW' :: AppState -> [[(AttrName, Char)]]
hammingW' state = let hammingW' state = let
header = ([], ' ') : map (["margin"],) ['0'..'3'] header = (mempty, ' ') : map (marginAttr,) ['0'..'3']
body = hammingBody state body = hammingBody state
in header : zipWith in header : zipWith
(\n row -> (["margin"], n) : row) (\n row -> (marginAttr, n) : row)
['0'..'3'] ['0'..'3']
body body
hammingBody :: AppState -> [[([String], Char)]] marginAttr :: AttrName
marginAttr = attrName "margin"
checkAttr :: AttrName
checkAttr = attrName "check"
zeroAttr :: AttrName
zeroAttr = checkAttr <> attrName "zero"
hammingBody :: AppState -> [[(AttrName, Char)]]
hammingBody state = let hammingBody state = let
code = state^.hammingCode code = state^.hammingCode
in map in map
@ -54,25 +75,25 @@ hammingBody state = let
) )
) )
) )
[ [ ( ["zero", "check"], 0x0001 ) [ [ ( zeroAttr, 0x0001 )
, ( ["check"], 0x0002 ) , ( checkAttr, 0x0002 )
, ( ["check"], 0x0004 ) , ( checkAttr, 0x0004 )
, ( [], 0x0008 ) , ( mempty, 0x0008 )
] ]
, [ ( ["check"], 0x0010 ) , [ ( checkAttr, 0x0010 )
, ( [], 0x0020 ) , ( mempty, 0x0020 )
, ( [], 0x0040 ) , ( mempty, 0x0040 )
, ( [], 0x0080 ) , ( mempty, 0x0080 )
] ]
, [ ( ["check"], 0x0100 ) , [ ( checkAttr, 0x0100 )
, ( [], 0x0200 ) , ( mempty, 0x0200 )
, ( [], 0x0400 ) , ( mempty, 0x0400 )
, ( [], 0x0800 ) , ( mempty, 0x0800 )
] ]
, [ ( [], 0x1000 ) , [ ( mempty, 0x1000 )
, ( [], 0x2000 ) , ( mempty, 0x2000 )
, ( [], 0x4000 ) , ( mempty, 0x4000 )
, ( [], 0x8000 ) , ( mempty, 0x8000 )
] ]
] ]

View File

@ -65,249 +65,99 @@ hammingW'Spec = describe "hammingW'" $ mapM_
] where ] where
mkState c = initialState & hammingCode .~ c mkState c = initialState & hammingCode .~ c
allZero = allZero =
[ [ ( [] [ [ ( mempty, ' ' )
, ' ' , ( marginAttr, '0' )
) , ( marginAttr, '1' )
, ( ["margin"] , ( marginAttr, '2' )
, '0' , ( marginAttr, '3' )
)
, ( ["margin"]
, '1'
)
, ( ["margin"]
, '2'
)
, ( ["margin"]
, '3'
)
] ]
, [ ( ["margin"] , [ ( marginAttr, '0' )
, '0' , ( zeroAttr, '0' )
) , ( checkAttr, '0' )
, ( ["zero", "check"] , ( checkAttr, '0' )
, '0' , ( mempty, '0' )
)
, ( ["check"]
, '0'
)
, ( ["check"]
, '0'
)
, ( []
, '0'
)
] ]
, [ ( ["margin"] , [ ( marginAttr, '1' )
, '1' , ( checkAttr, '0' )
) , ( mempty, '0' )
, ( ["check"] , ( mempty, '0' )
, '0' , ( mempty, '0' )
)
, ( []
, '0'
)
, ( []
, '0'
)
, ( []
, '0'
)
] ]
, [ ( ["margin"] , [ ( marginAttr, '2' )
, '2' , ( checkAttr, '0' )
) , ( mempty, '0' )
, ( ["check"] , ( mempty, '0' )
, '0' , ( mempty, '0' )
)
, ( []
, '0'
)
, ( []
, '0'
)
, ( []
, '0'
)
] ]
, [ ( ["margin"] , [ ( marginAttr, '3' )
, '3' , ( mempty, '0' )
) , ( mempty, '0' )
, ( [] , ( mempty, '0' )
, '0' , ( mempty, '0' )
)
, ( []
, '0'
)
, ( []
, '0'
)
, ( []
, '0'
)
] ]
] ]
allOne = allOne =
[ [ ( [] [ [ ( mempty, ' ' )
, ' ' , ( marginAttr, '0' )
) , ( marginAttr, '1' )
, ( ["margin"] , ( marginAttr, '2' )
, '0' , ( marginAttr, '3' )
)
, ( ["margin"]
, '1'
)
, ( ["margin"]
, '2'
)
, ( ["margin"]
, '3'
)
] ]
, [ ( ["margin"] , [ ( marginAttr, '0' )
, '0' , ( zeroAttr, '1' )
) , ( checkAttr, '1' )
, ( ["zero", "check"] , ( checkAttr, '1' )
, '1' , ( mempty, '1' )
)
, ( ["check"]
, '1'
)
, ( ["check"]
, '1'
)
, ( []
, '1'
)
] ]
, [ ( ["margin"] , [ ( marginAttr, '1' )
, '1' , ( checkAttr, '1' )
) , ( mempty, '1' )
, ( ["check"] , ( mempty, '1' )
, '1' , ( mempty, '1' )
)
, ( []
, '1'
)
, ( []
, '1'
)
, ( []
, '1'
)
] ]
, [ ( ["margin"] , [ ( marginAttr, '2' )
, '2' , ( checkAttr, '1' )
) , ( mempty, '1' )
, ( ["check"] , ( mempty, '1' )
, '1' , ( mempty, '1' )
)
, ( []
, '1'
)
, ( []
, '1'
)
, ( []
, '1'
)
] ]
, [ ( ["margin"] , [ ( marginAttr, '3' )
, '3' , ( mempty, '1' )
) , ( mempty, '1' )
, ( [] , ( mempty, '1' )
, '1' , ( mempty, '1' )
)
, ( []
, '1'
)
, ( []
, '1'
)
, ( []
, '1'
)
] ]
] ]
arbitrary = arbitrary =
[ [ ( [] [ [ ( mempty, ' ' )
, ' ' , ( marginAttr, '0' )
) , ( marginAttr, '1' )
, ( ["margin"] , ( marginAttr, '2' )
, '0' , ( marginAttr, '3' )
)
, ( ["margin"]
, '1'
)
, ( ["margin"]
, '2'
)
, ( ["margin"]
, '3'
)
] ]
, [ ( ["margin"] , [ ( marginAttr, '0' )
, '0' , ( zeroAttr, '0' )
) , ( checkAttr, '1' )
, ( ["zero", "check"] , ( checkAttr, '0' )
, '0' , ( mempty, '1' )
)
, ( ["check"]
, '1'
)
, ( ["check"]
, '0'
)
, ( []
, '1'
)
] ]
, [ ( ["margin"] , [ ( marginAttr, '1' )
, '1' , ( checkAttr, '1' )
) , ( mempty, '0' )
, ( ["check"] , ( mempty, '1' )
, '1' , ( mempty, '0' )
)
, ( []
, '0'
)
, ( []
, '1'
)
, ( []
, '0'
)
] ]
, [ ( ["margin"] , [ ( marginAttr, '2' )
, '2' , ( checkAttr, '0' )
) , ( mempty, '0' )
, ( ["check"] , ( mempty, '1' )
, '0' , ( mempty, '1' )
)
, ( []
, '0'
)
, ( []
, '1'
)
, ( []
, '1'
)
] ]
, [ ( ["margin"] , [ ( marginAttr, '3' )
, '3' , ( mempty, '1' )
) , ( mempty, '1' )
, ( [] , ( mempty, '0' )
, '1' , ( mempty, '0' )
)
, ( []
, '1'
)
, ( []
, '0'
)
, ( []
, '0'
)
] ]
] ]