Comments (10)
nice :D
That sounds like fun, I'll take a look at it.
Is that the total implementation or is there more? I'll probably not add it to the IConsole interface, then everyone that implements it will need to do it, but will create an extension method and put this in an extensions namespace,
For example ... something like
namespace Konsole.Extensions.BBCode;
public static class BBCodeExtensions
{
public static void WriteLineBBCode(this IConsole console, string line)
{
var exp = new Regex(@"\[color\=([^\]]+)\]([^\]]+)\[\/color\]");
var matches = exp.Matches(line);
foreach (Match match in matches)
{
Enum.TryParse(match.Groups[1].Value, true, out ConsoleColor consoleColor);
console.Write(consoleColor, match.Groups[2].Value);
}
console.WriteLine("");
}
}
mmm, just thinking... perhaps even cleaner might be a simple BBCode writer implementing IWrite
,
namespace Konsole.Extensions.BBCode;
public class BBCodeWriter : IWrite
{
public BBCodeWriter(IConsole console) {
...
}
public static void WriteLine(string line)
{
var exp = new Regex(@"\[color\=([^\]]+)\]([^\]]+)\[\/color\]");
var matches = exp.Matches(line);
foreach (Match match in matches)
{
Enum.TryParse(match.Groups[1].Value, true, out ConsoleColor consoleColor);
_writeLine(consoleColor, match.Groups[2].Value);
}
}
}
the downside of a separate writer for BBCode, is that you'd have to keep track of which one to use each time, so might be code look untidy e.g.
var bbCode = new BBCodeWriter(console);
console.WriteLine($"User : {user.Name}");
foreach(var msg in user. Logs) bbCode.WriteLine(msg);
vs
using Konsole.Extensions.BBCode;
...
console.WriteLine($"User : {user.Name}");
foreach(var msg in user. Logs) console. WriteLineBBCode(msg);
after writing the code above, I prefer the second option, i.e. as you described it originally, but you import it by including the extension method namespace.
from konsole.
That's all I have for BBCode for now. And even it has very limited use. For example it does not write text without color tag. So "abc[color=red]efg[/color] will output only "efg". So I use "[color=white]abc[/color][color=red]efg[/color]" which is longer but works.
Implementing IWrite looks cleaner, but what to do if I want to use HighSpeedWriter and BBCodeWriter at the same time?..
from konsole.
re: HighSpeedWriter
all of the methods above are compatible with highspeed writer :D
just remember to call flush.
from konsole.
Maybe good WriteLine signature would be void WriteLine(string line, Style style = null);
where
public class Style {
public bool UseBBCode = true;
// another style settings like font, size, etc. Don't know if it's possible with console
}
from konsole.
Maybe good WriteLine signature would be
void WriteLine(string line, Style style = null);
wherepublic class Style {
public bool UseBBCode = true;
// another style settings like font, size, etc. Don't know if it's possible with console
}
you can always write that yourself in your own private extension method.
I can't add that to Konsole because that will then become part of the contract and everyone implementing IConsole
would have to implement that. Or worse, two different programs passing an IConsole
between them may not actually be sharing or swapping compatible IConsole
references.
Write your own extensions methods
public static class MyExtensions
{
public static MyWriteXYZ(this IConsole console, MySettings settings) {
...
}
your settings can have a style property if you want. the sky is the limit
from konsole.
That's all I have for BBCode for now. And even it has very limited use. For example it does not write text without color tag. So "abc[color=red]efg[/color] will output only "efg". So I use "[color=white]abc[/color][color=red]efg[/color]" which is longer but works.
Implementing IWrite looks cleaner, but what to do if I want to use HighSpeedWriter and BBCodeWriter at the same time?..
perhaps you can do something like
// pseudo code
var remaining = text;
public static void WriteLineBBCode(this Iconsole console, string text)
lock(Window.StaticLocker)
{
var before = console.Colors;
try
{
while(remaining.Length > 1) {
int nextTag = GetNextTagStartPosition();
if(nextTag == 0) return;
console.Write(remaining.Sub(0, nextTag)); // will print in the current color, e.g. White
remaining = remaining.Sub(nextTag);
// the next text in 'remaining' is a tag
remaining = SetColorFromTagReturnRemainingWithoutTag();
}
} finally
{
// reset the consor color after printing BBC in case it's badly formatted and a missing closing tag
// and in case of exception, keep colors correct.
console.Colors = before;
}
}
});
private static string SetColorFromTagReturnRemainingWithoutTag(IConsole console) {
.. .your regex here to pick up next tag
}
from konsole.
After reading a bit more on wikipedia on BBCode it looks like BBCode is a type of markdown that is best suited for pages that are rendered on a web page. e.g. embedding links etc. An older type of Markdown. Most of the BBCode features (other than foreground and background color, won't be a able to be implemented in a text console. )
Font , size, underline, italic etc, all wont work.
- bullets are redundant, since you create them yourself in text.
So as it stands, you should write your own extension class, this doesn't look like something we can add to be part of the main Konsole package.
I'm happy to keep the conversation going if you want to discuss/ explore this more?
Cheers,
Alan
from konsole.
No, it's ok, i'm closing the issue if bold/italic etc wont work
from konsole.
No, it's ok, i'm closing the issue if bold/italic etc wont work
Unfortunately only 1 font style per console at a time, and it needs to be a fixedWidth font for obvious reasons. No italic, and no underline.
you can easily set colors. If you're allowed to create your own BBCode for yourself or your users, then you can very quickly implement something for setting foreground and background colors, which won't be complcated. e.g.
I am default color white [red]I am red[/]I am white again.
I am white [WHITE][red]I am red on white background [//]I am red on black[/]I am white on black
simple rules, capital = background. [/] means reset back to original console foreground color, [//] means reset back to original background color.
from konsole.
and [///] resets both foreground and background color to default.
e.g.
I am white on black [WHITE][red]I am red on white background [///] I am now white on black again. (default)
from konsole.
Related Issues (20)
- Prevent console window resize HOT 16
- ProgressBar does not expose current HOT 8
- IWrite allow StringBuilder type of parameter HOT 9
- SplitRows and SplitColumns without border HOT 1
- Embedded CrLf cause window isolation to break.
- HighSpeed writer to support resize on window size change
- Fix spelling, Boxstyle.Thickness to Thickness.
- Menu not explained in the README. HOT 2
- when splitRows with 3 splits, with even number of parent rows, the last split is not expanding by 1 row
- Konsole.Writer.MoveBufferArea throws System.PlatformNotSupportedException on Nix Platforms HOT 4
- ProgressBar not working on Linux HOT 2
- Does window support chinese character text auto wrap? HOT 4
- Trying to use Konsole for First Time HOT 3
- Ability to close boxes. HOT 9
- Support non "lined" borders (line numbers, blue statusbar, and text header) HOT 1
- Seemingly impossible divide by zero exception thrown HOT 4
- System.ArgumentOutOfRangeException in ProgressBar.Refresh() HOT 16
- How to prevent console from being moved by user? Snap windows feature is changing size of Console. HOT 1
- The simple initialization of ProgressBar is throwing "The handle is invalid." exception in VSCode while debugging. HOT 7
- Add image support HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from konsole.