v / examples / animated_help_text.v
92 lines · 81 sloc · 2.6 KB · efc8f7fb3ef7e90a088db64ad2c68dd0698b90d5
Raw
1import term.ui as tui
2import flag
3
4struct DocTest {
5 show_version bool @[short: v; xdoc: 'Show version and exit']
6 debug_level int @[long: debug; short: d; xdoc: 'Debug level']
7 level f32 @[only: l; xdoc: 'Do not show this']
8 example string
9 square bool
10 multi int @[only: m; repeats]
11 wroom []int @[short: w]
12 the_limit string
13}
14
15struct App {
16mut:
17 tui &tui.Context = unsafe { nil }
18 frame int
19 square string = '.____.\n| |\n| |\n|____|'
20 pad int = 1
21 direction int = 1
22}
23
24fn event(e &tui.Event, mut _app App) {
25 match e.typ {
26 .mouse_down {}
27 .mouse_drag {}
28 .mouse_up {}
29 .key_down {
30 if e.code == .c {
31 } else if e.code == .escape {
32 exit(0)
33 }
34 }
35 else {}
36 }
37}
38
39fn frame(mut app App) {
40 app.tui.clear()
41 app.frame++
42
43 if app.frame % 4 == 0 {
44 app.pad += app.direction
45 app.square = '${' '.repeat(app.pad)}.____.\n${' '.repeat(app.pad)}| |\n${' '.repeat(app.pad)}| |\n${' '.repeat(app.pad)}|____|'
46 }
47 if app.frame % 100 == 0 {
48 if app.direction > 0 {
49 app.direction = -1
50 } else {
51 app.direction = 1
52 }
53 }
54
55 help_text := flag.to_doc[DocTest](
56 version: '1.0'
57 description: 'Hello! This should show an *animated* example application description.
58We are at frame ${app.frame}.
59Press ESCAPE or Ctrl+C to exit'
60 footer: '
61Press ESCAPE or Ctrl+C to exit'
62 fields: {
63 'level': 'Level of lorem ipsum\nand more\nmany many many more.\nNotice how user newlines/format is kept since\ninput lines are all less or within\nthe default layout.description_padding\nand max width'
64 'example': 'Looong example text without newlines or anything else and lorem ipsum and more and many many many more. Should be auto fitted'
65 'multi': 'This flag can be repeated'
66 '-e, --extra': 'Secret flag that does not exist on the struct, but we want documented (in same format as the others)'
67 '-q, --quiet-and-quite-long-flag <string>': 'Mega long description and secret flag that does not exist on the struct, but we want documented. Also the flag has custom newlines\nand the flag line itself is super long'
68 'square': '${app.square}'
69 }
70 ) or { '' }
71
72 app.tui.draw_text(0, 0, '${help_text}')
73 app.tui.reset()
74
75 app.tui.flush()
76}
77
78type EventFn = fn (&tui.Event, voidptr)
79
80type FrameFn = fn (voidptr)
81
82fn main() {
83 mut app := &App{}
84 app.tui = tui.init(
85 user_data: app
86 event_fn: EventFn(event)
87 frame_fn: FrameFn(frame)
88 hide_cursor: true
89 frame_rate: 60
90 )
91 app.tui.run()!
92}
93