zsh-workers
 help / color / mirror / code / Atom feed
* RE: (m)-flag for boundary cases
@ 2017-03-21 13:02 Sebastian Gniazdowski
  2017-04-02 22:57 ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Sebastian Gniazdowski @ 2017-03-21 13:02 UTC (permalink / raw)
  To: zsh-workers

> Try this?  Works for characters that are 2 columns wide.  If you have
> a mix of character widths, probably a custom math function to use in the
> subscript ...
> 
> dw=3    # Desired width
> echo ${(mr:dw::_:)a[1,${(m)#a[1,dw-1]}>dw?dw/2:dw]}

Mixed character widths are possibile. Following cases can occur:

1. a="測試a句"; echo ${(mr:7:)a},
測試a句,

2. a="測試a句"; echo ${(mr:6:)a},
測試a句,

3. a="測試a句"; echo ${(mr:5:)a},
測試a,

4. a="測試a句"; echo ${(mr:4:)a},
測試,

Now a try with minus-one:
------------------------

1. a="測試a句"; echo ${(mr:7-1:)a},
測試a句,

2. a="測試a句"; echo ${(mr:6-1:)a},
測試a,

3. a="測試a句"; echo ${(mr:5-1:)a},
測試, # problem

4. a="測試a句"; echo ${(mr:4-1:)a},
測試,

So if one could fix the trailing-a absence, it seems it would work. Did following:

dw=5; a="測試a句"; echo ${(mr,${(m)#${(mr:dw:)a}[-1]} > 1 ? dw-1 : dw,)a}
測試a
dw=3; a="測試a句"; echo ${(mr,${(m)#${(mr:dw:)a}[-1]} > 1 ? dw-1 : dw,)a}
測

But not sure if I will commit this.

--
Sebastian Gniazdowski
psprint@zdharma.org


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: (m)-flag for boundary cases
  2017-03-21 13:02 (m)-flag for boundary cases Sebastian Gniazdowski
@ 2017-04-02 22:57 ` Bart Schaefer
  0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2017-04-02 22:57 UTC (permalink / raw)
  To: zsh-workers

On Mar 21,  2:02pm, Sebastian Gniazdowski wrote:
} Subject: RE: (m)-flag for boundary cases
}
} > dw=3    # Desired width
} > echo ${(mr:dw::_:)a[1,${(m)#a[1,dw-1]}>dw?dw/2:dw]}
} 
} Mixed character widths are possibile.
} 
[...]
} 
} So if one could fix the trailing-a absence, it seems it would work.

    # dw is the "desired width" [or "display width"] function
    # Declare to accept a string because subscripts hate extra commas,
    # and because we need to pass a parameter by name not by math value.
    # Call it in math context (e.g., a scalar slice subscript) like:
    #   dw(paramname:width)
    # Returns largest index w of a full character in $paramname, such
    # that ${paramname[1,w]} occupies $width or fewer display positions.
    dw() {                                  
      integer w
      set -- "${(@s,:,)1}"
      [[ -z ${(P)1} ]] && return -1                                
      for ((w=$2; ${(m)#${(P)1}[1,w]} > $2; w-- )); do :; done 
      return w
    }
    functions -M -s dw

Given the above this example --

    echo ${(mr:9::_:)string[1,dw(string:9)]}

-- always outputs a string nine display positions wide, padded on the
right with underscores, regardless of the mix of character widths in
the value of $string.

Optimizations, if any, left to the reader.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: (m)-flag for boundary cases
  2017-03-20 14:33 Sebastian Gniazdowski
  2017-03-20 16:00 ` Sebastian Gniazdowski
@ 2017-03-20 16:43 ` Bart Schaefer
  1 sibling, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2017-03-20 16:43 UTC (permalink / raw)
  To: zsh-workers

On Mar 20,  7:33am, Sebastian Gniazdowski wrote:
} Subject: (m)-flag for boundary cases
}
} Hello,
} 
} % a="0x6F220x5B57"
} % echo ${(mr:3::_:)a}
} 0x6F220x5B57
} % echo ${(mr:2::_:)a}
} 0x6F22
} 
} I think it would be better to trim first case down to single Asian
} character, and display: 0x6F22_. The point is that it gives predictable
} width.

Try this?  Works for characters that are 2 columns wide.  If you have
a mix of character widths, probably a custom math function to use in the
subscript ...

dw=3	# Desired width
echo ${(mr:dw::_:)a[1,${(m)#a[1,dw-1]}>dw?dw/2:dw]}


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: (m)-flag for boundary cases
  2017-03-20 14:33 Sebastian Gniazdowski
@ 2017-03-20 16:00 ` Sebastian Gniazdowski
  2017-03-20 16:43 ` Bart Schaefer
  1 sibling, 0 replies; 5+ messages in thread
From: Sebastian Gniazdowski @ 2017-03-20 16:00 UTC (permalink / raw)
  To: zsh-workers

On the other side, current padding never adds the padding character
unless the string is shorter than the given limit. With the change, it
would add "_" as described "漢_", suggesting there are no other
characters.

Maybe it should be: not add "_", but do skip the not-fitting character.
This would not meet the "constant length" property..
-- 
  Sebastian Gniazdowski
  psprint3@fastmail.com


^ permalink raw reply	[flat|nested] 5+ messages in thread

* (m)-flag for boundary cases
@ 2017-03-20 14:33 Sebastian Gniazdowski
  2017-03-20 16:00 ` Sebastian Gniazdowski
  2017-03-20 16:43 ` Bart Schaefer
  0 siblings, 2 replies; 5+ messages in thread
From: Sebastian Gniazdowski @ 2017-03-20 14:33 UTC (permalink / raw)
  To: zsh-workers

Hello,

% a="漢字"
% echo ${(mr:3::_:)a}
漢字
% echo ${(mr:2::_:)a}
漢

I think it would be better to trim first case down to single Asian
character, and display: 漢_. The point is that it gives predictable
width. Current version is occasionally 1 character too long.

I wonder how many people use (m)-flag. Never seen it mentioned here. I
think that nobody wants the off-by-1 behavior, and fixing it will not
break things, but make them automatically better, like for me – the flag
turned out to be a necessity and a solution, with acceptable off-by-1
problem, which will be automatically fixed by Zsh update. I've added
Asian characters support to ZUI via the flag, most important use case is
the (r) padding, but also good cooperation with (P) flag.

-- 
  Sebastian Gniazdowski
  psprint3@fastmail.com


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-04-02 22:57 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-21 13:02 (m)-flag for boundary cases Sebastian Gniazdowski
2017-04-02 22:57 ` Bart Schaefer
  -- strict thread matches above, loose matches on Subject: below --
2017-03-20 14:33 Sebastian Gniazdowski
2017-03-20 16:00 ` Sebastian Gniazdowski
2017-03-20 16:43 ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).