Skip to content

Commit

Permalink
Update brick to version 1.10
Browse files Browse the repository at this point in the history
 * Event handlers work on a state monad now.
 * The IsString instance for AttrName was removed.
  • Loading branch information
u-quark committed Nov 8, 2023
1 parent cf9a9e8 commit 05cc286
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 189 deletions.
7 changes: 3 additions & 4 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
flake = false;
};
brick = {
url = "github:jtdaugherty/brick?rev=3d34ef115631700ab6d46088357f7e2ab13c424e";
url = "github:jtdaugherty/brick";
flake = false;
};
base16-schemes = {
Expand Down
67 changes: 36 additions & 31 deletions src/GG/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@

module GG.State where

import Brick (EventM)
import qualified Brick.Widgets.List as L
import Control.Lens (Traversal', ix, over, set, (&),
(.~), (?~), (^?), (^?!), _Just)
import Control.Lens (Lens', (.=), (%=), (?=),
ix, singular, _Just, use, preuse)
import Control.Monad (forM_)
import Data.Generics.Product.Fields (field)
import Data.Time (ZonedTime)
import qualified Data.Vector as Vec
Expand Down Expand Up @@ -102,32 +104,35 @@ initState :: Config -> G.Repository -> G.Commit -> Reference -> [Commit] -> Time
initState config repository contCommit head l timers =
State {commitList=(L.list CommitListUI (Vec.fromList l) 1), openCommit=Nothing, notification=Nothing, ..}

updateRepoState :: G.Commit -> Reference -> [Commit] -> State -> State
updateRepoState commit head_ l =
set (field @"commitList" . L.listElementsL) (Vec.fromList l) .
set (field @"contCommit") commit . set (field @"head") head_

addMoreCommits :: [Commit] -> G.Commit -> State -> State
addMoreCommits moreCommits newContCommit =
over (field @"commitList" . L.listElementsL) (Vec.++ Vec.fromList moreCommits) .
set (field @"contCommit") newContCommit

updateCommitsPos :: Int -> State -> State
updateCommitsPos pos = set (field @"commitList" . L.listSelectedL) (Just pos)

commitL :: Int -> Traversal' State Commit
commitL i = field @"commitList" . L.listElementsL . ix i

openCommitDetails :: Int -> (G.DiffStats, G.DiffInfo) -> State -> State
openCommitDetails i (diffStats_, diffInfo_) state =
state' & commitL i .~ openCommit_ & field @"openCommit" ?~ OpenCommit i openCommit_ diffStats_ diffInfo_
where
openCommit_ = (state ^?! commitL i) & field @"open" .~ True
oldOpenCommitIxM = state ^? (field @"openCommit" . _Just . field @"openCommitIndex")
state' = maybe state (\i' -> state & (commitL i' . field @"open") .~ False) oldOpenCommitIxM

closeCommitDetails :: State -> State
closeCommitDetails state =
maybe state (\i -> state & (commitL i . field @"open") .~ False) oldOpenCommitIxM & field @"openCommit" .~ Nothing
where
oldOpenCommitIxM = state ^? (field @"openCommit" . _Just . field @"openCommitIndex")
type ModifyState a = EventM Name State a

updateRepoState :: G.Commit -> Reference -> [Commit] -> ModifyState ()
updateRepoState commit head_ l = do
field @"commitList" . L.listElementsL .= Vec.fromList l
field @"contCommit" .= commit
field @"head" .= head_

addMoreCommits :: [Commit] -> G.Commit -> ModifyState ()
addMoreCommits moreCommits newContCommit = do
field @"commitList" . L.listElementsL %= flip (Vec.++) (Vec.fromList moreCommits)
field @"contCommit" .= newContCommit

updateCommitsPos :: Int -> ModifyState ()
updateCommitsPos pos = field @"commitList" . L.listSelectedL .= Just pos

commitL :: Int -> Lens' State Commit
commitL i = field @"commitList" . L.listElementsL . singular (ix i)

openCommitDetails :: Int -> (G.DiffStats, G.DiffInfo) -> ModifyState ()
openCommitDetails i (diffStats, diffInfo) = do
oldOpenCommitIxM <- preuse (field @"openCommit" . _Just . field @"openCommitIndex")
forM_ oldOpenCommitIxM (\i' -> commitL i' . field @"open" .= False)
commitL i . field @"open" .= True
openCommit <- use (commitL i)
field @"openCommit" ?= OpenCommit i openCommit diffStats diffInfo

closeCommitDetails :: ModifyState ()
closeCommitDetails = do
oldOpenCommitIxM <- preuse (field @"openCommit" . _Just . field @"openCommitIndex")
forM_ oldOpenCommitIxM (\i' -> commitL i' . field @"open" .= False)
field @"openCommit" .= Nothing
9 changes: 5 additions & 4 deletions src/GG/Timers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
You should have received a copy of the GNU General Public License
along with gg. If not, see <https://www.gnu.org/licenses/>.
-}

module GG.Timers
( Timers
, initTimers
Expand Down Expand Up @@ -70,12 +71,12 @@ initTimers tickEvent resolution bChan = do
B.writeBChan bChan tickEvent
threadDelay resolution

tickEventHandler ::
(Ord name, Eq name, Ord timerName) => Timers state name timerName -> state -> B.EventM name (B.Next state)
tickEventHandler timers s = do
tickEventHandler :: (Ord a, Ord timerName) => Timers s a timerName -> B.EventM a s ()
tickEventHandler timers = do
s <- B.get
(s', ns') <- liftIO $ timersHandler timers s
B.put s'
mapM_ B.invalidateCacheEntry ns'
B.continue s'

timersHandler :: (Ord name, Eq name, Ord timerName) => Timers state name timerName -> state -> IO (state, [name])
timersHandler (Timers timersMVar blocker) state = do
Expand Down
Loading

0 comments on commit 05cc286

Please sign in to comment.