8
8
using NStack ;
9
9
using System ;
10
10
using System . Collections . Generic ;
11
+ using System . Diagnostics ;
11
12
using System . Linq ;
12
13
using System . Runtime . CompilerServices ;
14
+ using System . Threading . Tasks ;
15
+ using Unix . Terminal ;
13
16
14
17
namespace Terminal . Gui {
15
18
/// <summary>
@@ -681,7 +684,7 @@ public abstract class ConsoleDriver {
681
684
/// <param name="col">Column to move the cursor to.</param>
682
685
/// <param name="row">Row to move the cursor to.</param>
683
686
public abstract void Move ( int col , int row ) ;
684
-
687
+
685
688
/// <summary>
686
689
/// Adds the specified rune to the display at the current cursor position.
687
690
/// </summary>
@@ -696,11 +699,10 @@ public abstract class ConsoleDriver {
696
699
/// <returns></returns>
697
700
public static Rune MakePrintable ( Rune c )
698
701
{
699
- var controlChars = c & 0xFFFF ;
700
- if ( controlChars <= 0x1F || controlChars >= 0X7F && controlChars <= 0x9F ) {
702
+ if ( c <= 0x1F || ( c >= 0X7F && c <= 0x9F ) ) {
701
703
// ASCII (C0) control characters.
702
704
// C1 control characters (https://www.aivosto.com/articles/control-characters.html#c1)
703
- return new Rune ( controlChars + 0x2400 ) ;
705
+ return new Rune ( c + 0x2400 ) ;
704
706
}
705
707
706
708
return c ;
@@ -1390,4 +1392,74 @@ public void CreateColors (bool hasColors = true)
1390
1392
Colors . Error . Disabled = MakeColor ( Color . DarkGray , Color . White ) ;
1391
1393
}
1392
1394
}
1395
+
1396
+ /// <summary>
1397
+ /// Helper class for console drivers to invoke shell commands to interact with the clipboard.
1398
+ /// Used primarily by CursesDriver, but also used in Unit tests which is why it is in
1399
+ /// ConsoleDriver.cs.
1400
+ /// </summary>
1401
+ internal static class ClipboardProcessRunner {
1402
+ public static ( int exitCode , string result ) Bash ( string commandLine , string inputText = "" , bool waitForOutput = false )
1403
+ {
1404
+ var arguments = $ "-c \" { commandLine } \" ";
1405
+ var ( exitCode , result ) = Process ( "bash" , arguments , inputText , waitForOutput ) ;
1406
+
1407
+ return ( exitCode , result . TrimEnd ( ) ) ;
1408
+ }
1409
+
1410
+ public static ( int exitCode , string result ) Process ( string cmd , string arguments , string input = null , bool waitForOutput = true )
1411
+ {
1412
+ var output = string . Empty ;
1413
+
1414
+ using ( Process process = new Process {
1415
+ StartInfo = new ProcessStartInfo {
1416
+ FileName = cmd ,
1417
+ Arguments = arguments ,
1418
+ RedirectStandardOutput = true ,
1419
+ RedirectStandardError = true ,
1420
+ RedirectStandardInput = true ,
1421
+ UseShellExecute = false ,
1422
+ CreateNoWindow = true ,
1423
+ }
1424
+ } ) {
1425
+ var eventHandled = new TaskCompletionSource < bool > ( ) ;
1426
+ process . Start ( ) ;
1427
+ if ( ! string . IsNullOrEmpty ( input ) ) {
1428
+ process . StandardInput . Write ( input ) ;
1429
+ process . StandardInput . Close ( ) ;
1430
+ }
1431
+
1432
+ if ( ! process . WaitForExit ( 5000 ) ) {
1433
+ var timeoutError = $@ "Process timed out. Command line: { process . StartInfo . FileName } { process . StartInfo . Arguments } .";
1434
+ throw new TimeoutException ( timeoutError ) ;
1435
+ }
1436
+
1437
+ if ( waitForOutput && process . StandardOutput . Peek ( ) != - 1 ) {
1438
+ output = process . StandardOutput . ReadToEnd ( ) ;
1439
+ }
1440
+
1441
+ if ( process . ExitCode > 0 ) {
1442
+ output = $@ "Process failed to run. Command line: { cmd } { arguments } .
1443
+ Output: { output }
1444
+ Error: { process . StandardError . ReadToEnd ( ) } " ;
1445
+ }
1446
+
1447
+ return ( process . ExitCode , output ) ;
1448
+ }
1449
+ }
1450
+
1451
+ public static bool DoubleWaitForExit ( this System . Diagnostics . Process process )
1452
+ {
1453
+ var result = process . WaitForExit ( 500 ) ;
1454
+ if ( result ) {
1455
+ process . WaitForExit ( ) ;
1456
+ }
1457
+ return result ;
1458
+ }
1459
+
1460
+ public static bool FileExists ( this string value )
1461
+ {
1462
+ return ! string . IsNullOrEmpty ( value ) && ! value . Contains ( "not found" ) ;
1463
+ }
1464
+ }
1393
1465
}
0 commit comments