mirror of
https://github.com/Leonmmcoset/CMLeonOS.git
synced 2026-03-03 15:30:27 +00:00
Compare commits
3 Commits
5f4d97729a
...
c83988458a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c83988458a | ||
|
|
ffbc58eb40 | ||
|
|
c1e0651e4b |
@@ -1 +1 @@
|
|||||||
2026-02-26 13:45:40
|
2026-02-26 20:25:24
|
||||||
@@ -1 +1 @@
|
|||||||
e8e45ef
|
c1e0651
|
||||||
@@ -777,6 +777,14 @@ shutdown
|
|||||||
cuitest
|
cuitest
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### testtui
|
||||||
|
测试 TUI 框架。
|
||||||
|
|
||||||
|
**用法:**
|
||||||
|
```bash
|
||||||
|
testtui
|
||||||
|
```
|
||||||
|
|
||||||
### testgui
|
### testgui
|
||||||
测试图形界面。
|
测试图形界面。
|
||||||
|
|
||||||
@@ -812,8 +820,8 @@ snake
|
|||||||
**说明:**
|
**说明:**
|
||||||
- 使用方向键 (↑ ↓ ← →) 控制蛇的移动
|
- 使用方向键 (↑ ↓ ← →) 控制蛇的移动
|
||||||
- 按 ESC 或 Q 键退出游戏
|
- 按 ESC 或 Q 键退出游戏
|
||||||
- 蛇身用绿色 ■ 表示
|
- 蛇身用绿色 # 表示
|
||||||
- 食物用红色 ● 表示
|
- 食物用红色 O 表示
|
||||||
- 吃到食物得分 +10 分
|
- 吃到食物得分 +10 分
|
||||||
- 撞到墙壁或自己身体游戏结束
|
- 撞到墙壁或自己身体游戏结束
|
||||||
- 游戏区域:40x20 字符
|
- 游戏区域:40x20 字符
|
||||||
|
|||||||
595
editor/Nano.cs
595
editor/Nano.cs
@@ -1,9 +1,73 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using UniLua;
|
using UniLua;
|
||||||
|
|
||||||
namespace CMLeonOS
|
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
|
public class Nano
|
||||||
{
|
{
|
||||||
private string path;
|
private string path;
|
||||||
@@ -22,6 +86,7 @@ namespace CMLeonOS
|
|||||||
private FileSystem fileSystem;
|
private FileSystem fileSystem;
|
||||||
private UserSystem userSystem;
|
private UserSystem userSystem;
|
||||||
private Shell shell;
|
private Shell shell;
|
||||||
|
private NanoSettings settings = new NanoSettings();
|
||||||
|
|
||||||
private readonly (string, string)[] SHORTCUTS = new (string, string)[]
|
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));
|
string line = lines[i].Substring(scrollX, Math.Min(consoleWidth, lines[i].Length - scrollX));
|
||||||
|
|
||||||
if (IsLuaFile())
|
if (IsLuaFile() && settings.EnableSyntaxHighlight)
|
||||||
{
|
{
|
||||||
RenderLuaLine(line, consoleWidth);
|
RenderLuaLine(line, consoleWidth);
|
||||||
}
|
}
|
||||||
@@ -140,6 +205,8 @@ namespace CMLeonOS
|
|||||||
int pos = 0;
|
int pos = 0;
|
||||||
bool inString = false;
|
bool inString = false;
|
||||||
bool inComment = false;
|
bool inComment = false;
|
||||||
|
bool inLongComment = false;
|
||||||
|
char stringDelimiter = '\0';
|
||||||
|
|
||||||
while (pos < line.Length && pos < consoleWidth)
|
while (pos < line.Length && pos < consoleWidth)
|
||||||
{
|
{
|
||||||
@@ -149,42 +216,130 @@ namespace CMLeonOS
|
|||||||
Console.Write(line[pos]);
|
Console.Write(line[pos]);
|
||||||
pos++;
|
pos++;
|
||||||
}
|
}
|
||||||
else if (line.Substring(pos).StartsWith("--"))
|
else if (inLongComment)
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
|
if (pos + 1 < line.Length && line[pos] == ']' && line[pos + 1] == ']')
|
||||||
|
{
|
||||||
|
inLongComment = false;
|
||||||
|
Console.Write(line[pos]);
|
||||||
|
pos++;
|
||||||
|
Console.Write(line[pos]);
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
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;
|
inComment = true;
|
||||||
Console.ForegroundColor = ConsoleColor.Green;
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
Console.Write(line.Substring(pos));
|
Console.Write(line.Substring(pos));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else if (inString)
|
else if (inString)
|
||||||
{
|
|
||||||
if (line[pos] == '"' || line[pos] == '\'')
|
|
||||||
{
|
{
|
||||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||||
|
if (line[pos] == stringDelimiter)
|
||||||
|
{
|
||||||
|
if (pos + 1 < line.Length && line[pos + 1] == stringDelimiter)
|
||||||
|
{
|
||||||
Console.Write(line[pos]);
|
Console.Write(line[pos]);
|
||||||
inString = false;
|
pos++;
|
||||||
|
Console.Write(line[pos]);
|
||||||
|
pos++;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
|
||||||
Console.Write(line[pos]);
|
Console.Write(line[pos]);
|
||||||
}
|
inString = false;
|
||||||
pos++;
|
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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (line[pos] == '"' || line[pos] == '\'')
|
if (line[pos] == '"' || line[pos] == '\'')
|
||||||
{
|
{
|
||||||
inString = true;
|
inString = true;
|
||||||
|
stringDelimiter = line[pos];
|
||||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||||
Console.Write(line[pos]);
|
Console.Write(line[pos]);
|
||||||
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))
|
else if (IsLuaKeyword(line, pos))
|
||||||
{
|
{
|
||||||
Console.ForegroundColor = ConsoleColor.Cyan;
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||||
Console.Write(line[pos]);
|
string keyword = GetLuaKeyword(line, pos);
|
||||||
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
|
else
|
||||||
{
|
{
|
||||||
@@ -203,16 +358,50 @@ namespace CMLeonOS
|
|||||||
Console.ResetColor();
|
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)
|
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)
|
foreach (string keyword in keywords)
|
||||||
{
|
{
|
||||||
if (pos + keyword.Length <= line.Length && line.Substring(pos, keyword.Length) == keyword)
|
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] : ' ';
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -221,10 +410,136 @@ namespace CMLeonOS
|
|||||||
return false;
|
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.
|
// Insert a new line at the cursor.
|
||||||
private void InsertLine()
|
private void InsertLine()
|
||||||
{
|
{
|
||||||
string line = lines[currentLine];
|
string line = lines[currentLine];
|
||||||
|
|
||||||
|
if (IsLuaFile() && settings.EnableSmartIndent)
|
||||||
|
{
|
||||||
|
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
|
||||||
|
{
|
||||||
if (linePos == line.Length)
|
if (linePos == line.Length)
|
||||||
{
|
{
|
||||||
lines.Insert(currentLine + 1, string.Empty);
|
lines.Insert(currentLine + 1, string.Empty);
|
||||||
@@ -234,21 +549,51 @@ namespace CMLeonOS
|
|||||||
lines.Insert(currentLine + 1, line.Substring(linePos, line.Length - linePos));
|
lines.Insert(currentLine + 1, line.Substring(linePos, line.Length - linePos));
|
||||||
lines[currentLine] = line.Remove(linePos, line.Length - linePos);
|
lines[currentLine] = line.Remove(linePos, line.Length - linePos);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
updatedLinesStart = currentLine;
|
updatedLinesStart = currentLine;
|
||||||
updatedLinesEnd = lines.Count - 1;
|
updatedLinesEnd = lines.Count - 1;
|
||||||
|
|
||||||
currentLine += 1;
|
currentLine += 1;
|
||||||
linePos = 0;
|
linePos = lines[currentLine].Length;
|
||||||
|
|
||||||
modified = true;
|
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.
|
// Insert text at the cursor.
|
||||||
private void Insert(string text)
|
private void Insert(string text)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < text.Length; i++)
|
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++;
|
linePos++;
|
||||||
|
|
||||||
updatedLinesStart = currentLine;
|
updatedLinesStart = currentLine;
|
||||||
@@ -257,6 +602,54 @@ namespace CMLeonOS
|
|||||||
modified = true;
|
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()
|
private void InsertTab()
|
||||||
{
|
{
|
||||||
Insert(" ");
|
Insert(" ");
|
||||||
@@ -279,6 +672,17 @@ namespace CMLeonOS
|
|||||||
}
|
}
|
||||||
else
|
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);
|
lines[currentLine] = lines[currentLine].Remove(linePos - 1, 1);
|
||||||
linePos--;
|
linePos--;
|
||||||
|
|
||||||
@@ -288,6 +692,15 @@ namespace CMLeonOS
|
|||||||
modified = true;
|
modified = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool IsMatchingPair(char open, char close)
|
||||||
|
{
|
||||||
|
return (open == '(' && close == ')') ||
|
||||||
|
(open == '[' && close == ']') ||
|
||||||
|
(open == '{' && close == '}') ||
|
||||||
|
(open == '"' && close == '"') ||
|
||||||
|
(open == '\'' && close == '\'');
|
||||||
|
}
|
||||||
|
|
||||||
// Move the cursor left.
|
// Move the cursor left.
|
||||||
private void MoveLeft()
|
private void MoveLeft()
|
||||||
{
|
{
|
||||||
@@ -812,6 +1225,9 @@ namespace CMLeonOS
|
|||||||
}
|
}
|
||||||
switch (key.Key)
|
switch (key.Key)
|
||||||
{
|
{
|
||||||
|
case ConsoleKey.F2:
|
||||||
|
ShowSettingsMenu();
|
||||||
|
return true;
|
||||||
case ConsoleKey.Tab:
|
case ConsoleKey.Tab:
|
||||||
InsertTab();
|
InsertTab();
|
||||||
break;
|
break;
|
||||||
@@ -917,12 +1333,163 @@ namespace CMLeonOS
|
|||||||
RenderShortcuts(SHORTCUTS);
|
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()
|
public void Start()
|
||||||
{
|
{
|
||||||
Console.BackgroundColor = ConsoleColor.Black;
|
Console.BackgroundColor = ConsoleColor.Black;
|
||||||
Console.ForegroundColor = ConsoleColor.White;
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
Console.Clear();
|
Console.Clear();
|
||||||
|
|
||||||
|
settings.Load(@"0:\system\nano.dat");
|
||||||
|
|
||||||
RenderUI();
|
RenderUI();
|
||||||
while (!quit)
|
while (!quit)
|
||||||
{
|
{
|
||||||
@@ -937,6 +1504,8 @@ namespace CMLeonOS
|
|||||||
UpdateScrolling();
|
UpdateScrolling();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
settings.Save(@"0:\system\nano.dat");
|
||||||
|
|
||||||
Console.BackgroundColor = ConsoleColor.Black;
|
Console.BackgroundColor = ConsoleColor.Black;
|
||||||
Console.ForegroundColor = ConsoleColor.White;
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
Console.Clear();
|
Console.Clear();
|
||||||
|
|||||||
Reference in New Issue
Block a user