-
Notifications
You must be signed in to change notification settings - Fork 685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #2865. Unknown character sequence while resizing terminal. #2951
Changes from all commits
bfa76a2
2411b8a
6af9e83
164f1b7
fee503e
1516580
6603935
68805cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -341,6 +341,7 @@ void ProcessInput () | |
Key k = Key.Null; | ||
|
||
if (code == Curses.KEY_CODE_YES) { | ||
var lastWch = wch; | ||
while (code == Curses.KEY_CODE_YES && wch == Curses.KeyResize) { | ||
ProcessWinChange (); | ||
code = Curses.get_wch (out wch); | ||
|
@@ -379,6 +380,25 @@ void ProcessInput () | |
} else if (wch >= 325 && wch <= 327) { // Shift+Alt+(F1 - F3) | ||
wch -= 60; | ||
k = Key.ShiftMask | Key.AltMask | MapCursesKey (wch); | ||
} else { | ||
code = Curses.get_wch (out wch); | ||
if (code == 0) { | ||
switch (wch) { | ||
// Shift code. | ||
case 16: | ||
keyModifiers.Shift = true; | ||
break; | ||
default: | ||
if (lastWch == Curses.KeyResize && wch == 91) { | ||
// Returns this keys to the std input which is a CSI (\x1b[). | ||
Curses.ungetch (91); // [ | ||
Curses.ungetch (27); // Esc | ||
return; | ||
} else { | ||
throw new Exception (); | ||
} | ||
} | ||
} | ||
} | ||
keyDownHandler (new KeyEvent (k, MapKeyModifiers (k))); | ||
keyHandler (new KeyEvent (k, MapKeyModifiers (k))); | ||
|
@@ -388,8 +408,6 @@ void ProcessInput () | |
|
||
// Special handling for ESC, we want to try to catch ESC+letter to simulate alt-letter as well as Alt-Fkey | ||
if (wch == 27) { | ||
Curses.timeout (10); | ||
|
||
code = Curses.get_wch (out int wch2); | ||
|
||
if (code == Curses.KEY_CODE_YES) { | ||
|
@@ -425,6 +443,56 @@ void ProcessInput () | |
} else if (wch >= (uint)Key.A && wch <= (uint)Key.Z) { | ||
keyModifiers.Shift = true; | ||
keyModifiers.Alt = true; | ||
} else if (wch2 == Curses.KeySS3) { | ||
while (code > -1) { | ||
code = Curses.get_wch (out wch2); | ||
if (code == 0) { | ||
switch (wch2) { | ||
case 16: | ||
BDisp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
keyModifiers.Shift = true; | ||
break; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may be smart enough, or have a good enough memory to remember what all these integers are, but I'm not. Where do these codes come from? They're not ASCII codes. What are they? I'm asking you to please make this code more readable and maintainable while you are in here. We should always be making code we touch simpler and more readable, even if we understand it ourselves. I have just spent 10 minutes trying to figure out where 108, 109, ...121 come from. No reader of the code should need to do that! And I still don't have a clue other than what the curses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found this: Seems to imply shift is Anyway, I thought the whole point of ncurses was to map things up to the API. I think there is some sort of termcap setting that will cause ncurses on the mac to surface these using the |
||
case 108: | ||
k = (Key)'+'; | ||
break; | ||
case 109: | ||
k = (Key)'-'; | ||
break; | ||
case 112: | ||
k = Key.InsertChar; | ||
break; | ||
case 113: | ||
k = Key.End; | ||
break; | ||
case 114: | ||
k = Key.CursorDown; | ||
break; | ||
case 115: | ||
k = Key.PageDown; | ||
break; | ||
case 116: | ||
k = Key.CursorLeft; | ||
break; | ||
case 117: | ||
k = Key.Clear; | ||
break; | ||
case 118: | ||
k = Key.CursorRight; | ||
break; | ||
case 119: | ||
k = Key.Home; | ||
break; | ||
case 120: | ||
k = Key.CursorUp; | ||
break; | ||
case 121: | ||
k = Key.PageUp; | ||
break; | ||
default: | ||
k = (Key)wch2; | ||
break; | ||
} | ||
} | ||
} | ||
} else if (wch2 < 256) { | ||
k = (Key)wch2; | ||
keyModifiers.Alt = true; | ||
|
@@ -559,7 +627,6 @@ void ProcessContinuousButtonPressed (MouseFlags mouseFlag, Point pos) | |
public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler) | ||
{ | ||
// Note: Curses doesn't support keydown/up events and thus any passed keyDown/UpHandlers will never be called | ||
Curses.timeout (0); | ||
this.keyHandler = keyHandler; | ||
this.keyDownHandler = keyDownHandler; | ||
this.keyUpHandler = keyUpHandler; | ||
|
@@ -572,9 +639,7 @@ public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandle | |
return true; | ||
}); | ||
|
||
mLoop.WinChanged += () => { | ||
ProcessInput (); | ||
}; | ||
mLoop.WinChanged += () => ProcessWinChange (); | ||
} | ||
|
||
public override void Init (Action terminalResized) | ||
|
@@ -585,6 +650,7 @@ public override void Init (Action terminalResized) | |
try { | ||
window = Curses.initscr (); | ||
Curses.set_escdelay (10); | ||
Curses.nodelay (window.Handle, true); | ||
} catch (Exception e) { | ||
throw new Exception ($"Curses failed to initialize, the exception is: {e.Message}"); | ||
} | ||
|
@@ -673,13 +739,11 @@ public override void Init (Action terminalResized) | |
|
||
ResizeScreen (); | ||
UpdateOffScreen (); | ||
|
||
} | ||
|
||
public override void ResizeScreen () | ||
{ | ||
Clip = new Rect (0, 0, Cols, Rows); | ||
Curses.refresh (); | ||
} | ||
|
||
public override void UpdateOffScreen () | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#pragma warning disable format | ||
|
||
#pragma warning restore format | ||
using System; | ||
using System.Collections.Generic; | ||
using Terminal.Gui; | ||
|
||
namespace UICatalog.Scenarios { | ||
[ScenarioMetadata (Name: "MainLoopTimeouts", Description: "MainLoop Timeouts")] | ||
[ScenarioCategory ("Tests")] | ||
public class MainLoopTimeouts : Scenario { | ||
static readonly List<string> GlobalList = new () { "1" }; | ||
static readonly ListView GlobalListView = new () { Width = Dim.Fill (), Height = Dim.Fill () }; | ||
|
||
static Label CounterLabel; | ||
static Label BlinkingLabel; | ||
|
||
static int Counter = 0; | ||
|
||
static object _listToken = null; | ||
static object _blinkToken = null; | ||
static object _countToken = null; | ||
|
||
public override void Init (ColorScheme colorScheme) | ||
{ | ||
Application.Init (); | ||
|
||
var startButton = new Button ("Start"); | ||
var stopButton = new Button ("Stop") { Y = 1 }; | ||
var container = new View () { X = Pos.Center (), Y = Pos.Center (), Width = 8, Height = 8, ColorScheme = Colors.Error }; | ||
|
||
CounterLabel = new Label ("0") { X = Pos.X (container), Y = Pos.Y (container) - 2 }; | ||
BlinkingLabel = new Label ("Blink") { X = Pos.X (container), Y = Pos.Bottom (container) + 1 }; | ||
|
||
startButton.Clicked += Start; | ||
stopButton.Clicked += Stop; | ||
|
||
GlobalListView.SetSource (GlobalList); | ||
container.Add (GlobalListView); | ||
|
||
Application.Top.Add (container, CounterLabel, BlinkingLabel); | ||
Application.Top.Add (startButton, stopButton); | ||
Application.Run (); | ||
Application.Shutdown (); | ||
} | ||
|
||
public override void Run () | ||
{ | ||
} | ||
|
||
private static void Start () | ||
{ | ||
_listToken = Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), Add); | ||
_blinkToken = Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (1000), Blink); | ||
_countToken = Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (1000), Count); | ||
} | ||
|
||
private static void Stop () | ||
{ | ||
Application.MainLoop.RemoveTimeout (_listToken); | ||
Application.MainLoop.RemoveTimeout (_blinkToken); | ||
Application.MainLoop.RemoveTimeout (_countToken); | ||
} | ||
|
||
private static bool Add (MainLoop mainLoop) | ||
{ | ||
Application.MainLoop.Invoke (() => { | ||
GlobalList.Add (new Random ().Next (100).ToString ()); | ||
GlobalListView.MoveDown (); | ||
}); | ||
|
||
return true; | ||
} | ||
|
||
private static bool Blink (MainLoop mainLoop) | ||
{ | ||
Application.MainLoop.Invoke (() => { | ||
if (BlinkingLabel.Visible) { | ||
BlinkingLabel.Visible = false; | ||
System.Diagnostics.Debug.WriteLine (BlinkingLabel.Visible); | ||
} else { | ||
BlinkingLabel.Visible = true; | ||
System.Diagnostics.Debug.WriteLine (BlinkingLabel.Visible); | ||
} | ||
|
||
}); | ||
|
||
return true; | ||
} | ||
|
||
private static bool Count (MainLoop mainLoop) | ||
{ | ||
Application.MainLoop.Invoke (() => { | ||
Counter++; | ||
CounterLabel.Text = Counter.ToString (); | ||
}); | ||
|
||
return true; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As an example, this would be more readable if it were
if (wch == (int)Key.Esc)
.