debug_handler.dart 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import 'package:convert/convert.dart';
  2. import 'package:xterm/terminal/csi.dart';
  3. import 'package:xterm/util/ansi_color.dart';
  4. class DebugHandler {
  5. final _buffer = StringBuffer();
  6. var _enabled = false;
  7. void enable([bool enabled = true]) {
  8. _enabled = enabled;
  9. }
  10. void _checkBuffer() {
  11. if (!_enabled) return;
  12. if (_buffer.isNotEmpty) {
  13. print(AnsiColor.cyan('┤') + _buffer.toString() + AnsiColor.cyan('├'));
  14. _buffer.clear();
  15. }
  16. }
  17. void onCsi(CSI csi) {
  18. if (!_enabled) return;
  19. _checkBuffer();
  20. print(AnsiColor.green('<CSI $csi>'));
  21. }
  22. void onEsc(int charAfterEsc) {
  23. if (!_enabled) return;
  24. _checkBuffer();
  25. print(AnsiColor.green('<ESC ${String.fromCharCode(charAfterEsc)}>'));
  26. }
  27. void onOsc(List<String> params) {
  28. if (!_enabled) return;
  29. _checkBuffer();
  30. print(AnsiColor.yellow('<OSC $params>'));
  31. }
  32. void onSbc(int codePoint) {
  33. if (!_enabled) return;
  34. _checkBuffer();
  35. print(AnsiColor.magenta('<SBC ${hex.encode([codePoint])}>'));
  36. }
  37. void onChar(int codePoint) {
  38. if (!_enabled) return;
  39. _buffer.writeCharCode(codePoint);
  40. }
  41. void onMetrics(String metrics) {
  42. if (!_enabled) return;
  43. print(AnsiColor.blue('<MRC $metrics>'));
  44. }
  45. void onError(String error) {
  46. if (!_enabled) return;
  47. print(AnsiColor.red('<ERR $error>'));
  48. }
  49. void onMsg(Object msg) {
  50. if (!_enabled) return;
  51. print(AnsiColor.green('<MSG $msg>'));
  52. }
  53. }