@@ -5,6 +5,7 @@ module System.Console.Haskeline.Command.Completion(
5
5
completionCmd
6
6
) where
7
7
8
+ import System.Console.Haskeline.Backend.WCWidth (gsWidth )
8
9
import System.Console.Haskeline.Command
9
10
import System.Console.Haskeline.Command.Undo
10
11
import System.Console.Haskeline.Key
@@ -120,27 +121,26 @@ makeLines :: [String] -> Layout -> [String]
120
121
makeLines ws layout = let
121
122
minColPad = 2
122
123
printWidth = width layout
123
- maxLength = min printWidth (maximum (map length ws) + minColPad)
124
- numCols = printWidth `div` maxLength
125
- ls = if maxLength >= printWidth
124
+ maxWidth = min printWidth (maximum (map (gsWidth . stringToGraphemes) ws) + minColPad)
125
+ numCols = printWidth `div` maxWidth
126
+ ls = if maxWidth >= printWidth
126
127
then map (: [] ) ws
127
128
else splitIntoGroups numCols ws
128
- in map (padWords maxLength ) ls
129
+ in map (padWords maxWidth ) ls
129
130
130
- -- Add spaces to the end of each word so that it takes up the given length .
131
- -- Don't padd the word in the last column, since printing a space in the last column
131
+ -- Add spaces to the end of each word so that it takes up the given visual width .
132
+ -- Don't pad the word in the last column, since printing a space in the last column
132
133
-- causes a line wrap on some terminals.
133
134
padWords :: Int -> [String ] -> String
134
135
padWords _ [x] = x
135
136
padWords _ [] = " "
136
- padWords len (x: xs) = x ++ replicate (len - glength x) ' '
137
- ++ padWords len xs
137
+ padWords wid (x: xs) = x ++ replicate (wid - widthOf x) ' '
138
+ ++ padWords wid xs
138
139
where
139
- -- kludge: compute the length in graphemes, not chars.
140
- -- but don't use graphemes for the max length, since I'm not convinced
141
- -- that would work correctly. (This way, the worst that can happen is
142
- -- that columns are longer than necessary.)
143
- glength = length . stringToGraphemes
140
+ -- kludge: compute the width in graphemes, not chars.
141
+ -- also use graphemes for the max width so that multi-width characters
142
+ -- such as CJK letters are aligned correctly.
143
+ widthOf = gsWidth . stringToGraphemes
144
144
145
145
-- Split xs into rows of length n,
146
146
-- such that the list increases incrementally along the columns.
0 commit comments