-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config file on ~/.config/rsi-break/settings.ini
Also update logic.
- Loading branch information
1 parent
2a77d7f
commit 402bab3
Showing
7 changed files
with
95 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module RsiBreak.Actions (getOrCreateConfigFile, storeSettingsOnConfigFile) where | ||
|
||
import Control.Monad (unless) | ||
import Data.Ini.Config.Bidir | ||
import Data.Text.IO qualified as TIO (readFile, writeFile) | ||
import RsiBreak.Model.Settings | ||
import System.Directory | ||
import System.FilePath ((</>)) | ||
|
||
getOrCreateConfigFile :: IO TimerSetting | ||
getOrCreateConfigFile = do | ||
dir <- getXdgDirectory XdgConfig "rsi-break" | ||
let file = dir </> "settings.ini" | ||
settingsFileExist <- doesFileExist file | ||
unless settingsFileExist $ | ||
createNewInitialSettings dir file | ||
settingFileContent <- TIO.readFile file | ||
let eIniSettings = parseIni settingFileContent defaultIni | ||
case eIniSettings of | ||
Left _err -> do | ||
removeDirectoryRecursive dir | ||
createNewInitialSettings dir file | ||
pure defSetting | ||
Right ini' -> pure (getIniValue ini') | ||
where | ||
createNewInitialSettings dir file = do | ||
createDirectory dir | ||
TIO.writeFile file (serializeIni defaultIni) | ||
|
||
defaultIni :: Ini TimerSetting | ||
defaultIni = | ||
setIniUpdatePolicy | ||
(defaultUpdatePolicy{updateGeneratedCommentPolicy = CommentPolicyAddFieldComment}) | ||
(ini defSetting timerSettingSpec) | ||
|
||
storeSettingsOnConfigFile :: TimerSetting -> IO () | ||
storeSettingsOnConfigFile updatedSettings = do | ||
let updatedIni = updateIni updatedSettings defaultIni | ||
dir <- getXdgDirectory XdgConfig "rsi-break" | ||
let file = dir </> "settings.ini" | ||
TIO.writeFile file (serializeIni updatedIni) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
module RsiBreak.Model.Settings where | ||
|
||
import Control.Lens (makeLenses) | ||
import Data.Ini.Config.Bidir | ||
import RsiBreak.Model.Minutes (Minutes) | ||
|
||
data TimerSetting = TimerSetting | ||
{ _workInterval :: Minutes | ||
, _restInterval :: Minutes | ||
} | ||
deriving (Eq, Show) | ||
|
||
$(makeLenses 'TimerSetting) | ||
|
||
defSetting :: TimerSetting | ||
defSetting = TimerSetting 20 10 | ||
|
||
timerSettingSpec :: IniSpec TimerSetting () | ||
timerSettingSpec = | ||
section "TimerSetting" $ do | ||
workInterval | ||
.= field "workInterval" number | ||
& comment ["The desired work interval in minutes"] | ||
restInterval | ||
.= field "restInterval" number | ||
& comment ["The desired rest interval in minutes"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters