-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
could do some more work on the formatting, but features are complete now
- Loading branch information
1 parent
bf31e1b
commit 5f7edb4
Showing
6 changed files
with
95 additions
and
18 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace GoCommando.Internals | ||
{ | ||
static class StringExtensions | ||
{ | ||
public static string WrappedAt(this string str, int width) | ||
{ | ||
var twoLineBreaks = Environment.NewLine + Environment.NewLine; | ||
|
||
var sections = str.Split(new[] { twoLineBreaks }, | ||
StringSplitOptions.RemoveEmptyEntries); | ||
|
||
return string.Join(twoLineBreaks, sections.Select(section => WrapSection(section, width))); | ||
} | ||
|
||
static string WrapSection(string section, int width) | ||
{ | ||
var oneLongString = string.Join(" ", | ||
section.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)); | ||
|
||
var words = oneLongString.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); | ||
|
||
var builder = new StringBuilder(); | ||
|
||
var currentLineLength = 0; | ||
|
||
for (var index = 0; index < words.Length; index++) | ||
{ | ||
var word = words[index]; | ||
builder.Append(word); | ||
currentLineLength += word.Length; | ||
|
||
if (index < words.Length - 1) | ||
{ | ||
var nextWord = words[index]; | ||
|
||
var spaceLeftOnCurrentLine = width - currentLineLength - 1; // -1 to leave room for space... | ||
var nextWordIsTooLong = nextWord.Length > spaceLeftOnCurrentLine; | ||
|
||
if (nextWordIsTooLong) | ||
{ | ||
builder.AppendLine(); | ||
currentLineLength = 0; | ||
} | ||
else | ||
{ | ||
builder.Append(" "); | ||
currentLineLength++; | ||
} | ||
} | ||
} | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
public static string Indented(this string str, int indent) | ||
{ | ||
var indentedLines = str | ||
.Split(new[] { Environment.NewLine }, StringSplitOptions.None) | ||
.Select(line => string.Concat(new string(' ', indent), line)); | ||
|
||
return string.Join(Environment.NewLine, indentedLines); | ||
} | ||
} | ||
} |
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