SHOW:
|
|
- or go back to the newest paste.
1 | -- Buffer API, By Symmetryc | |
2 | -- Edited by AgentE382 to change ":" OO-syntax to "." OO-syntax. (First step to redirect target based on this.) | |
3 | -- Edited by AgentE382 to add preliminary redirect-target capability. | |
4 | -- Edited by AgentE382 to add full redirect-target functionality. | |
5 | -- Edited by AgentE382 to fix full redirect-target functionality. | |
6 | return { | |
7 | new = function() | |
8 | local redirectStack = {} | |
9 | ||
10 | local out = term.native | |
11 | ||
12 | local self = { | |
13 | act = {pos = {}}; | |
14 | pos = {}; | |
15 | back = colors.white; | |
16 | text = colors.lightGray; | |
17 | blink = true | |
18 | } | |
19 | ||
20 | - | act[#act + 1] = {term.setCursorPos, selfpos[1], selfpos[2]} |
20 | + | |
21 | local act = self.act | |
22 | local selfpos = self.pos | |
23 | local append = true | |
24 | - | act[#act + 1] = {term.setBackgroundColor, self.back} |
24 | + | |
25 | act[#act + 1] = {out.setCursorPos, selfpos[1], selfpos[2]} | |
26 | append = false | |
27 | end | |
28 | if self.back ~= act.back then | |
29 | - | act[#act + 1] = {term.setTextColor, self.text} |
29 | + | act[#act + 1] = {out.setBackgroundColor, self.back} |
30 | act.back = self.back | |
31 | append = false | |
32 | end | |
33 | if self.text ~= act.text then | |
34 | - | act[#act + 1] = {term.setCursorBlink, self.blink} |
34 | + | act[#act + 1] = {out.setTextColor, self.text} |
35 | act.text = self.text | |
36 | append = false | |
37 | end | |
38 | if self.blink ~= act.blink then | |
39 | act[#act + 1] = {out.setCursorBlink, self.blink} | |
40 | act.blink = self.blink | |
41 | append = false | |
42 | end | |
43 | - | act[#act + 1] = {term.write, line} |
43 | + | |
44 | if append then | |
45 | act[#act][2] = act[#act][2]..line | |
46 | append = false | |
47 | else | |
48 | act[#act + 1] = {out.write, line} | |
49 | - | act[#act + 1] = {term.setCursorPos, 1, selfpos[2]} |
49 | + | |
50 | selfpos[1] = selfpos[1] + #line | |
51 | if nl == "\n" then | |
52 | selfpos[1] = 1 | |
53 | selfpos[2] = selfpos[2] + 1 | |
54 | act[#act + 1] = {out.setCursorPos, 1, selfpos[2]} | |
55 | end | |
56 | end | |
57 | act.pos = {selfpos[1], selfpos[2]} | |
58 | return self | |
59 | end; | |
60 | function self.draw(self) | |
61 | for i, v in ipairs(self.act) do | |
62 | if v[3] then | |
63 | v[1](v[2], v[3]) | |
64 | else | |
65 | v[1](v[2]) | |
66 | end | |
67 | end | |
68 | self.act = {} | |
69 | - | for i = #a, 1 do |
69 | + | |
70 | - | a[i] = nil |
70 | + | |
71 | ||
72 | function self.clear() | |
73 | local a = self.act | |
74 | if a[#a][1] ~= out.clear then | |
75 | a[#a + 1] = {out.clear} | |
76 | end | |
77 | - | if a[#a][1] ~= term.clearLine then |
77 | + | |
78 | - | a[#a + 1] = {term.clearLine} |
78 | + | |
79 | ||
80 | function self.clearLine() | |
81 | local a = self.act | |
82 | if a[#a][1] ~= out.clearLine then | |
83 | a[#a + 1] = {out.clearLine} | |
84 | end | |
85 | return self | |
86 | end | |
87 | ||
88 | function self.getCursorPos() | |
89 | local p = self.pos | |
90 | return p[1], p[2] | |
91 | end | |
92 | ||
93 | function self.setCursorPos(x, y) | |
94 | local p = self.pos | |
95 | p[1], p[2] = x, y | |
96 | return self | |
97 | end | |
98 | ||
99 | - | self.isColor = term.isColor |
99 | + | |
100 | self.blink = state | |
101 | - | self.getSize = term.getSize |
101 | + | |
102 | end | |
103 | ||
104 | - | term.redirect(target) |
104 | + | function self.isColor() |
105 | return out.isColor and out.isColor() | |
106 | end | |
107 | ||
108 | function self.getSize() | |
109 | - | term.restore() |
109 | + | return out.getSize() |
110 | end | |
111 | ||
112 | function self.redirect(target) | |
113 | redirectStack[#redirectStack + 1] = out | |
114 | out = target | |
115 | - | if a[#a][1] ~= term.scroll then |
115 | + | |
116 | - | a[#a + 1] = {term.scroll, n} |
116 | + | |
117 | ||
118 | function self.restore() | |
119 | out = redirectStack[#redirectStack] or out | |
120 | redirectStack[#redirectStack] = nil | |
121 | return self | |
122 | end | |
123 | ||
124 | function self.scroll(n) | |
125 | local a = self.act | |
126 | if a[#a][1] ~= out.scroll then | |
127 | a[#a + 1] = {out.scroll, n} | |
128 | else | |
129 | local s = a[#a] | |
130 | s[2] = s[2] + n | |
131 | end | |
132 | return self | |
133 | end | |
134 | ||
135 | function self.setTextColor(color) | |
136 | self.text = color | |
137 | return self | |
138 | end | |
139 | ||
140 | function self.setBackgroundColor(color) | |
141 | self.back = color | |
142 | return self | |
143 | end | |
144 | ||
145 | return self | |
146 | end; | |
147 | } |