| 313 | } |
| 314 | |
| 315 | std::string ScriptConsole::tail(size_t maxBytes) const |
| 316 | { |
| 317 | std::lock_guard<std::mutex> lock(mMutex); |
| 318 | |
| 319 | // Walk back from the newest line until the joined text would exceed the |
| 320 | // budget, so the tail always ends on the last thing the script said — which |
| 321 | // is where a picoc diagnostic is. |
| 322 | size_t bytes = 0, first = mLines.size(); |
| 323 | while (first > 0) { |
| 324 | const size_t cost = mLines[first - 1].size() + 1; |
| 325 | if (bytes + cost > maxBytes) { |
| 326 | break; |
| 327 | } |
| 328 | bytes += cost; |
| 329 | first--; |
| 330 | } |
| 331 | |
| 332 | std::string out; |
| 333 | out.reserve(bytes); |
| 334 | for (size_t i = first; i < mLines.size(); i++) { |
| 335 | out += mLines[i]; |
| 336 | out += '\n'; |
| 337 | } |
| 338 | return out; |
| 339 | } |
| 340 | |
| 341 | /* ---- C shim ------------------------------------------------------------- */ |
| 342 | /* picoc's stdlib and platform layer are C; these are the only entry points |