1 module jcli.text.widgets.shortcuts;
2 
3 import jcli.text;
4 
5 struct ShortcutsWidget(uint count)
6 {
7     static struct Shortcut
8     {
9         string key;
10         string desc;
11     }
12 
13     Shortcut[count] shortcuts;
14     AnsiColour bg;
15     AnsiStyleSet keyStyle;
16     AnsiStyleSet descStyle;
17 
18     void render(TextBuffer buffer)
19     {
20         auto area = Rect(0, buffer.height-1, buffer.width, buffer.height);
21         buffer.setCells(area, " ", AnsiStyleSet.init.bg(this.bg));
22         
23         foreach(shortcut; this.shortcuts)
24         {
25             Vector lastChar;
26             buffer.setCell(Vector(area.left, area.top), " ", this.keyStyle);
27             area.left++;
28             buffer.setString(area, shortcut.key, lastChar, this.keyStyle);
29             area.left = lastChar.x + 1;
30             buffer.setCell(Vector(area.left, area.top), " ", this.keyStyle);
31             area.left += 2;
32             buffer.setString(area, shortcut.desc, lastChar, this.descStyle);
33             area.left = lastChar.x + 2;
34         }
35     }
36 }
37 
38 struct ShortcutsWidgetBuilder(uint count)
39 {
40     private ShortcutsWidget!count _widget;
41 
42     typeof(this) withShortcut(uint index, string key, string desc)
43     {
44         this._widget.shortcuts[index] = typeof(_widget).Shortcut(key, desc);
45         return this;
46     }
47 
48     typeof(this) withBackground(AnsiColour colour)
49     {
50         this._widget.bg = colour;
51         return this;
52     }
53 
54     typeof(this) withKeyStyle(AnsiStyleSet style)
55     {
56         this._widget.keyStyle = style;
57         return this;
58     }
59 
60     typeof(this) withDescriptionStyle(AnsiStyleSet style)
61     {
62         this._widget.descStyle = style;
63         return this;
64     }
65 
66     auto build()
67     {
68         return this._widget;
69     }
70 }