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

measureAlignment can only match first character matching predicate #32

Closed
Xitian9 opened this issue Mar 30, 2022 · 3 comments
Closed

measureAlignment can only match first character matching predicate #32

Xitian9 opened this issue Mar 30, 2022 · 3 comments

Comments

@Xitian9
Copy link
Collaborator

Xitian9 commented Mar 30, 2022

Based on the way measureAlignment :: Cell a => (Char -> Bool) -> a -> AlignInfo is defined and implemented, it will always match the first character within the cell which satisfies the predicate. Unfortunately this is not sufficient for all the cases one might want. You might want to match the first character on the right, or you might want to use a predicate which considers more than characters.

In my case, I want to align on decimal points, but among numbers which might use a mix of commas and periods for decimal separators. Furthermore, they might use the other symbol as a thousands separator. Here's an example of the output I'd like:

$1 005.15
£2,167.00
15.011,18€
¥1,235

I haven't yet figured out what the best approach to this problem is, but measureAlignment is not up to snuff. Clearly the job of marking which of these characters is the decimal point would not be the job of table-layout and would be up to me, but even if I could mark it somehow, measureAlignment couldn't take it into account since it only sees a single character at a time from the left. At the very least, we'd need to generalise the form of the predicate somehow.

@Xitian9
Copy link
Collaborator Author

Xitian9 commented Mar 30, 2022

Actually, I think this can be accomplished without any changes. I think it just requires a somewhat odd implementation of Cell.

data PointedNumber = PointedNumber String Char String

instance Cell PointedNumber where
  measureAlignment _ (PointedNumber w p d) = AlignInfo (length w) (Just $ length d)

I'll close this issue, but it might be good to have a record of it in case anybody comes across a similar need.

@muesli4
Copy link
Owner

muesli4 commented Oct 29, 2022

This could be implemented but probably needs a bit more thought:

  • Are some places more important than others?
  • Which parts would we like to show?
  • Where (or on which segment) is the whole cell then aligned?

There is also the more general question whether a text input is sufficient enough to get a good output rendered. For example, for scientific output one might want to use the exponent notation but just for numbers that are too big. But getting semantics into input would probably open up a whole new can of worms. But for anyone interested, knock yourself out. 😆

@Xitian9
Copy link
Collaborator Author

Xitian9 commented Oct 29, 2022

I agree that this is probably too specific to be included in anything we provide. But it's a useful trick to keep in mind for specific use cases people may have.

But more generally, the type of measureAlignment :: (Char -> Bool) -> a -> AlignInfo restricts us to only considering single characters sequentially. Would a more general type be more useful?

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