Skip to content

Commit

Permalink
Use CSV for balance reports + add graphs (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhantac authored Aug 18, 2024
1 parent ca293d1 commit a2c6461
Show file tree
Hide file tree
Showing 21 changed files with 648 additions and 348 deletions.
25 changes: 0 additions & 25 deletions accounting/command.go

This file was deleted.

76 changes: 0 additions & 76 deletions accounting/hledger.go

This file was deleted.

27 changes: 0 additions & 27 deletions accounting/register_data.go

This file was deleted.

32 changes: 22 additions & 10 deletions config.debug.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,46 @@
"startDate": "2021",
"period": "yearly",
"reports": [
{
"name": "income statement",
"cmd": "hledger incomestatement -f data/hledger.journal "
},
{
"name": "assets",
"cmd": "hledger balance type:a -f data/hledger.journal "
"cmd": "hledger balance type:a -f data/hledger.journal"
},
{
"name": "expenses-lock",
"cmd": "hledger balance type:x",
"locked": true
},
{
"name": "expenses",
"cmd": "hledger balance type:x -f data/hledger.journal "
"cmd": "hledger balance type:x"
},
{
"name": "revenue",
"cmd": "hledger balance type:r -f data/hledger.journal "
"cmd": "hledger balance type:r -f data/hledger.journal"
},
{
"name": "liabilities",
"cmd": "hledger balance type:l -f data/hledger.journal "
},
{
"name": "income statement",
"cmd": "hledger incomestatement -f data/hledger.journal ",
"locked": true
"cmd": "hledger balance type:l -f data/hledger.journal"
},
{
"name": "balance sheet",
"cmd": "hledger balancesheet -f data/hledger.journal "
},
{
"name": "register",
"cmd": "hledger register -f data/hledger.journal"
},
{
"name": "accounts",
"cmd": "hledger accounts -f data/hledger.journal",
"cmd": "hledger accounts -f data/hledger.journal"
},
{
"name": "commodities",
"cmd": "hledger commodities",
"locked": true
}
]
Expand Down
41 changes: 40 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,43 @@
{
"startDate": "2024",
"period": "monthly"
"period": "monthly",
"reports": [
{
"name": "assets",
"cmd": "hledger balance type:a"
},
{
"name": "expenses",
"cmd": "hledger balance type:x"
},
{
"name": "revenue",
"cmd": "hledger balance type:r"
},
{
"name": "liabilities",
"cmd": "hledger balance type:l"
},
{
"name": "income statement",
"cmd": "hledger incomestatement "
},
{
"name": "balance sheet",
"cmd": "hledger balancesheet "
},
{
"name": "accounts",
"cmd": "hledger accounts "
},
{
"name": "register",
"cmd": "hledger register"
},
{
"name": "commodities",
"cmd": "hledger commodities",
"locked": true
}
]
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ require (
github.com/charmbracelet/bubbles v0.14.0
github.com/charmbracelet/bubbletea v0.26.4
github.com/charmbracelet/lipgloss v0.6.0
github.com/guptarohit/asciigraph v0.7.1
github.com/matryer/is v1.4.1
github.com/siddhantac/hledger v0.9.3
)

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/guptarohit/asciigraph v0.7.1 h1:K+JWbRc04XEfv8BSZgNuvhCmpbvX4+9NYd/UxXVnAuk=
github.com/guptarohit/asciigraph v0.7.1/go.mod h1:dYl5wwK4gNsnFf9Zp+l06rFiDZ5YtXM6x7SRWZ3KGag=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/matryer/is v1.4.1 h1:55ehd8zaGABKLXQUe2awZ99BD/PTc2ls+KV/dXphgEQ=
github.com/matryer/is v1.4.1/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand Down
11 changes: 1 addition & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@ package main
import (
"flag"
"log"
"puffin/accounting"
"puffin/ui"

hlgo "github.com/siddhantac/hledger"
)

func main() {
var journalFile, hledgerExecutable string
var isDebug bool
var configFile string

flag.StringVar(&configFile, "cfg", "", "config file")
flag.StringVar(&journalFile, "file", "", "journal filename")
flag.StringVar(&hledgerExecutable, "exe", "hledger", "hledger executable")
flag.BoolVar(&isDebug, "debug", false, "run in debug mode")
flag.Parse()

Expand All @@ -29,8 +23,5 @@ func main() {
}
}

hl := hlgo.New(hledgerExecutable, journalFile)
hlcmd := accounting.NewHledgerCmd(hl)

ui.Start(hlcmd, cfg, isDebug)
ui.Start(cfg, isDebug)
}
60 changes: 49 additions & 11 deletions ui/command.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,60 @@
package ui

import (
"io"
"puffin/accounting"
"fmt"
"log"
"os/exec"
"strings"

"github.com/siddhantac/hledger"
)

func runCommand(cmd string) func(options hledger.Options) string {
return func(options hledger.Options) string {
buf, err := accounting.RunCommand(cmd, options)
if err != nil {
return err.Error()
}
b, err := io.ReadAll(buf)
func runCommand(cmd string) func(id int, options hledger.Options) content {
return func(id int, options hledger.Options) content {
c := content{id: id}

args := strings.Split(cmd, " ")

opts := options.Build()
args = append(args, opts...)
log.Printf("cmd: %v", args)

cmd := exec.Command(args[0], args[1:]...)
result, err := cmd.CombinedOutput()
if err != nil {
return err.Error()
log.Printf("error: %v: %s", err, string(result))
c.err = fmt.Errorf("%s\n%v", string(result), err)
return c
}
return string(b)

c.msg = string(result)
return c
}
}

func detectCommand(id int, report Report, dataTransformers []dataTransformer) ContentModel {
if report.Locked {
pg := newPager(id, report.Name, report.Locked, runCommand(report.Cmd), cmdUnknown)
return newMainView(id, report.Name, pg)
}

args := strings.Split(report.Cmd, " ")
switch args[1] {
case "balance", "bal":
log.Printf("create tableGraph")
tg := newTableGraph(id, report.Name, report.Locked, runCommand(report.Cmd), cmdBalance, dataTransformers)
return newMainView(id, report.Name, tg)
case "register", "reg":
log.Printf("create table")
tbl := newTable("register", nil, id, runCommand(report.Cmd), report.Locked, cmdRegister, nil)
return newMainView(id, report.Name, tbl)
case "accounts", "acc":
log.Printf("create pager")
pg := newPager(id, report.Name, report.Locked, runCommand(report.Cmd), cmdAccounts)
return newMainView(id, report.Name, pg)
default:
log.Printf("create pager")
pg := newPager(id, report.Name, report.Locked, runCommand(report.Cmd), cmdUnknown)
return newMainView(id, report.Name, pg)
}
}
Loading

0 comments on commit a2c6461

Please sign in to comment.