Skip to content

Commit

Permalink
Echo piped input back to console
Browse files Browse the repository at this point in the history
  • Loading branch information
ajryan committed Feb 7, 2013
1 parent 8ae7257 commit 549964c
Showing 1 changed file with 75 additions and 53 deletions.
128 changes: 75 additions & 53 deletions Program.cs
Expand Up @@ -6,57 +6,79 @@

namespace WinHaste
{
class Program
{
private static readonly Regex _HasteKeyRegex = new Regex(@"{""key"":""(?<key>[a-z].*)""}", RegexOptions.Compiled);

[STAThread]
static void Main(string[] args)
{
var parameters = new Parameters(args);
var parseResult = parameters.Parse();
if (parseResult != Parameters.ParseResult.Success)
{
Console.WriteLine(parameters.Usage);
Console.WriteLine(Environment.NewLine + "ERROR: " + parseResult);
return;
}

string haste;
if (!String.IsNullOrEmpty(parameters.Filename))
{
haste = File.ReadAllText(parameters.Filename);
}
else
{
haste = String.Empty;

int consoleKey = Console.Read();
while (consoleKey != -1)
{
var consoleChar = Convert.ToChar(consoleKey);
haste += consoleChar;

consoleKey = Console.Read();
}
}

using (var client = new WebClient())
{
var response = client.UploadString(parameters.Url + "/documents", haste);
var match = _HasteKeyRegex.Match(response);

if (!match.Success)
{
Console.WriteLine(response);
return;
}

string hasteUrl = "http://hastebin.com/" + match.Groups["key"];

Clipboard.SetText(hasteUrl);
Console.WriteLine("\r\nHaste URL: {0} (copied to clipboard)", hasteUrl);
}
}
}
class Program
{
private static readonly Regex _HasteKeyRegex = new Regex(@"{""key"":""(?<key>[a-z].*)""}", RegexOptions.Compiled);

[STAThread]
static void Main(string[] args)
{
var parameters = new Parameters(args);
var parseResult = parameters.Parse();
if (parseResult != Parameters.ParseResult.Success)
{
Console.WriteLine(parameters.Usage);
Console.WriteLine(Environment.NewLine + "ERROR: " + parseResult);
return;
}

string haste;
if (!String.IsNullOrEmpty(parameters.Filename))
{
haste = File.ReadAllText(parameters.Filename);
}
else
{
bool piped = IsInputPiped();

haste = String.Empty;

int consoleKey = Console.Read();
while (consoleKey != -1)
{
var consoleChar = Convert.ToChar(consoleKey);

if (piped)
{
Console.Write(consoleChar);
Console.Out.Flush();
}

haste += consoleChar;

consoleKey = Console.Read();
}
}

using (var client = new WebClient())
{
var response = client.UploadString(parameters.Url + "/documents", haste);
var match = _HasteKeyRegex.Match(response);

if (!match.Success)
{
Console.WriteLine(response);
return;
}

string hasteUrl = "http://hastebin.com/" + match.Groups["key"];

Clipboard.SetText(hasteUrl);
Console.WriteLine("\r\nHaste URL: {0} (copied to clipboard)", hasteUrl);
}
}

private static bool IsInputPiped()
{
try
{
var tmp = Console.KeyAvailable;
return false;
}
catch (InvalidOperationException)
{
return true;
}
}
}
}

1 comment on commit 549964c

@ajryan
Copy link
Owner Author

@ajryan ajryan commented on 549964c Feb 7, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about the tabs-to-spaces. Try this view.

Please sign in to comment.