mirror of
https://github.com/TermoraDev/termora.git
synced 2026-01-16 02:12:58 +08:00
fix: snippet \ character escape (#625)
This commit is contained in:
@@ -18,6 +18,16 @@ class SnippetAction private constructor() : AnAction(I18n.getString("termora.sni
|
|||||||
}
|
}
|
||||||
|
|
||||||
const val SNIPPET = "SnippetAction"
|
const val SNIPPET = "SnippetAction"
|
||||||
|
|
||||||
|
// \r \n \t \a \e \b
|
||||||
|
private val SpecialChars = mutableMapOf(
|
||||||
|
'r' to '\r',
|
||||||
|
'n' to '\n',
|
||||||
|
't' to '\t',
|
||||||
|
'a' to ControlCharacters.BEL,
|
||||||
|
'e' to ControlCharacters.ESC,
|
||||||
|
'b' to ControlCharacters.BS
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun actionPerformed(evt: AnActionEvent) {
|
override fun actionPerformed(evt: AnActionEvent) {
|
||||||
@@ -27,31 +37,39 @@ class SnippetAction private constructor() : AnAction(I18n.getString("termora.sni
|
|||||||
|
|
||||||
fun runSnippet(snippet: Snippet, writer: TerminalWriter) {
|
fun runSnippet(snippet: Snippet, writer: TerminalWriter) {
|
||||||
if (snippet.type != SnippetType.Snippet) return
|
if (snippet.type != SnippetType.Snippet) return
|
||||||
val map = mapOf(
|
writer.write(TerminalWriter.WriteRequest.fromBytes(unescape(snippet.snippet).toByteArray(writer.getCharset())))
|
||||||
"\n" to ControlCharacters.LF,
|
}
|
||||||
"\r" to ControlCharacters.CR,
|
|
||||||
"\t" to ControlCharacters.TAB,
|
private fun unescape(text: String): String {
|
||||||
"\b" to ControlCharacters.BS,
|
val chars = text.toCharArray()
|
||||||
"\\a" to ControlCharacters.BEL,
|
val sb = StringBuilder()
|
||||||
"\\e" to ControlCharacters.ESC,
|
|
||||||
)
|
|
||||||
val chars = snippet.snippet.toCharArray()
|
|
||||||
for (i in chars.indices) {
|
for (i in chars.indices) {
|
||||||
val c = chars[i]
|
val c = chars[i]
|
||||||
if (i == 0) continue
|
|
||||||
if (c != '\n') continue
|
// 不是特殊字符不处理
|
||||||
if (chars[i - 1] != '\\') continue
|
if (SpecialChars.containsKey(c).not()) {
|
||||||
// 每一行的最后一个 \ 比较特殊,先转成 null 然后再去 unescapeJava
|
sb.append(c)
|
||||||
chars[i - 1] = Char.Null
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
var text = chars.joinToString(StringUtils.EMPTY)
|
// 特殊字符前面不是 `\` 不处理
|
||||||
text = StringEscapeUtils.unescapeJava(text)
|
if (chars.getOrNull(i - 1) != '\\') {
|
||||||
for (e in map.entries) {
|
sb.append(c)
|
||||||
text = text.replace(e.key, e.value.toString())
|
continue
|
||||||
}
|
}
|
||||||
text = text.replace(Char.Null, '\\')
|
|
||||||
|
|
||||||
writer.write(TerminalWriter.WriteRequest.fromBytes(text.toByteArray(writer.getCharset())))
|
// 如果构成的字符串是:\\r 就会生成 \r 字符串,并非转译成:CR
|
||||||
|
if (chars.getOrNull(i - 2) == '\\') {
|
||||||
|
sb.deleteCharAt(sb.length - 1)
|
||||||
|
sb.append(c)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// 命中条件之后,那么 sb 最后一个字符肯定是 \
|
||||||
|
sb.deleteCharAt(sb.length - 1)
|
||||||
|
sb.append(SpecialChars.getValue(c))
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.toString()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user