Skip to content

Commit 7c9e9a6

Browse files
committed
fix: 更正一些cmd命令的处理方式,使测试htop数据正常显示
1 parent 2e375d0 commit 7c9e9a6

File tree

4 files changed

+102
-29
lines changed

4 files changed

+102
-29
lines changed

llcomNext/LLCOM/Models/TerminalCommand.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public enum TerminalCommand
7171
MultipleStyle,
7272
}
7373

74-
public class TerminalCommandCheck
74+
public static class TerminalCommandCheck
7575
{
7676
/// <summary>
7777
/// 分析给定的字符切片,判断是否为终端命令
@@ -95,6 +95,9 @@ public static ((TerminalCommand, (int, int)), int) Do(ReadOnlySpan<char> slice)
9595
{
9696
return ((singleCmd, (0,0)), 1);
9797
}
98+
//判断下是不是\x1b(?或\x1b)?
99+
if(slice.Length >= 3 && slice[0] == '\x1b' && (slice[1] == '(' || slice[1] == ')'))
100+
return ((TerminalCommand.Unknown, (0,0)), 3); //先当成未知命令处理
98101
//判断是否为转义字符
99102
if(slice[0] != '\x1b' || slice[1] != '[' || slice.Length < 3)
100103
{
@@ -113,9 +116,10 @@ public static ((TerminalCommand, (int, int)), int) Do(ReadOnlySpan<char> slice)
113116
int code = 0;
114117
char cmd = '\0';
115118
int i = 2; //从第三个字符开始分析,最多分析到第10个字符
116-
while (i < slice.Length && i < 10 && char.IsDigit(slice[i]))
119+
while (i < slice.Length && i < 10 && (char.IsDigit(slice[i]) || slice[i] == '?'))
117120
{
118-
code = code * 10 + (slice[i] - '0'); //将数字字符转换为数字
121+
if(slice[i] != '?')
122+
code = code * 10 + (slice[i] - '0'); //将数字字符转换为数字
119123
i++;
120124
}
121125
if (i < slice.Length)
@@ -181,10 +185,10 @@ public static ((TerminalCommand, (int, int)), int) Do(ReadOnlySpan<char> slice)
181185
1 => TerminalCommand.Bold, //加粗
182186
4 => TerminalCommand.Underline, //下划线
183187
7 => TerminalCommand.Reverse, //反转颜色
184-
_ when code >= 30 && code <= 37 => TerminalCommand.ForegroundColor, //前景色
185-
_ when code >= 90 && code <= 97 => TerminalCommand.ForegroundColor, //前景色
186-
_ when code >= 40 && code <= 47 => TerminalCommand.BackgroundColor, //背景色
187-
_ when code >= 100 && code <= 107 => TerminalCommand.BackgroundColor, //背景色
188+
_ when code is >= 30 and <= 37 => TerminalCommand.ForegroundColor, //前景色
189+
_ when code is >= 90 and <= 97 => TerminalCommand.ForegroundColor, //前景色
190+
_ when code is >= 40 and <= 47 => TerminalCommand.BackgroundColor, //背景色
191+
_ when code is >= 100 and <= 107 => TerminalCommand.BackgroundColor, //背景色
188192
_ => TerminalCommand.None //不匹配
189193
};
190194
if (mr != TerminalCommand.None)

llcomNext/LLCOM/Models/TerminalObject.cs

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ private void TerminalChanged()
2727

2828
/// 用于存放终端数据的缓存
2929
private List<List<TerminalBlock>> CacheLines { get; } = [];
30-
30+
3131
//当前光标位置
3232
/// X从0开始,最大可到达窗口宽度(再增加就需要换行了)
3333
private int PositionX { get; set; } = 0;
34+
3435
/// Y从0开始,最大可到达窗口高度-1
3536
private int PositionY { get; set; } = 0;
3637

@@ -95,6 +96,8 @@ private void AddText(char[] texts)//TODO)) 保持private
9596
{
9697
//获取实际的字符宽度
9798
var length = UnicodeCalculator.GetWidth(chars[0]);
99+
if (length < 0)
100+
length = 0;
98101
//挖去可用空间
99102
space -= length;
100103
if(space < 0)//剩余空间不足,别添加了
@@ -687,9 +690,9 @@ public void AddChars(ReadOnlySpan<char> chars)
687690
break;
688691
case TerminalCommand.MoveCursorTo:
689692
//光标移动到指定位置
690-
//p1表示行数,p2表示列数
691-
PositionX = p1 - 1;
692-
PositionY = p2 - 1;
693+
//p1表示列数,p2表示行数
694+
PositionY = p1 - 1;
695+
PositionX = p2 - 1;
693696
if(PositionX < 0)
694697
PositionX = 0; //不能小于0
695698
if(PositionX >= _windowWidth)
@@ -723,41 +726,58 @@ public void AddChars(ReadOnlySpan<char> chars)
723726
break;
724727
case TerminalCommand.Reverse:
725728
//反转颜色
726-
(CurrentState.Foreground, CurrentState.Background) = (CurrentState.Background, CurrentState.Foreground);
729+
CurrentState.Background = -1;
730+
CurrentState.Foreground = -1;
727731
break;
728732
case TerminalCommand.ForegroundColor:
729733
//前景色
730734
//p1表示颜色代码
731-
if (p1 is >= 30 and <= 37)
732-
{
733-
//设置前景色
734-
CurrentState.Foreground = p1 - 30;
735-
}
736-
else if (p1 == 39)
737-
{
738-
//重置前景色
735+
if (p1 == 39) //重置前景色
739736
CurrentState.Foreground = -1;
740-
}
737+
else//设置前景色
738+
CurrentState.Foreground = p1;
741739
break;
742740
case TerminalCommand.BackgroundColor:
743741
//背景色
744742
//p1表示颜色代码
745-
if (p1 is >= 40 and <= 47)
743+
if (p1 == 49) //重置背景色
744+
CurrentState.Background = -1;
745+
else//设置背景色
746+
CurrentState.Background = p1;
747+
break;
748+
case TerminalCommand.MultipleStyle:
749+
//多重样式
750+
switch (p1)
746751
{
747-
//设置背景色
748-
CurrentState.Background = p1 - 40;
752+
case 0://重置样式
753+
CurrentState = new TerminalBlock("");
754+
break;
755+
case 1://加粗
756+
CurrentState.IsBold = true;
757+
break;
758+
case 4://下划线
759+
CurrentState.IsUnderLine = true;
760+
break;
761+
case 7://反转颜色
762+
CurrentState.Background = -1;
763+
CurrentState.Foreground = -1;
764+
break;
749765
}
750-
else if (p1 == 49)
766+
if(p2 is >= 30 and <= 37) CurrentState.Foreground = p2; //前景色
767+
if(p2 is >= 90 and <= 97) CurrentState.Foreground = p2; //前景色
768+
if(p2 is >= 40 and <= 47 )CurrentState.Background = p2; //背景色
769+
if(p2 is >= 100 and <= 107)CurrentState.Background = p2; //背景色
770+
if(p2 == 1) CurrentState.IsBold = true; //加粗
771+
if(p2 == 4) CurrentState.IsUnderLine = true; //下划
772+
if (p2 == 7) //反转颜色
751773
{
752-
//重置背景色
753774
CurrentState.Background = -1;
775+
CurrentState.Foreground = -1;
754776
}
755777
break;
756778
default:
757779
throw new ArgumentOutOfRangeException();
758780
}
759-
//指针往后挪
760-
ptr += len;
761781
}
762782

763783
//如果最后还有未处理的文本

0 commit comments

Comments
 (0)