From 53e949fec3c3068d704c06c4d7270debc11fa3dc Mon Sep 17 00:00:00 2001 From: jk <47693+sectore@users.noreply.github.com> Date: Tue, 26 Nov 2024 13:27:52 +0100 Subject: [PATCH] option to ignore previous stored app data --- README.md | 4 +++- src/TUI.hs | 42 +++++++++++++++++++++++++++++------------- src/TUI/Config.hs | 30 ++++++++++++++++-------------- src/TUI/Storage.hs | 22 +++++++++++++++++++++- 4 files changed, 69 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 2070787..3f30c67 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ soon tick-tock-tui --help Usage: tick-tock-tui [-m|--mempool URL] [-r|--refresh SECONDS] - [-s|--storage DIRECTORY] + [-s|--storage DIRECTORY] [-i|--ignore] TUI app to handle Bitcoin data provided by Mempool: fees, blocks and price converter. @@ -31,6 +31,8 @@ Available options: (default: 180) -s,--storage DIRECTORY Folder to store application state (default: "~/.local/state/tick-tock-tui") + -i,--ignore Ignore previous stored application state to use + default data instead. -h,--help Show this help text ``` diff --git a/src/TUI.hs b/src/TUI.hs index 20e81b4..a9f39d0 100644 --- a/src/TUI.hs +++ b/src/TUI.hs @@ -16,12 +16,19 @@ import Control.Concurrent.STM.TChan (TChan) import Control.Monad (forever) import Control.Monad.IO.Class (liftIO) import Control.Monad.Reader (ReaderT (..)) +import Data.Functor ((<&>)) +import Data.Maybe (fromMaybe) import Data.Time.LocalTime (getCurrentTimeZone) +import System.Directory ( + XdgDirectory (..), + getXdgDirectory, + ) import TUI.Attr (tuiAttrMap) import TUI.Config (Config (..), getConfig) import TUI.Events (appEvent, startEvent) import qualified TUI.Service.Mempool as M import TUI.Service.Types +import TUI.Storage (defaultStorage) import qualified TUI.Storage as STG import TUI.Types import TUI.Utils (customMainWithInterval, fps) @@ -31,9 +38,18 @@ import TUI.Widgets.Converter (initialConverterData, mkConverterForm) run :: IO () run = do -- get `Config` from args - config <- getConfig - -- get `TUIStorage` from file - mStorage <- liftIO $ STG.load $ cfgStorageDirectory config + config <- + getXdgDirectory XdgState "tick-tock-tui" + >>= getConfig (MempoolUrl "https://mempool.space") + storage <- + -- by ignoring previous stored data, return default data. + if cfgIgnoreStorage config + then pure defaultStorage + else do + -- Try to get `TUIStorage` data from file or use default data + STG.load (cfgStorageDirectory config) + <&> fromMaybe defaultStorage + -- out channel to send messages from TUI app outCh <- newTChanIO -- in channel to send messages into TUI app @@ -54,17 +70,17 @@ run = do initialState <- getCurrentTimeZone >>= \tz -> - let initialFiat = maybe USD stgSelectedFiat mStorage - initialBitcoin = maybe BTC stgSelectedBitcoin mStorage - initialBtcAmount = maybe (Amount 0.00021) stgBtcAmount mStorage + let initialFiat = stgSelectedFiat storage + initialBitcoin = stgSelectedBitcoin storage + initialBtcAmount = stgBtcAmount storage in pure TUIState { timeZone' = tz - , _currentView = maybe FeesView stgCurrentView mStorage - , _converterForm = mkConverterForm (initialConverterData initialFiat initialBitcoin initialBtcAmount) + , _currentView = stgCurrentView storage + , _converterForm = mkConverterForm $ initialConverterData initialFiat initialBitcoin initialBtcAmount , _prevConverterForm = Nothing - , _animate = maybe False stgAnimate mStorage - , _extraInfo = maybe False stgExtraInfo mStorage + , _animate = stgAnimate storage + , _extraInfo = stgExtraInfo storage , _tick = 0 , _fetchTick = 0 , _lastFetchTick = 0 @@ -72,9 +88,9 @@ run = do , _prices = NotAsked , _fees = NotAsked , _block = NotAsked - , _selectedFiat = maybe initialFiat stgSelectedFiat mStorage - , _selectedBitcoin = maybe initialBitcoin stgSelectedBitcoin mStorage - , _showMenu = maybe False stgShowMenu mStorage + , _selectedFiat = initialFiat + , _selectedBitcoin = initialBitcoin + , _showMenu = stgShowMenu storage } -- run TUI app (lastState, _) <- customMainWithInterval interval (Just inCh) (theApp outCh config) initialState diff --git a/src/TUI/Config.hs b/src/TUI/Config.hs index b4ff585..c80a9cb 100644 --- a/src/TUI/Config.hs +++ b/src/TUI/Config.hs @@ -1,21 +1,19 @@ module TUI.Config where import Options.Applicative -import System.Directory ( - XdgDirectory (..), - getXdgDirectory, - ) + import TUI.Types (MempoolUrl (..)) data Config = Config { cfgMempoolUrl :: !MempoolUrl , cfgReloadInterval :: !Int , cfgStorageDirectory :: !FilePath + , cfgIgnoreStorage :: !Bool } deriving (Show) -parser :: FilePath -> Parser Config -parser defaultStorageDirectory = +parser :: FilePath -> MempoolUrl -> Parser Config +parser defaultStorageDirectory (MempoolUrl defaultMempoolUrl) = Config <$> ( MempoolUrl <$> strOption @@ -23,7 +21,7 @@ parser defaultStorageDirectory = <> short 'm' <> metavar "URL" <> help "Mempool URL" - <> value "https://mempool.space" + <> value defaultMempoolUrl <> showDefault ) ) @@ -45,16 +43,20 @@ parser defaultStorageDirectory = <> value defaultStorageDirectory <> showDefault ) + <*> switch + ( long "ignore" + <> short 'i' + <> help "Ignore previous stored application state to use default data instead." + ) -getConfig :: IO Config -getConfig = do - defaultStorageDirectory <- getXdgDirectory XdgState "tick-tock-tui" - execParser (opts defaultStorageDirectory) +getConfig :: MempoolUrl -> FilePath -> IO Config +getConfig defaultMempoolUrl defaultStorageDirectory = do + execParser opts where - opts :: FilePath -> ParserInfo Config - opts path = + opts :: ParserInfo Config + opts = info - (parser path <**> helper) + (parser defaultStorageDirectory defaultMempoolUrl <**> helper) ( fullDesc <> progDesc "TUI app to handle Bitcoin data provided by Mempool: fees, blocks and price converter." <> header "tick-tock-tui" diff --git a/src/TUI/Storage.hs b/src/TUI/Storage.hs index da573b6..ea3f66e 100644 --- a/src/TUI/Storage.hs +++ b/src/TUI/Storage.hs @@ -1,4 +1,10 @@ -module TUI.Storage (save, load, toStorage, getStoragePath) where +module TUI.Storage ( + save, + load, + toStorage, + getStoragePath, + defaultStorage, +) where import Brick.Forms (formState) import qualified Data.Aeson as A @@ -9,8 +15,22 @@ import System.Directory ( doesFileExist, ) import System.FilePath (()) +import TUI.Service.Types (Amount (..), Bitcoin (..), Fiat (..)) import TUI.Types +-- | Default instance +defaultStorage :: TUIStorage +defaultStorage = + TUIStorage + { stgCurrentView = FeesView + , stgAnimate = False + , stgExtraInfo = False + , stgSelectedFiat = USD + , stgShowMenu = False + , stgSelectedBitcoin = BTC + , stgBtcAmount = Amount 0.00021 + } + stgVersion :: Int stgVersion = 1