mirror of
https://github.com/lancatlin/mdsh.git
synced 2026-11-05 19:30:12 +08:00
feat: add csvToTable function
This commit is contained in:
1 parent
534c27b6ca
commit
6fa452b123
2 files changed
+24
-3
No files matched your search
@@ -34,12 +34,12 @@ params:
|
|||||||
|
|
||||||
### Expense Summary by Category
|
### Expense Summary by Category
|
||||||
|
|
||||||
{{ shell "hledger -f $f bal expenses -b $b -e $e --depth 3 --sort amount --budget" }}
|
{{ raw "hledger -f $f bal expenses -b $b -e $e --depth 3 --sort amount --budget -O csv" | csvToTable }}
|
||||||
|
|
||||||
## Net Income:
|
## Net Income:
|
||||||
|
|
||||||
{{shell "hledger -f $f income -b $b -e $e --monthly"}}
|
{{ raw "hledger -f $f income -b $b -e $e --monthly -O csv" | csvToTable }}
|
||||||
|
|
||||||
## Ending balance:
|
## Ending balance:
|
||||||
|
|
||||||
{{shell "hledger -f $f balancesheet -e $e"}}
|
{{ raw "hledger -f $f balancesheet -e $e -O csv " | csvToTable }}
|
||||||
@@ -2,9 +2,11 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/csv"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,9 +27,28 @@ func (ctx TemplateContext) funcMap() template.FuncMap {
|
|||||||
"sh": ctx.inline,
|
"sh": ctx.inline,
|
||||||
"shell": ctx.codeBlock,
|
"shell": ctx.codeBlock,
|
||||||
"raw": ctx.raw,
|
"raw": ctx.raw,
|
||||||
|
"csvToTable": csvToTable,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func csvToTable(input string) string {
|
||||||
|
r := csv.NewReader(strings.NewReader(input))
|
||||||
|
records, err := r.ReadAll()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("> Failed to parse CSV: %s\n> Input: %s\n", err.Error(), input)
|
||||||
|
}
|
||||||
|
result := ""
|
||||||
|
if len(records) == 0 {
|
||||||
|
return fmt.Sprintf("Error: records is empty: %s\n", input)
|
||||||
|
}
|
||||||
|
result += "| " + strings.Join(records[0], " | ") + " |\n"
|
||||||
|
result += "|" + strings.Repeat(" --- |", len(records[0])) + "\n"
|
||||||
|
for _, row := range records[1:] {
|
||||||
|
result += "| " + strings.Join(row, " | ") + " |\n"
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
func (ctx TemplateContext) OutputFile(meta TemplateMeta) (output io.WriteCloser, err error) {
|
func (ctx TemplateContext) OutputFile(meta TemplateMeta) (output io.WriteCloser, err error) {
|
||||||
if meta.Output == "" {
|
if meta.Output == "" {
|
||||||
return os.Stdout, nil
|
return os.Stdout, nil
|
||||||
|
|||||||
Reference in new issue
Block a user