Terminal.Gui icon indicating copy to clipboard operation
Terminal.Gui copied to clipboard

MakePrintable and coloured blocks (image to text)

Open AngryAnt opened this issue 2 years ago • 1 comments

I am looking to integrate a bit of code I use in a regular terminal app to output images converted to coloured character blocks. My attempt has gone through ConsoleDriver.MakePrintable, but I am unsure if this is sufficient for display in normal Label views - by simply setting its Text attribute.

My attempt currently just yields a sea of control character output:

NStack.ustring rendering = await RenderImage ("examle.png", 40, 40);
IEnumerable<Rune> runes = rendering.Select (character => ConsoleDriver.MakePrintable (character));
NStack.ustring result = NStack.ustring.Make (runes)!;

m_Label.Text = result;

RenderImage runs https://hpjansson.org/chafa/ and pipes back the output. In a normal terminal app this works wonderfully when just passed to Console.Write, but as described, my attempt above results in control character output like [0m [7m [38; 5; 255m.

Do I need to do some sort of render context escape to properly handle these characters?

AngryAnt avatar Dec 21 '23 11:12 AngryAnt

What kind of return is chafa producing? is it just characters or is it console control codes for colors etc?

I think you are best creating a new View subclass and adding your logic there. But without diving into the library you are using its hard to advise.

If you can get the view to return the characters seperate from the colors as say 2d arrays then you can just override the draw method.

There is a similar example View in v2 that could be a good place to start to see how a similar problem is tackled natively. Obviously it is very simple in comparison to chafa because it is intended just for demonstration purposes.

image

tznind avatar Dec 21 '23 13:12 tznind