Compare commits

...

3 Commits

Author SHA1 Message Date
Leonmmcoset
c83988458a 更新文档 2026-02-26 22:36:40 +08:00
Leonmmcoset
ffbc58eb40 Nano超级强大功能更新 2026-02-26 21:12:06 +08:00
Leonmmcoset
c1e0651e4b 更好的Lua代码高亮 2026-02-26 18:38:07 +08:00
4 changed files with 604 additions and 27 deletions

View File

@@ -1 +1 @@
2026-02-26 13:45:40
2026-02-26 20:25:24

View File

@@ -1 +1 @@
e8e45ef
c1e0651

View File

@@ -777,6 +777,14 @@ shutdown
cuitest
```
### testtui
测试 TUI 框架。
**用法:**
```bash
testtui
```
### testgui
测试图形界面。
@@ -812,8 +820,8 @@ snake
**说明:**
- 使用方向键 (↑ ↓ ← →) 控制蛇的移动
- 按 ESC 或 Q 键退出游戏
- 蛇身用绿色 表示
- 食物用红色 表示
- 蛇身用绿色 # 表示
- 食物用红色 O 表示
- 吃到食物得分 +10 分
- 撞到墙壁或自己身体游戏结束
- 游戏区域40x20 字符

View File

@@ -1,9 +1,73 @@
using System;
using System.Collections.Generic;
using System.IO;
using UniLua;
namespace CMLeonOS
{
public class NanoSettings
{
public bool EnableSmartIndent { get; set; } = true;
public bool EnableAutoCompleteBrackets { get; set; } = true;
public bool EnableAutoCompleteQuotes { get; set; } = true;
public bool EnableSmartDelete { get; set; } = true;
public bool EnableSyntaxHighlight { get; set; } = true;
public void Save(string filePath)
{
try
{
bool smartIndent = EnableSmartIndent;
bool autoBrackets = EnableAutoCompleteBrackets;
bool autoQuotes = EnableAutoCompleteQuotes;
bool smartDelete = EnableSmartDelete;
bool syntaxHighlight = EnableSyntaxHighlight;
string content = $"{smartIndent},{autoBrackets},{autoQuotes},{smartDelete},{syntaxHighlight}";
using (StreamWriter sw = new StreamWriter(filePath))
{
sw.Write(content);
}
}
catch
{
}
}
public void Load(string filePath)
{
try
{
if (File.Exists(filePath))
{
using (StreamReader sr = new StreamReader(filePath))
{
string content = sr.ReadToEnd();
string[] parts = content.Split(',');
if (parts.Length >= 5)
{
bool smartIndent, autoBrackets, autoQuotes, smartDelete, syntaxHighlight;
bool.TryParse(parts[0], out smartIndent);
bool.TryParse(parts[1], out autoBrackets);
bool.TryParse(parts[2], out autoQuotes);
bool.TryParse(parts[3], out smartDelete);
bool.TryParse(parts[4], out syntaxHighlight);
EnableSmartIndent = smartIndent;
EnableAutoCompleteBrackets = autoBrackets;
EnableAutoCompleteQuotes = autoQuotes;
EnableSmartDelete = smartDelete;
EnableSyntaxHighlight = syntaxHighlight;
}
}
}
}
catch
{
}
}
}
public class Nano
{
private string path;
@@ -22,6 +86,7 @@ namespace CMLeonOS
private FileSystem fileSystem;
private UserSystem userSystem;
private Shell shell;
private NanoSettings settings = new NanoSettings();
private readonly (string, string)[] SHORTCUTS = new (string, string)[]
{
@@ -107,7 +172,7 @@ namespace CMLeonOS
{
string line = lines[i].Substring(scrollX, Math.Min(consoleWidth, lines[i].Length - scrollX));
if (IsLuaFile())
if (IsLuaFile() && settings.EnableSyntaxHighlight)
{
RenderLuaLine(line, consoleWidth);
}
@@ -140,6 +205,8 @@ namespace CMLeonOS
int pos = 0;
bool inString = false;
bool inComment = false;
bool inLongComment = false;
char stringDelimiter = '\0';
while (pos < line.Length && pos < consoleWidth)
{
@@ -149,42 +216,130 @@ namespace CMLeonOS
Console.Write(line[pos]);
pos++;
}
else if (line.Substring(pos).StartsWith("--"))
else if (inLongComment)
{
inComment = true;
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(line.Substring(pos));
break;
}
else if (inString)
{
if (line[pos] == '"' || line[pos] == '\'')
if (pos + 1 < line.Length && line[pos] == ']' && line[pos + 1] == ']')
{
Console.ForegroundColor = ConsoleColor.Yellow;
inLongComment = false;
Console.Write(line[pos]);
inString = false;
pos++;
Console.Write(line[pos]);
pos++;
}
else
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(line[pos]);
pos++;
}
}
else if (pos + 1 < line.Length && line[pos] == '-' && line[pos + 1] == '-')
{
if (pos + 3 < line.Length && line[pos + 2] == '[' && line[pos + 3] == '[')
{
inLongComment = true;
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(line.Substring(pos));
break;
}
else
{
inComment = true;
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(line.Substring(pos));
break;
}
}
else if (inString)
{
Console.ForegroundColor = ConsoleColor.Yellow;
if (line[pos] == stringDelimiter)
{
if (pos + 1 < line.Length && line[pos + 1] == stringDelimiter)
{
Console.Write(line[pos]);
pos++;
Console.Write(line[pos]);
pos++;
}
else
{
Console.Write(line[pos]);
inString = false;
pos++;
}
}
else if (line[pos] == '\\')
{
Console.Write(line[pos]);
pos++;
if (pos < line.Length)
{
Console.Write(line[pos]);
pos++;
}
}
else
{
Console.Write(line[pos]);
pos++;
}
pos++;
}
else
{
if (line[pos] == '"' || line[pos] == '\'')
{
inString = true;
stringDelimiter = line[pos];
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(line[pos]);
pos++;
}
else if (IsLuaNumber(line, pos))
{
Console.ForegroundColor = ConsoleColor.Magenta;
while (pos < line.Length && IsLuaNumber(line, pos))
{
Console.Write(line[pos]);
pos++;
}
}
else if (IsLuaOperator(line, pos))
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
if (pos + 1 < line.Length && IsLuaOperator(line, pos + 1))
{
Console.Write(line[pos]);
pos++;
Console.Write(line[pos]);
pos++;
}
else
{
Console.Write(line[pos]);
pos++;
}
}
else if (IsLuaKeyword(line, pos))
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write(line[pos]);
pos++;
string keyword = GetLuaKeyword(line, pos);
Console.Write(keyword);
pos += keyword.Length;
}
else if (IsLuaBuiltin(line, pos))
{
Console.ForegroundColor = ConsoleColor.DarkCyan;
string builtin = GetLuaBuiltin(line, pos);
Console.Write(builtin);
pos += builtin.Length;
}
else if (IsLuaFunctionCall(line, pos))
{
Console.ForegroundColor = ConsoleColor.DarkMagenta;
string funcName = GetLuaFunctionName(line, pos);
Console.Write(funcName);
pos += funcName.Length;
}
else
{
@@ -203,16 +358,50 @@ namespace CMLeonOS
Console.ResetColor();
}
private bool IsLuaNumber(string line, int pos)
{
if (pos >= line.Length) return false;
char c = line[pos];
if (!char.IsDigit(c) && c != '.') return false;
if (c == '.')
{
if (pos + 1 >= line.Length) return false;
return char.IsDigit(line[pos + 1]);
}
return true;
}
private bool IsLuaOperator(string line, int pos)
{
if (pos >= line.Length) return false;
char c = line[pos];
return c == '+' || c == '-' || c == '*' || c == '/' || c == '^' ||
c == '%' || c == '#' || c == '=' || c == '~' ||
c == '<' || c == '>' || c == '&' || c == '|' ||
c == '(' || c == ')' || c == '[' || c == ']' ||
c == '{' || c == '}' || c == ',' || c == ';';
}
private bool IsLuaKeyword(string line, int pos)
{
string[] keywords = { "function", "end", "if", "then", "else", "elseif", "while", "do", "for", "return", "local", "true", "false", "nil", "and", "or", "not", "break", "repeat", "until" };
string[] keywords = {
"function", "end", "if", "then", "else", "elseif", "while", "do",
"for", "return", "local", "true", "false", "nil", "and", "or",
"not", "break", "repeat", "until", "in", "goto", "self"
};
foreach (string keyword in keywords)
{
if (pos + keyword.Length <= line.Length && line.Substring(pos, keyword.Length) == keyword)
{
char prevChar = pos > 0 ? line[pos - 1] : ' ';
char nextChar = pos + keyword.Length < line.Length ? line[pos + keyword.Length] : ' ';
if (!char.IsLetterOrDigit(nextChar))
if (!char.IsLetterOrDigit(prevChar) && !char.IsLetterOrDigit(nextChar) && prevChar != '_' && nextChar != '_')
{
return true;
}
@@ -221,34 +410,190 @@ namespace CMLeonOS
return false;
}
private string GetLuaKeyword(string line, int pos)
{
string[] keywords = {
"function", "end", "if", "then", "else", "elseif", "while", "do",
"for", "return", "local", "true", "false", "nil", "and", "or",
"not", "break", "repeat", "until", "in", "goto", "self"
};
foreach (string keyword in keywords)
{
if (pos + keyword.Length <= line.Length && line.Substring(pos, keyword.Length) == keyword)
{
char prevChar = pos > 0 ? line[pos - 1] : ' ';
char nextChar = pos + keyword.Length < line.Length ? line[pos + keyword.Length] : ' ';
if (!char.IsLetterOrDigit(prevChar) && !char.IsLetterOrDigit(nextChar) && prevChar != '_' && nextChar != '_')
{
return keyword;
}
}
}
return "";
}
private bool IsLuaBuiltin(string line, int pos)
{
string[] builtins = {
"print", "type", "tonumber", "tostring", "pairs", "ipairs",
"next", "select", "unpack", "assert", "error", "pcall",
"xpcall", "load", "loadstring", "loadfile", "dofile",
"require", "setmetatable", "getmetatable", "rawget", "rawset",
"rawequal", "rawlen", "gcinfo", "collectgarbage", "newproxy"
};
foreach (string builtin in builtins)
{
if (pos + builtin.Length <= line.Length && line.Substring(pos, builtin.Length) == builtin)
{
char prevChar = pos > 0 ? line[pos - 1] : ' ';
char nextChar = pos + builtin.Length < line.Length ? line[pos + builtin.Length] : ' ';
if (!char.IsLetterOrDigit(prevChar) && !char.IsLetterOrDigit(nextChar) && prevChar != '_' && nextChar != '_')
{
return true;
}
}
}
return false;
}
private string GetLuaBuiltin(string line, int pos)
{
string[] builtins = {
"print", "type", "tonumber", "tostring", "pairs", "ipairs",
"next", "select", "unpack", "assert", "error", "pcall",
"xpcall", "load", "loadstring", "loadfile", "dofile",
"require", "setmetatable", "getmetatable", "rawget", "rawset",
"rawequal", "rawlen", "gcinfo", "collectgarbage", "newproxy"
};
foreach (string builtin in builtins)
{
if (pos + builtin.Length <= line.Length && line.Substring(pos, builtin.Length) == builtin)
{
char prevChar = pos > 0 ? line[pos - 1] : ' ';
char nextChar = pos + builtin.Length < line.Length ? line[pos + builtin.Length] : ' ';
if (!char.IsLetterOrDigit(prevChar) && !char.IsLetterOrDigit(nextChar) && prevChar != '_' && nextChar != '_')
{
return builtin;
}
}
}
return "";
}
private bool IsLuaFunctionCall(string line, int pos)
{
if (pos >= line.Length || !char.IsLetter(line[pos]) && line[pos] != '_') return false;
int endPos = pos;
while (endPos < line.Length && (char.IsLetterOrDigit(line[endPos]) || line[endPos] == '_'))
{
endPos++;
}
if (endPos < line.Length && line[endPos] == '(')
{
return true;
}
return false;
}
private string GetLuaFunctionName(string line, int pos)
{
int endPos = pos;
while (endPos < line.Length && (char.IsLetterOrDigit(line[endPos]) || line[endPos] == '_'))
{
endPos++;
}
return line.Substring(pos, endPos - pos);
}
// Insert a new line at the cursor.
private void InsertLine()
{
string line = lines[currentLine];
if (linePos == line.Length)
if (IsLuaFile() && settings.EnableSmartIndent)
{
lines.Insert(currentLine + 1, string.Empty);
string currentIndent = GetLineIndent(line);
if (linePos == line.Length)
{
lines.Insert(currentLine + 1, currentIndent);
}
else
{
lines.Insert(currentLine + 1, currentIndent + line.Substring(linePos, line.Length - linePos));
lines[currentLine] = line.Remove(linePos, line.Length - linePos);
}
if (line.TrimEnd().EndsWith("then") || line.TrimEnd().EndsWith("do") || line.TrimEnd().EndsWith("else") || line.TrimEnd().EndsWith("elseif") || line.TrimEnd().EndsWith("repeat"))
{
lines[currentLine + 1] = lines[currentLine + 1] + " ";
}
}
else
{
lines.Insert(currentLine + 1, line.Substring(linePos, line.Length - linePos));
lines[currentLine] = line.Remove(linePos, line.Length - linePos);
if (linePos == line.Length)
{
lines.Insert(currentLine + 1, string.Empty);
}
else
{
lines.Insert(currentLine + 1, line.Substring(linePos, line.Length - linePos));
lines[currentLine] = line.Remove(linePos, line.Length - linePos);
}
}
updatedLinesStart = currentLine;
updatedLinesEnd = lines.Count - 1;
currentLine += 1;
linePos = 0;
linePos = lines[currentLine].Length;
modified = true;
}
private string GetLineIndent(string line)
{
int indent = 0;
foreach (char c in line)
{
if (c == ' ')
{
indent++;
}
else if (c == '\t')
{
indent += 4;
}
else
{
break;
}
}
return new string(' ', indent);
}
// Insert text at the cursor.
private void Insert(string text)
{
for (int i = 0; i < text.Length; i++)
{
lines[currentLine] = lines[currentLine].Insert(linePos, text[i].ToString());
char c = text[i];
if (IsLuaFile())
{
c = HandleLuaAutoComplete(c);
}
lines[currentLine] = lines[currentLine].Insert(linePos, c.ToString());
linePos++;
updatedLinesStart = currentLine;
@@ -257,6 +602,54 @@ namespace CMLeonOS
modified = true;
}
private char HandleLuaAutoComplete(char c)
{
string line = lines[currentLine];
if (settings.EnableAutoCompleteBrackets)
{
if (c == '(')
{
lines[currentLine] = lines[currentLine].Insert(linePos, ")");
return '(';
}
else if (c == '[')
{
lines[currentLine] = lines[currentLine].Insert(linePos, "]");
return '[';
}
else if (c == '{')
{
lines[currentLine] = lines[currentLine].Insert(linePos, "}");
return '{';
}
}
if (settings.EnableAutoCompleteQuotes)
{
if (c == '"')
{
if (linePos > 0 && line[linePos - 1] == '\\')
{
return '"';
}
lines[currentLine] = lines[currentLine].Insert(linePos, "\"");
return '"';
}
else if (c == '\'')
{
if (linePos > 0 && line[linePos - 1] == '\\')
{
return '\'';
}
lines[currentLine] = lines[currentLine].Insert(linePos, "'");
return '\'';
}
}
return c;
}
private void InsertTab()
{
Insert(" ");
@@ -279,6 +672,17 @@ namespace CMLeonOS
}
else
{
if (IsLuaFile() && settings.EnableSmartDelete)
{
string line = lines[currentLine];
char charToDelete = line[linePos - 1];
if (linePos < line.Length && IsMatchingPair(charToDelete, line[linePos]))
{
lines[currentLine] = lines[currentLine].Remove(linePos, 1);
}
}
lines[currentLine] = lines[currentLine].Remove(linePos - 1, 1);
linePos--;
@@ -288,6 +692,15 @@ namespace CMLeonOS
modified = true;
}
private bool IsMatchingPair(char open, char close)
{
return (open == '(' && close == ')') ||
(open == '[' && close == ']') ||
(open == '{' && close == '}') ||
(open == '"' && close == '"') ||
(open == '\'' && close == '\'');
}
// Move the cursor left.
private void MoveLeft()
{
@@ -812,6 +1225,9 @@ namespace CMLeonOS
}
switch (key.Key)
{
case ConsoleKey.F2:
ShowSettingsMenu();
return true;
case ConsoleKey.Tab:
InsertTab();
break;
@@ -917,12 +1333,163 @@ namespace CMLeonOS
RenderShortcuts(SHORTCUTS);
}
private void ShowSettingsMenu()
{
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
int consoleWidth = 80;
int consoleHeight = 25;
string title = " Nano Settings";
string[] options = new string[]
{
"1. Smart Indent",
"2. Auto Complete Brackets",
"3. Auto Complete Quotes",
"4. Smart Delete",
"5. Syntax Highlight",
"6. Save and Exit"
};
int maxOptionLength = 0;
foreach (string option in options)
{
if (option.Length > maxOptionLength)
{
maxOptionLength = option.Length;
}
}
int menuWidth = maxOptionLength + 10;
int menuHeight = options.Length + 4;
int menuX = (consoleWidth - menuWidth) / 2;
int menuY = (consoleHeight - menuHeight) / 2;
int titleX = menuX + (menuWidth - title.Length) / 2;
Console.SetCursorPosition(titleX, menuY);
Console.Write(title);
int borderLeft = menuX;
int borderRight = menuX + menuWidth - 1;
int borderTop = menuY;
int borderBottom = menuY + menuHeight - 1;
for (int i = 0; i < menuWidth; i++)
{
Console.SetCursorPosition(borderLeft + i, borderTop);
Console.Write("-");
}
for (int i = 0; i < menuHeight; i++)
{
Console.SetCursorPosition(borderLeft, borderTop + 1 + i);
Console.Write("|");
Console.SetCursorPosition(borderRight, borderTop + 1 + i);
Console.Write("|");
}
for (int i = 0; i < menuWidth; i++)
{
Console.SetCursorPosition(borderLeft + i, borderBottom);
Console.Write("-");
}
for (int i = 0; i < options.Length; i++)
{
int optionY = borderTop + 2 + i;
Console.SetCursorPosition(borderLeft + 2, optionY);
Console.Write(options[i]);
string status = GetSettingStatus(i);
int statusX = borderRight - status.Length - 2;
Console.SetCursorPosition(statusX, optionY);
Console.Write($"[{status}]");
}
int promptY = borderBottom - 2;
string prompt = "Use 1-6 to toggle, ESC to exit";
int promptX = menuX + (menuWidth - prompt.Length) / 2;
Console.SetCursorPosition(promptX, promptY);
Console.Write(prompt);
while (true)
{
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Escape)
{
break;
}
else if (key.KeyChar >= '1' && key.KeyChar <= '6')
{
int option = key.KeyChar - '1';
ToggleSetting(option);
ShowSettingsMenu();
break;
}
}
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
RenderUI();
updatedLinesStart = 0;
updatedLinesEnd = lines.Count - 1;
}
private string GetSettingStatus(int option)
{
switch (option)
{
case 0:
return settings.EnableSmartIndent ? "ON " : "OFF";
case 1:
return settings.EnableAutoCompleteBrackets ? "ON " : "OFF";
case 2:
return settings.EnableAutoCompleteQuotes ? "ON " : "OFF";
case 3:
return settings.EnableSmartDelete ? "ON " : "OFF";
case 4:
return settings.EnableSyntaxHighlight ? "ON " : "OFF";
default:
return "";
}
}
private void ToggleSetting(int option)
{
switch (option)
{
case 0:
settings.EnableSmartIndent = !settings.EnableSmartIndent;
break;
case 1:
settings.EnableAutoCompleteBrackets = !settings.EnableAutoCompleteBrackets;
break;
case 2:
settings.EnableAutoCompleteQuotes = !settings.EnableAutoCompleteQuotes;
break;
case 3:
settings.EnableSmartDelete = !settings.EnableSmartDelete;
break;
case 4:
settings.EnableSyntaxHighlight = !settings.EnableSyntaxHighlight;
break;
case 5:
break;
}
}
public void Start()
{
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
settings.Load(@"0:\system\nano.dat");
RenderUI();
while (!quit)
{
@@ -936,6 +1503,8 @@ namespace CMLeonOS
HandleInput();
UpdateScrolling();
}
settings.Save(@"0:\system\nano.dat");
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;