Skip to content

Commit

Permalink
main: add colwidth option
Browse files Browse the repository at this point in the history
  • Loading branch information
halseth committed Sep 7, 2023
1 parent 040f142 commit 319bddb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
11 changes: 11 additions & 0 deletions cmd/tapsim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/halseth/tapsim/file"
"github.com/halseth/tapsim/output"
"github.com/halseth/tapsim/script"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -87,6 +88,10 @@ func main() {
Name: "tagfile",
Usage: "optional json file map from hex values to human-readable tags",
},
&cli.IntFlag{
Name: "colwidth",
Usage: "output column witdth (default: 40)",
},
},
},
}
Expand Down Expand Up @@ -115,6 +120,12 @@ func parse(cCtx *cli.Context) error {
}

func execute(cCtx *cli.Context) error {

colWidth := cCtx.Int("colwidth")
if colWidth > 0 {
output.ColumnWidth = colWidth
}

var scriptStr []string
scriptFile := cCtx.String("script")
scriptFiles := cCtx.String("scripts")
Expand Down
24 changes: 12 additions & 12 deletions output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/btcsuite/btcd/txscript"
)

var ColumnWidth = 40

func StackToString(stack [][]byte) []string {
var str []string
for i := len(stack) - 1; i >= 0; i-- {
Expand Down Expand Up @@ -58,19 +60,17 @@ func WitnessToString(witness [][]byte) []string {
return str
}

const columnWidth = 40

func ExecutionTable(pc int, script, stack, altStack, witness []string,
tags map[string]string) string {

fullWidth := 4 * (columnWidth + 2)
fullWidth := 4 * (ColumnWidth + 2)
s := strings.Repeat("-", fullWidth)
s += "\n"
s += fmt.Sprintf(" %s| %s| %s| %s\n",
FixedWidth(columnWidth, "script", tags),
FixedWidth(columnWidth, "stack", tags),
FixedWidth(columnWidth, "alt stack", tags),
FixedWidth(columnWidth, "witness", tags),
FixedWidth(ColumnWidth, "script", tags),
FixedWidth(ColumnWidth, "stack", tags),
FixedWidth(ColumnWidth, "alt stack", tags),
FixedWidth(ColumnWidth, "witness", tags),
)
s += strings.Repeat("-", fullWidth)
s += "\n"
Expand Down Expand Up @@ -105,10 +105,10 @@ func ExecutionTable(pc int, script, stack, altStack, witness []string,

s += fmt.Sprintf("%s%s| %s| %s| %s\n",
pcC,
FixedWidth(columnWidth, scr, tags),
FixedWidth(columnWidth, stk, tags),
FixedWidth(columnWidth, alt, tags),
FixedWidth(columnWidth, wit, tags),
FixedWidth(ColumnWidth, scr, tags),
FixedWidth(ColumnWidth, stk, tags),
FixedWidth(ColumnWidth, alt, tags),
FixedWidth(ColumnWidth, wit, tags),
)

if scr == "" && stk == "" && alt == "" && wit == "" {
Expand All @@ -118,7 +118,7 @@ func ExecutionTable(pc int, script, stack, altStack, witness []string,
row++
}

s += strings.Repeat("-", 4*(columnWidth+2))
s += strings.Repeat("-", 4*(ColumnWidth+2))
s += "\n"

return s
Expand Down

0 comments on commit 319bddb

Please sign in to comment.