print: fix wide char support, add tests (#242)
The print command wasn't lining up amounts with wide chars in account names, fixed it properly this time. Transaction and Posting's Show instances should also be wide-char-aware now.
This commit is contained in:
		
							parent
							
								
									e6d8a9d1bc
								
							
						
					
					
						commit
						3b40edba9c
					
				| @ -54,7 +54,6 @@ import Data.Ord | |||||||
| import Data.Time.Calendar | import Data.Time.Calendar | ||||||
| import Safe | import Safe | ||||||
| import Test.HUnit | import Test.HUnit | ||||||
| import Text.Printf |  | ||||||
| 
 | 
 | ||||||
| import Hledger.Utils | import Hledger.Utils | ||||||
| import Hledger.Data.Types | import Hledger.Data.Types | ||||||
| @ -90,12 +89,12 @@ showPosting p@Posting{paccount=a,pamount=amt,ptype=t} = | |||||||
|     where |     where | ||||||
|       ledger3ishlayout = False |       ledger3ishlayout = False | ||||||
|       acctnamewidth = if ledger3ishlayout then 25 else 22 |       acctnamewidth = if ledger3ishlayout then 25 else 22 | ||||||
|       showaccountname = printf ("%-"++(show acctnamewidth)++"s") . bracket . elideAccountName width |       showaccountname = fitString (Just acctnamewidth) Nothing False False . bracket . elideAccountName width | ||||||
|       (bracket,width) = case t of |       (bracket,width) = case t of | ||||||
|                           BalancedVirtualPosting -> (\s -> "["++s++"]", acctnamewidth-2) |                           BalancedVirtualPosting -> (\s -> "["++s++"]", acctnamewidth-2) | ||||||
|                           VirtualPosting -> (\s -> "("++s++")", acctnamewidth-2) |                           VirtualPosting -> (\s -> "("++s++")", acctnamewidth-2) | ||||||
|                           _ -> (id,acctnamewidth) |                           _ -> (id,acctnamewidth) | ||||||
|       showamount = padleft 12 . showMixedAmount |       showamount = padLeftWide 12 . showMixedAmount | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| showComment :: String -> String | showComment :: String -> String | ||||||
|  | |||||||
| @ -136,9 +136,7 @@ showTransaction' elide t = | |||||||
|               ++ [""] |               ++ [""] | ||||||
|     where |     where | ||||||
|       descriptionline = rstrip $ concat [date, status, code, desc, samelinecomment] |       descriptionline = rstrip $ concat [date, status, code, desc, samelinecomment] | ||||||
|       date = showdate (tdate t) ++ maybe "" showedate (tdate2 t) |       date = showDate (tdate t) ++ maybe "" (("="++) . showDate) (tdate2 t) | ||||||
|       showdate = printf "%-10s" . showDate |  | ||||||
|       showedate = printf "=%s" . showdate |  | ||||||
|       status | tstatus t == Cleared = " *" |       status | tstatus t == Cleared = " *" | ||||||
|              | tstatus t == Pending = " !" |              | tstatus t == Pending = " !" | ||||||
|              | otherwise            = "" |              | otherwise            = "" | ||||||
| @ -183,12 +181,13 @@ postingAsLines elideamount ps p = | |||||||
|       case renderCommentLines (pcomment p) of []   -> ("",[]) |       case renderCommentLines (pcomment p) of []   -> ("",[]) | ||||||
|                                               c:cs -> (c,cs) |                                               c:cs -> (c,cs) | ||||||
|     showacct p = |     showacct p = | ||||||
|       indent $ showstatus p ++ printf (printf "%%-%ds" w) (showAccountName Nothing (ptype p) (paccount p)) |       indent $ | ||||||
|  |         showstatus p ++ fitString (Just w) Nothing False True (showAccountName Nothing (ptype p) (paccount p)) | ||||||
|         where |         where | ||||||
|           showstatus p = if pstatus p == Cleared then "* " else "" |           showstatus p = if pstatus p == Cleared then "* " else "" | ||||||
|           w = maximum $ map (length . paccount) ps |           w = maximum $ map (strWidth . paccount) ps | ||||||
|     showamt = |     showamt = | ||||||
|         padleft 12 . showMixedAmount |         padLeftWide 12 . showMixedAmount | ||||||
| 
 | 
 | ||||||
| tests_postingAsLines = [ | tests_postingAsLines = [ | ||||||
|    "postingAsLines" ~: do |    "postingAsLines" ~: do | ||||||
|  | |||||||
| @ -266,7 +266,7 @@ fitto w h s = intercalate "\n" $ take h $ rows ++ repeat blankline | |||||||
| 
 | 
 | ||||||
| -- Functions below treat wide (eg CJK) characters as double-width. | -- Functions below treat wide (eg CJK) characters as double-width. | ||||||
| 
 | 
 | ||||||
| -- | General-purpose single-line string layout function. | -- | General-purpose wide-char-aware single-line string layout function. | ||||||
| -- It can left- or right-pad a short string to a minimum width. | -- It can left- or right-pad a short string to a minimum width. | ||||||
| -- It can left- or right-clip a long string to a maximum width, optionally inserting an ellipsis (the third argument). | -- It can left- or right-clip a long string to a maximum width, optionally inserting an ellipsis (the third argument). | ||||||
| -- It clips and pads on the right when the fourth argument is true, otherwise on the left. | -- It clips and pads on the right when the fourth argument is true, otherwise on the left. | ||||||
|  | |||||||
| @ -1,31 +1,67 @@ | |||||||
| # alignment calculations should handle wide characters | # ALl output should preserve alignment etc. when showing wide characters. | ||||||
|  | # XXX For a better test, the sample journals should have commodity symbols. | ||||||
|  | # XXX How to test hledger-ui ? | ||||||
| 
 | 
 | ||||||
| # 1. register, account name | # 1. | ||||||
| hledger -f - register | hledger -f chinese.journal print | ||||||
| <<< |  | ||||||
| 1/1 |  | ||||||
|   知  1 |  | ||||||
|   b |  | ||||||
| >>> | >>> | ||||||
| 2015/01/01                      知                               1             1 | 2014/01/01 transaction 1 | ||||||
|                                 b                               -1             0 |     㐀                1 | ||||||
|  |     㐀:㐁            -1 | ||||||
|  | 
 | ||||||
|  | 2014/01/02 transaction 2 | ||||||
|  |     㐀:㐁:㐂                1 | ||||||
|  |     㐀:㐁:㐂:㐃            -1 | ||||||
|  | 
 | ||||||
|  | 2014/01/03 transaction 3 | ||||||
|  |     㐀:㐁:㐂:㐃:㐄             1 | ||||||
|  |     㐀                        -1 | ||||||
|  | 
 | ||||||
| >>>=0 | >>>=0 | ||||||
| 
 | 
 | ||||||
| # # 2. balance, commodity symbol | # 2. | ||||||
| # hledger -f - balance | hledger -f chinese.journal register --width 80 | ||||||
| # <<< | >>> | ||||||
| # 1/1 | 2014/01/01 transaction 1        㐀                               1             1 | ||||||
| #   a   知1 |                                 㐀:㐁                           -1             0 | ||||||
| #   b   $-1 | 2014/01/02 transaction 2        㐀:㐁:㐂                         1             1 | ||||||
| # >>> |                                 㐀:㐁:㐂:㐃                     -1             0 | ||||||
| #                  知1  a | 2014/01/03 transaction 3        㐀:㐁:㐂:㐃:㐄                   1             1 | ||||||
| #                  $-1  b |                                 㐀                              -1             0 | ||||||
| # -------------------- | >>>=0 | ||||||
| #                  $-1 | 
 | ||||||
| #                  知1 | # 3. | ||||||
| # >>>=0 | hledger -f chinese.journal balance | ||||||
|  | >>> | ||||||
|  |                    0  㐀:㐁 | ||||||
|  |                    1    㐂 | ||||||
|  |                    0      㐃 | ||||||
|  |                    1        㐄 | ||||||
|  | -------------------- | ||||||
|  |                    0 | ||||||
|  | >>>=0 | ||||||
|  | 
 | ||||||
|  | # 4. | ||||||
|  | hledger -f chinese.journal balance -Y | ||||||
|  | >>> | ||||||
|  | Balance changes in 2014: | ||||||
|  | 
 | ||||||
|  |                 ||  2014  | ||||||
|  | ================++======= | ||||||
|  |  㐀:㐁          ||    -1  | ||||||
|  |  㐀:㐁:㐂       ||     1  | ||||||
|  |  㐀:㐁:㐂:㐃    ||    -1  | ||||||
|  |  㐀:㐁:㐂:㐃:㐄 ||     1  | ||||||
|  | ----------------++------- | ||||||
|  |                 ||     0  | ||||||
|  | 
 | ||||||
|  | >>>=0 | ||||||
|  | 
 | ||||||
|  | # 5. | ||||||
|  | 
 | ||||||
|  | # 6. | ||||||
|  | 
 | ||||||
|  | # 7. | ||||||
|  | 
 | ||||||
|  | # 8. | ||||||
| 
 | 
 | ||||||
| # import Text.Data.ICU.Char |  | ||||||
| # case property EastAsianWidth c of |  | ||||||
| #   Wide -> 2 |  | ||||||
| #   _    -> 1 |  | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user