@@ -155,28 +155,100 @@ public void AddText(char[] texts)//TODO)) 保持private
155155 }
156156 else
157157 {
158- //TODO)) 这里需要处理光标重叠的情况
159- // //查找一下看开始的位置是重叠到哪个元素了
160- // var hitIndex = 0;
161- // var hitLength = 0;
162- // foreach (var block in oldLine)
163- // {
164- // if (hitLength + block.Length > oldX)
165- // break;
166- // hitLength += block.Length;
167- // hitIndex++;
168- // }
169- // //查找看看结束的位置是重叠到哪个元素了
170- // var endIndex = hitIndex;
171- // var endLength = hitLength;
172- // for(int i = hitIndex; i < oldLine.Count; i++)
173- // {
174- // if (endLength + oldLine[i].Length > _positionX)
175- // break;
176- // endLength += oldLine[i].Length;
177- // endIndex++;
178- // }
158+ //把这一行按字符拆碎
159+ var tempLine = new List < TerminalBlock > ( ) ;
160+ foreach ( var block in needChangeLine )
161+ {
162+ //拆碎
163+ var tempChars = block . Text . ToCharArray ( ) ;
164+ foreach ( var c in tempChars )
165+ {
166+ var length = UnicodeCalculator . GetWidth ( c ) ;
167+ //塞到临时行中
168+ tempLine . Add ( block . MakeNew ( c . ToString ( ) ) ) ;
169+ //如果字符宽度超过1,加入空白填位
170+ length -- ;
171+ while ( length > 0 )
172+ {
173+ tempLine . Add ( block . MakeNew ( string . Empty ) ) ;
174+ length -- ;
175+ }
176+ }
177+ }
178+
179+ //待添加的字符列表
180+ //null表示前一个字符占据了这个位置
181+ List < char ? > charsForInsert = [ ] ;
182+ foreach ( var c in line . Text . ToCharArray ( ) )
183+ {
184+ charsForInsert . Add ( c ) ;
185+ var length = UnicodeCalculator . GetWidth ( c ) ;
186+ length -- ;
187+ while ( length > 0 )
188+ {
189+ charsForInsert . Add ( null ) ;
190+ length -- ;
191+ }
192+ }
193+ //一个个替换或者添加,从oldX开始
194+ var currentX = oldX ;
195+ //先判断第一个位置是否是空字符
196+ if ( string . IsNullOrEmpty ( tempLine [ currentX ] . Text ) )
197+ {
198+ //如果是空字符,则表示往前找可以找到一个占位符
199+ var charIndex = currentX ;
200+ while ( charIndex > 0 )
201+ {
202+ charIndex -- ;
203+ //如果找到的不是空字符,说明可以替换
204+ if ( ! string . IsNullOrEmpty ( tempLine [ charIndex ] . Text ) )
205+ break ;
206+ }
207+ //将找到的字符全部替换成空格
208+ for ( int i = charIndex ; i < currentX ; i ++ )
209+ {
210+ //替换成空格
211+ tempLine [ i ] . Text = " " ;
212+ }
213+ }
214+ //开始替换
215+ while ( charsForInsert . Count > 0 )
216+ {
217+ var s = string . Empty ;
218+ if ( charsForInsert [ 0 ] != null )
219+ s = charsForInsert [ 0 ] . ToString ( ) ;
220+ charsForInsert . RemoveAt ( 0 ) ;
179221
222+ //看看当前位置有没有字符,没有的话就新建一个
223+ if ( currentX >= tempLine . Count )
224+ {
225+ //添加一个新的块
226+ tempLine . Add ( line . MakeNew ( s ) ) ;
227+ }
228+ else
229+ {
230+ //有东西的话就替换掉
231+ tempLine [ currentX ] = line . MakeNew ( s ) ;
232+ }
233+ currentX ++ ;
234+ }
235+ //检查下currentX后面有没有空字符
236+ //如果有空字符,需要替换成空格
237+ while ( currentX < tempLine . Count )
238+ {
239+ //替换成空格
240+ if ( string . IsNullOrEmpty ( tempLine [ currentX ] . Text ) )
241+ tempLine [ currentX ] . Text = " " ;
242+ else
243+ break ;
244+ currentX ++ ;
245+ }
246+
247+ //处理完了,把needChangeLine替换掉
248+ needChangeLine . Clear ( ) ;
249+ //添加数据
250+ foreach ( var block in tempLine )
251+ needChangeLine . Add ( block ) ;
180252 }
181253
182254 //优化当前这一行数据块
0 commit comments