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

Beautify command line help output #78

Closed
maettu-this opened this issue Jul 20, 2017 · 3 comments
Closed

Beautify command line help output #78

maettu-this opened this issue Jul 20, 2017 · 3 comments

Comments

@maettu-this
Copy link
Collaborator

Suggesting to limit the command line help's text width to the console width, in order to prevent line breaks and improve readability.

Some ideas taken from my own ArgsHandler making use of StringEx:

  • Console.Out.Write(GetHelpText(Console.WindowWidth - 1)); // -1 ensures that lines that exactly match the number of characters do not lead to an empty line (due to the NewLine which is added).
  • string GetHelpText(int maxWidth)
  • string SplitIntoLines(int maxWidth, int indent, string text)
string SplitIntoLines(int maxWidth, int indent, string text)
{
	StringBuilder lines = new StringBuilder();

	int width = (maxWidth - indent);
	foreach (string line in StringEx.SplitLexically(text, width))
		lines.AppendLine(IndentSpace(indent) + line);

	return (lines.ToString());
}
string[] SplitLexically(string str, int desiredChunkLength)
{
	List<string> chunks = new List<string>();
	string[] newLineSeparators = new string[] { Environment.NewLine, "\n", "\r" };

	foreach (string paragraph in str.Split(newLineSeparators, StringSplitOptions.None))
		chunks.AddRange(SplitLexicallyWithoutTakingNewLineIntoAccount(paragraph, desiredChunkLength));

	return (chunks.ToArray());
}
string[] SplitLexicallyWithoutTakingNewLineIntoAccount(string str, int desiredChunkLength)
{
	List<int> spaces = new List<int>();

	// Retrieve all spaces within the string:
	int i = 0;
	while ((i = str.IndexOf(' ', i)) >= 0)
	{
		spaces.Add(i);
		i++;
	}

	// Add an extra space at the end of the string to ensure that the last chunk is split properly:
	spaces.Add(str.Length);

	// Split the string into the desired chunk size taking word boundaries into account:
	int startIndex = 0;
	List<string> chunks = new List<string>();
	while (startIndex < str.Length)
	{
		// Find the furthermost split position:
		int spaceIndex = spaces.FindLastIndex(value => (value <= (startIndex + desiredChunkLength)));
		
		int splitIndex;
		if (spaceIndex >= 0)
			splitIndex = spaces[spaceIndex];
		else
			splitIndex = (startIndex + desiredChunkLength);

		// Limit to split within the string and execute the split:
		splitIndex = Int32Ex.LimitToBounds(splitIndex, startIndex, str.Length);
		int length = (splitIndex - startIndex);
		chunks.Add(str.Substring(startIndex, length));
		startIndex += length;

		// Remove the already used spaces from the collection to ensure those spaces are not used again:
		if (spaceIndex >= 0)
		{
			spaces.RemoveRange(0, (spaceIndex + 1));
			startIndex++; // Advance an extra character to compensate the space.
		}
	}

	return (chunks.ToArray());
}
@oleg-shilo
Copy link
Owner

Excellent suggestion. Txs.
Scheduled for the next release

@oleg-shilo
Copy link
Owner

oleg-shilo commented Jul 26, 2017

Done.

Thank you for sharing your code. It provided an excellent opportunity to improve CLI support.

I have used a slightly modified version of your SplitLexicallyWithoutTakingNewLineIntoAccount.
I have also introduced a very primitive "markup syntax" to allow sub-paragraphs (sub-indents) so common for CLI help content:

image

image

oleg-shilo added a commit that referenced this issue Aug 4, 2017
Minor usability improvements

* Added System.dll auto-referencing on EnableDbgPrint:true to allow regular expressions to be used in auto-injected dbg.cs.
* Issue #78: Beautify command line help output
* Issue #71: Issue with multiple indirect relative paths
* Added ResolveRelativeFromParentScriptLocation for ALL non absolute paths in //css_import
@maettu-this
Copy link
Collaborator Author

Great, looks very good now :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants