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.Widgets.Core (hBox, str, vBox, withAttr)
@ -39,15 +47,19 @@ hammingW
-> Widget ResName
hammingW = withAttr (attrName "hamming")
. vBox
. map
( hBox
. map
( \(ns, v) -> foldr
(withAttr . attrName)
(str [v])
ns
)
)
. map (hBox . map (\(a, w) -> withAttr a $ str [w]))
. hammingW'
hammingAttr :: AttrName
hammingAttr = attrName "hamming"
hammingMarginAttr :: AttrName
hammingMarginAttr = hammingAttr <> marginAttr
hammingCheckAttr :: AttrName
hammingCheckAttr = hammingAttr <> checkAttr
hammingZeroAttr :: AttrName
hammingZeroAttr = hammingAttr <> zeroAttr
--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
<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 #-}
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 Lens.Micro (_2, (^.), (%~))
import Brick.AttrMap (AttrName, attrName)
import Hamming.App.Types
hammingW' :: AppState -> [[([String], Char)]]
hammingW' :: AppState -> [[(AttrName, Char)]]
hammingW' state = let
header = ([], ' ') : map (["margin"],) ['0'..'3']
header = (mempty, ' ') : map (marginAttr,) ['0'..'3']
body = hammingBody state
in header : zipWith
(\n row -> (["margin"], n) : row)
(\n row -> (marginAttr, n) : row)
['0'..'3']
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
code = state^.hammingCode
in map
@ -54,25 +75,25 @@ hammingBody state = let
)
)
)
[ [ ( ["zero", "check"], 0x0001 )
, ( ["check"], 0x0002 )
, ( ["check"], 0x0004 )
, ( [], 0x0008 )
[ [ ( zeroAttr, 0x0001 )
, ( checkAttr, 0x0002 )
, ( checkAttr, 0x0004 )
, ( mempty, 0x0008 )
]
, [ ( ["check"], 0x0010 )
, ( [], 0x0020 )
, ( [], 0x0040 )
, ( [], 0x0080 )
, [ ( checkAttr, 0x0010 )
, ( mempty, 0x0020 )
, ( mempty, 0x0040 )
, ( mempty, 0x0080 )
]
, [ ( ["check"], 0x0100 )
, ( [], 0x0200 )
, ( [], 0x0400 )
, ( [], 0x0800 )
, [ ( checkAttr, 0x0100 )
, ( mempty, 0x0200 )
, ( mempty, 0x0400 )
, ( mempty, 0x0800 )
]
, [ ( [], 0x1000 )
, ( [], 0x2000 )
, ( [], 0x4000 )
, ( [], 0x8000 )
, [ ( mempty, 0x1000 )
, ( mempty, 0x2000 )
, ( mempty, 0x4000 )
, ( mempty, 0x8000 )
]
]

View File

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