Files
Aleksandar Pesic 5a2e2a5292 async / await pattern improved in FluentTerminal.App.ViewModels project (#647)
* Continuation options and code cosmetics in async methods.

* ConfigureAwait in FluentTerminal.App.ViewModels project adjusted.
2020-01-08 18:40:50 +01:00

44 lines
1.4 KiB
C#

using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Windows.ApplicationModel.DataTransfer;
namespace FluentTerminal.App.Services
{
public class ClipboardService : IClipboardService
{
/// <summary>
/// Right trim whitespaces for each line.
/// </summary>
private static readonly Regex RTrimMultiLinesPattern = new Regex(@"([^\S\r\n]+)([\r\n])", RegexOptions.Compiled);
private readonly ISettingsService _settingsService;
public ClipboardService(ISettingsService settingsService)
{
_settingsService = settingsService;
}
public Task<string> GetTextAsync()
{
var content = Clipboard.GetContent();
if (content.Contains(StandardDataFormats.Text))
{
return content.GetTextAsync().AsTask();
}
// Otherwise return a new task that just sends an empty string.
return Task.FromResult(string.Empty);
}
public void SetText(string text)
{
if (_settingsService.GetApplicationSettings().RTrimCopiedLines)
{
text = RTrimMultiLinesPattern.Replace(text, "$2");
}
var dataPackage = new DataPackage();
dataPackage.SetText(text);
Clipboard.SetContent(dataPackage);
}
}
}