Skip to content

Commit

Permalink
move bal and reg to top level
Browse files Browse the repository at this point in the history
  • Loading branch information
howeyc committed May 1, 2021
1 parent 36d34c5 commit 6d9a09b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 44 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ Example usage:
```sh
export LEDGER_FILE="ledger.dat"

ledger print bal
ledger print bal Cash
ledger print reg
ledger print ledger
ledger bal
ledger bal Cash
ledger reg
ledger print
ledger stats
ledger lint

ledger -f other_ledger.dat print reg
ledger -f other_ledger.dat reg
```

### Import Transactions
Expand Down
37 changes: 25 additions & 12 deletions ledger/cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"log"
"math/big"
"os"
"sort"
Expand Down Expand Up @@ -84,8 +85,16 @@ func cliTransactions() ([]*ledger.Transaction, error) {

// printCmd represents the print command
var printCmd = &cobra.Command{
Use: "print",
Short: "Print balance, register, or ledger.",
Use: "print [account-substring-filter]...",
Short: "Print transactions in ledger file format",
Run: func(cmd *cobra.Command, args []string) {
generalLedger, err := cliTransactions()
if err != nil {
log.Fatalln(err)
}

PrintLedger(generalLedger, args, columnWidth)
},
}

func init() {
Expand All @@ -94,12 +103,11 @@ func init() {
var startDate, endDate time.Time
startDate = time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local)
endDate = time.Now().Add(time.Hour * 24)

printCmd.PersistentFlags().StringVarP(&startString, "begin-date", "b", startDate.Format(transactionDateFormat), "Begin date of transaction processing.")
printCmd.PersistentFlags().StringVarP(&endString, "end-date", "e", endDate.Format(transactionDateFormat), "End date of transaction processing.")
printCmd.PersistentFlags().StringVar(&payeeFilter, "payee", "", "Filter output to payees that contain this string.")
printCmd.PersistentFlags().IntVar(&columnWidth, "columns", 80, "Set a column width for output.")
printCmd.PersistentFlags().BoolVar(&columnWide, "wide", false, "Wide output (same as --columns=132).")
printCmd.Flags().StringVarP(&startString, "begin-date", "b", startDate.Format(transactionDateFormat), "Begin date of transaction processing.")
printCmd.Flags().StringVarP(&endString, "end-date", "e", endDate.Format(transactionDateFormat), "End date of transaction processing.")
printCmd.Flags().StringVar(&payeeFilter, "payee", "", "Filter output to payees that contain this string.")
printCmd.Flags().IntVar(&columnWidth, "columns", 80, "Set a column width for output.")
printCmd.Flags().BoolVar(&columnWide, "wide", false, "Wide output (same as --columns=132).")
}

// PrintBalances prints out account balances formatted to a window set to a width of columns.
Expand All @@ -125,25 +133,30 @@ func PrintBalances(accountList []*ledger.Account, printZeroBalances bool, depth,

// PrintTransaction prints a transaction formatted to fit in specified column width.
func PrintTransaction(trans *ledger.Transaction, columns int) {
WriteTransaction(os.Stdout, trans, columns)
}

// WriteTransaction writes a transaction formatted to fit in specified column width.
func WriteTransaction(w io.Writer, trans *ledger.Transaction, columns int) {
for _, c := range trans.Comments {
fmt.Println(c)
fmt.Fprintln(w, c)
}

// Print accounts sorted by name
sort.Slice(trans.AccountChanges, func(i, j int) bool {
return trans.AccountChanges[i].Name < trans.AccountChanges[j].Name
})

fmt.Printf("%s %s\n", trans.Date.Format(transactionDateFormat), trans.Payee)
fmt.Fprintf(w, "%s %s\n", trans.Date.Format(transactionDateFormat), trans.Payee)
for _, accChange := range trans.AccountChanges {
outBalanceString := accChange.Balance.FloatString(displayPrecision)
spaceCount := columns - 4 - utf8.RuneCountInString(accChange.Name) - utf8.RuneCountInString(outBalanceString)
if spaceCount < 1 {
spaceCount = 1
}
fmt.Printf(" %s%s%s\n", accChange.Name, strings.Repeat(" ", spaceCount), outBalanceString)
fmt.Fprintf(w, " %s%s%s\n", accChange.Name, strings.Repeat(" ", spaceCount), outBalanceString)
}
fmt.Println("")
fmt.Fprintln(w, "")
}

// PrintLedger prints all transactions as a formatted ledger file.
Expand Down
12 changes: 11 additions & 1 deletion ledger/cmd/printBalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"strings"
"time"

"github.com/howeyc/ledger"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -38,7 +39,16 @@ var balanceCmd = &cobra.Command{
}

func init() {
printCmd.AddCommand(balanceCmd)
rootCmd.AddCommand(balanceCmd)

var startDate, endDate time.Time
startDate = time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local)
endDate = time.Now().Add(time.Hour * 24)
balanceCmd.Flags().StringVarP(&startString, "begin-date", "b", startDate.Format(transactionDateFormat), "Begin date of transaction processing.")
balanceCmd.Flags().StringVarP(&endString, "end-date", "e", endDate.Format(transactionDateFormat), "End date of transaction processing.")
balanceCmd.Flags().StringVar(&payeeFilter, "payee", "", "Filter output to payees that contain this string.")
balanceCmd.Flags().IntVar(&columnWidth, "columns", 80, "Set a column width for output.")
balanceCmd.Flags().BoolVar(&columnWide, "wide", false, "Wide output (same as --columns=132).")

balanceCmd.Flags().StringVar(&period, "period", "", "Split output into periods (Monthly,Quarterly,SemiYearly,Yearly).")
balanceCmd.Flags().BoolVar(&showEmptyAccounts, "empty", false, "Show empty (zero balance) accounts.")
Expand Down
25 changes: 0 additions & 25 deletions ledger/cmd/printLedger.go

This file was deleted.

12 changes: 11 additions & 1 deletion ledger/cmd/printRegister.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"strings"
"time"

"github.com/howeyc/ledger"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -37,7 +38,16 @@ var registerCmd = &cobra.Command{
}

func init() {
printCmd.AddCommand(registerCmd)
rootCmd.AddCommand(registerCmd)

var startDate, endDate time.Time
startDate = time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local)
endDate = time.Now().Add(time.Hour * 24)
registerCmd.Flags().StringVarP(&startString, "begin-date", "b", startDate.Format(transactionDateFormat), "Begin date of transaction processing.")
registerCmd.Flags().StringVarP(&endString, "end-date", "e", endDate.Format(transactionDateFormat), "End date of transaction processing.")
registerCmd.Flags().StringVar(&payeeFilter, "payee", "", "Filter output to payees that contain this string.")
registerCmd.Flags().IntVar(&columnWidth, "columns", 80, "Set a column width for output.")
registerCmd.Flags().BoolVar(&columnWide, "wide", false, "Wide output (same as --columns=132).")

registerCmd.Flags().StringVar(&period, "period", "", "Split output into periods (Monthly,Quarterly,SemiYearly,Yearly).")
}

0 comments on commit 6d9a09b

Please sign in to comment.