diff options
| author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-05-06 11:44:47 +0200 |
|---|---|---|
| committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-05-20 12:43:55 +0200 |
| commit | dcd5d62501e61e0f6901d4d4687ab56430a4b8a7 (patch) | |
| tree | 4501938052c0f62279eaae66c34811d4b5232fa2 /spec | |
| parent | 1d64b5790f26760cb830336ccca9d51474b73ae8 (diff) | |
| download | luasystem-dcd5d62501e61e0f6901d4d4687ab56430a4b8a7.tar.gz luasystem-dcd5d62501e61e0f6901d4d4687ab56430a4b8a7.tar.bz2 luasystem-dcd5d62501e61e0f6901d4d4687ab56430a4b8a7.zip | |
add example for reading a line from the terminal, non-blocking
Handles utf8, and character width
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/04-term_spec.lua | 192 |
1 files changed, 191 insertions, 1 deletions
diff --git a/spec/04-term_spec.lua b/spec/04-term_spec.lua index 9ca37e9..ee4145a 100644 --- a/spec/04-term_spec.lua +++ b/spec/04-term_spec.lua | |||
| @@ -4,6 +4,19 @@ require("spec.helpers") | |||
| 4 | 4 | ||
| 5 | describe("Terminal:", function() | 5 | describe("Terminal:", function() |
| 6 | 6 | ||
| 7 | local wincodepage | ||
| 8 | |||
| 9 | setup(function() | ||
| 10 | wincodepage = system.getconsoleoutputcp() | ||
| 11 | assert(system.setconsoleoutputcp(65001)) | ||
| 12 | end) | ||
| 13 | |||
| 14 | teardown(function() | ||
| 15 | assert(system.setconsoleoutputcp(wincodepage)) | ||
| 16 | end) | ||
| 17 | |||
| 18 | |||
| 19 | |||
| 7 | describe("isatty()", function() | 20 | describe("isatty()", function() |
| 8 | 21 | ||
| 9 | local newtmpfile = require("pl.path").tmpname | 22 | local newtmpfile = require("pl.path").tmpname |
| @@ -93,7 +106,7 @@ describe("Terminal:", function() | |||
| 93 | 106 | ||
| 94 | 107 | ||
| 95 | 108 | ||
| 96 | describe("getconsoleflags()", function() | 109 | pending("getconsoleflags()", function() |
| 97 | 110 | ||
| 98 | pending("returns the consoleflags, if called without flags", function() | 111 | pending("returns the consoleflags, if called without flags", function() |
| 99 | print"1" | 112 | print"1" |
| @@ -111,4 +124,181 @@ for k,v in pairs(debug.getinfo(system.isatty)) do print(k,v) end | |||
| 111 | end) | 124 | end) |
| 112 | 125 | ||
| 113 | end) | 126 | end) |
| 127 | |||
| 128 | |||
| 129 | |||
| 130 | pending("setconsoleflags()", function() | ||
| 131 | |||
| 132 | pending("sets the consoleflags, if called with flags", function() | ||
| 133 | end) | ||
| 134 | |||
| 135 | end) | ||
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | pending("tcgetattr()", function() | ||
| 140 | |||
| 141 | pending("sets the consoleflags, if called with flags", function() | ||
| 142 | end) | ||
| 143 | |||
| 144 | end) | ||
| 145 | |||
| 146 | |||
| 147 | |||
| 148 | pending("tcsetattr()", function() | ||
| 149 | |||
| 150 | pending("sets the consoleflags, if called with flags", function() | ||
| 151 | end) | ||
| 152 | |||
| 153 | end) | ||
| 154 | |||
| 155 | |||
| 156 | |||
| 157 | pending("getconsolecp()", function() | ||
| 158 | |||
| 159 | pending("sets the consoleflags, if called with flags", function() | ||
| 160 | end) | ||
| 161 | |||
| 162 | end) | ||
| 163 | |||
| 164 | |||
| 165 | |||
| 166 | pending("setconsolecp()", function() | ||
| 167 | |||
| 168 | pending("sets the consoleflags, if called with flags", function() | ||
| 169 | end) | ||
| 170 | |||
| 171 | end) | ||
| 172 | |||
| 173 | |||
| 174 | |||
| 175 | pending("getconsoleoutputcp()", function() | ||
| 176 | |||
| 177 | pending("sets the consoleflags, if called with flags", function() | ||
| 178 | end) | ||
| 179 | |||
| 180 | end) | ||
| 181 | |||
| 182 | |||
| 183 | |||
| 184 | pending("setconsoleoutputcp()", function() | ||
| 185 | |||
| 186 | pending("sets the consoleflags, if called with flags", function() | ||
| 187 | end) | ||
| 188 | |||
| 189 | end) | ||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | pending("getnonblock()", function() | ||
| 194 | |||
| 195 | pending("sets the consoleflags, if called with flags", function() | ||
| 196 | end) | ||
| 197 | |||
| 198 | end) | ||
| 199 | |||
| 200 | |||
| 201 | |||
| 202 | pending("setnonblock()", function() | ||
| 203 | |||
| 204 | pending("sets the consoleflags, if called with flags", function() | ||
| 205 | end) | ||
| 206 | |||
| 207 | end) | ||
| 208 | |||
| 209 | |||
| 210 | |||
| 211 | pending("termsize()", function() | ||
| 212 | |||
| 213 | pending("sets the consoleflags, if called with flags", function() | ||
| 214 | end) | ||
| 215 | |||
| 216 | end) | ||
| 217 | |||
| 218 | |||
| 219 | |||
| 220 | describe("utf8cwidth()", function() | ||
| 221 | |||
| 222 | local ch1 = string.char(226, 130, 172) -- "€" single | ||
| 223 | local ch2 = string.char(240, 159, 154, 128) -- "🚀" double | ||
| 224 | local ch3 = string.char(228, 189, 160) -- "你" double | ||
| 225 | local ch4 = string.char(229, 165, 189) -- "好" double | ||
| 226 | |||
| 227 | it("handles zero width characters", function() | ||
| 228 | assert.same({0}, {system.utf8cwidth("")}) -- empty string returns 0-size | ||
| 229 | assert.same({nil, 'Character width determination failed'}, {system.utf8cwidth("\a")}) -- bell character | ||
| 230 | assert.same({nil, 'Character width determination failed'}, {system.utf8cwidth("\27")}) -- escape character | ||
| 231 | end) | ||
| 232 | |||
| 233 | it("handles single width characters", function() | ||
| 234 | assert.same({1}, {system.utf8cwidth("a")}) | ||
| 235 | assert.same({1}, {system.utf8cwidth(ch1)}) | ||
| 236 | end) | ||
| 237 | |||
| 238 | it("handles double width characters", function() | ||
| 239 | assert.same({2}, {system.utf8cwidth(ch2)}) | ||
| 240 | assert.same({2}, {system.utf8cwidth(ch3)}) | ||
| 241 | assert.same({2}, {system.utf8cwidth(ch4)}) | ||
| 242 | end) | ||
| 243 | |||
| 244 | it("returns the width of the first character in the string", function() | ||
| 245 | assert.same({nil, 'Character width determination failed'}, {system.utf8cwidth("\a" .. ch1)}) -- bell character + EURO | ||
| 246 | assert.same({1}, {system.utf8cwidth(ch1 .. ch2)}) | ||
| 247 | assert.same({2}, {system.utf8cwidth(ch2 .. ch3 .. ch4)}) | ||
| 248 | end) | ||
| 249 | |||
| 250 | end) | ||
| 251 | |||
| 252 | |||
| 253 | |||
| 254 | describe("utf8swidth()", function() | ||
| 255 | |||
| 256 | local ch1 = string.char(226, 130, 172) -- "€" single | ||
| 257 | local ch2 = string.char(240, 159, 154, 128) -- "🚀" double | ||
| 258 | local ch3 = string.char(228, 189, 160) -- "你" double | ||
| 259 | local ch4 = string.char(229, 165, 189) -- "好" double | ||
| 260 | |||
| 261 | it("handles zero width characters", function() | ||
| 262 | assert.same({0}, {system.utf8swidth("")}) -- empty string returns 0-size | ||
| 263 | assert.same({nil, 'Character width determination failed'}, {system.utf8swidth("\a")}) -- bell character | ||
| 264 | assert.same({nil, 'Character width determination failed'}, {system.utf8swidth("\27")}) -- escape character | ||
| 265 | end) | ||
| 266 | |||
| 267 | it("handles multi-character UTF8 strings", function() | ||
| 268 | assert.same({15}, {system.utf8swidth("hello " .. ch1 .. ch2 .. " world")}) | ||
| 269 | assert.same({16}, {system.utf8swidth("hello " .. ch3 .. ch4 .. " world")}) | ||
| 270 | end) | ||
| 271 | |||
| 272 | end) | ||
| 273 | |||
| 274 | |||
| 275 | |||
| 276 | pending("termbackup()", function() | ||
| 277 | |||
| 278 | end) | ||
| 279 | |||
| 280 | |||
| 281 | |||
| 282 | pending("termrestore()", function() | ||
| 283 | |||
| 284 | end) | ||
| 285 | |||
| 286 | |||
| 287 | |||
| 288 | pending("termwrap()", function() | ||
| 289 | |||
| 290 | end) | ||
| 291 | |||
| 292 | |||
| 293 | |||
| 294 | pending("readkey()", function() | ||
| 295 | |||
| 296 | end) | ||
| 297 | |||
| 298 | |||
| 299 | |||
| 300 | pending("readansi()", function() | ||
| 301 | |||
| 302 | end) | ||
| 303 | |||
| 114 | end) | 304 | end) |
