Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • i think this must be the only way, really. if the terminal doesn't support double-width chars, then it doesn't much matter what wcswidth() has to say about anything at all. Commented Nov 23, 2015 at 23:22
  • In practice, the only problem I've had with this method is plink, which sets TERM=xterm even though it doesn't respond to any control sequence. But I don't use very exotic terminals. Commented Nov 24, 2015 at 0:15
  • Thanks. but the idea was to get that information prior to display the string on the terminal (to know where to display it, that's a follow-up on the recent question about displaying a string on the right of the terminal, maybe I should have mentioned that though my real question was really about how to get to wcswidth from the shell). @mikeserv, yes wcswidth() may be wrong about how a specific terminal would display a particular string, but that's as close as you can get to a terminal-independant solution and that's what col/ksh-printf use on my system. Commented Nov 24, 2015 at 11:54
  • I'm aware of that, but wcswidth isn't directly accessible except via less-portable features (you could do this in perl, by making some assumptions - see search.cpan.org/dist/Text-CharWidth/CharWidth.pm). The right-alignment question by the way could be (perhaps) improved by writing the string to the lower-left and then using the cursor-position and insert-controls to shift it to the lower-right. Commented Nov 24, 2015 at 11:59
  • 1
    @StéphaneChazelas - fold is apparently spec'd to handle multi-byte and extended width characters. Here's how it should handle backspace: The current count of line width shall be decremented by one, although the count never shall become negative. The fold utility shall not insert a <newline> immediately before or after any <backspace>, unless the following character has a width greater than 1 and would cause the line width to exceed width. maybe fold -w[num] and pr +[num] could be teamed up somehow? Commented Nov 24, 2015 at 14:26