From 4590583adcd1128dd6d47f672507b713e955b1c7 Mon Sep 17 00:00:00 2001 From: Aliwoto Date: Sun, 4 Jul 2021 09:40:24 +0000 Subject: [PATCH] Added Local support for xsel Signed-off-by: Aliwoto --- src/TextCopy/LinuxClipboard_2.0.cs | 26 ++++++++++++++++++++++++-- src/TextCopy/LinuxClipboard_2.1.cs | 26 ++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/TextCopy/LinuxClipboard_2.0.cs b/src/TextCopy/LinuxClipboard_2.0.cs index 2e2ca741..5fbf7558 100644 --- a/src/TextCopy/LinuxClipboard_2.0.cs +++ b/src/TextCopy/LinuxClipboard_2.0.cs @@ -6,11 +6,25 @@ static class LinuxClipboard { + /// + /// Determine whether we should use a local path for xsel binary or not. + /// When start up, we will check if xsel exists in the + /// or not. + /// If it exists, we will use a local xsel for it, otherwise this property + /// will be false. + /// You can also set it manually, but if it's false, xsel binary + /// should exists in the user's path. + /// + /// + /// true if we have to use local path, otherwise false. + /// + public static bool UseLocal { get; set; } static bool isWsl; static LinuxClipboard() { isWsl = Environment.GetEnvironmentVariable("WSL_DISTRO_NAME") != null; + UseLocal = File.Exists(AppContext.BaseDirectory + "xsel"); } public static Task SetTextAsync(string text, CancellationToken cancellation) @@ -32,7 +46,7 @@ public static void SetText(string text) } else { - BashRunner.Run($"cat {tempFileName} | xsel -i --clipboard "); + BashRunner.Run($"cat {tempFileName} | {GetXsel()} -i --clipboard "); } } finally @@ -57,7 +71,7 @@ public static string GetText() } else { - BashRunner.Run($"xsel -o --clipboard > {tempFileName}"); + BashRunner.Run($"{GetXsel()} -o --clipboard > {tempFileName}"); } return File.ReadAllText(tempFileName); } @@ -66,5 +80,13 @@ public static string GetText() File.Delete(tempFileName); } } + private static string GetXsel() + { + if (UseLocal) + { + return "./" + "xsel"; + } + return "xsel"; + } } #endif \ No newline at end of file diff --git a/src/TextCopy/LinuxClipboard_2.1.cs b/src/TextCopy/LinuxClipboard_2.1.cs index 907eab5d..a6a544f3 100644 --- a/src/TextCopy/LinuxClipboard_2.1.cs +++ b/src/TextCopy/LinuxClipboard_2.1.cs @@ -6,11 +6,25 @@ static class LinuxClipboard { + /// + /// Determine whether we should use a local path for xsel binary or not. + /// When start up, we will check if xsel exists in the + /// or not. + /// If it exists, we will use a local xsel for it, otherwise this property + /// will be false. + /// You can also set it manually, but if it's false, xsel binary + /// should exists in the user's path. + /// + /// + /// true if we have to use local path, otherwise false. + /// + public static bool UseLocal { get; set; } static bool isWsl; static LinuxClipboard() { isWsl = Environment.GetEnvironmentVariable("WSL_DISTRO_NAME") != null; + UseLocal = File.Exists(AppContext.BaseDirectory + "xsel"); } public static async Task SetTextAsync(string text, CancellationToken cancellation) @@ -43,7 +57,7 @@ static void InnerSetText(string tempFileName) } else { - BashRunner.Run($"cat {tempFileName} | xsel -i --clipboard "); + BashRunner.Run($"cat {tempFileName} | {GetXsel()} -i --clipboard "); } } finally @@ -88,8 +102,16 @@ static void InnerGetText(string tempFileName) } else { - BashRunner.Run($"xsel -o --clipboard > {tempFileName}"); + BashRunner.Run($"{GetXsel()} -o --clipboard > {tempFileName}"); } } + private static string GetXsel() + { + if (UseLocal) + { + return "./" + "xsel"; + } + return "xsel"; + } } #endif \ No newline at end of file