add basic input navigation, newlines are broken for now
This commit is contained in:
parent
a3bbbd09a8
commit
6ae94d33b0
|
@ -629,8 +629,8 @@ namespace telegram {
|
||||||
public override void Handler(List<string> inputParams) {
|
public override void Handler(List<string> inputParams) {
|
||||||
try {
|
try {
|
||||||
if (inputParams.Count == 0) {
|
if (inputParams.Count == 0) {
|
||||||
currentInputLine = "/e " + ((TdApi.MessageContent.MessageText) lastMessage?.Content)?.Text?.Text;
|
SetInputLine("/e " + ((TdApi.MessageContent.MessageText) lastMessage?.Content)?.Text?.Text);
|
||||||
Emojis.ForEach(em => currentInputLine = currentInputLine.Replace(em.Item2, em.Item1));
|
Emojis.ForEach(em => SetInputLine(currentInputLine.Replace(em.Item2, em.Item1)));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -702,7 +702,7 @@ namespace telegram {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
currentInputLine = $"/rd {result.Id} ";
|
SetInputLine($"/rd {result.Id} ");
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
lock (@lock)
|
lock (@lock)
|
||||||
|
|
|
@ -377,6 +377,63 @@ namespace telegram {
|
||||||
new Tuple<string, string>(":shrugm:", Emoji.ManShrugging.Sequence.AsString)
|
new Tuple<string, string>(":shrugm:", Emoji.ManShrugging.Sequence.AsString)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public static void InsertToInputLine(string strToInsert) {
|
||||||
|
var part1 = currentInputLine.Substring(0, currentInputPos);
|
||||||
|
var part2 = currentInputLine.Substring(currentInputPos);
|
||||||
|
currentInputLine = part1 + strToInsert + part2;
|
||||||
|
currentInputPos += strToInsert.Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void SetInputLine(string newInputLine) {
|
||||||
|
currentInputLine = newInputLine;
|
||||||
|
currentInputPos = newInputLine.Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void RemoveFromInputLine(bool word = false) {
|
||||||
|
var part1 = currentInputLine.Substring(0, currentInputPos);
|
||||||
|
var oldlen = part1.Length;
|
||||||
|
var part2 = currentInputLine.Substring(currentInputPos);
|
||||||
|
if (word) {
|
||||||
|
var lastIndex = part1.TrimEnd().LastIndexOf(" ", StringComparison.Ordinal);
|
||||||
|
if (lastIndex < 0)
|
||||||
|
lastIndex = 0;
|
||||||
|
part1 = part1.Substring(0, lastIndex);
|
||||||
|
if (lastIndex != 0)
|
||||||
|
part1 += " ";
|
||||||
|
//if (part1.EndsWith("⏎"))
|
||||||
|
// part1 = part1.Remove(part1.Length - 1);
|
||||||
|
|
||||||
|
var newlen = part1.Length;
|
||||||
|
|
||||||
|
currentInputLine = part1 + part2;
|
||||||
|
currentInputPos -= oldlen - newlen;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentInputLine = part1.Substring(0, part1.Length - 1) + part2;
|
||||||
|
currentInputPos--;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void RemoveFromInputLineForward(bool word = false) {
|
||||||
|
var part1 = currentInputLine.Substring(0, currentInputPos);
|
||||||
|
var part2 = currentInputLine.Substring(currentInputPos);
|
||||||
|
if (word) {
|
||||||
|
var index = part2.IndexOf(" ", StringComparison.Ordinal);
|
||||||
|
if (index < 0)
|
||||||
|
index = part2.Length - 1;
|
||||||
|
part2 = part2.Substring(0, index);
|
||||||
|
if (index != 0)
|
||||||
|
part2 = " " + part2;
|
||||||
|
//if (part2.StartsWith("⏎"))
|
||||||
|
// part2 = part2.Remove(part1.Length - 1);
|
||||||
|
|
||||||
|
currentInputLine = part1 + part2;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentInputLine = part1 + part2.Substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
public static readonly List<ConsoleKey> SpecialKeys = new List<ConsoleKey> {
|
public static readonly List<ConsoleKey> SpecialKeys = new List<ConsoleKey> {
|
||||||
ConsoleKey.Backspace,
|
ConsoleKey.Backspace,
|
||||||
ConsoleKey.Tab,
|
ConsoleKey.Tab,
|
||||||
|
|
|
@ -13,12 +13,10 @@ using static telegram.CommandManager;
|
||||||
namespace telegram {
|
namespace telegram {
|
||||||
/*
|
/*
|
||||||
* TODO:
|
* TODO:
|
||||||
|
* fix newlines with input nav...
|
||||||
* unreads are unreliable in secret chats!
|
* unreads are unreliable in secret chats!
|
||||||
* mute,unmute chats
|
* mute,unmute chats
|
||||||
* photo & document download & show externally
|
* photo & document download & show externally
|
||||||
* cursor input nav (up/down history, (alt +) left/right)
|
|
||||||
* ref: http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html#cursor-navigation
|
|
||||||
* ref: https://en.wikipedia.org/wiki/ANSI_escape_code#Escape_sequences
|
|
||||||
* refactor everything
|
* refactor everything
|
||||||
* re-evaluate ClearCurrentConsoleLine function
|
* re-evaluate ClearCurrentConsoleLine function
|
||||||
* When TDLib 1.6 is released: implement contacts
|
* When TDLib 1.6 is released: implement contacts
|
||||||
|
@ -36,9 +34,10 @@ namespace telegram {
|
||||||
public static volatile Td.TdApi.Message lastMessage;
|
public static volatile Td.TdApi.Message lastMessage;
|
||||||
public static volatile bool quitting;
|
public static volatile bool quitting;
|
||||||
public static volatile string currentInputLine = "";
|
public static volatile string currentInputLine = "";
|
||||||
public static volatile List<string> messageQueue = new List<string>();
|
public static volatile int currentInputPos;
|
||||||
public static volatile List<string> missedMessages = new List<string>();
|
public static volatile List<string> messageQueue = new List<string>();
|
||||||
public static volatile string prefix = "[tgcli";
|
public static volatile List<string> missedMessages = new List<string>();
|
||||||
|
public static volatile string prefix = "[tgcli";
|
||||||
public static volatile bool silent;
|
public static volatile bool silent;
|
||||||
|
|
||||||
public static volatile object @lock = new object();
|
public static volatile object @lock = new object();
|
||||||
|
@ -195,8 +194,10 @@ namespace telegram {
|
||||||
else
|
else
|
||||||
output += "]";
|
output += "]";
|
||||||
output += " > ";
|
output += " > ";
|
||||||
|
var prefixlen = GetActualStringWidth(output);
|
||||||
output += TruncateMessageStart(currentInputLine, Console.LargestWindowWidth - GetActualStringWidth(output));
|
output += TruncateMessageStart(currentInputLine, Console.LargestWindowWidth - GetActualStringWidth(output));
|
||||||
Console.Write(output);
|
Console.Write(output);
|
||||||
|
Console.Write($"\u001b[{currentInputPos + prefixlen + 1}G");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,7 +210,7 @@ namespace telegram {
|
||||||
break;
|
break;
|
||||||
case ConsoleKey.Enter when currentInputLine.StartsWith("/"): {
|
case ConsoleKey.Enter when currentInputLine.StartsWith("/"): {
|
||||||
var command = currentInputLine.Substring(1);
|
var command = currentInputLine.Substring(1);
|
||||||
currentInputLine = "";
|
SetInputLine("");
|
||||||
HandleCommand(command);
|
HandleCommand(command);
|
||||||
ScreenUpdate();
|
ScreenUpdate();
|
||||||
return;
|
return;
|
||||||
|
@ -222,36 +223,74 @@ namespace telegram {
|
||||||
}
|
}
|
||||||
case ConsoleKey.Enter:
|
case ConsoleKey.Enter:
|
||||||
SendMessage(currentInputLine, currentChatId);
|
SendMessage(currentInputLine, currentChatId);
|
||||||
currentInputLine = "";
|
SetInputLine("");
|
||||||
ScreenUpdate();
|
ScreenUpdate();
|
||||||
break;
|
break;
|
||||||
case ConsoleKey.Backspace when currentInputLine.Length >= 1:
|
case ConsoleKey.Backspace when currentInputLine.Length >= 1 && currentInputPos >= 1:
|
||||||
if (key.Modifiers.HasFlag(ConsoleModifiers.Alt)) {
|
if (key.Modifiers.HasFlag(ConsoleModifiers.Alt)) {
|
||||||
var lastIndex = currentInputLine.TrimEnd().LastIndexOf(" ", StringComparison.Ordinal);
|
RemoveFromInputLine(true);
|
||||||
if (lastIndex < 0)
|
|
||||||
lastIndex = 0;
|
|
||||||
currentInputLine = currentInputLine.Substring(0, lastIndex);
|
|
||||||
if (lastIndex != 0)
|
|
||||||
currentInputLine += " ";
|
|
||||||
if (currentInputLine.EndsWith("⏎"))
|
|
||||||
currentInputLine = currentInputLine.Remove(currentInputLine.Length - 1);
|
|
||||||
ScreenUpdate();
|
ScreenUpdate();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
currentInputLine = currentInputLine.Substring(0, currentInputLine.Length - 1);
|
RemoveFromInputLine();
|
||||||
if (currentInputLine.EndsWith("⏎"))
|
//if (currentInputLine.EndsWith("⏎"))
|
||||||
currentInputLine = currentInputLine.Remove(currentInputLine.Length - 1);
|
// currentInputLine = currentInputLine.Remove(currentInputLine.Length - 1);
|
||||||
ScreenUpdate();
|
ScreenUpdate();
|
||||||
break;
|
break;
|
||||||
|
case ConsoleKey.Delete when currentInputLine.Length >= 1 && currentInputPos < currentInputLine.Length:
|
||||||
|
if (key.Modifiers.HasFlag(ConsoleModifiers.Alt)) {
|
||||||
|
RemoveFromInputLineForward(true);
|
||||||
|
ScreenUpdate();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
RemoveFromInputLineForward();
|
||||||
|
//if (currentInputLine.EndsWith("⏎"))
|
||||||
|
// currentInputLine = currentInputLine.Remove(currentInputLine.Length - 1);
|
||||||
|
ScreenUpdate();
|
||||||
|
break;
|
||||||
|
case ConsoleKey.LeftArrow when key.Modifiers.HasFlag(ConsoleModifiers.Shift):
|
||||||
|
if (currentInputPos == 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
var part1 = currentInputLine.Substring(0, currentInputPos);
|
||||||
|
var lastIndex = part1.TrimEnd().LastIndexOf(" ", StringComparison.Ordinal);
|
||||||
|
if (lastIndex < 0)
|
||||||
|
lastIndex = 0;
|
||||||
|
currentInputPos = lastIndex;
|
||||||
|
ScreenUpdate();
|
||||||
|
break;
|
||||||
|
case ConsoleKey.RightArrow when key.Modifiers.HasFlag(ConsoleModifiers.Shift):
|
||||||
|
if (currentInputPos >= currentInputLine.Length)
|
||||||
|
break;
|
||||||
|
|
||||||
|
var index = currentInputLine.IndexOf(" ", currentInputPos + 1, StringComparison.Ordinal);
|
||||||
|
currentInputPos = index + 1;
|
||||||
|
if (index < 0)
|
||||||
|
currentInputPos = currentInputLine.Length;
|
||||||
|
ScreenUpdate();
|
||||||
|
break;
|
||||||
|
case ConsoleKey.LeftArrow:
|
||||||
|
if (currentInputPos > 0)
|
||||||
|
currentInputPos--;
|
||||||
|
ScreenUpdate();
|
||||||
|
break;
|
||||||
|
case ConsoleKey.RightArrow:
|
||||||
|
if (currentInputPos < currentInputLine.Length)
|
||||||
|
currentInputPos++;
|
||||||
|
ScreenUpdate();
|
||||||
|
break;
|
||||||
|
case ConsoleKey.UpArrow: break;
|
||||||
|
case ConsoleKey.DownArrow: break;
|
||||||
default: {
|
default: {
|
||||||
switch (key.Key) {
|
switch (key.Key) {
|
||||||
case ConsoleKey.N when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
case ConsoleKey.N when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
||||||
currentInputLine += "⏎ ";
|
InsertToInputLine("⏎ ");
|
||||||
ScreenUpdate();
|
ScreenUpdate();
|
||||||
return;
|
return;
|
||||||
case ConsoleKey.D when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
case ConsoleKey.D when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
||||||
HandleCommand("q");
|
HandleCommand(currentChatId == 0 ? "q" : "c");
|
||||||
ScreenUpdate();
|
ScreenUpdate();
|
||||||
return;
|
return;
|
||||||
case ConsoleKey.Q when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
case ConsoleKey.Q when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
||||||
|
@ -267,8 +306,12 @@ namespace telegram {
|
||||||
ScreenUpdate();
|
ScreenUpdate();
|
||||||
return;
|
return;
|
||||||
case ConsoleKey.O when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
case ConsoleKey.O when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
||||||
if (string.IsNullOrWhiteSpace(currentInputLine))
|
SetInputLine(currentInputLine switch {
|
||||||
currentInputLine = "/o ";
|
"/o " => "/os ",
|
||||||
|
"/os " => "/o ",
|
||||||
|
"" => "/o ",
|
||||||
|
_ => currentInputLine
|
||||||
|
});
|
||||||
ScreenUpdate();
|
ScreenUpdate();
|
||||||
return;
|
return;
|
||||||
case ConsoleKey.L when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
case ConsoleKey.L when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
||||||
|
@ -278,7 +321,7 @@ namespace telegram {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!SpecialKeys.Contains(key.Key)) {
|
if (!SpecialKeys.Contains(key.Key)) {
|
||||||
currentInputLine += key.KeyChar;
|
InsertToInputLine(key.KeyChar.ToString());
|
||||||
ScreenUpdate();
|
ScreenUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue