-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform.go
More file actions
97 lines (84 loc) · 2.46 KB
/
Copy pathplatform.go
File metadata and controls
97 lines (84 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package wui
// Platform identifies which renderer is running.
type Platform int
const (
// PlatformTUI is a terminal build (bubbletea + lipgloss).
PlatformTUI Platform = iota
// PlatformWeb is a WASM build rendering to the DOM.
PlatformWeb
)
func (p Platform) String() string {
if p == PlatformWeb {
return "web"
}
return "tui"
}
// Current reports which renderer this binary was built for. It is a
// build-time constant per binary, so branches on it cost nothing at
// runtime and the unused branch's data is still compiled in (both sides
// must type-check).
func Current() Platform { return currentPlatform }
// IsTUI reports whether this is a terminal build.
func IsTUI() bool { return currentPlatform == PlatformTUI }
// IsWeb reports whether this is a WASM build.
func IsWeb() bool { return currentPlatform == PlatformWeb }
// PlatformText returns tui in terminal builds and web in browser
// builds — the string form of the common case, where only the wording
// differs between platforms:
//
// wui.Text(wui.PlatformText("press q to quit", "close this tab to quit"))
func PlatformText(tui, web string) string {
if currentPlatform == PlatformWeb {
return web
}
return tui
}
// PlatformValue returns tui in terminal builds and web in browser
// builds. It is the generic form of PlatformText, for any value type.
func PlatformValue[T any](tui, web T) T {
if currentPlatform == PlatformWeb {
return web
}
return tui
}
// OnPlatform renders tui in terminal builds and web in browser builds.
// Either may be nil to render nothing on that platform:
//
// wui.OnPlatform(
// wui.Text("Tab/Shift+Tab to move, Enter to activate"),
// wui.Text("Click anything, or Tab through the page"),
// )
func OnPlatform(tui, web Element) Element {
if currentPlatform == PlatformWeb {
return orEmpty(web)
}
return orEmpty(tui)
}
// TUIOnly renders its children only in terminal builds; browser builds
// render nothing.
func TUIOnly(children ...Element) Element {
if currentPlatform != PlatformTUI {
return Empty()
}
return groupOf(children)
}
// WebOnly renders its children only in WASM builds; terminal builds
// render nothing.
func WebOnly(children ...Element) Element {
if currentPlatform != PlatformWeb {
return Empty()
}
return groupOf(children)
}
func groupOf(children []Element) Element {
if len(children) == 1 {
return orEmpty(children[0])
}
return Box(Column, children...)
}
func orEmpty(el Element) Element {
if el == nil {
return Empty()
}
return el
}