diff options
author | Minijackson <minijackson@riseup.net> | 2022-12-22 12:19:59 +0100 |
---|---|---|
committer | Minijackson <minijackson@riseup.net> | 2022-12-22 12:19:59 +0100 |
commit | 92a02c34628343153b33602eae00cef46e28d191 (patch) | |
tree | 8622ec528d24e456be22d984d93aa9bcafc97399 /examples/lib-dfscq-log/dfscq-log.lua | |
download | diaphragm-92a02c34628343153b33602eae00cef46e28d191.tar.gz diaphragm-92a02c34628343153b33602eae00cef46e28d191.zip |
WIP
Diffstat (limited to 'examples/lib-dfscq-log/dfscq-log.lua')
-rw-r--r-- | examples/lib-dfscq-log/dfscq-log.lua | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/examples/lib-dfscq-log/dfscq-log.lua b/examples/lib-dfscq-log/dfscq-log.lua new file mode 100644 index 0000000..c54e4f2 --- /dev/null +++ b/examples/lib-dfscq-log/dfscq-log.lua | |||
@@ -0,0 +1,124 @@ | |||
1 | local diaphragm = require("diaphragm") | ||
2 | |||
3 | -- TODO: anyone can use metatable? | ||
4 | |||
5 | local Blocks = diaphragm.Drawable:new() | ||
6 | Blocks.elements = {} | ||
7 | Blocks.unit_width = diaphragm.float() | ||
8 | |||
9 | function Blocks:draw() | ||
10 | local len = #self.elements | ||
11 | assert(len >= 1, "Blocks must have at least 1 element") | ||
12 | |||
13 | local total_grow = 0 | ||
14 | for el in self.elements do | ||
15 | total_grow = el.grow or 1 | ||
16 | end | ||
17 | |||
18 | diaphragm.constrain(self.width == self.unit_width * total_grow) | ||
19 | |||
20 | local param = self.elements[0] | ||
21 | |||
22 | -- TODO: this would be written with layout.hstack | ||
23 | |||
24 | local block = diaphragm.shape.Rectangle:new() | ||
25 | block.fill(param.color) | ||
26 | diaphragm.constrain(block.left() == self.left()) | ||
27 | diaphragm.constrain(block.top() == self.top()) | ||
28 | diaphragm.constrain(block.bottom() == self.bottom()) | ||
29 | diaphragm.constrain(block.width() == (param.grow or 1) * self.unit_width) | ||
30 | |||
31 | block.draw() | ||
32 | |||
33 | local previous_right = block.right() | ||
34 | |||
35 | for i = 2, len do | ||
36 | param = self.elements[i] | ||
37 | |||
38 | block = diaphragm.shape.Rectangle:new() | ||
39 | block.fill(param.color) | ||
40 | |||
41 | diaphragm.constrain(block.left() == previous_right) | ||
42 | diaphragm.constrain(block.top() == self.top()) | ||
43 | diaphragm.constrain(block.bottom() == self.bottom()) | ||
44 | diaphragm.constrain(block.width() == (param.grow or 1) * self.unit_width) | ||
45 | |||
46 | block.draw() | ||
47 | |||
48 | previous_right = block.right() | ||
49 | end | ||
50 | end | ||
51 | |||
52 | -- Library | ||
53 | ---------- | ||
54 | |||
55 | local function block(color) | ||
56 | local res = diaphragm.shape.Rectangle:new() | ||
57 | res.fill(color) | ||
58 | return res | ||
59 | end | ||
60 | |||
61 | local function blocks_by_len(count, color) | ||
62 | local res = {} | ||
63 | for i = 1, count do | ||
64 | res[i] = block(color) | ||
65 | end | ||
66 | |||
67 | -- TODO: rename cons? Misleading | ||
68 | diaphragm.cons.same_size(res) | ||
69 | return diaphragm.layout.hstack(res) | ||
70 | end | ||
71 | |||
72 | local function layer(config) | ||
73 | local name = config.name or "" | ||
74 | local entries = config.entries or {} | ||
75 | |||
76 | -- TODO: how to do it without box? | ||
77 | -- TODO: implement union or combine | ||
78 | -- probably combine because | ||
79 | -- union could remove some | ||
80 | -- strokes | ||
81 | return diaphragm.layout.box({}) | ||
82 | end | ||
83 | |||
84 | -- Parameters | ||
85 | ------------- | ||
86 | |||
87 | local blue = diaphragm.color.from_rgb(0.35, 0.35, 1.) | ||
88 | |||
89 | local active_txn_count = 5 | ||
90 | |||
91 | -- Log API | ||
92 | ---------- | ||
93 | |||
94 | local log_api = layer({ | ||
95 | name = "LogAPI", | ||
96 | entries = { | ||
97 | { | ||
98 | name = "activeTxn", | ||
99 | content = blocks_by_len(active_txn_count, blue), | ||
100 | }, | ||
101 | }, | ||
102 | }) | ||
103 | |||
104 | -- Group Commit | ||
105 | --------------- | ||
106 | |||
107 | -- local group_commit = nil | ||
108 | |||
109 | -- Disk Log | ||
110 | ----------- | ||
111 | |||
112 | -- local disk_log = nil | ||
113 | |||
114 | -- Applier | ||
115 | ---------- | ||
116 | |||
117 | -- local applier = nil | ||
118 | |||
119 | diaphragm.draw(diaphragm.layout.vstack({ | ||
120 | log_api, | ||
121 | -- group_commit, | ||
122 | -- disk_log, | ||
123 | -- applier, | ||
124 | })) | ||