diff --git a/Makefile b/Makefile index 5de20df..c6fe818 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,12 @@ GO_FILES=$(shell find $(ROOT_DIR) -name '*.go') # Gather list of expected binaries BINARIES=nview +# Extract Go module name from go.mod +GOMODULE=$(shell grep ^module $(ROOT_DIR)/go.mod | awk '{ print $$2 }') + +# Set version strings based on git tag and current ref +GO_LDFLAGS=-ldflags "-s -w -X '$(GOMODULE)/internal/version.Version=$(shell git describe --tags --exact-match 2>/dev/null)' -X '$(GOMODULE)/internal/version.CommitHash=$(shell git rev-parse --short HEAD)'" + .PHONY: build mod-tidy clean test all: format build @@ -30,4 +36,6 @@ test: tidy # Build our program binaries # Depends on GO_FILES to determine when rebuild is needed $(BINARIES): mod-tidy $(GO_FILES) - go build -ldflags="-s -w" -o $(@) . + go build \ + $(GO_LDFLAGS) \ + -o $(@) . diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 0000000..f1ab60d --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,31 @@ +// Copyright 2023 Blink Labs, LLC. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package version + +import ( + "fmt" +) + +// These are populated at build time +var Version string +var CommitHash string + +func GetVersionString() string { + if Version != "" { + return fmt.Sprintf("%s (commit %s)", Version, CommitHash) + } else { + return fmt.Sprintf("devel (commit %s)", CommitHash) + } +} diff --git a/main.go b/main.go index a3cb6aa..35e73fe 100644 --- a/main.go +++ b/main.go @@ -33,6 +33,8 @@ import ( "github.com/rivo/tview" "github.com/shirou/gopsutil/v3/process" terminal "golang.org/x/term" + + "github.com/blinklabs-io/nview/internal/version" ) // Global command line flags @@ -298,10 +300,17 @@ func getTestText(ctx context.Context, promMetrics *PromMetrics) string { var sb strings.Builder + // Style / UI + var width = 71 + + var twoColWidth int = (width - 3) / 2 + var twoColSecond int = twoColWidth + 2 + // Main section uptime := timeLeft(uptimes) - sb.WriteString(fmt.Sprintf(" Uptime: [blue]%s[white]\n", uptime)) - sb.WriteString(fmt.Sprintf("%s\n", strings.Repeat("-", 20))) + sb.WriteString(fmt.Sprintf(" Uptime: [blue]%-"+strconv.Itoa(twoColSecond-9-len(uptime))+"s[white]", uptime)) + sb.WriteString(fmt.Sprintf(" nview Version: [blue]%-"+strconv.Itoa(twoColWidth)+"s[white]\n", version.GetVersionString())) + sb.WriteString(fmt.Sprintf("%s\n", strings.Repeat("-", width+1))) // Epoch progress var epochProgress float32 @@ -470,8 +479,9 @@ func getHomeText(ctx context.Context, promMetrics *PromMetrics) string { // Main section uptime := timeLeft(uptimes) - sb.WriteString(fmt.Sprintf(" Uptime: [blue]%s[white]\n", uptime)) - sb.WriteString(fmt.Sprintf("%s\n", strings.Repeat("-", 20))) + sb.WriteString(fmt.Sprintf(" Uptime: [blue]%-"+strconv.Itoa(twoColSecond-9-len(uptime))+"s[white]", uptime)) + sb.WriteString(fmt.Sprintf(" nview Version: [blue]%-"+strconv.Itoa(twoColWidth)+"s[white]\n", version.GetVersionString())) + sb.WriteString(fmt.Sprintf("%s\n", strings.Repeat("-", width+1))) // Epoch progress var epochProgress float32 @@ -860,10 +870,17 @@ func getInfoText(ctx context.Context) string { var sb strings.Builder + // Style / UI + var width = 71 + + var twoColWidth int = (width - 3) / 2 + var twoColSecond int = twoColWidth + 2 + // Main section uptime := timeLeft(uptimes) - sb.WriteString(fmt.Sprintf(" Uptime: [blue]%s[white]\n", uptime)) - sb.WriteString(fmt.Sprintf("%s\n", strings.Repeat("-", 20))) + sb.WriteString(fmt.Sprintf(" Uptime: [blue]%-"+strconv.Itoa(twoColSecond-9-len(uptime))+"s[white]", uptime)) + sb.WriteString(fmt.Sprintf(" nview Version: [blue]%-"+strconv.Itoa(twoColWidth)+"s[white]\n", version.GetVersionString())) + sb.WriteString(fmt.Sprintf("%s\n", strings.Repeat("-", width+1))) if showPeers { sb.WriteString(fmt.Sprintf( @@ -919,10 +936,17 @@ func getPeerText(ctx context.Context) string { var sb strings.Builder + // Style / UI + var width = 71 + + var twoColWidth int = (width - 3) / 2 + var twoColSecond int = twoColWidth + 2 + // Main section uptime := timeLeft(uptimes) - sb.WriteString(fmt.Sprintf(" Uptime: [blue]%s[white]\n", uptime)) - sb.WriteString(fmt.Sprintf("%s\n", strings.Repeat("-", 20))) + sb.WriteString(fmt.Sprintf(" Uptime: [blue]%-"+strconv.Itoa(twoColSecond-9-len(uptime))+"s[white]", uptime)) + sb.WriteString(fmt.Sprintf(" nview Version: [blue]%-"+strconv.Itoa(twoColWidth)+"s[white]\n", version.GetVersionString())) + sb.WriteString(fmt.Sprintf("%s\n", strings.Repeat("-", width+1))) // bail on FreeBSD due to missing connections support if runtime.GOOS == "freebsd" { @@ -1024,7 +1048,6 @@ func getPeerText(ctx context.Context) string { charMarked = string('▌') charUnmarked = string('▖') } - width := 71 granularity := width - 3 granularitySmall := granularity / 2 if checkPeers {