1- using System . Diagnostics ;
2-
1+ #nullable enable
32namespace Terminal . Gui . App ;
43
54/// <summary>Provides cut, copy, and paste support for the OS clipboard.</summary>
@@ -20,24 +19,19 @@ namespace Terminal.Gui.App;
2019/// </remarks>
2120public static class Clipboard
2221{
23- private static string _contents = string . Empty ;
22+ private static string ? _contents = string . Empty ;
2423
2524 /// <summary>Gets (copies from) or sets (pastes to) the contents of the OS clipboard.</summary>
26- public static string Contents
25+ public static string ? Contents
2726 {
2827 get
2928 {
3029 try
3130 {
3231 if ( IsSupported )
3332 {
34- string clipData = Application . Driver ? . Clipboard . GetClipboardData ( ) ;
35-
36- if ( clipData is null )
37- {
38- // throw new InvalidOperationException ($"{Application.Driver?.GetType ().Name}.GetClipboardData returned null instead of string.Empty");
39- clipData = string . Empty ;
40- }
33+ // throw new InvalidOperationException ($"{Application.Driver?.GetType ().Name}.GetClipboardData returned null instead of string.Empty");
34+ string ? clipData = Application . Driver ? . Clipboard ? . GetClipboardData ( ) ?? string . Empty ;
4135
4236 _contents = clipData ;
4337 }
@@ -55,12 +49,9 @@ public static string Contents
5549 {
5650 if ( IsSupported )
5751 {
58- if ( value is null )
59- {
60- value = string . Empty ;
61- }
52+ value ??= string . Empty ;
6253
63- Application . Driver ? . Clipboard . SetClipboardData ( value ) ;
54+ Application . Driver ? . Clipboard ? . SetClipboardData ( value ) ;
6455 }
6556
6657 _contents = value ;
@@ -74,126 +65,5 @@ public static string Contents
7465
7566 /// <summary>Returns true if the environmental dependencies are in place to interact with the OS clipboard.</summary>
7667 /// <remarks></remarks>
77- public static bool IsSupported => Application . Driver ? . Clipboard . IsSupported ?? false ;
78-
79- /// <summary>Copies the _contents of the OS clipboard to <paramref name="result"/> if possible.</summary>
80- /// <param name="result">The _contents of the OS clipboard if successful, <see cref="string.Empty"/> if not.</param>
81- /// <returns><see langword="true"/> the OS clipboard was retrieved, <see langword="false"/> otherwise.</returns>
82- public static bool TryGetClipboardData ( out string result )
83- {
84- if ( IsSupported && Application . Driver ! . Clipboard . TryGetClipboardData ( out result ) )
85- {
86- _contents = result ;
87-
88- return true ;
89- }
90-
91- result = string . Empty ;
92-
93- return false ;
94- }
95-
96- /// <summary>Pastes the <paramref name="text"/> to the OS clipboard if possible.</summary>
97- /// <param name="text">The text to paste to the OS clipboard.</param>
98- /// <returns><see langword="true"/> the OS clipboard was set, <see langword="false"/> otherwise.</returns>
99- public static bool TrySetClipboardData ( string text )
100- {
101- if ( IsSupported && Application . Driver ! . Clipboard . TrySetClipboardData ( text ) )
102- {
103- _contents = text ;
104-
105- return true ;
106- }
107-
108- return false ;
109- }
110- }
111-
112- /// <summary>
113- /// Helper class for console drivers to invoke shell commands to interact with the clipboard. Used primarily by
114- /// CursesDriver, but also used in Unit tests which is why it is in IConsoleDriver.cs.
115- /// </summary>
116- internal static class ClipboardProcessRunner
117- {
118- public static ( int exitCode , string result ) Bash (
119- string commandLine ,
120- string inputText = "" ,
121- bool waitForOutput = false
122- )
123- {
124- var arguments = $ "-c \" { commandLine } \" ";
125- ( int exitCode , string result ) = Process ( "bash" , arguments , inputText , waitForOutput ) ;
126-
127- return ( exitCode , result . TrimEnd ( ) ) ;
128- }
129-
130- public static bool DoubleWaitForExit ( this Process process )
131- {
132- bool result = process . WaitForExit ( 500 ) ;
133-
134- if ( result )
135- {
136- process . WaitForExit ( ) ;
137- }
138-
139- return result ;
140- }
141-
142- public static bool FileExists ( this string value ) { return ! string . IsNullOrEmpty ( value ) && ! value . Contains ( "not found" ) ; }
143-
144- public static ( int exitCode , string result ) Process (
145- string cmd ,
146- string arguments ,
147- string input = null ,
148- bool waitForOutput = true
149- )
150- {
151- var output = string . Empty ;
152-
153- using ( var process = new Process
154- {
155- StartInfo = new ( )
156- {
157- FileName = cmd ,
158- Arguments = arguments ,
159- RedirectStandardOutput = true ,
160- RedirectStandardError = true ,
161- RedirectStandardInput = true ,
162- UseShellExecute = false ,
163- CreateNoWindow = true
164- }
165- } )
166- {
167- TaskCompletionSource < bool > eventHandled = new ( ) ;
168- process . Start ( ) ;
169-
170- if ( ! string . IsNullOrEmpty ( input ) )
171- {
172- process . StandardInput . Write ( input ) ;
173- process . StandardInput . Close ( ) ;
174- }
175-
176- if ( ! process . WaitForExit ( 5000 ) )
177- {
178- var timeoutError =
179- $@ "Process timed out. Command line: { process . StartInfo . FileName } { process . StartInfo . Arguments } .";
180-
181- throw new TimeoutException ( timeoutError ) ;
182- }
183-
184- if ( waitForOutput && process . StandardOutput . Peek ( ) != - 1 )
185- {
186- output = process . StandardOutput . ReadToEnd ( ) ;
187- }
188-
189- if ( process . ExitCode > 0 )
190- {
191- output = $@ "Process failed to run. Command line: { cmd } { arguments } .
192- Output: { output }
193- Error: { process . StandardError . ReadToEnd ( ) } " ;
194- }
195-
196- return ( process . ExitCode , output ) ;
197- }
198- }
199- }
68+ public static bool IsSupported => Application . Driver ? . Clipboard ? . IsSupported ?? false ;
69+ }
0 commit comments