Skip to content

Commit

Permalink
add - doc - Added figlet wrapping
Browse files Browse the repository at this point in the history
---

We've added a way to wrap your Figlet fonts.

---

Type: add
Breaking: False
Doc Required: True
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Jul 4, 2024
1 parent 1bceb62 commit 0012411
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 54 deletions.
34 changes: 34 additions & 0 deletions Textify.Demos.Offline/Fixtures/Cases/FigletPrintWrap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Textify Copyright (C) 2023-2024 Aptivi
//
// This file is part of Textify
//
// Textify is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Textify is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using System;
using Textify.Figlet;

namespace Textify.Demos.Offline.Fixtures.Cases
{
public class FigletPrintWrap : IFixture
{
public string FixtureID => "FigletPrintWrap";
public void RunFixture()
{
string figlet = FigletTools.RenderFiglet("Hello!", "speed", 20);
Console.WriteLine(figlet);
}
}
}
1 change: 1 addition & 0 deletions Textify.Demos.Offline/Fixtures/FixtureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ internal static class FixtureManager

// Figlet
new FigletPrint(),
new FigletPrintWrap(),
];

internal static IFixture GetFixtureFromName(string name)
Expand Down
80 changes: 42 additions & 38 deletions Textify.Demos.Offline/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
{
"profiles": {
"WSL": {
"commandName": "WSL2",
"distributionName": ""
},
"Textify - Offline - AnalyzeSpaces": {
"commandName": "Project",
"commandLineArgs": "AnalyzeSpaces"
},
"Textify - Offline - RepairSpaces": {
"commandName": "Project",
"commandLineArgs": "RepairSpaces"
},
"Textify - Offline - QueryUnicode": {
"commandName": "Project",
"commandLineArgs": "QueryUnicode"
},
"Textify - Offline - VersionInfo": {
"commandName": "Project",
"commandLineArgs": "VersionInfo"
},
"Textify - Offline - VersionInfoRev": {
"commandName": "Project",
"commandLineArgs": "VersionInfoRev"
},
"Textify - Offline - WordGet": {
"commandName": "Project",
"commandLineArgs": "WordGet"
},
"Textify - Offline - NameGenerator": {
"commandName": "Project",
"commandLineArgs": "NameGenerator"
},
"Textify - Offline - FigletPrint": {
"commandName": "Project",
"commandLineArgs": "FigletPrint"
"profiles": {
"WSL": {
"commandName": "WSL2",
"distributionName": ""
},
"Textify - Offline - AnalyzeSpaces": {
"commandName": "Project",
"commandLineArgs": "AnalyzeSpaces"
},
"Textify - Offline - RepairSpaces": {
"commandName": "Project",
"commandLineArgs": "RepairSpaces"
},
"Textify - Offline - QueryUnicode": {
"commandName": "Project",
"commandLineArgs": "QueryUnicode"
},
"Textify - Offline - VersionInfo": {
"commandName": "Project",
"commandLineArgs": "VersionInfo"
},
"Textify - Offline - VersionInfoRev": {
"commandName": "Project",
"commandLineArgs": "VersionInfoRev"
},
"Textify - Offline - WordGet": {
"commandName": "Project",
"commandLineArgs": "WordGet"
},
"Textify - Offline - NameGenerator": {
"commandName": "Project",
"commandLineArgs": "NameGenerator"
},
"Textify - Offline - FigletPrint": {
"commandName": "Project",
"commandLineArgs": "FigletPrint"
},
"Textify - Offline - FigletPrintWrap": {
"commandName": "Project",
"commandLineArgs": "FigletPrintWrap"
}
}
}
}
}
48 changes: 37 additions & 11 deletions Textify.Figlet/FigletTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ public static class FigletTools
/// </summary>
/// <param name="Text">Text</param>
/// <param name="FigletFont">Target figlet font</param>
public static string[] GetFigletLines(string Text, FigletFont FigletFont)
/// <param name="width">Max width of the resultant figlet rendered text. Pass 0 for single-line.</param>
public static string[] GetFigletLines(string Text, FigletFont FigletFont, int width = 0)
{
Text = FigletFont.Render(Text);
Text = FigletFont.Render(Text, null, width);
return Text.SplitNewLines();
}

Expand All @@ -55,16 +56,18 @@ public static string[] GetFigletLines(string Text, FigletFont FigletFont)
/// </summary>
/// <param name="Text">Text</param>
/// <param name="FigletFont">Target figlet font</param>
public static int GetFigletHeight(string Text, FigletFont FigletFont) =>
GetFigletLines(Text, FigletFont).Length;
/// <param name="width">Max width of the resultant figlet rendered text. Pass 0 for single-line.</param>
public static int GetFigletHeight(string Text, FigletFont FigletFont, int width = 0) =>
GetFigletLines(Text, FigletFont, width).Length;

/// <summary>
/// Gets the figlet text width
/// </summary>
/// <param name="Text">Text</param>
/// <param name="FigletFont">Target figlet font</param>
public static int GetFigletWidth(string Text, FigletFont FigletFont) =>
GetFigletLines(Text, FigletFont).Max((line) => line.Length);
/// <param name="width">Max width of the resultant figlet rendered text. Pass 0 for single-line.</param>
public static int GetFigletWidth(string Text, FigletFont FigletFont, int width = 0) =>
GetFigletLines(Text, FigletFont, width).Max((line) => line.Length);

/// <summary>
/// Gets the figlet fonts
Expand Down Expand Up @@ -107,7 +110,7 @@ public static FigletFont GetFigletFont(string FontName)
public static string RenderFiglet(string Text, string figletFontName, params object[] Vars)
{
var FigletFont = GetFigletFont(figletFontName);
return RenderFiglet(Text, FigletFont, Vars);
return RenderFiglet(Text, FigletFont, 0, Vars);
}

/// <summary>
Expand All @@ -116,16 +119,39 @@ public static string RenderFiglet(string Text, string figletFontName, params obj
/// <param name="Text">Text to render</param>
/// <param name="FigletFont">Figlet font instance to render. Consult <see cref="GetFigletFonts()"/> for more info.</param>
/// <param name="Vars">Variables to use when formatting the string</param>
public static string RenderFiglet(string Text, FigletFont FigletFont, params object[] Vars)
public static string RenderFiglet(string Text, FigletFont FigletFont, params object[] Vars) =>
RenderFiglet(Text, FigletFont, 0, Vars);

/// <summary>
/// Renders the figlet font
/// </summary>
/// <param name="Text">Text to render</param>
/// <param name="figletFontName">Figlet font name to render. Consult <see cref="GetFigletFonts()"/> for more info.</param>
/// <param name="Vars">Variables to use when formatting the string</param>
/// <param name="width">Max width of the resultant figlet rendered text. Pass 0 for single-line.</param>
public static string RenderFiglet(string Text, string figletFontName, int width, params object[] Vars)
{
var FigletFont = GetFigletFont(figletFontName);
return RenderFiglet(Text, FigletFont, width, Vars);
}

/// <summary>
/// Renders the figlet font
/// </summary>
/// <param name="Text">Text to render</param>
/// <param name="FigletFont">Figlet font instance to render. Consult <see cref="GetFigletFonts()"/> for more info.</param>
/// <param name="Vars">Variables to use when formatting the string</param>
/// <param name="width">Max width of the resultant figlet rendered text. Pass 0 for single-line.</param>
public static string RenderFiglet(string Text, FigletFont FigletFont, int width, params object[] Vars)
{
// Get the figlet font name.
string figletFontName = FigletFont.Name;
if (string.IsNullOrEmpty(figletFontName))
return "";

// Now, render the figlet and add to the cache
string cachedFigletKey = $"[{cachedFiglets.Count} - {figletFontName}] {Text}";
string cachedFigletKeyToAdd = $"[{cachedFiglets.Count + 1} - {figletFontName}] {Text}";
string cachedFigletKey = $"[{cachedFiglets.Count} - {figletFontName}] {Text} {width}";
string cachedFigletKeyToAdd = $"[{cachedFiglets.Count + 1} - {figletFontName}] {Text} {width}";
if (cachedFiglets.ContainsKey(cachedFigletKey))
return cachedFiglets[cachedFigletKey];
else
Expand All @@ -135,7 +161,7 @@ public static string RenderFiglet(string Text, FigletFont FigletFont, params obj
Text = TextTools.FormatString(Text, Vars);

// Write the font
Text = string.Join("\n", GetFigletLines(Text, FigletFont));
Text = string.Join("\n", GetFigletLines(Text, FigletFont, width));
cachedFiglets.Add(cachedFigletKeyToAdd, Text);
return Text;
}
Expand Down
45 changes: 40 additions & 5 deletions Textify.Figlet/Utilities/Lines/FigletFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Textify.General;

namespace Textify.Figlet.Utilities.Lines
{
Expand Down Expand Up @@ -118,12 +119,15 @@ public bool Contains(char c)
/// </summary>
/// <param name="message">The text to render.</param>
/// <param name="smushOverride">Optional override for the smush settings. Defaults to <c>null</c>, meaning the font's default setting is used.</param>
/// <param name="width">Max width of the resultant figlet rendered text. Pass 0 for single-line.</param>
/// <returns></returns>
public string Render(string message, int? smushOverride = null)
public string Render(string message, int? smushOverride = null, int width = 0)
{
var smush = smushOverride ?? _smushMode;
var outputLineBuilders = Enumerable.Range(0, Height).Select(_ => new StringBuilder()).ToList();
FigletCharacter lastCh = null;
bool needsWidthCalc = width > 0;
int widthLine = 0;

foreach (var c in message)
{
Expand All @@ -150,7 +154,8 @@ public string Render(string message, int? smushOverride = null)
var lineSpaceTrim = Math.Min(lineSpace, toMove);
for (var i = 0; i < lineSpaceTrim; i++)
{
if (outputLine[outputLine.Length - 1] != ' ') break;
if (outputLine[outputLine.Length - 1] != ' ')
break;
toMove--;
outputLine.Length--;
}
Expand All @@ -159,10 +164,18 @@ public string Render(string message, int? smushOverride = null)

if (outputLine.Length != 0)
{
string content = toMove == 0 ? charLine.Content : charLine.Content.Substring(toMove);
if (needsWidthCalc && outputLine.ToString().SplitNewLines()[widthLine].Length + content.Length > width)
{
widthLine++;
foreach (var builder in outputLineBuilders)
builder.Append("\n");
}

var smushCharIndex = outputLine.Length - 1;
var cl = outputLine[smushCharIndex];

outputLine.Append(toMove == 0 ? charLine.Content : charLine.Content.Substring(toMove));
outputLine.Append(content);

if (toMove != 0 && outputLine.Length != 0 && ch.Lines[row].Content.Length != 0)
{
Expand All @@ -175,13 +188,21 @@ public string Render(string message, int? smushOverride = null)
}
else
{
outputLine.Append(charLine.Content);
string content = charLine.Content;
if (needsWidthCalc && outputLine.ToString().SplitNewLines()[widthLine].Length + content.Length > width)
{
widthLine++;
foreach (var builder in outputLineBuilders)
builder.Append("\n");
}
outputLine.Append(content);
}
}

lastCh = ch;
}

// Make the output lines and process them after trimming them
var res = new StringBuilder();
var outputLines = outputLineBuilders.Select((sb) => sb.Replace(_hardBlank, ' ').ToString()).ToList();

Expand All @@ -201,8 +222,22 @@ public string Render(string message, int? smushOverride = null)
outputLines.RemoveAt(line);
}

// Move the newline indicators to their right places
int times = outputLines[0].SplitNewLines().Length;
var finalOutputLines = new List<string>(Enumerable.Range(0, outputLines.Count * times).Select((_) => ""));
for (int i = 0; i < outputLines.Count; i++)
{
string outputLine = outputLines[i];
string[] outputLinesAdditional = outputLine.SplitNewLines();
for (int lineIdx = 0; lineIdx < outputLinesAdditional.Length; lineIdx++)
{
string outputLineAdditional = outputLinesAdditional[lineIdx];
finalOutputLines[i + (outputLines.Count * lineIdx)] = outputLineAdditional;
}
}

// Now, add the lines
foreach (string outputLine in outputLines)
foreach (var outputLine in finalOutputLines)
res.AppendLine(outputLine);
return res.ToString(0, res.Length - Environment.NewLine.Length);

Expand Down

0 comments on commit 0012411

Please sign in to comment.