Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

RE: (m)-flag for boundary cases



> 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@xxxxxxxxxxx



Messages sorted by: Reverse Date, Date, Thread, Author