|
| 1 | +using System; |
1 | 2 | using System.IO; |
2 | 3 |
|
3 | 4 | static class LinuxClipboard |
4 | 5 | { |
| 6 | + private enum ClipCmd |
| 7 | + { |
| 8 | + Incompatible, |
| 9 | + XClip, |
| 10 | + XSel, |
| 11 | + } |
| 12 | + private static ClipCmd Cmd = ClipCmd.Incompatible; |
| 13 | + |
| 14 | + public static string GetCompatibilityProblem() |
| 15 | + { |
| 16 | + return @"Myra: At least one package needs to be installed on the system: 'xclip', 'xsel'. Clipboard function will be limited."; |
| 17 | + } |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Return true if 'xclip' or 'xsel' is installed on the system. |
| 21 | + /// </summary> |
| 22 | + public static bool SystemIsCompatible() |
| 23 | + { |
| 24 | + ClipCmd clipCmd = ClipCmd.Incompatible; |
| 25 | + |
| 26 | + if(TestBashCommandExists("xsel --version")) //dash differences are intentional |
| 27 | + { |
| 28 | + clipCmd = ClipCmd.XSel; |
| 29 | + } |
| 30 | + else if (TestBashCommandExists("xclip -version")) |
| 31 | + { |
| 32 | + clipCmd = ClipCmd.XClip; |
| 33 | + } |
| 34 | + |
| 35 | + Cmd = clipCmd; |
| 36 | + return Cmd != ClipCmd.Incompatible; |
| 37 | + } |
| 38 | + private static bool TestBashCommandExists(string cmd) |
| 39 | + { |
| 40 | + string bashMsg; |
| 41 | + try |
| 42 | + { |
| 43 | + bashMsg = BashRunner.Run(cmd); |
| 44 | + } |
| 45 | + catch (Exception e) |
| 46 | + { |
| 47 | + bashMsg = e.Message; |
| 48 | + } |
| 49 | + return !bashMsg.Contains("Could not execute process."); |
| 50 | + } |
| 51 | + |
5 | 52 | public static void SetText(string text) |
6 | 53 | { |
7 | 54 | var tempFileName = Path.GetTempFileName(); |
8 | 55 | File.WriteAllText(tempFileName, text); |
9 | 56 | try |
10 | 57 | { |
11 | | - BashRunner.Run($"cat {tempFileName} | xclip"); |
| 58 | + switch (Cmd) |
| 59 | + { |
| 60 | + case ClipCmd.XClip: |
| 61 | + BashRunner.Run($"cat {tempFileName} | xclip -sel clip"); |
| 62 | + break; |
| 63 | + case ClipCmd.XSel: |
| 64 | + BashRunner.Run($"cat {tempFileName} | xsel -ib"); |
| 65 | + break; |
| 66 | + case ClipCmd.Incompatible: |
| 67 | + throw new SystemException(GetCompatibilityProblem()); |
| 68 | + default: |
| 69 | + throw new NotImplementedException(Cmd.ToString()); |
| 70 | + } |
12 | 71 | } |
13 | 72 | finally |
14 | 73 | { |
15 | 74 | File.Delete(tempFileName); |
16 | 75 | } |
17 | 76 | } |
18 | | - |
| 77 | + |
19 | 78 | public static string GetText() |
20 | 79 | { |
21 | 80 | var tempFileName = Path.GetTempFileName(); |
22 | 81 | try |
23 | 82 | { |
24 | | - BashRunner.Run($"xclip -o > {tempFileName}"); |
| 83 | + switch (Cmd) |
| 84 | + { |
| 85 | + case ClipCmd.XClip: |
| 86 | + BashRunner.Run($"xclip -o -sel clip > {tempFileName}"); |
| 87 | + break; |
| 88 | + case ClipCmd.XSel: |
| 89 | + BashRunner.Run($"xsel -ob > {tempFileName}"); |
| 90 | + break; |
| 91 | + case ClipCmd.Incompatible: |
| 92 | + throw new SystemException(GetCompatibilityProblem()); |
| 93 | + default: |
| 94 | + throw new NotImplementedException(Cmd.ToString()); |
| 95 | + } |
25 | 96 | return File.ReadAllText(tempFileName); |
26 | 97 | } |
27 | 98 | finally |
|
0 commit comments