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

Support rich formatting #7

Open
andersnm opened this issue Nov 1, 2017 · 3 comments
Open

Support rich formatting #7

andersnm opened this issue Nov 1, 2017 · 3 comments

Comments

@andersnm
Copy link
Owner

andersnm commented Nov 1, 2017

Specifically: global color and alignment, variable width space, repeat-to-fill characters

The basic goal is to return something allowing the caller to render the formatted value more precisely with an arbitrary rendering backend, like f.ex a 2D canvas, SVG, HTML, while remaining neutral to font size and cell width.

@igitur FYI, these are my thoughts but have no immediate plans to implement myself.

F.ex a new method NumberFormat.FormatRich() could return a class shape like:

class FormatResult {
	Alignment Alignment
	Color Color
	List<FormatCommand> Commands
}

class FormatCommand {
	Command Command
	string Text
}

enum Command {
	SpaceForText,
	RepeatText,
	Text
}

enum Alignment {
	Left,
	Right,
	Center
}

enum Color {
	Red,
	Black,
	...
}

The Formatter class should probably operate on these classes internally instead of a StringBuilder, and rewrite the current Format() method to reduce the FormatResult to a string.

And I don't necessarily think these are good class/enum/method names: finding good names is the hardest.

@igitur
Copy link
Contributor

igitur commented Nov 1, 2017

So do you want to return an abstract syntax tree or something like that that represents the rich text? It will be left to the user to render the AST as he wants to.

@andersnm
Copy link
Owner Author

andersnm commented Nov 1, 2017

Essentially yes, that's the idea. The user is given a list of strings with rendering hints + global color/alignment info.

Alternatively, the library could of returned a single string with escaped formatting codes, or even XML/JSON, but then users need to parse that to render it. Such an approach would look like:

string NumberFormat.FormatRich(object value, CultureInfo, out Alignment alignment, out Color color)

That's not too bad. The returned string mostly looks like a normal formatted value, except contains escape codes for the '_' and '*' space and repeat characters.

At the moment I have no strong preference for either direction, both should work.

@igitur
Copy link
Contributor

igitur commented Nov 1, 2017 via email

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

No branches or pull requests

2 participants