Add color to Console.ReadLine(); - c#

I'm using https://github.com/tomakita/Colorful.Console, but I don't mind solutions that wouldn't include this nuget.
using System;
using System.Collections.Generic;
using System.Drawing;
using Colorful;
namespace CMDAdv
{
class Program
{
static void Main()
{
Colorful.Console.WriteLineFormatted(Dialog.intro, Color.Tomato, ColorfulDescription());
Advance("What will you do?", "examine shimmer");
Colorful.Console.Clear();
Advance("continue", "no");
}
static void Advance(string writeOutput, string required)
{
string input;
Colorful.Console.WriteLine(writeOutput, Color.BlanchedAlmond);
input = ReadLine();
while (!input.Equals(required, StringComparison.OrdinalIgnoreCase))
{
Colorful.Console.WriteLine("Try again.", Color.Red);
input = Colorful.Console.ReadLine();
}
}
static string ReadLine(string input, string color)
{
input = Colorful.Console.ReadLine();
color = Colorful.Console.ToString(Color.Red);
return input;
}
static Formatter[] ColorfulDescription()
{
Formatter[] coloredText = new Formatter[]
{
new Formatter("shimmer", Color.LightBlue)
};
return coloredText;
}
}
}
As you can see I tried to add color to ReadLine using a method but that doesn't really work. Anyone know how to do this?
I'm using Colorful.Console, also I have tried different ways such as ReadLine(Color.Pink); etc etc.

Related

Formating a string (mask?)

I found that with long.Parse, ToString can take argument and I can format it to desired string, for example.
Input:
Console.WriteLine(long.Parse("123").ToString("#-#-#"));
Output:
1-2-3
I wanted to do something similar with string, lets say I wanna parse string to format ####-###-####. Is there any way to do it without regex with one liner like example above?
EDIT
Ok, so I may be misunderstood, I didn't want to parse numbers, but string instead. I can do in python like:
'{}-{}-{}'.format(*'abc') and I will receive a-b-c. In C# it seems to work only with numbers.
Try this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Text;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, world!");
Console.WriteLine("helloWorld".ToPhoneNumber("###-###-####"));
}
}
public static class AdvancedFormatString
{
public static string ToPhoneNumber(this string strArg, string outputformat)
{
if (outputformat == null)
return strArg;
var sb = new StringBuilder();
var i = 0;
foreach (var c in outputformat)
{
if (c == '#')
{
if (i < strArg.Length)
{
sb.Append(strArg[i]);
}
i++;
}
else
{
sb.Append(c);
}
}
return sb.ToString();
}
}
}

Invoking A Method From Another File C#

I'm having trouble calling my methods. I have 2 separate files. When the user types S then the shake method in my other file will be invoked. So then when the user gets the answer it will be random.
I confused about how to bring that method in another file. Below are both files.
Program.cs:
static void Main(string[] args)
{
Console.WriteLine("Main program!");
Console.WriteLine("Welcome to the Magic 8 Ball");
Console.WriteLine("What would you like to do?");
Console.WriteLine("(S)hake the Ball");
Console.WriteLine("(A)sk a Question");
Console.WriteLine("(G)et the Answer");
Console.WriteLine("(E)xit the Game");
Magic8Ball_Logic.Magic8Ball ball = new Magic8Ball_Logic.Magic8Ball();
string input = Console.ReadLine().ToUpper();
public static string userAnswer = "";
do
{
if (input == "S")
{
if (userAnswer != null)
{
Console.WriteLine("Searching the Mystic Realms(RAM) for the answer");
}
else
{
//Call Method Shake()
}
}
else if (input == "A") {
userAnswer = Console.ReadLine();
}
else if (input == "G") {
//Call Method GetAnswer()
}
} while (input != "E");
}
Magic8Ball.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Magic8Ball_Logic
{
public class Magic8Ball
{
private List<string> _answers;
private int _currentIndex;
private string randomString;
public Magic8Ball()
{
_answers = new List<string>();
_answers.Add("It is certain.");
_answers.Add("It is decidedly so.");
_answers.Add("Without a doubt.");
}
public Magic8Ball(List<string> answers)
{
//I won't use the 20 default. use the ones passed in .
_answers = answers;
}
public void Shake()
{
//picking the index of the answer to show the user
Random r = new Random();
int index = r.Next(_answers.Count);
randomString = _answers[index];
}
public string GetAnswer()
{
//using the index picked by shake to return the answer
//return "";
return randomString;
}
public int AnswerCount
{
get { return _answers.Count; }
}
/* public override string ToString()
{
foreach (var el in _answers)
{
return el;
}
}*/
}
}
First of all you must create an object of this class,then invoke the method.
**Edit**
ball.Shake();
As it was written here: https://stackoverflow.com/a/55986849/10128127
Your Program.cs should be updated like:
replace this comment
//Call Method Shake() with
ball.Shake();
replace this comment
//Call Method GetAnswer() with
ball.GetAnswer();

How to extract a complete method inside a class?

I'm trying to extract a complete method which is inside a cs file.
for instance.. suppose we have a class like this...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GetMethodNm
{
class MyClass
{
public int ComplexMethod(int param1, int myCustomValue)
{
if (param1 == myCustomValue)
{
return 54;
}
else
{
return 0;
}
}
public string ComplexMethodV(int param1, int myCustomValue)
{
if (param1 < myCustomValue)
{
return "300";
}
else
{
return "My custom value to return";
}
}
public bool ComplexMethodX(int param1, int myCustomValue)
{
if (param1 == myCustomValue)
{
return true;
}
else
{
return false;
}
}
}
}
Then I need to extract the method reading the cs file ComplexMethodV .. How can I do this? I have tried with reflection but I can only get the name and some things inside of it.. but I would need the literal method within.
Using Roslyn tasks like this are relitively easy. In a project add the NuGet package Microsoft.CodeAnalysis.CSharp, then just use the following code
using System;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace SandboxConsole
{
class Program
{
public static void Main()
{
var text = File.ReadAllText("MyClass.cs");
var tree = CSharpSyntaxTree.ParseText(text);
var method = tree.GetRoot().DescendantNodes()
.OfType<MethodDeclarationSyntax>()
.First(x => x.Identifier.Text == "ComplexMethodV");
Console.WriteLine(method.ToFullString());
Console.ReadLine();
}
}
}
This outputs the text
public string ComplexMethodV(int param1, int myCustomValue)
{
if (param1 < myCustomValue)
{
return "300";
}
else
{
return "My custom value to return";
}
}
See the Wiki for more advanced tutorials on how to do things like parse entire solutions.

3rd party Pdf library significantly slower when running NUnit

I am evaluating Winnovative's PdfToText library and have run into something that concerns me.
Everything runs fine and I am able to extract the text content from a small 20k or less pdf immediately if I am running a console application. However, if I call the same code from the NUnit gui running it takes 15-25 seconds (I've verified it's PdfToText by putting a breakpoint on the line that extracts the text and hitting F10 to see how long it takes to advance to the next line).
This concerns me because I'm not sure where to lay blame since I don't know the cause. Is there a problem with NUnit or PdfToText? All I want to do is extract the text from a pdf, but 20 seconds is completely unreasonable if I'm going to see this behavior under certain conditions. If it's just when running NUnit, that's acceptable, but otherwise I'll have to look elsewhere.
It's easier to demonstrate the problem using a complete VS Solution (2010), so here's the link to make it easier to setup and run (no need to download NUnit or PdfToText or even a sample pdf):
http://dl.dropbox.com/u/273037/PdfToTextProblem.zip (You may have to change the reference to PdfToText to use the x86 dll if you're running on a 32-bit machine).
Just hit F5 and the NUnit Gui runner will load.
I'm not tied to this library, if you have suggestions, I've tried iTextSharp (way too expensive for 2 lines of code), and looked at Aspose (I didn't try it, but the SaaS license is $11k). But they either lack the required functionality or are way too expensive.
(comment turned into answer)
How complex are your PDFs? The 4.1.6 version of iText allows for a closed sourced solution. Although 4.1.6 doesn't directly have a text extractor it isn't too terribly hard to write one using the PdfReader and GetPageContent().
Below is the code I used to extract the text from the PDF using iTextSharp v4.1.6. If it seems overly verbose, it's related to how I'm using it and the flexibility required.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf;
namespace ClassLibrary1
{
public class PdfToken
{
private PdfToken(int type, string value)
{
Type = type;
Value = value;
}
public static PdfToken Create(PRTokeniser tokenizer)
{
return new PdfToken(tokenizer.TokenType, tokenizer.StringValue);
}
public int Type { get; private set; }
public string Value { get; private set; }
public bool IsOperand
{
get
{
return Type == PRTokeniser.TK_OTHER;
}
}
}
public class PdfOperation
{
public PdfOperation(PdfToken operationToken, IEnumerable<PdfToken> arguments)
{
Name = operationToken.Value;
Arguments = arguments;
}
public string Name { get; private set; }
public IEnumerable<PdfToken> Arguments { get; private set; }
}
public interface IPdfParsingStrategy
{
void Execute(PdfOperation op);
}
public class PlainTextParsingStrategy : IPdfParsingStrategy
{
StringBuilder text = new StringBuilder();
public PlainTextParsingStrategy()
{
}
public String GetText()
{
return text.ToString();
}
#region IPdfParsingStrategy Members
public void Execute(PdfOperation op)
{
// see Adobe PDF specs for additional operations
switch (op.Name)
{
case "TJ":
PrintText(op);
break;
case "Tm":
SetMatrix(op);
break;
case "Tf":
SetFont(op);
break;
case "S":
PrintSection(op);
break;
case "G":
case "g":
case "rg":
SetColor(op);
break;
}
}
#endregion
bool newSection = false;
private void PrintSection(PdfOperation op)
{
text.AppendLine("------------------------------------------------------------");
newSection = true;
}
private void PrintNewline(PdfOperation op)
{
text.AppendLine();
}
private void PrintText(PdfOperation op)
{
if (newSection)
{
newSection = false;
StringBuilder header = new StringBuilder();
PrintText(op, header);
}
PrintText(op, text);
}
private static void PrintText(PdfOperation op, StringBuilder text)
{
foreach (PdfToken t in op.Arguments)
{
switch (t.Type)
{
case PRTokeniser.TK_STRING:
text.Append(t.Value);
break;
case PRTokeniser.TK_NUMBER:
text.Append(" ");
break;
}
}
}
String lastFont = String.Empty;
String lastFontSize = String.Empty;
private void SetFont(PdfOperation op)
{
var args = op.Arguments.ToList();
string font = args[0].Value;
string size = args[1].Value;
//if (font != lastFont || size != lastFontSize)
// text.AppendLine();
lastFont = font;
lastFontSize = size;
}
String lastX = String.Empty;
String lastY = String.Empty;
private void SetMatrix(PdfOperation op)
{
var args = op.Arguments.ToList();
string x = args[4].Value;
string y = args[5].Value;
if (lastY != y)
text.AppendLine();
else if (lastX != x)
text.Append(" ");
lastX = x;
lastY = y;
}
String lastColor = String.Empty;
private void SetColor(PdfOperation op)
{
lastColor = PrintCommand(op).Replace(" ", "_");
}
private static string PrintCommand(PdfOperation op)
{
StringBuilder text = new StringBuilder();
foreach (PdfToken t in op.Arguments)
text.AppendFormat("{0} ", t.Value);
text.Append(op.Name);
return text.ToString();
}
}
}
And here's how I call it:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using iTextSharp.text.pdf;
namespace ClassLibrary1
{
public class PdfExtractor
{
public static string GetText(byte[] pdfBuffer)
{
PlainTextParsingStrategy strategy = new PlainTextParsingStrategy();
ParsePdf(pdfBuffer, strategy);
return strategy.GetText();
}
private static void ParsePdf(byte[] pdf, IPdfParsingStrategy strategy)
{
PdfReader reader = new PdfReader(pdf);
for (int i = 1; i <= reader.NumberOfPages; i++)
{
byte[] page = reader.GetPageContent(i);
if (page != null)
{
PRTokeniser tokenizer = new PRTokeniser(page);
List<PdfToken> parameters = new List<PdfToken>();
while (tokenizer.NextToken())
{
var token = PdfToken.Create(tokenizer);
if (token.IsOperand)
{
strategy.Execute(new PdfOperation(token, parameters));
parameters.Clear();
}
else
{
parameters.Add(token);
}
}
}
}
}
}
}

How to extract the url+parameters out of a bbcode url tag?

The following code outputs:
http://www.google.com
http://www.google.com&lang
What is the simplest way to change the code so it outputs:
http://www.google.com
http://www.google.com&lang=en&param2=this&param3=that
CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestRegex9928228
{
class Program
{
static void Main(string[] args)
{
string text1 = "try out [url=http://www.google.com]this site (http://www.google.com)[/url]";
Console.WriteLine(text1.ExtractParameterFromBbcodeUrlElement());
string text2 = "try out [url=http://www.google.com&lang=en&param1=this&param2=that]this site (http://www.google.com)[/url]";
Console.WriteLine(text2.ExtractParameterFromBbcodeUrlElement());
Console.ReadLine();
}
}
public static class StringHelpers
{
public static string ExtractParameterFromBbcodeUrlElement(this string line)
{
if (line == null)
return "";
else
{
if (line.Contains("]"))
{
List<string> parts = line.BreakIntoParts(']');
if (parts[0].Contains("="))
{
List<string> sides = parts[0].BreakIntoParts('=');
if (sides.Count > 1)
return sides[1];
else
return "";
}
else
return "";
}
else
return "";
}
}
public static List<string> BreakIntoParts(this string line, char separator)
{
if (String.IsNullOrEmpty(line))
return new List<string>();
else
return line.Split(separator).Select(p => p.Trim()).ToList();
}
}
}
Simplest or most efficient? You're asking two different questions it seems. Simplest would be something like this:
Change:
List<string> sides = parts[0].BreakIntoParts('=');
if (sides.Count > 1)
return sides[1];
To:
List<string> sides = parts[0].BreakIntoParts('=');
if (sides.Count > 1)
return parts[0].Replace(sides[0], "");
Edit: Looks like you changed title to remove "most efficient". Here's the simplest change (fewest lines of code changed) that I see.

Categories