Skip to content

Commit 82642b4

Browse files
committed
ui, web: Create journal on startup if it doesn't exist
1 parent 748420c commit 82642b4

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

hledger-ui/Hledger/UI/Main.hs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ Copyright (c) 2007-2015 Simon Michael <simon@joyful.com>
44
Released under GPL version 3 or later.
55
-}
66
{-# LANGUAGE CPP #-}
7-
{-# LANGUAGE OverloadedStrings #-}
87
{-# LANGUAGE MultiParamTypeClasses #-}
8+
{-# LANGUAGE MultiWayIf #-}
9+
{-# LANGUAGE OverloadedStrings #-}
910

1011
module Hledger.UI.Main where
1112

@@ -30,6 +31,7 @@ import System.Exit
3031
import System.Directory
3132
import System.FilePath
3233
import System.FSNotify
34+
import Text.Printf (printf)
3335
import Brick
3436

3537
#if MIN_VERSION_brick(0,16,0)
@@ -66,15 +68,14 @@ main = do
6668
{ inputopts_ = (inputopts_ copts) { auto_ = True }
6769
, reportopts_ = (reportopts_ copts) { forecast_ = True }
6870
}
69-
70-
-- when (debug_ $ cliopts_ opts) $ printf "%s\n" prognameandversion >> printf "opts: %s\n" (show opts)
71-
run $ opts { cliopts_ = copts' }
72-
where
73-
run opts
74-
| "help" `inRawOpts` (rawopts_ $ cliopts_ opts) = putStr (showModeUsage uimode) >> exitSuccess
75-
| "version" `inRawOpts` (rawopts_ $ cliopts_ opts) = putStrLn prognameandversion >> exitSuccess
76-
| "binary-filename" `inRawOpts` (rawopts_ $ cliopts_ opts) = putStrLn (binaryfilename progname)
77-
| otherwise = withJournalDo (cliopts_ opts) (runBrickUi opts)
71+
when (debug_ copts' > 0) $ printf "%s\n" prognameandversion >> printf "opts: %s\n" (show opts)
72+
if
73+
| "help" `inRawOpts` rawopts_ copts' -> putStr (showModeUsage uimode) >> exitSuccess
74+
| "version" `inRawOpts` rawopts_ copts' -> putStrLn prognameandversion >> exitSuccess
75+
| "binary-filename" `inRawOpts` rawopts_ copts' -> putStrLn (binaryfilename progname)
76+
| otherwise -> do
77+
mapM_ ensureJournalFileExists =<< journalFilePathFromOpts copts'
78+
withJournalDo copts' (runBrickUi $ opts { cliopts_ = copts' })
7879

7980
runBrickUi :: UIOpts -> Journal -> IO ()
8081
runBrickUi uopts@UIOpts{cliopts_=copts@CliOpts{inputopts_=_iopts,reportopts_=ropts}} j = do

hledger-web/Hledger/Web/Main.hs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE MultiWayIf #-}
12
{-# LANGUAGE OverloadedStrings #-}
23
{-|
34
@@ -33,8 +34,15 @@ import Hledger.Web.WebOptions
3334
hledgerWebMain :: IO ()
3435
hledgerWebMain = do
3536
opts <- getHledgerWebOpts
36-
when (debug_ (cliopts_ opts) > 0) $ printf "%s\n" prognameandversion >> printf "opts: %s\n" (show opts)
37-
runWith opts
37+
let copts = cliopts_ opts
38+
when (debug_ copts > 0) $ printf "%s\n" prognameandversion >> printf "opts: %s\n" (show opts)
39+
if
40+
| "help" `inRawOpts` rawopts_ copts -> putStr (showModeUsage webmode) >> exitSuccess
41+
| "version" `inRawOpts` rawopts_ copts -> putStrLn prognameandversion >> exitSuccess
42+
| "binary-filename" `inRawOpts` rawopts_ copts -> putStrLn (binaryfilename progname)
43+
| otherwise -> do
44+
mapM_ ensureJournalFileExists =<< journalFilePathFromOpts copts
45+
withJournalDo copts (web opts)
3846

3947
hledgerWebDev :: IO (Int, Application)
4048
hledgerWebDev =
@@ -44,13 +52,6 @@ hledgerWebDev =
4452
Yesod.Default.Config.loadConfig
4553
(configSettings Development) {csParseExtra = parseExtra}
4654

47-
runWith :: WebOpts -> IO ()
48-
runWith opts
49-
| "help" `inRawOpts` rawopts_ (cliopts_ opts) = putStr (showModeUsage webmode) >> exitSuccess
50-
| "version" `inRawOpts` rawopts_ (cliopts_ opts) = putStrLn prognameandversion >> exitSuccess
51-
| "binary-filename" `inRawOpts` rawopts_ (cliopts_ opts) = putStrLn (binaryfilename progname)
52-
| otherwise = withJournalDo (cliopts_ opts) (web opts)
53-
5455
-- | The web command.
5556
web :: WebOpts -> Journal -> IO ()
5657
web opts j = do

0 commit comments

Comments
 (0)