-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathTabsExample.scala
81 lines (72 loc) · 2.6 KB
/
TabsExample.scala
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
package tuiexamples
import tui._
import tui.crossterm.CrosstermJni
import tui.widgets.tabs.TabsWidget
import tui.widgets.BlockWidget
object TabsExample {
case class App(
titles: Array[String],
var index: Int = 0
) {
def next(): Unit =
index = (index + 1) % titles.length
def previous(): Unit =
if (index > 0) {
index -= 1
} else {
index = titles.length - 1
}
}
def main(args: Array[String]): Unit = withTerminal { (jni, terminal) =>
// create app and run it
val app = App(titles = Array("Tab0", "Tab1", "Tab2", "Tab3"))
run_app(terminal, app, jni);
}
def run_app(terminal: Terminal, app: App, jni: CrosstermJni): Unit =
while (true) {
terminal.draw(f => ui(f, app))
jni.read() match {
case key: tui.crossterm.Event.Key =>
key.keyEvent.code match {
case char: tui.crossterm.KeyCode.Char if char.c() == 'q' => return
case _: tui.crossterm.KeyCode.Right => app.next()
case _: tui.crossterm.KeyCode.Left => app.previous()
case _ => ()
}
case _ => ()
}
}
def ui(f: Frame, app: App): Unit = {
val chunks = Layout(
direction = Direction.Vertical,
margin = Margin(5, 5),
constraints = Array(Constraint.Length(3), Constraint.Min(0))
).split(f.size)
val block = BlockWidget(style = Style(bg = Some(Color.White), fg = Some(Color.Black)))
f.renderWidget(block, f.size)
val titles = app.titles
.map { t =>
val (first, rest) = t.splitAt(1)
Spans.from(
Span.styled(first, Style(fg = Some(Color.Yellow))),
Span.styled(rest, Style(fg = Some(Color.Green)))
)
}
val tabs = TabsWidget(
titles = titles,
block = Some(BlockWidget(borders = Borders.ALL, title = Some(Spans.nostyle("Tabs")))),
selected = app.index,
style = Style(fg = Some(Color.Cyan)),
highlightStyle = Style(addModifier = Modifier.BOLD, bg = Some(Color.Black))
)
f.renderWidget(tabs, chunks(0))
val inner = app.index match {
case 0 => BlockWidget(title = Some(Spans.nostyle("Inner 0")), borders = Borders.ALL)
case 1 => BlockWidget(title = Some(Spans.nostyle("Inner 1")), borders = Borders.ALL)
case 2 => BlockWidget(title = Some(Spans.nostyle("Inner 2")), borders = Borders.ALL)
case 3 => BlockWidget(title = Some(Spans.nostyle("Inner 3")), borders = Borders.ALL)
case _ => sys.error("unreachable")
}
f.renderWidget(inner, chunks(1))
}
}