Skip to content

Commit 9e62989

Browse files
authored
Merge pull request #20 from vitwit/anil/display
highlight colors
2 parents 3439927 + 76121ae commit 9e62989

2 files changed

Lines changed: 94 additions & 11 deletions

File tree

.github/workflows/go.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ jobs:
6363
go tool cover -html=coverage.txt -o coverage.html
6464
6565
- name: Save coverage report
66-
uses: actions/upload-artifact@v3
66+
uses: actions/upload-artifact@v4
6767
with:
6868
name: coverage-report
6969
path: coverage.html
@@ -96,7 +96,7 @@ jobs:
9696
GOOS=windows GOARCH=amd64 go build -o bin/cosmoscope-windows-amd64.exe ./cmd/cosmoscope
9797
9898
- name: Upload artifacts
99-
uses: actions/upload-artifact@v3
99+
uses: actions/upload-artifact@v4
100100
with:
101101
name: binaries
102102
path: bin/

internal/portfolio/display.go

Lines changed: 92 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package portfolio
33
import (
44
"fmt"
55
"os"
6+
"sort"
67
"strings"
78
"time"
89

@@ -72,6 +73,11 @@ func PrintFooter(balances []Balance) {
7273
}
7374

7475
func printDetailedView(balances []Balance) {
76+
// Sort balances by USDValue descending
77+
sort.Slice(balances, func(i, j int) bool {
78+
return balances[i].USDValue > balances[j].USDValue
79+
})
80+
7581
table := tablewriter.NewWriter(os.Stdout)
7682
table.SetHeader([]string{"Account", "Network", "Token", "Amount", "USD Value"})
7783
table.SetAutoMergeCells(false)
@@ -86,14 +92,38 @@ func printDetailedView(balances []Balance) {
8692
tablewriter.Colors{tablewriter.Bold},
8793
)
8894

95+
// Determine min and max USDValue for gradient
96+
var minUSD, maxUSD float64
97+
if len(balances) > 0 {
98+
minUSD, maxUSD = balances[len(balances)-1].USDValue, balances[0].USDValue
99+
}
100+
89101
for _, b := range balances {
90-
table.Append([]string{
102+
row := []string{
91103
truncateString(b.Account, 20),
92104
b.Network,
93105
b.Token,
94106
fmt.Sprintf("%.4f", b.Amount),
95107
fmt.Sprintf("$%.2f", b.USDValue),
96-
})
108+
}
109+
110+
// Calculate normalized value (0 = min, 1 = max)
111+
norm := 0.0
112+
if maxUSD > minUSD {
113+
norm = (b.USDValue - minUSD) / (maxUSD - minUSD)
114+
}
115+
116+
// Assign color: top 20% bold green, next 30% normal green, rest no color
117+
var color tablewriter.Colors
118+
if norm >= 0.8 {
119+
color = tablewriter.Colors{tablewriter.FgHiGreenColor, tablewriter.Bold}
120+
} else if norm >= 0.5 {
121+
color = tablewriter.Colors{tablewriter.FgGreenColor}
122+
} else {
123+
color = tablewriter.Colors{} // default
124+
}
125+
126+
table.Rich(row, []tablewriter.Colors{color, color, color, color, color})
97127
}
98128

99129
titleColor.Println("Detailed Balance View:")
@@ -102,6 +132,11 @@ func printDetailedView(balances []Balance) {
102132
}
103133

104134
func printPortfolioSummary(balances []Balance) {
135+
tokens = make(map[string]*struct {
136+
amount float64
137+
usdValue float64
138+
})
139+
totalValue = 0
105140
for _, b := range balances {
106141
if _, exists := tokens[b.Token]; !exists {
107142
tokens[b.Token] = &struct {
@@ -114,6 +149,32 @@ func printPortfolioSummary(balances []Balance) {
114149
totalValue += b.USDValue
115150
}
116151

152+
// Collect token summaries for sorting
153+
type tokenRow struct {
154+
token string
155+
amount float64
156+
usdValue float64
157+
}
158+
var rows []tokenRow
159+
for token, sum := range tokens {
160+
rows = append(rows, tokenRow{
161+
token: token,
162+
amount: sum.amount,
163+
usdValue: sum.usdValue,
164+
})
165+
}
166+
167+
// Sort by USD value descending
168+
sort.Slice(rows, func(i, j int) bool {
169+
return rows[i].usdValue > rows[j].usdValue
170+
})
171+
172+
// Determine min and max USDValue for gradient
173+
var minUSD, maxUSD float64
174+
if len(rows) > 0 {
175+
minUSD, maxUSD = rows[len(rows)-1].usdValue, rows[0].usdValue
176+
}
177+
117178
table := tablewriter.NewWriter(os.Stdout)
118179
table.SetHeader([]string{"Token", "Amount", "USD Value", "Share %"})
119180
table.SetAutoMergeCells(false)
@@ -127,14 +188,36 @@ func printPortfolioSummary(balances []Balance) {
127188
tablewriter.Colors{tablewriter.Bold},
128189
)
129190

130-
for token, sum := range tokens {
131-
share := (sum.usdValue / totalValue) * 100
132-
table.Append([]string{
133-
token,
134-
fmt.Sprintf("%.4f", sum.amount),
135-
fmt.Sprintf("$%.2f", sum.usdValue),
191+
for _, row := range rows {
192+
share := (row.usdValue / totalValue) * 100
193+
rowData := []string{
194+
row.token,
195+
fmt.Sprintf("%.4f", row.amount),
196+
fmt.Sprintf("$%.2f", row.usdValue),
136197
fmt.Sprintf("%.2f%%", share),
137-
})
198+
}
199+
200+
// Calculate normalized value (0 = min, 1 = max)
201+
norm := 0.0
202+
if maxUSD > minUSD {
203+
norm = (row.usdValue - minUSD) / (maxUSD - minUSD)
204+
}
205+
206+
// Assign color: top 20% bold blue, next 20% normal blue, next 20% light blue, next 10% very light blue, rest no color
207+
var color tablewriter.Colors
208+
if norm >= 0.8 {
209+
color = tablewriter.Colors{tablewriter.FgHiBlueColor, tablewriter.Bold} // bold blue
210+
} else if norm >= 0.6 {
211+
color = tablewriter.Colors{tablewriter.FgBlueColor} // normal blue
212+
} else if norm >= 0.4 {
213+
color = tablewriter.Colors{tablewriter.FgHiBlueColor} // light blue
214+
} else if norm >= 0.3 {
215+
color = tablewriter.Colors{tablewriter.FgBlueColor} // very light blue (reuse normal blue, but no bold)
216+
} else {
217+
color = tablewriter.Colors{} // default
218+
}
219+
220+
table.Rich(rowData, []tablewriter.Colors{color, color, color, color})
138221
}
139222

140223
titleColor.Println("Portfolio Summary:")

0 commit comments

Comments
 (0)