Skip to content

Commit

Permalink
option to ignore previous stored app data
Browse files Browse the repository at this point in the history
  • Loading branch information
sectore committed Nov 26, 2024
1 parent 185e19c commit 53e949f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 29 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
```

Expand Down
42 changes: 29 additions & 13 deletions src/TUI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -54,27 +70,27 @@ 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
, maxFetchTick' = cfgReloadInterval config * fps -- seconds -> fps
, _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
Expand Down
30 changes: 16 additions & 14 deletions src/TUI/Config.hs
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
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
( long "mempool"
<> short 'm'
<> metavar "URL"
<> help "Mempool URL"
<> value "https://mempool.space"
<> value defaultMempoolUrl
<> showDefault
)
)
Expand All @@ -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"
Expand Down
22 changes: 21 additions & 1 deletion src/TUI/Storage.hs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down

0 comments on commit 53e949f

Please sign in to comment.