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:
- automated transactions ( = ... , ~ ... )
- balance assertions ( AMT1=AMT2 )
- fixed lot prices ( {...} )
- historical prices ( P ... )
Some features are not currently parsed and will cause an error, eg:
- balance assignments
- some top level directives like "account"
There can also be subtle differences in parser behaviour, eg
comments may be permissible in different places.
Some features are not currently parsed and will cause an error, eg
ledger's more recent top-level directives. There can also be subtle
differences in parser behaviour.
### 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
vice versa.
- hledger's default commodity directive (D) sets the commodity for
subsequent commodityless amounts, and sets that commodity's display
settings if such an amount is the first seen. ledger uses D only for
commodity display settings and for the entry command.
- hledger's default commodity directive (D) sets the commodity to be
used for subsequent commodityless amounts, and also sets that
commodity's display settings if such an amount is the first
seen. ledger uses D only for commodity display settings and for the
entry command.
- hledger generates a description for timelog sessions, instead of
taking it from the clock-out entry

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -38,7 +38,7 @@
# 4. handle conditions assigning multiple fields
rm -rf t.rules$$; printf 'fields date, description, amount\ndate-format %%d/%%Y/%%m\ncurrency $\naccount1 assets:myacct\nif Flubber\n account2 acct\n comment cmt' >t.rules$$; echo '10/2009/09,Flubber Co,50' | hledgerdev -f- print --rules-file t.rules$$; rm -rf t.rules$$
>>>
2009/09/10 Flubber Co ;cmt
2009/09/10 Flubber Co ; cmt
acct $-50
assets:myacct $50

View File

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