feat(calc,ui): complete tight-mode HEX entry with cross-base chaining and result formatting
- Add formatResultEntryForBase + update Equals/Mod/ChangeSign so committed results format in the active display base (e.g. HEX shows "C8", "1B0" not decimal). - Robust per-line centering for dynamic A-F hexRow and keypad rows under display (prevents split key borders/decorations on bottom row). - Updated test expectation for HEX result digits. - Enables validated cross-base flow: DEC entry, Tab to HEX, continue op, see result in active base, Tab back converts displayed value correctly. - style: gofmt -s all touched sources (ui_test.go, version_test.go etc.). - ci: add lint and cross targets to Makefile. - cross produces the 6 platform binaries expected by release.yml on v* tags. - lint target for local parity (CI continues to use golangci-lint-action). This is the v0.3.0 release-ready state (user-validated: 100 DEC -> Tab HEX +64 = C8 -> Tab DEC 200).
This commit is contained in:
parent
b78f6d7c2b
commit
88338a2446
22
Makefile
22
Makefile
@ -1,4 +1,4 @@
|
|||||||
.PHONY: test test-short test-cover build install clean help
|
.PHONY: test test-short test-cover build install clean lint cross help
|
||||||
|
|
||||||
SHELL := /bin/bash
|
SHELL := /bin/bash
|
||||||
|
|
||||||
@ -51,3 +51,23 @@ help:
|
|||||||
@echo " make test - run all tests with race detector"
|
@echo " make test - run all tests with race detector"
|
||||||
@echo " make test-short - short tests with race"
|
@echo " make test-short - short tests with race"
|
||||||
@echo " make clean - remove build/"
|
@echo " make clean - remove build/"
|
||||||
|
@echo " make lint - run golangci-lint (requires golangci-lint in PATH; CI uses action)"
|
||||||
|
@echo " make cross - cross-compile for linux/darwin/windows (amd64/arm64) for releases"
|
||||||
|
|
||||||
|
lint:
|
||||||
|
@command -v golangci-lint >/dev/null 2>&1 || { echo >&2 "golangci-lint not found in PATH (install via 'go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest' or rely on CI)"; exit 1; }
|
||||||
|
golangci-lint run ./...
|
||||||
|
|
||||||
|
cross: deps
|
||||||
|
@mkdir -p build
|
||||||
|
@echo "Cross-compiling gralculator (VERSION=$(VERSION) COMMIT=$(COMMIT))..."
|
||||||
|
@for os in linux darwin windows; do \
|
||||||
|
for arch in amd64 arm64; do \
|
||||||
|
ext=""; if [ "$$os" = "windows" ]; then ext=".exe"; fi; \
|
||||||
|
out="build/gralculator-$$os-$$arch$$ext"; \
|
||||||
|
echo " $$os/$$arch -> $$out"; \
|
||||||
|
CGO_ENABLED=0 GOOS=$$os GOARCH=$$arch go build -trimpath -ldflags "$(LDFLAGS)" -o "$$out" . || exit 1; \
|
||||||
|
done; \
|
||||||
|
done
|
||||||
|
@echo "✅ Cross builds complete:"
|
||||||
|
@ls -lh build/gralculator-* 2>/dev/null | cat || true
|
||||||
|
|||||||
@ -189,7 +189,7 @@ func (e *Engine) EnterDigit(d rune) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Default (DEC): decimal digits only (validate to keep entry clean)
|
// Default (DEC): decimal digits only (validate to keep entry clean)
|
||||||
if (d >= '0' && d <= '9') {
|
if d >= '0' && d <= '9' {
|
||||||
if e.entry == "0" || e.entry == "" {
|
if e.entry == "0" || e.entry == "" {
|
||||||
e.entry = string(d)
|
e.entry = string(d)
|
||||||
} else {
|
} else {
|
||||||
@ -229,7 +229,7 @@ func (e *Engine) Equals() {
|
|||||||
right := e.currentNumericValue()
|
right := e.currentNumericValue()
|
||||||
result := e.applyPending(e.pendingLeft, right, e.pendingOp)
|
result := e.applyPending(e.pendingLeft, right, e.pendingOp)
|
||||||
e.accumulator = result
|
e.accumulator = result
|
||||||
e.entry = formatResultEntry(result)
|
e.entry = formatResultEntryForBase(result, e.base)
|
||||||
e.pendingOp = ""
|
e.pendingOp = ""
|
||||||
e.pendingLeft = 0
|
e.pendingLeft = 0
|
||||||
}
|
}
|
||||||
@ -255,7 +255,7 @@ func (e *Engine) applyPending(left, right float64, op string) float64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// formatResultEntry produces a clean entry string for a committed result.
|
// formatResultEntry produces a clean entry string for a committed result (DEC).
|
||||||
func formatResultEntry(v float64) string {
|
func formatResultEntry(v float64) string {
|
||||||
if math.IsInf(v, 0) || math.IsNaN(v) {
|
if math.IsInf(v, 0) || math.IsNaN(v) {
|
||||||
return "0"
|
return "0"
|
||||||
@ -272,13 +272,40 @@ func formatResultEntry(v float64) string {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// formatResultEntryForBase produces the result string formatted for the current display base.
|
||||||
|
// For HEX/BIN/OCT this yields the appropriate integer digit string (so that FormatForDisplay's
|
||||||
|
// raw-entry short-circuit for HEX and currentNumericValue's base-aware parsing both see correct
|
||||||
|
// digits for the result). This makes results appear in the active base (e.g. 1FFE not 8190 in HEX).
|
||||||
|
func formatResultEntryForBase(v float64, b Base) string {
|
||||||
|
if b == BaseDEC {
|
||||||
|
return formatResultEntry(v)
|
||||||
|
}
|
||||||
|
if math.IsInf(v, 0) || math.IsNaN(v) {
|
||||||
|
return "0"
|
||||||
|
}
|
||||||
|
i := int64(v)
|
||||||
|
if i < 0 {
|
||||||
|
i = 0
|
||||||
|
}
|
||||||
|
switch b {
|
||||||
|
case BaseHEX:
|
||||||
|
return strings.ToUpper(strconv.FormatInt(i, 16))
|
||||||
|
case BaseBIN:
|
||||||
|
return strconv.FormatInt(i, 2)
|
||||||
|
case BaseOCT:
|
||||||
|
return strconv.FormatInt(i, 8)
|
||||||
|
default:
|
||||||
|
return formatResultEntry(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Mod performs immediate modulo using the current entry as the right operand
|
// Mod performs immediate modulo using the current entry as the right operand
|
||||||
// (useful for a dedicated MOD button that acts like = for modulo).
|
// (useful for a dedicated MOD button that acts like = for modulo).
|
||||||
func (e *Engine) Mod() {
|
func (e *Engine) Mod() {
|
||||||
right := e.currentNumericValue()
|
right := e.currentNumericValue()
|
||||||
result := math.Mod(e.accumulator, right)
|
result := math.Mod(e.accumulator, right)
|
||||||
e.accumulator = result
|
e.accumulator = result
|
||||||
e.entry = formatResultEntry(result)
|
e.entry = formatResultEntryForBase(result, e.base)
|
||||||
e.pendingOp = ""
|
e.pendingOp = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -307,7 +334,7 @@ func (e *Engine) ChangeSign() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
e.accumulator = -e.accumulator
|
e.accumulator = -e.accumulator
|
||||||
e.entry = formatResultEntry(e.accumulator)
|
e.entry = formatResultEntryForBase(e.accumulator, e.base)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Backspace removes the last character from the current entry.
|
// Backspace removes the last character from the current entry.
|
||||||
|
|||||||
@ -274,8 +274,9 @@ func TestEnterDigit_HEX(t *testing.T) {
|
|||||||
e.SetOperator("+")
|
e.SetOperator("+")
|
||||||
e.EnterDigit('1') // simple commit
|
e.EnterDigit('1') // simple commit
|
||||||
e.Equals()
|
e.Equals()
|
||||||
if got := e.FormatForDisplay(); got != "432" {
|
// Result is formatted according to the current base (HEX), so 431+1 = 1B0 hex not decimal 432.
|
||||||
t.Errorf("after hex commit +1: want 432, got %s", got)
|
if got := e.FormatForDisplay(); got != "1B0" {
|
||||||
|
t.Errorf("after hex commit +1: want 1B0, got %s", got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -233,7 +233,7 @@ func (a *App) View() string {
|
|||||||
numKey := keyStyle.Copy()
|
numKey := keyStyle.Copy()
|
||||||
opKey := keyStyle.Copy().Foreground(lipgloss.Color("63")).Background(lipgloss.Color("235"))
|
opKey := keyStyle.Copy().Foreground(lipgloss.Color("63")).Background(lipgloss.Color("235"))
|
||||||
clearKey := keyStyle.Copy().Foreground(lipgloss.Color("203")).Background(lipgloss.Color("52"))
|
clearKey := keyStyle.Copy().Foreground(lipgloss.Color("203")).Background(lipgloss.Color("52"))
|
||||||
modKey := keyStyle.Copy().Foreground(lipgloss.Color("214")) // orange-ish for MOD
|
modKey := keyStyle.Copy().Foreground(lipgloss.Color("214")) // orange-ish for MOD
|
||||||
hexKey := keyStyle.Copy().Foreground(lipgloss.Color("214")).Background(lipgloss.Color("235")) // for A-F, only shown in HEX mode
|
hexKey := keyStyle.Copy().Foreground(lipgloss.Color("214")).Background(lipgloss.Color("235")) // for A-F, only shown in HEX mode
|
||||||
|
|
||||||
makeKey := func(label string) string {
|
makeKey := func(label string) string {
|
||||||
@ -278,6 +278,34 @@ func (a *App) View() string {
|
|||||||
hexRow := lipgloss.JoinHorizontal(lipgloss.Top,
|
hexRow := lipgloss.JoinHorizontal(lipgloss.Top,
|
||||||
hexBtn("A"), spacer, hexBtn("B"), spacer, hexBtn("C"), spacer, hexBtn("D"), spacer, hexBtn("E"), spacer, hexBtn("F"),
|
hexBtn("A"), spacer, hexBtn("B"), spacer, hexBtn("C"), spacer, hexBtn("D"), spacer, hexBtn("E"), spacer, hexBtn("F"),
|
||||||
)
|
)
|
||||||
|
hexW := lipgloss.Width(hexRow)
|
||||||
|
// Robust per-line centering of the pre-joined 5-key rows under the 6-key hex row.
|
||||||
|
// We pad each individual line of the row block explicitly. This avoids any subtle
|
||||||
|
// artifacts from Style.Width+Align or PlaceHorizontal when the input is already a
|
||||||
|
// multi-line string containing ANSI border sequences. Ensures every line (including
|
||||||
|
// the bottom border lines of the last row) has exactly the same width so vertical
|
||||||
|
// borders and bottom edges line up cleanly with no dangling fragments or splits.
|
||||||
|
centerUnderHex := func(block string) string {
|
||||||
|
if block == "" {
|
||||||
|
return block
|
||||||
|
}
|
||||||
|
lines := strings.Split(block, "\n")
|
||||||
|
for i, line := range lines {
|
||||||
|
cw := lipgloss.Width(line)
|
||||||
|
if cw >= hexW {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
pad := hexW - cw
|
||||||
|
left := pad / 2
|
||||||
|
right := pad - left
|
||||||
|
lines[i] = strings.Repeat(" ", left) + line + strings.Repeat(" ", right)
|
||||||
|
}
|
||||||
|
return strings.Join(lines, "\n")
|
||||||
|
}
|
||||||
|
row1 = centerUnderHex(row1)
|
||||||
|
row2 = centerUnderHex(row2)
|
||||||
|
row3 = centerUnderHex(row3)
|
||||||
|
row4 = centerUnderHex(row4)
|
||||||
rawGrid = lipgloss.JoinVertical(lipgloss.Left, hexRow, row1, row2, row3, row4)
|
rawGrid = lipgloss.JoinVertical(lipgloss.Left, hexRow, row1, row2, row3, row4)
|
||||||
} else {
|
} else {
|
||||||
// Normal mode: standard 4-row grid (no A-F)
|
// Normal mode: standard 4-row grid (no A-F)
|
||||||
@ -287,10 +315,24 @@ func (a *App) View() string {
|
|||||||
// Center the key grid directly under the display.
|
// Center the key grid directly under the display.
|
||||||
// No enclosing container — just the individually styled keys.
|
// No enclosing container — just the individually styled keys.
|
||||||
// When in HEX the A-F row appears at the top of the keypad.
|
// When in HEX the A-F row appears at the top of the keypad.
|
||||||
keypad := lipgloss.NewStyle().
|
// Explicit per-line centering of the entire rawGrid (the stacked hex+arith rows,
|
||||||
Width(dispW).
|
// where arith rows are already internally padded to hexW) into dispW.
|
||||||
Align(lipgloss.Center).
|
// We do this manually instead of lipgloss.NewStyle().Width(dispW).Align(Center).Render
|
||||||
Render(rawGrid)
|
// because the latter was producing inconsistent left padding on certain lines
|
||||||
|
// (especially bottom border lines containing box-drawing chars + heavy ANSI),
|
||||||
|
// causing the bottoms of the last row's keys to shift 1-2 columns relative to
|
||||||
|
// their own tops and sides.
|
||||||
|
kpLines := strings.Split(rawGrid, "\n")
|
||||||
|
for j := range kpLines {
|
||||||
|
cw := lipgloss.Width(kpLines[j])
|
||||||
|
if cw < dispW {
|
||||||
|
p := dispW - cw
|
||||||
|
l := p / 2
|
||||||
|
r := p - l
|
||||||
|
kpLines[j] = strings.Repeat(" ", l) + kpLines[j] + strings.Repeat(" ", r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
keypad := strings.Join(kpLines, "\n")
|
||||||
|
|
||||||
// Hint (minimal, non-wrapping, like gostations final player)
|
// Hint (minimal, non-wrapping, like gostations final player)
|
||||||
hint := lipgloss.NewStyle().Faint(true).Render("Tab:BASE m:MOD BackSpace:C Del:AC")
|
hint := lipgloss.NewStyle().Faint(true).Render("Tab:BASE m:MOD BackSpace:C Del:AC")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user