Skip to content
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

Pw 45 clipboard api #46

Merged
merged 4 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions PWManager.Application/Services/Interfaces/IClipboard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace PWManager.Application.Services.Interfaces;

public interface IClipboard {

void WriteClipboard(string val);

void ClearClipboard();
}
31 changes: 31 additions & 0 deletions PWManager.CLI/Abstractions/Clipboard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using PWManager.Application.Exceptions;
using PWManager.Application.Services.Interfaces;

namespace PWManager.CLI.Abstractions;

public class Clipboard : IClipboard {

public void WriteClipboard(string val) {
if (OperatingSystem.IsWindows()) {
var escaped = val.Replace("\n", "\\n").Replace("\"", "\"\"");
$"echo \"{escaped}\" | Set-Clipboard"
.PowerShell();
}
else if(OperatingSystem.IsMacOS()) {
var escaped = val.Replace("\n", "\\n").Replace("\"", "\\\"");
$"echo -n \"{escaped}\" | pbcopy"
.Bash();
}else if (OperatingSystem.IsLinux()) {
var escaped = val.Replace("\n", "\\n").Replace("\"", "\\\"");
$"echo -n \"{escaped}\" | xclip -selection c"
.Bash();
}
else {
throw new UserFeedbackException("Your Operating System does not support the clipboard functionality");
}
}

public void ClearClipboard() {
WriteClipboard(" ");
}
}
14 changes: 14 additions & 0 deletions PWManager.CLI/Abstractions/OperatingSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Runtime.InteropServices;

namespace PWManager.CLI.Abstractions;

public static class OperatingSystem {
public static bool IsWindows() =>
RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

public static bool IsMacOS() =>
RuntimeInformation.IsOSPlatform(OSPlatform.OSX);

public static bool IsLinux() =>
RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
}
40 changes: 40 additions & 0 deletions PWManager.CLI/Abstractions/Shell.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Diagnostics;

namespace PWManager.CLI.Abstractions;

public static class Shell {

public static string Bash(this string cmd) {
var escapedArgs = cmd.Replace("\"", "\\\"");
string result = Run("/bin/bash", $"-c \"{escapedArgs}\"");
return result;
}

public static string Bat(this string cmd) {
var escapedArgs = cmd.Replace("\"", "\\\"");
string result = Run("cmd.exe", $"/c \"{escapedArgs}\"");
return result;
}

public static string PowerShell(this string cmd) {
var escapedArgs = cmd.Replace("\"", "\\\"");
string result = Run("powershell.exe", $"/c \"{escapedArgs}\"");
return result;
}

private static string Run (string filename, string arguments){
var process = new Process() {
StartInfo = new ProcessStartInfo {
FileName = filename,
Arguments = arguments,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = false,
}
};
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return result;
}
}
2 changes: 2 additions & 0 deletions PWManager.CLI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using PWManager.Application;
using PWManager.Application.Context;
using PWManager.Application.Services.Interfaces;
using PWManager.CLI;
using PWManager.CLI.Abstractions;
using PWManager.CLI.ExtensionMethods;
Expand All @@ -12,6 +13,7 @@
services.AddSingleton<IApplicationEnvironment,CliEnvironment>();
// Add all services to DI
services.AddSingleton<IRunner, ConsoleRunner>();
services.AddTransient<IClipboard, Clipboard>();
// Add Layers
services.AddApplicationServices();
services.AddDataServices();
Expand Down
Loading