Last active
December 31, 2015 07:49
-
-
Save jpluimers/7956271 to your computer and use it in GitHub Desktop.
PasteText: the reverse of clip.exe; pastes Clipboard.GetText() or Clipboard.GetFileDropList() to the standard output.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Collections.Specialized; | |
using System.Linq; | |
using System.Windows.Forms; | |
namespace PasteText | |
{ | |
class Program | |
{ | |
private const string STR_PasteText = "PasteText.exe"; | |
[STAThread] | |
static void Main(string[] args) | |
{ | |
bool help = showHelp(args); | |
if (!help) | |
pasteText(args); | |
} | |
private static bool showHelp(string[] args) | |
{ | |
bool help = args.Contains("/?") || args.Contains("-?") || args.Contains("/h") || args.Contains("-h") || args.Contains("--help"); | |
if (help) | |
{ | |
Console.Out.WriteLine(@"{0} [/?|-?|/h|-h|--help] [/r|-r|--raw] | |
pastes Clipboard.GetText() or Clipboard.GetFileDropList() to the standard output | |
Optional arguments: | |
/?|-?|/h|-h|--help: | |
shows this help | |
/r|-r|--raw: | |
pastes raw text (default: pastes text as is) | |
Example: | |
{0} | clip.exe | |
pastes the text through clip.exe, effectively stripping any formatting.", STR_PasteText); | |
} | |
return help; | |
} | |
private static void pasteText(string[] args) | |
{ | |
if (Clipboard.ContainsText()) | |
{ | |
bool rawOutput = args.Contains("/r") || args.Contains("-r") || args.Contains("--raw"); | |
string text = Clipboard.GetText(); | |
if (rawOutput) | |
{ | |
Console.Out.Write(text); | |
} | |
else | |
{ | |
// the order of the separators is important: CRLF, LFCR, LF, CR | |
// http://msdn.microsoft.com/en-us/library/vstudio/tabh47cf | |
// TODO: in the future add more Unicode separators | |
// http://en.wikipedia.org/wiki/Newline#Representations | |
// http://en.wikipedia.org/wiki/Newline#Unicode | |
string[] separators = new[] { "\r\n", "\n\r", "\n", "\r", }; | |
string[] lines = text.Split(separators, StringSplitOptions.None); | |
foreach (var line in lines) | |
{ | |
Console.Out.WriteLine(line); | |
} | |
} | |
} | |
else if (Clipboard.ContainsFileDropList()) | |
{ | |
StringCollection files = Clipboard.GetFileDropList(); | |
foreach (string file in files) | |
{ | |
Console.Out.WriteLine(file); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment