Emojis render with trailing spaces within Boxes
Hi there! First time using ink, and I'm really enjoying it so far, but I've run into a snag for my project.
I'm making a small terminal-based board game and want to display emojis inside Box elements, but as the title says, every time I render a Box containing a Text element with emojis inside, any row containing emoji gets one space appended per emoji, breaking the right side of the box, which is otherwise appropriately-sized for its content.
The extra spaces do not seem to appear when emojis are printed in a Text element outside of a Box.
Example code:
const Board = () => {
return (
<Box flexDirection='column' alignItems='flex-start'>
<Box borderStyle='round'>
<Text>🌊</Text>
</Box>
<Box borderStyle='round'>
<Text>🌊🌊</Text>
</Box>
<Box borderStyle='round'>
<Text>🌊🌊🌊</Text>
</Box>
</Box>
)
};
Example output:

Also wanted to mention I'm in a programming bootcamp at the moment and looking for open source projects to contribute to.
If this doesn't sound like a very complicated issue to solve, I'd love to take a crack at it! Some assistance in where to look for the cause of the issue would be appreciated too.
So, after a little bit of digging, I hacked render-border to work exclusively with double-width characters by setting the x of the right side of the border to x + width / 2, and got some pleasing results with the same example:

Is there any way we could keep track of how many double-width characters exist in a text string? If so, we could make adjustments to that formula to get to a more robust solution.
The more I look into this issue and the pieces involved, the less clear-cut the solution is. While my hack above works with single line boxes (or multi-line boxes with the same number of characters per-line), it falls apart on multi-line boxes with a different numbers of characters per-line, causing lines with fewer characters to have right borders which are offset inwards:

This is because the x position of the right side of the border is determined as a uniform count of characters from the left side, based on the computed width of the whole box. As soon as variable-width characters are introduced to this, that position can no longer be handled uniformly, and would instead have to be calculated per-line, requiring what I would imagine to be a lot of rewriting of the border drawing code.
I'd still like to continue trying to find a solution for this, if it's at all feasible. If anyone has any ideas, I'd love to hear them.
Edit: The more I think about it, the best solution would be to add some kind of overrideLength property in
Here's the super annoying reason behind this:
https://hsivonen.fi/string-length/
From the sound of it, V8 itself would need to be fixed. Although, a hack-work-around would be to find all emoji matches in a string, and change the length to 1 would work too. I don't know how performance intensive this would be, but if any given string includes any of these symbols:
https://raw.githubusercontent.com/omnidan/node-emoji/master/lib/emoji.json
then, the fix would be to individually check the length of that symbol, let's say it's 7, and subtract 6 from what the full length of the string is computed to be. I don't know.
If it is computationally expensive, perhaps adding a new property to <Text like containsEmoji="true" would be helpful! Alternatively, include a new top-level tag that emits the length 1 somehow and contains just 1 emoji within, then no calculations would be needed at all.
That's the quirkiness of some emojis, sometimes they take 2 characters to display for some reason. Not super educated about this subject, but if anyone feels adventurous, go ahead and take a stab a this!
Hey, I tried to take a stab a few days ago, but I couldn't find in the code where length of a particular "Text" node is output... Do you have any hints?
See https://github.com/vadimdemedes/ink/blob/master/src/measure-text.ts.
For a temporary workaround, I'm wrapping my emoji in a transform that adds an additional space. It only works if you have the box at a defined width though. I also haven't tried this out with multiple lines inside a single box yet.
const Heart = () => (
<Transform transform={(output) => output + " "}>
<Text>❤️ </Text>
</Transform>
);
Another workaround if the workaround mentioned by @geoolson doesn't work for the emoji you are trying to use. (for example 🔍)
<Transform transform={(output) => output.slice(0, -1)}>
<Text>🔍 {text}</Text>
</Transform>
This one also seems to work well with % based width.
I found a fix for this in #444 and it renders correctly without transform workarounds:

@dundalek this is great! I am having the same issue, but I noticed your PR never passed. Any word on the work to get this incorporated?
@vadimdemedes Why are you closing these line of issues without any explanation?
Edit: my bad, watched this using mobile and it looked like 'close' message without related PR
@goldbergyoni The explanation is provided by the close message: it was closed because PR #444 resolved the issue and was merged.
Yes, watched this using mobile and it looked like 'close' message without related PR. My bad. Thanks