问题描述
我正在为面向 linux_x64 的 Flutter Desktop 编写一些代码。
我正在从一些应用程序中提取一些日志,这些日志的语法如下:
-
使用
检查日志文件less logfile
ESC(BESC[mauthentication-msESC(BESC[m
-
使用
cat -vte logfile
检查日志文件我得到了这个:^[(B^[[mauthentication-ms^[(B^[[m$
-
Future<String> readAsstring = file.readAsstring(); readAsstring.then((String value) => _log = utf8.decode(value.runes.toList()));
我在 SelectableText 小部件中获得此输出
(B[mauthentication-ms(B[m
我对这种行为真的很困惑,所以如果有人对此建议有经验,欢迎!
有两个选项:
编辑:
我解决了导入 tint plugin: tint: ^2.0.0
并更改 Dart 代码(使用 tint 插件中的 strip()
方法)如下:
Future<String> readAsstring = file.readAsstring();
readAsstring.then((String value) => _log = value.strip());
解决方法
那些有趣的字符被称为转义序列,程序使用它们来打印颜色和斜体等等。
终端旨在解码这些转义序列,但常规程序不知道如何处理它们。 less
和 cat
正在打印文件中的内容,是您运行它们的终端对它们进行解码。
你必须让你的程序通过这样的一段代码并删除所有的转义序列:
m = "h\x1b[34mello\x1b(A.\x1b[H" # Text full of random escape sequences
c = 0 # A count variable
p = True # Are we not in an escape sequence?
o = "" # The output variable
for l in m:
if l == "\x1b":
p = False
elif p:
o += l
elif l in "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm": # Most (maybe all) escape sequences end in letters.
p = True
c += 1 # Move on to the next letter in the input string
print(o) # Text without escape sequences