@@ -7,87 +7,56 @@ import (
7
7
)
8
8
9
9
func (m ViewModel ) View () string {
10
- qrRender := lipgloss .JoinVertical (
11
- lipgloss .Center ,
12
- style .Yellow .Render (m .hint ),
13
- "" ,
14
- qrStyle .Render (m .asciiQR ),
15
- urlStyle .Render (m .urlTxn ),
16
- )
17
-
18
- if m .asciiQR == "" || m .urlTxn == "" {
19
- return lipgloss .JoinVertical (
20
- lipgloss .Center ,
21
- "No QR Code or TxnURL available" ,
22
- "\n " ,
23
- m .controls .View ())
24
- }
25
-
26
- // Build QR Parts
27
- hint := yellow .Render (m .hint )
28
10
qrCode := qrStyle .Render (m .asciiQR )
29
- url := urlStyle .Render (strings .Replace (m .urlTxn , m .Data .Address , m .FormatedAddress (), 1 ))
30
-
31
- controls := m .controls .View ()
32
-
33
- qrFullRender := lipgloss .JoinVertical (
34
- lipgloss .Center ,
35
- hint ,
36
- "" ,
37
- qrCode ,
38
- url ,
39
- )
11
+ qrWidth := lipgloss .Width (qrCode )
12
+ qrHeight := lipgloss .Height (qrCode )
13
+ title := ""
14
+ if m .IsOnline {
15
+ title = "Online Transaction"
16
+ } else {
17
+ title = "Offline Transaction"
18
+ }
40
19
41
- remainingHeight := max (0 , m .Height - lipgloss .Height (controls ))
42
- isLargeScreen := lipgloss .Height (qrFullRender ) <= remainingHeight && lipgloss .Width (qrFullRender ) <= m .Width
43
- isSmallScreen := lipgloss .Height (qrCode ) <= remainingHeight && lipgloss .Width (qrCode ) < m .Width
20
+ url := ""
21
+ if lipgloss .Width (m .urlTxn ) > qrWidth {
22
+ url = m .urlTxn [:(qrWidth - 3 )] + "..."
23
+ } else {
24
+ url = m .urlTxn
25
+ }
44
26
45
- if isLargeScreen {
46
- qrRenderPadHeight := max (0 , remainingHeight - 2 - lipgloss .Height (qrFullRender ))
47
- qrPad := strings .Repeat ("\n " , max (0 , qrRenderPadHeight / 2 ))
48
- if qrRenderPadHeight > 2 {
49
- return lipgloss .JoinVertical (
50
- lipgloss .Center ,
51
- qrPad ,
52
- qrFullRender ,
53
- qrPad ,
54
- m .controls .View (),
55
- )
56
- }
57
- return lipgloss .JoinVertical (
58
- lipgloss .Center ,
59
- qrFullRender ,
60
- m .controls .View (),
27
+ var render string
28
+ if qrWidth > m .Width || qrHeight + 2 > m .Height {
29
+ text := style .Red .Render ("QR Code too large to display... Please adjust terminal dimensions or font." )
30
+ padHeight := max (0 , m .Height - lipgloss .Height (text ))
31
+ padHString := strings .Repeat ("\n " , padHeight / 2 )
32
+ padWidth := max (0 , m .Width - lipgloss .Width (text ))
33
+ padWString := strings .Repeat (" " , padWidth / 2 )
34
+ paddedStr := lipgloss .JoinVertical (
35
+ lipgloss .Left ,
36
+ padHString ,
37
+ lipgloss .JoinHorizontal (lipgloss .Left , padWString , style .Red .Render ("QR Code too large to display... Please adjust terminal dimensions or font." )),
61
38
)
62
- }
63
- if isSmallScreen {
64
- isQrPadded := lipgloss .Height (qrCode ) < remainingHeight && lipgloss .Width (qrCode ) < m .Width
65
- if isQrPadded {
66
- qrRenderPadHeight := max (0 , remainingHeight - 2 - lipgloss .Height (qrCode ))
67
- qrPad := strings .Repeat ("\n " , max (0 , qrRenderPadHeight / 2 ))
68
- return lipgloss .JoinVertical (
69
- lipgloss .Center ,
70
- qrPad ,
71
- qrCode ,
72
- qrPad ,
73
- controls )
39
+ render = style .ApplyBorder (m .Width , m .Height , "8" ).Render (paddedStr )
40
+ } else {
41
+ qRemainingWidth := max (0 , (m .Width - lipgloss .Width (qrCode ))/ 2 )
42
+ qrCode = lipgloss .JoinHorizontal (lipgloss .Left , strings .Repeat (" " , qRemainingWidth ), qrCode , strings .Repeat (" " , qRemainingWidth ))
43
+ qRemainingHeight := max (0 , (m .Height - 2 - lipgloss .Height (qrCode ))/ 2 )
44
+ if qrHeight + 2 == m .Height {
45
+ qrCode = lipgloss .JoinVertical (lipgloss .Center , style .Yellow .Render (m .hint ), qrCode , urlStyle .Render (url ))
46
+ } else {
47
+ qrCode = lipgloss .JoinVertical (lipgloss .Center , strings .Repeat ("\n " , qRemainingHeight ), style .Yellow .Render (m .hint ), qrCode , urlStyle .Render (url ))
48
+
74
49
}
75
- return lipgloss .JoinVertical (
76
- lipgloss .Center ,
77
- qrCode ,
78
- controls )
50
+ render = style .ApplyBorder (m .Width , m .Height , "8" ).Render (qrCode )
79
51
}
80
-
81
- padHeight := max (0 , remainingHeight - lipgloss .Height (controls ))
82
- padHString := strings .Repeat ("\n " , padHeight / 2 )
83
- text := red .Render ("QR Code too large to display... Please adjust terminal dimensions or font." )
84
- padWidth := max (0 , m .Width - lipgloss .Width (text ))
85
- padWString := strings .Repeat (" " , padWidth / 2 )
86
- return lipgloss .JoinVertical (
87
- lipgloss .Left ,
88
- padHString ,
89
- lipgloss .JoinHorizontal (lipgloss .Left , padWString , red .Render ("QR Code too large to display... Please adjust terminal dimensions or font." )),
90
- padHString ,
91
- m .controls .View (),
52
+ return style .WithNavigation (
53
+ m .navigation ,
54
+ style .WithControls (
55
+ m .controls ,
56
+ style .WithTitle (
57
+ title ,
58
+ render ,
59
+ ),
60
+ ),
92
61
)
93
62
}
0 commit comments