print: comment positions (same line or next line) are now preserved

This commit is contained in:
Simon Michael 2013-09-10 10:32:49 -07:00
parent 7ed0705398
commit f9656a21af
9 changed files with 79 additions and 76 deletions

20
FAQ.md
View File

@ -141,17 +141,12 @@ and ledger if you avoid ledger's most advanced features.
Some ledger features are parsed but ignored, eg: Some ledger features are parsed but ignored, eg:
- automated transactions ( = ... , ~ ... ) - automated transactions ( = ... , ~ ... )
- balance assertions ( AMT1=AMT2 )
- fixed lot prices ( {...} ) - fixed lot prices ( {...} )
- historical prices ( P ... ) - historical prices ( P ... )
Some features are not currently parsed and will cause an error, eg: Some features are not currently parsed and will cause an error, eg
ledger's more recent top-level directives. There can also be subtle
- balance assignments differences in parser behaviour.
- some top level directives like "account"
There can also be subtle differences in parser behaviour, eg
comments may be permissible in different places.
### What other functionality differences are there ? ### What other functionality differences are there ?
@ -184,10 +179,11 @@ comments may be permissible in different places.
ledger print shows only the secondary date with --aux-date, but not ledger print shows only the secondary date with --aux-date, but not
vice versa. vice versa.
- hledger's default commodity directive (D) sets the commodity for - hledger's default commodity directive (D) sets the commodity to be
subsequent commodityless amounts, and sets that commodity's display used for subsequent commodityless amounts, and also sets that
settings if such an amount is the first seen. ledger uses D only for commodity's display settings if such an amount is the first
commodity display settings and for the entry command. seen. ledger uses D only for commodity display settings and for the
entry command.
- hledger generates a description for timelog sessions, instead of - hledger generates a description for timelog sessions, instead of
taking it from the clock-out entry taking it from the clock-out entry

View File

@ -685,10 +685,10 @@ The most basic reporting commands are `print`, `register` and `balance`:
#### print #### print
The print command displays full transactions from the journal file, tidily The print command displays full transactions from the journal file,
formatted and showing all amounts explicitly. The output of print is tidily formatted and showing all amounts explicitly. The output of
always a valid hledger journal, but it might not preserve the original print is always a valid hledger journal, but it does always not
content absolutely intact (eg comments.) preserve all original content exactly (eg directives).
hledger's print command also shows all unit prices in effect, or (with hledger's print command also shows all unit prices in effect, or (with
-B/--cost) shows cost amounts. -B/--cost) shows cost amounts.

View File

@ -6,6 +6,7 @@ title: hledger news
## unreleased ## unreleased
- print: comment positions (same line or next line) are now preserved
- register: `--average/-A` shows a running average, like ledger - register: `--average/-A` shows a running average, like ledger
- queries: `sym:REGEXP` matches (whole) commodity symbols - queries: `sym:REGEXP` matches (whole) commodity symbols
- queries: `amt` now uses the = operator by default, eg amt:50 finds amounts equal to 50 - queries: `amt` now uses the = operator by default, eg amt:50 finds amounts equal to 50

View File

@ -106,19 +106,17 @@ tests_showTransactionUnelided = [
pstatus=True, pstatus=True,
paccount="a", paccount="a",
pamount=Mixed [usd 1, hrs 2], pamount=Mixed [usd 1, hrs 2],
pcomment="pcomment1\npcomment2\n", pcomment="\npcomment2\n",
ptype=RegularPosting, ptype=RegularPosting,
ptags=[("ptag1","val1"),("ptag2","val2")] ptags=[("ptag1","val1"),("ptag2","val2")]
} }
] ]
} }
`gives` unlines [ `gives` unlines [
"2012/05/14=2012/05/15 (code) desc", "2012/05/14=2012/05/15 (code) desc ; tcomment1",
" ;tcomment1",
" ; tcomment2", " ; tcomment2",
" $1.00", " $1.00",
" * a 2.0h", " * a 2.0h",
" ;pcomment1",
" ; pcomment2", " ; pcomment2",
"" ""
] ]
@ -128,29 +126,38 @@ tests_showTransactionUnelided = [
showTransaction' :: Bool -> Transaction -> String showTransaction' :: Bool -> Transaction -> String
showTransaction' elide t = showTransaction' elide t =
unlines $ [descriptionline] unlines $ [descriptionline]
++ multilinecomment ++ newlinecomments
++ (postingsAsLines elide t (tpostings t)) ++ (postingsAsLines elide t (tpostings t))
++ [""] ++ [""]
where where
descriptionline = rstrip $ concat [date, status, code, desc, inlinecomment] descriptionline = rstrip $ concat [date, status, code, desc, samelinecomment]
date = showdate (tdate t) ++ maybe "" showedate (tdate2 t) date = showdate (tdate t) ++ maybe "" showedate (tdate2 t)
showdate = printf "%-10s" . showDate showdate = printf "%-10s" . showDate
showedate = printf "=%s" . showdate showedate = printf "=%s" . showdate
status = if tstatus t then " *" else "" status = if tstatus t then " *" else ""
code = if length (tcode t) > 0 then printf " (%s)" $ tcode t else "" code = if length (tcode t) > 0 then printf " (%s)" $ tcode t else ""
desc = if null d then "" else " " ++ d where d = tdescription t desc = if null d then "" else " " ++ d where d = tdescription t
(inlinecomment, multilinecomment) = commentLines $ tcomment t (samelinecomment, newlinecomments) =
case renderCommentLines (tcomment t) of [] -> ("",[])
c:cs -> (c,cs)
-- Render a transaction or posting's comment as indented, semicolon-prefixed comment lines - -- Render a transaction or posting's comment as indented, semicolon-prefixed comment lines.
-- an inline comment (when it's a single line) or multiple lines. renderCommentLines :: String -> [String]
commentLines :: String -> (String, [String]) renderCommentLines s = case lines s of ("":ls) -> "":map commentprefix ls
commentLines s ls -> map commentprefix ls
| null s = ("", [])
| length ls == 1 = (prefix $ head ls, [])
| otherwise = ("", (prefix $ head ls):(map prefix $ tail ls))
where where
ls = lines s commentprefix = indent . ("; "++)
prefix = indent . (";"++)
-- -- Render a transaction or posting's comment as semicolon-prefixed comment lines -
-- -- an inline (same-line) comment if it's a single line, otherwise multiple indented lines.
-- commentLines' :: String -> (String, [String])
-- commentLines' s
-- | null s = ("", [])
-- | length ls == 1 = (prefix $ head ls, [])
-- | otherwise = ("", (prefix $ head ls):(map prefix $ tail ls))
-- where
-- ls = lines s
-- prefix = indent . (";"++)
postingsAsLines :: Bool -> Transaction -> [Posting] -> [String] postingsAsLines :: Bool -> Transaction -> [Posting] -> [String]
postingsAsLines elide t ps postingsAsLines elide t ps
@ -161,11 +168,13 @@ postingsAsLines elide t ps
postingAsLines :: Bool -> [Posting] -> Posting -> [String] postingAsLines :: Bool -> [Posting] -> Posting -> [String]
postingAsLines elideamount ps p = postingAsLines elideamount ps p =
postinglines postinglines
++ multilinecomment ++ newlinecomments
where where
postinglines = map rstrip $ lines $ concatTopPadded [showacct p, " ", amount, inlinecomment] postinglines = map rstrip $ lines $ concatTopPadded [showacct p, " ", amount, samelinecomment]
amount = if elideamount then "" else showamt (pamount p) amount = if elideamount then "" else showamt (pamount p)
(inlinecomment, multilinecomment) = commentLines $ pcomment p (samelinecomment, newlinecomments) =
case renderCommentLines (pcomment p) of [] -> ("",[])
c:cs -> (c,cs)
showacct p = showacct p =
indent $ showstatus p ++ printf (printf "%%-%ds" w) (showAccountName Nothing (ptype p) (paccount p)) indent $ showstatus p ++ printf (printf "%%-%ds" w) (showAccountName Nothing (ptype p) (paccount p))
where where
@ -188,8 +197,7 @@ tests_postingAsLines = [
} }
`gives` [ `gives` [
" $1.00", " $1.00",
" * a 2.0h", " * a 2.0h ; pcomment1",
" ;pcomment1",
" ; pcomment2", " ; pcomment2",
" ; tag3: val3 " " ; tag3: val3 "
] ]

View File

@ -73,7 +73,7 @@ newtype MixedAmount = Mixed [Amount] deriving (Eq,Ord)
data PostingType = RegularPosting | VirtualPosting | BalancedVirtualPosting data PostingType = RegularPosting | VirtualPosting | BalancedVirtualPosting
deriving (Eq,Show) deriving (Eq,Show)
type Tag = (String, String) type Tag = (String, String) -- ^ A tag name and (possibly empty) value.
data Posting = Posting { data Posting = Posting {
pdate :: Maybe Day, -- ^ this posting's date, if different from the transaction's pdate :: Maybe Day, -- ^ this posting's date, if different from the transaction's
@ -81,9 +81,9 @@ data Posting = Posting {
pstatus :: Bool, pstatus :: Bool,
paccount :: AccountName, paccount :: AccountName,
pamount :: MixedAmount, pamount :: MixedAmount,
pcomment :: String, -- ^ this posting's non-tag comment lines, as a single non-indented string pcomment :: String, -- ^ this posting's comment lines, as a single non-indented multi-line string
ptype :: PostingType, ptype :: PostingType,
ptags :: [Tag], ptags :: [Tag], -- ^ tag names and values, extracted from the comment
pbalanceassertion :: Maybe MixedAmount, -- ^ optional: the expected balance in the account after this posting pbalanceassertion :: Maybe MixedAmount, -- ^ optional: the expected balance in the account after this posting
ptransaction :: Maybe Transaction -- ^ this posting's parent transaction (co-recursive types). ptransaction :: Maybe Transaction -- ^ this posting's parent transaction (co-recursive types).
-- Tying this knot gets tedious, Maybe makes it easier/optional. -- Tying this knot gets tedious, Maybe makes it easier/optional.
@ -100,10 +100,10 @@ data Transaction = Transaction {
tstatus :: Bool, -- XXX tcleared ? tstatus :: Bool, -- XXX tcleared ?
tcode :: String, tcode :: String,
tdescription :: String, tdescription :: String,
tcomment :: String, -- ^ this transaction's non-tag comment lines, as a single non-indented string tcomment :: String, -- ^ this transaction's comment lines, as a single non-indented multi-line string
ttags :: [Tag], ttags :: [Tag], -- ^ tag names and values, extracted from the comment
tpostings :: [Posting], -- ^ this transaction's postings tpostings :: [Posting], -- ^ this transaction's postings
tpreceding_comment_lines :: String tpreceding_comment_lines :: String -- ^ any comment lines immediately preceding this transaction
} deriving (Eq) } deriving (Eq)
data ModifierTransaction = ModifierTransaction { data ModifierTransaction = ModifierTransaction {

View File

@ -817,19 +817,15 @@ emptyline = do many spacenonewline
followingcomment :: GenParser Char JournalContext String followingcomment :: GenParser Char JournalContext String
followingcomment = followingcomment =
-- ptrace "followingcomment" -- ptrace "followingcomment"
(do first <- many spacenonewline >> followingcommentline do samelinecomment <- many spacenonewline >> (try commentline <|> (newline >> return ""))
rest <- many (try (many1 spacenonewline >> followingcommentline)) newlinecomments <- many (try (many1 spacenonewline >> commentline))
return $ unlines $ first:rest return $ unlines $ samelinecomment:newlinecomments
) <|>
do
many spacenonewline >> newline
rest <- many (try (many1 spacenonewline >> followingcommentline))
return $ unlines rest
followingcommentline :: GenParser Char JournalContext String commentline :: GenParser Char JournalContext String
followingcommentline = do commentline = do
-- ptrace "followingcommentline" -- ptrace "commentline"
char ';' char ';'
many spacenonewline
l <- anyChar `manyTill` eolof l <- anyChar `manyTill` eolof
optional newline optional newline
return l return l

View File

@ -31,24 +31,28 @@ hledgerdev -f - print
>>>=0 >>>=0
# 3. print should preserve comments # 3. print should preserve transaction (entry) comments and which line they're on
hledgerdev -f - print hledgerdev -f - print
<<< <<<
; isolated journal comment ; leading journal comment, not preserved
; pre-transaction journal comment ; transaction preceding comment, not preserved
2009/1/1 x ; transaction comment 2009/1/1 x ; transaction same line comment
a 1 ; posting 1 comment ; transaction new line comment
; posting 1 comment 2 a 1 ; posting 1 same line comment
; posting 1 new line comment
b b
; posting 2 comment ; posting 2 new line comment
; post-transaction journal comment ; journal comment right after the transaction, not preserved
; trailing journal comment, not preserved
>>> >>>
2009/01/01 x ; transaction comment 2009/01/01 x ; transaction same line comment
a 1 ; transaction new line comment
; posting 1 comment a 1 ; posting 1 same line comment
; posting 1 comment 2 ; posting 1 new line comment
b -1 ; posting 2 comment b -1
; posting 2 new line comment
>>>2 >>>2
>>>=0 >>>=0

View File

@ -12,14 +12,12 @@ hledgerdev -f - print
; posting-2-tag-2: ; posting-2-tag-2:
; non-metadata: ; non-metadata:
>>> >>>
2010/01/01 2010/01/01 ; txntag1: txn val 1
; txntag1: txn val 1
; txntag2: txn val 2 ; txntag2: txn val 2
a 1 a 1
; posting1tag1: posting 1 val 1 ; posting1tag1: posting 1 val 1
; posting1tag2: ; posting1tag2:
b -1 b -1 ; posting-2-tag-1: posting 2 val 1
; posting-2-tag-1: posting 2 val 1
; posting-2-tag-2: ; posting-2-tag-2:
>>>2 >>>2