Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Don´t print humanized format in single-command mode. #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ func bigFloat(s string) *decimal.Big {

// commafWithDigits idea comes from the humanize library, but was modified to
// work with decimal numbers.
func commafWithDigits(v *decimal.Big, decimals int) string {
func commafWithDigits(n *decimal.Big, decimals int) string {
// Make a copy so we won't modify the original value (passed by pointer).
v := big().Copy(n)

buf := &bytes.Buffer{}
if v.Sign() < 0 {
buf.Write([]byte{'-'})
Expand Down Expand Up @@ -86,7 +89,7 @@ func stripTrailingDigits(s string, digits int) string {

// formatNumber formats the number using base and decimals. For bases different
// than 10, non-integer floating numbers are truncated.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to add the parameter to the comment.

func formatNumber(ctx decimal.Context, n *decimal.Big, base, decimals int) string {
func formatNumber(ctx decimal.Context, n *decimal.Big, base, decimals int, single bool) string {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"single" doesn't make much sense inside the function. Maybe we should replace it inside the function with "raw", meaning "raw printing" (no formatting) or something similar. In fact, I think we should create two functions, "format" and "formatRaw" that would call this function with raw set to false and true, respectively. Then we could change the rest of the code to call the right function, instead of adding one more mysterious parameter to the call.

// Print NaN without suffix numbers.
if n.IsNaN(0) {
return strings.TrimRight(fmt.Sprint(n), "0123456789")
Expand Down Expand Up @@ -134,8 +137,8 @@ func formatNumber(ctx decimal.Context, n *decimal.Big, base, decimals int) strin
buf.WriteString(fmt.Sprintf("0x%x%s", n64, suffix))
default:
h := commafWithDigits(n, decimals)
// Only print humanized format when it differs from original value.
if h != clean {
// Only print humanized format when it differs from original value, and not in single-command mode
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Try to keep comments formatted to 80 columns.

if h != clean && !single {
suffix = " (" + h + ")"
}
buf.WriteString(clean + suffix)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func calc(stack *stackType, cmd string) error {

if autoprint {
if single {
fmt.Println(stack.top()) // plain print to stdout
fmt.Println(formatNumber(ctx, stack.top(), ops.base, ops.decimals, true)) // plain print to stdout
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Avoid inline comments (hard to read, scroll all the way to the right, and no capitalization in this case.)

} else {
stack.printTop(ctx, ops.base, ops.decimals) // pretty print to terminal
}
Expand Down
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func TestFormatNumber(t *testing.T) {
{16, big().Add(bigUint(0xff), bigFloat("0.5")).SetSignbit(true), "-0xff (truncated from -255.5)"},
}
for _, tt := range casetests {
got := formatNumber(ctx, tt.input, tt.base, 6)
got := formatNumber(ctx, tt.input, tt.base, 6, false)
if got != tt.want {
t.Fatalf("diff: base: %d, input: %v, want: %q, got: %q", tt.base, tt.input, tt.want, got)
}
Expand Down
4 changes: 2 additions & 2 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (x *stackType) top() *decimal.Big {

// printTop displays the top of the stack using the base indicated.
func (x *stackType) printTop(ctx decimal.Context, base, decimals int) {
color.Cyan("= %s", formatNumber(ctx, x.top(), base, decimals))
color.Cyan("= %s", formatNumber(ctx, x.top(), base, decimals, false))
}

// print displays the contents of the stack using the base indicated.
Expand All @@ -67,6 +67,6 @@ func (x *stackType) print(ctx decimal.Context, base, decimals int) {
case last - 1:
tag = " y"
}
fmt.Printf("%s: %s\n", tag, formatNumber(ctx, x.list[ix], base, decimals))
fmt.Printf("%s: %s\n", tag, formatNumber(ctx, x.list[ix], base, decimals, false))
}
}
Loading