开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (3)
标签 (3)
master
zadjii-msft-patch-readme
richturn/retarget-echocon
1904.29002
1810.02002
1708.14008
master
分支 (3)
标签 (3)
master
zadjii-msft-patch-readme
richturn/retarget-echocon
1904.29002
1810.02002
1708.14008
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (3)
标签 (3)
master
zadjii-msft-patch-readme
richturn/retarget-echocon
1904.29002
1810.02002
1708.14008
Terminal
/
src
/
host
/
selectionInput.cpp
Terminal
/
src
/
host
/
selectionInput.cpp
selectionInput.cpp 37.30 KB
一键复制 编辑 原始数据 按行查看 历史
Dustin Howett 提交于 2019年05月03日 06:29 +08:00 . Initial release of the Windows Terminal source code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "precomp.h"
#include "search.h"
#include "../interactivity/inc/ServiceLocator.hpp"
#include "../types/inc/convert.hpp"
#include <algorithm>
using namespace Microsoft::Console::Types;
// Routine Description:
// - Handles a keyboard event for extending the current selection
// - Must be called when the console is in selecting state.
// Arguments:
// - pInputKeyInfo : The key press state information from the keyboard
// Return Value:
// - True if the event is handled. False otherwise.
Selection::KeySelectionEventResult Selection::HandleKeySelectionEvent(const INPUT_KEY_INFO* const pInputKeyInfo)
{
const CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
const auto inputServices = ServiceLocator::LocateInputServices();
FAIL_FAST_IF(!IsInSelectingState());
const WORD wVirtualKeyCode = pInputKeyInfo->GetVirtualKey();
const bool ctrlPressed = WI_IsFlagSet(inputServices->GetKeyState(VK_CONTROL), KEY_PRESSED);
// if escape or ctrl-c, cancel selection
if (!IsMouseButtonDown())
{
if (wVirtualKeyCode == VK_ESCAPE)
{
ClearSelection();
return Selection::KeySelectionEventResult::EventHandled;
}
else if (wVirtualKeyCode == VK_RETURN ||
// C-c, C-Ins. C-S-c Is also handled by this case.
((ctrlPressed) && (wVirtualKeyCode == 'C' || wVirtualKeyCode == VK_INSERT)))
{
Telemetry::Instance().SetKeyboardTextEditingUsed();
// copy selection
return Selection::KeySelectionEventResult::CopyToClipboard;
}
else if (gci.GetEnableColorSelection() &&
('0' <= wVirtualKeyCode) &&
('9' >= wVirtualKeyCode))
{
if (_HandleColorSelection(pInputKeyInfo))
{
return Selection::KeySelectionEventResult::EventHandled;
}
}
}
if (!IsMouseInitiatedSelection())
{
if (_HandleMarkModeSelectionNav(pInputKeyInfo))
{
return Selection::KeySelectionEventResult::EventHandled;
}
}
else if (!IsMouseButtonDown())
{
// if the existing selection is a line selection
if (IsLineSelection())
{
// try to handle it first if we've used a valid keyboard command to extend the selection
if (HandleKeyboardLineSelectionEvent(pInputKeyInfo))
{
return Selection::KeySelectionEventResult::EventHandled;
}
}
// if in mouse selection mode and user hits a key, cancel selection
if (!IsSystemKey(wVirtualKeyCode)) {
ClearSelection();
}
}
return Selection::KeySelectionEventResult::EventNotHandled;
}
// Routine Description:
// - Checks if a keyboard event can be handled by HandleKeyboardLineSelectionEvent
// Arguments:
// - pInputKeyInfo : The key press state information from the keyboard
// Return Value:
// - True if the event can be handled. False otherwise.
// NOTE:
// - Keyboard handling cases in this function should be synchronized with HandleKeyboardLineSelectionEvent
bool Selection::s_IsValidKeyboardLineSelection(const INPUT_KEY_INFO* const pInputKeyInfo)
{
bool fIsValidCombination = false;
const WORD wVirtualKeyCode = pInputKeyInfo->GetVirtualKey();
if (pInputKeyInfo->IsShiftOnly())
{
switch (wVirtualKeyCode)
{
case VK_LEFT:
case VK_RIGHT:
case VK_UP:
case VK_DOWN:
case VK_NEXT:
case VK_PRIOR:
case VK_HOME:
case VK_END:
fIsValidCombination = true;
}
}
else if (pInputKeyInfo->IsShiftAndCtrlOnly())
{
switch (wVirtualKeyCode)
{
case VK_LEFT:
case VK_RIGHT:
case VK_UP:
case VK_DOWN:
case VK_HOME:
case VK_END:
fIsValidCombination = true;
}
}
return fIsValidCombination;
}
// Routine Description:
// - Modifies the given selection point to the edge of the next (or previous) word.
// - By default operates in a left-to-right fashion.
// Arguments:
// - fReverse: Specifies that this function should operate in reverse. E.g. Right-to-left.
// - bufferSize: The dimensions of the screen buffer.
// - coordAnchor: The point within the buffer (inside the edges) where this selection started.
// - coordSelPoint: Defines selection region from coordAnchor to this point. Modified to define the new selection region.
// Return Value:
// - <none>
COORD Selection::WordByWordSelection(const bool fReverse,
const Viewport& bufferSize,
const COORD coordAnchor,
const COORD coordSelPoint) const
{
const CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
const SCREEN_INFORMATION& screenInfo = gci.GetActiveOutputBuffer();
COORD outCoord = coordSelPoint;
// first move one character in the requested direction
if (!fReverse)
{
bufferSize.IncrementInBounds(outCoord);
}
else
{
bufferSize.DecrementInBounds(outCoord);
}
// get the character at the new position
auto charData = *screenInfo.GetTextDataAt(outCoord);
// we want to go until the state change from delim to non-delim
bool fCurrIsDelim = IsWordDelim(charData);
bool fPrevIsDelim;
// find the edit-line boundaries that we can highlight
COORD coordMaxLeft;
COORD coordMaxRight;
const bool fSuccess = s_GetInputLineBoundaries(&coordMaxLeft, &coordMaxRight);
// if line boundaries fail, then set them to the buffer corners so they don't restrict anything.
if (!fSuccess)
{
coordMaxLeft.X = bufferSize.Left();
coordMaxLeft.Y = bufferSize.Top();
coordMaxRight.X = bufferSize.RightInclusive();
coordMaxRight.Y = bufferSize.BottomInclusive();
}
// track whether we failed to move during an operation
// if we failed to move, we hit the end of the buffer and should just highlight to there and be done.
bool fMoveSucceeded = false;
// determine if we're highlighting more text or unhighlighting already selected text.
bool fUnhighlighting;
if (!fReverse)
{
// if the selection point is left of the anchor, then we're unhighlighting when moving right
fUnhighlighting = Utils::s_CompareCoords(outCoord, coordAnchor) < 0;
}
else
{
// if the selection point is right of the anchor, then we're unhighlighting when moving left
fUnhighlighting = Utils::s_CompareCoords(outCoord, coordAnchor) > 0;
}
do
{
// store previous state
fPrevIsDelim = fCurrIsDelim;
// to make us "sticky" within the edit line, stop moving once we've reached a given max position left/right
// users can repeat the command to move past the line and continue word selecting
// if we're at the max position left, stop moving
if (Utils::s_CompareCoords(outCoord, coordMaxLeft) == 0)
{
// set move succeeded to false as we can't move any further
fMoveSucceeded = false;
break;
}
// if we're at the max position right, stop moving.
// we don't want them to "word select" past the end of the edit line as there's likely nothing there.
// (thus >= and not == like left)
if (Utils::s_CompareCoords(outCoord, coordMaxRight) >= 0)
{
// set move succeeded to false as we can't move any further.
fMoveSucceeded = false;
break;
}
if (!fReverse)
{
fMoveSucceeded = bufferSize.IncrementInBounds(outCoord);
}
else
{
fMoveSucceeded = bufferSize.DecrementInBounds(outCoord);
}
if (!fMoveSucceeded)
{
break;
}
// get the character associated with the new position
charData = *screenInfo.GetTextDataAt(outCoord);
fCurrIsDelim = IsWordDelim(charData);
// This is a bit confusing.
// If we're going Left to Right (!fReverse)...
// - Then we want to keep going UNTIL (!) we move from a delimiter (fPrevIsDelim) to a normal character (!fCurrIsDelim)
// This will then eat up all delimiters after a word and stop once we reach the first letter of the next word.
// If we're going Right to Left (fReverse)...
// - Then we want to keep going UNTIL (!) we move from a normal character (!fPrevIsDelim) to a delimeter (fCurrIsDelim)
// This will eat up all letters of the word and stop once we see the delimiter before the word.
} while (!fReverse ? !(fPrevIsDelim && !fCurrIsDelim) : !(!fPrevIsDelim && fCurrIsDelim));
// To stop the loop, we had to move the cursor one too far to figure out that the delta occurred from delimeter to not (or vice versa)
// Therefore move back by one character after proceeding through the loop.
// EXCEPT:
// 1. If we broke out of the loop by reaching the beginning of the buffer, leave it alone.
// 2. If we're un-highlighting a region, also leave it alone.
// This is an oddity that occurs because our cursor is on a character, not between two characters like most text editors.
// We want the current position to be ON the first letter of the word (or the last delimeter after the word) so it stays highlighted.
if (fMoveSucceeded && !fUnhighlighting)
{
if (!fReverse)
{
bufferSize.DecrementInBounds(outCoord);
}
else
{
bufferSize.IncrementInBounds(outCoord);
}
FAIL_FAST_IF(!fMoveSucceeded); // we should never fail to move forward after having moved backward
}
return outCoord;
}
// Routine Description:
// - Handles a keyboard event for manipulating line-mode selection with the keyboard
// - If called when console isn't in selecting state, will start a new selection.
// Arguments:
// - inputKeyInfo : The key press state information from the keyboard
// Return Value:
// - True if the event is handled. False otherwise.
// NOTE:
// - Keyboard handling cases in this function should be synchronized with IsValidKeyboardLineSelection
bool Selection::HandleKeyboardLineSelectionEvent(const INPUT_KEY_INFO* const pInputKeyInfo)
{
const CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
const WORD wVirtualKeyCode = pInputKeyInfo->GetVirtualKey();
// if this isn't a valid key combination for this function, exit quickly.
if (!s_IsValidKeyboardLineSelection(pInputKeyInfo))
{
return false;
}
Telemetry::Instance().SetKeyboardTextSelectionUsed();
// if we're not currently selecting anything, start a new mouse selection
if (!IsInSelectingState())
{
InitializeMouseSelection(gci.GetActiveOutputBuffer().GetTextBuffer().GetCursor().GetPosition());
// force that this is a line selection
_AlignAlternateSelection(true);
ShowSelection();
// if we did shift+left/right, then just exit
if (pInputKeyInfo->IsShiftOnly())
{
switch (wVirtualKeyCode)
{
case VK_LEFT:
case VK_RIGHT:
return true;
}
}
}
// anchor is the first clicked position
const COORD coordAnchor = _coordSelectionAnchor;
// rect covers the entire selection
const SMALL_RECT rectSelection = _srSelectionRect;
// the selection point is the other corner of the rectangle from the anchor that we're about to manipulate
COORD coordSelPoint;
coordSelPoint.X = coordAnchor.X == rectSelection.Left ? rectSelection.Right : rectSelection.Left;
coordSelPoint.Y = coordAnchor.Y == rectSelection.Top ? rectSelection.Bottom : rectSelection.Top;
// this is the maximum size of the buffer
const auto bufferSize = gci.GetActiveOutputBuffer().GetBufferSize();
const SHORT sWindowHeight = gci.GetActiveOutputBuffer().GetViewport().Height();
FAIL_FAST_IF(!bufferSize.IsInBounds(coordSelPoint));
// retrieve input line information. If we are selecting from within the input line, we need
// to bound ourselves within the input data first and not move into the back buffer.
COORD coordInputLineStart;
COORD coordInputLineEnd;
bool fHaveInputLine = s_GetInputLineBoundaries(&coordInputLineStart, &coordInputLineEnd);
if (pInputKeyInfo->IsShiftOnly())
{
switch (wVirtualKeyCode)
{
// shift + left/right extends the selection by one character, wrapping at screen edge
case VK_LEFT:
{
bufferSize.DecrementInBounds(coordSelPoint);
break;
}
case VK_RIGHT:
{
bufferSize.IncrementInBounds(coordSelPoint);
// if we're about to split a character in half, keep moving right
try
{
const auto attr = gci.GetActiveOutputBuffer().GetCellDataAt(coordSelPoint)->DbcsAttr();
if (attr.IsTrailing())
{
bufferSize.IncrementInBounds(coordSelPoint);
}
}
CATCH_LOG();
break;
}
// shift + up/down extends the selection by one row, stopping at top or bottom of screen
case VK_UP:
{
if (coordSelPoint.Y > bufferSize.Top())
{
coordSelPoint.Y--;
}
break;
}
case VK_DOWN:
{
if (coordSelPoint.Y < bufferSize.BottomInclusive())
{
coordSelPoint.Y++;
}
break;
}
// shift + pgup/pgdn extends selection up or down one full screen
case VK_NEXT:
{
coordSelPoint.Y += sWindowHeight; // TODO: potential overflow
if (coordSelPoint.Y > bufferSize.BottomInclusive())
{
coordSelPoint.Y = bufferSize.BottomInclusive();
}
break;
}
case VK_PRIOR:
{
coordSelPoint.Y -= sWindowHeight; // TODO: potential underflow
if (coordSelPoint.Y < bufferSize.Top())
{
coordSelPoint.Y = bufferSize.Top();
}
break;
}
// shift + home/end extends selection to beginning or end of line
case VK_HOME:
{
/*
Prompt sample:
qwertyuiopasdfg
C:\>dir /p /w C
:\windows\syste
m32
The input area runs from the d in "dir" to the space after the 2 in "32"
We want to stop the HOME command from running to the beginning of the line only
if we're on the first input line because then it would capture the prompt.
So if the selection point we're manipulating is currently anywhere in the
"dir /p /w C" area, then pressing home should only move it on top of the "d" in "dir".
But if it's already at the "d" in dir, pressing HOME again should move us to the
beginning of the line anyway to collect up the prompt as well.
*/
// if we're in the input line
if (fHaveInputLine)
{
// and the selection point is inside the input line area
if (Utils::s_CompareCoords(coordSelPoint, coordInputLineStart) > 0)
{
// and we're on the same line as the beginning of the input
if (coordInputLineStart.Y == coordSelPoint.Y)
{
// then only back up to the start of the input
coordSelPoint.X = coordInputLineStart.X;
break;
}
}
}
// otherwise, fall through and select to the head of the line.
coordSelPoint.X = 0;
break;
}
case VK_END:
{
/*
Prompt sample:
qwertyuiopasdfg
C:\>dir /p /w C
:\windows\syste
m32
The input area runs from the d in "dir" to the space after the 2 in "32"
We want to stop the END command from running to the space after the "32" because
that's just where the cursor lies to let more text get entered and not actually
a valid selection area.
So if the selection point is anywhere on the "m32", pressing end should move it
to on top of the "2".
Additionally, if we're starting within the output buffer (qwerty, etc. and C:\>), then
pressing END should stop us before we enter the input line the first time.
So if we're anywhere on "C:\", we should select up to the ">" character and no further
until a subsequent press of END.
At the subsequent press of END when we're on the ">", we should move to the end of the input
line or the end of the screen, whichever comes first.
*/
// if we're in the input line
if (fHaveInputLine)
{
// and the selection point is inside the input area
if (Utils::s_CompareCoords(coordSelPoint, coordInputLineStart) >= 0)
{
// and we're on the same line as the end of the input
if (coordInputLineEnd.Y == coordSelPoint.Y)
{
// and we're not already on the end of the input...
if (coordSelPoint.X < coordInputLineEnd.X)
{
// then only use end to the end of the input
coordSelPoint.X = coordInputLineEnd.X;
break;
}
}
}
else
{
// otherwise if we're outside and on the same line as the start of the input
if (coordInputLineStart.Y == coordSelPoint.Y)
{
// calculate the end of the outside/output buffer position
const short sEndOfOutputPos = coordInputLineStart.X - 1;
// if we're not already on the very last character...
if (coordSelPoint.X < sEndOfOutputPos)
{
// then only move to just before the beginning of the input
coordSelPoint.X = sEndOfOutputPos;
break;
}
else if (coordSelPoint.X == sEndOfOutputPos)
{
// if we were on the last character,
// then if the end of the input line is also on this current line,
// move to that.
if (coordSelPoint.Y == coordInputLineEnd.Y)
{
coordSelPoint.X = coordInputLineEnd.X;
break;
}
}
}
}
}
// otherwise, fall through and go to selecting the whole line to the end.
coordSelPoint.X = bufferSize.RightInclusive();
break;
}
}
}
else if (pInputKeyInfo->IsShiftAndCtrlOnly())
{
switch (wVirtualKeyCode)
{
// shift + ctrl + left/right extends selection to next/prev word boundary
case VK_LEFT:
{
coordSelPoint = WordByWordSelection(true, bufferSize, coordAnchor, coordSelPoint);
break;
}
case VK_RIGHT:
{
coordSelPoint = WordByWordSelection(false, bufferSize, coordAnchor, coordSelPoint);
break;
}
// shift + ctrl + up/down does the same thing that shift + up/down does
case VK_UP:
{
if (coordSelPoint.Y > bufferSize.Top())
{
coordSelPoint.Y--;
}
break;
}
case VK_DOWN:
{
if (coordSelPoint.Y < bufferSize.BottomInclusive())
{
coordSelPoint.Y++;
}
break;
}
// shift + ctrl + home/end extends selection to top or bottom of buffer from selection
case VK_HOME:
{
COORD coordValidStart;
GetValidAreaBoundaries(&coordValidStart, nullptr);
coordSelPoint = coordValidStart;
break;
}
case VK_END:
{
COORD coordValidEnd;
GetValidAreaBoundaries(nullptr, &coordValidEnd);
coordSelPoint = coordValidEnd;
break;
}
}
}
// ensure we're not planting the cursor in the middle of a double-wide character.
try
{
const auto attr = gci.GetActiveOutputBuffer().GetCellDataAt(coordSelPoint)->DbcsAttr();
if (attr.IsTrailing())
{
// try to move off by highlighting the lead half too.
bool fSuccess = bufferSize.DecrementInBounds(coordSelPoint);
// if that fails, move off to the next character
if (!fSuccess)
{
bufferSize.IncrementInBounds(coordSelPoint);
}
}
}
CATCH_LOG();
ExtendSelection(coordSelPoint);
return true;
}
// Routine Description:
// - Checks whether the ALT key was pressed when this method was called.
// - ALT is the modifier for the alternate selection mode, so this will set state accordingly.
// Arguments:
// - <none> (Uses global key state)
// Return Value:
// - <none>
void Selection::CheckAndSetAlternateSelection()
{
_fUseAlternateSelection = !!(ServiceLocator::LocateInputServices()->GetKeyState(VK_MENU) & KEY_PRESSED);
}
// Routine Description:
// - Handles a keyboard event for manipulating color selection
// - If called when console isn't in selecting state, will start a new selection.
// Arguments:
// - pInputKeyInfo : The key press state information from the keyboard
// Return Value:
// - True if the event is handled. False otherwise.
bool Selection::_HandleColorSelection(const INPUT_KEY_INFO* const pInputKeyInfo)
{
CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
const WORD wVirtualKeyCode = pInputKeyInfo->GetVirtualKey();
// It's a numeric key, a text mode buffer and the color selection regkey is set,
// then check to see if the user want's to color the selection or search and
// highlight the selection.
bool fAltPressed = pInputKeyInfo->IsAltPressed();
bool fShiftPressed = pInputKeyInfo->IsShiftPressed();
bool fCtrlPressed = false;
// Shift implies a find-and-color operation.
// We only support finding a string, not a block.
// If it is line selection, we can assemble that across multiple lines to make a search term.
// But if it is block selection and the selected area is > 1 line in height, ignore the shift because we can't search.
// Also ignore if there is no current selection.
if ((fShiftPressed) && (!IsAreaSelected() || (!IsLineSelection() && (_srSelectionRect.Top != _srSelectionRect.Bottom ))))
{
fShiftPressed = false;
}
// If CTRL + ALT together, then we interpret as ALT (eg on French
// keyboards AltGr == RALT+LCTRL, but we want it to behave as ALT).
if (!fAltPressed)
{
fCtrlPressed = pInputKeyInfo->IsCtrlPressed();
}
SCREEN_INFORMATION& screenInfo = gci.GetActiveOutputBuffer();
// Clip the selection to within the console buffer
screenInfo.ClipToScreenBuffer(&_srSelectionRect);
// If ALT or CTRL are pressed, then color the selected area.
// ALT+n => fg, CTRL+n => bg
if (fAltPressed || fCtrlPressed)
{
ULONG ulAttr = wVirtualKeyCode - '0' + 6;
if (fCtrlPressed)
{
// Setting background color. Set fg color to black.
ulAttr <<= 4;
}
else
{
// Set foreground color. Maintain the current console bg color.
ulAttr |= gci.GetActiveOutputBuffer().GetAttributes().GetLegacyAttributes() & 0xf0;
}
// If shift was pressed as well, then this is actually a
// find-and-color request. Otherwise just color the selection.
if (fShiftPressed)
{
try
{
const auto selectionRects = GetSelectionRects();
if (selectionRects.size() > 0)
{
// Pull the selection out of the buffer to pass to the
// search function. Clamp to max search string length.
// We just copy the bytes out of the row buffer.
std::wstring str;
for (const auto& selectRect : selectionRects)
{
auto it = screenInfo.GetTextDataAt(COORD{ selectRect.Left, selectRect.Top });
for (SHORT i = 0; i < (selectRect.Right - selectRect.Left + 1); ++i)
{
str.append((*it).begin(), (*it).end());
it++;
}
}
// Clear the selection and call the search / mark function.
ClearSelection();
Telemetry::Instance().LogColorSelectionUsed();
Search search(screenInfo, str, Search::Direction::Forward, Search::Sensitivity::CaseInsensitive);
while (search.FindNext())
{
search.Color(TextAttribute{ static_cast<WORD>(ulAttr) });
}
}
}
CATCH_LOG();
}
else
{
ColorSelection(_srSelectionRect, TextAttribute{ static_cast<WORD>(ulAttr) });
ClearSelection();
}
return true;
}
return false;
}
// Routine Description:
// - Handles a keyboard event for selection in mark mode
// Arguments:
// - pInputKeyInfo : The key press state information from the keyboard
// Return Value:
// - True if the event is handled. False otherwise.
bool Selection::_HandleMarkModeSelectionNav(const INPUT_KEY_INFO* const pInputKeyInfo)
{
CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
const WORD wVirtualKeyCode = pInputKeyInfo->GetVirtualKey();
// we're selecting via keyboard -- handle keystrokes
if (wVirtualKeyCode == VK_RIGHT ||
wVirtualKeyCode == VK_LEFT ||
wVirtualKeyCode == VK_UP ||
wVirtualKeyCode == VK_DOWN ||
wVirtualKeyCode == VK_NEXT ||
wVirtualKeyCode == VK_PRIOR ||
wVirtualKeyCode == VK_END ||
wVirtualKeyCode == VK_HOME)
{
SCREEN_INFORMATION& ScreenInfo = gci.GetActiveOutputBuffer();
TextBuffer& textBuffer = ScreenInfo.GetTextBuffer();
SHORT iNextRightX = 0;
SHORT iNextLeftX = 0;
const COORD cursorPos = textBuffer.GetCursor().GetPosition();
try
{
auto it = ScreenInfo.GetCellLineDataAt(cursorPos);
// calculate next right
if (it->DbcsAttr().IsLeading())
{
iNextRightX = 2;
}
else
{
iNextRightX = 1;
}
// calculate next left
if (cursorPos.X > 0)
{
it--;
if (it->DbcsAttr().IsTrailing())
{
iNextLeftX = 2;
}
else if (it->DbcsAttr().IsLeading())
{
if (cursorPos.X - 1 > 0)
{
it--;
if (it->DbcsAttr().IsTrailing())
{
iNextLeftX = 3;
}
else
{
iNextLeftX = 2;
}
}
else
{
iNextLeftX = 1;
}
}
else
{
iNextLeftX = 1;
}
}
}
CATCH_LOG();
Cursor& cursor = textBuffer.GetCursor();
switch (wVirtualKeyCode)
{
case VK_RIGHT:
{
if (cursorPos.X + iNextRightX < ScreenInfo.GetBufferSize().Width())
{
cursor.IncrementXPosition(iNextRightX);
}
break;
}
case VK_LEFT:
{
if (cursorPos.X > 0)
{
cursor.DecrementXPosition(iNextLeftX);
}
break;
}
case VK_UP:
{
if (cursorPos.Y > 0)
{
cursor.DecrementYPosition(1);
}
break;
}
case VK_DOWN:
{
if (cursorPos.Y + 1 < ScreenInfo.GetTerminalBufferSize().Height())
{
cursor.IncrementYPosition(1);
}
break;
}
case VK_NEXT:
{
cursor.IncrementYPosition(ScreenInfo.GetViewport().Height() - 1);
const COORD coordBufferSize = ScreenInfo.GetTerminalBufferSize().Dimensions();
if (cursor.GetPosition().Y >= coordBufferSize.Y)
{
cursor.SetYPosition(coordBufferSize.Y - 1);
}
break;
}
case VK_PRIOR:
{
cursor.DecrementYPosition(ScreenInfo.GetViewport().Height() - 1);
if (cursor.GetPosition().Y < 0)
{
cursor.SetYPosition(0);
}
break;
}
case VK_END:
{
// End by itself should go to end of current line. Ctrl-End should go to end of buffer.
cursor.SetXPosition(ScreenInfo.GetBufferSize().RightInclusive());
if (pInputKeyInfo->IsCtrlPressed())
{
COORD coordValidEnd;
GetValidAreaBoundaries(nullptr, &coordValidEnd);
// Adjust Y position of cursor to the final line with valid text
cursor.SetYPosition(coordValidEnd.Y);
}
break;
}
case VK_HOME:
{
// Home by itself should go to the beginning of the current line. Ctrl-Home should go to the beginning of
// the buffer
cursor.SetXPosition(0);
if (pInputKeyInfo->IsCtrlPressed())
{
cursor.SetYPosition(0);
}
break;
}
default:
FAIL_FAST_HR(E_NOTIMPL);
}
// see if shift is down. if so, we're extending the selection. otherwise, we're resetting the anchor
if (ServiceLocator::LocateInputServices()->GetKeyState(VK_SHIFT) & KEY_PRESSED)
{
// if we're just starting to "extend" our selection from moving around as a cursor
// then attempt to set the alternate selection state based on the ALT key right now
if (!IsAreaSelected())
{
CheckAndSetAlternateSelection();
}
ExtendSelection(cursor.GetPosition());
}
else
{
// if the selection was not empty, reset the anchor
if (IsAreaSelected())
{
HideSelection();
_dwSelectionFlags &= ~CONSOLE_SELECTION_NOT_EMPTY;
_fUseAlternateSelection = false;
}
cursor.SetHasMoved(true);
_coordSelectionAnchor = textBuffer.GetCursor().GetPosition();
ScreenInfo.MakeCursorVisible(_coordSelectionAnchor, false);
_srSelectionRect.Left = _srSelectionRect.Right = _coordSelectionAnchor.X;
_srSelectionRect.Top = _srSelectionRect.Bottom = _coordSelectionAnchor.Y;
}
return true;
}
return false;
}
#pragma region Calculation/Support for keyboard selection
// Routine Description:
// - Retrieves the boundaries of the input line (first and last char positions)
// Arguments:
// - pcoordInputStart - Position of the first character in the input line
// - pcoordInputEnd - Position of the last character in the input line
// Return Value:
// - If true, the boundaries returned are valid. If false, they should be discarded.
[[nodiscard]]
bool Selection::s_GetInputLineBoundaries(_Out_opt_ COORD* const pcoordInputStart, _Out_opt_ COORD* const pcoordInputEnd)
{
const CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
const auto bufferSize = gci.GetActiveOutputBuffer().GetBufferSize();
auto& textBuffer = gci.GetActiveOutputBuffer().GetTextBuffer();
const auto pendingCookedRead = gci.HasPendingCookedRead();
const auto isVisible = CommandLine::Instance().IsVisible();
// if we have no read data, we have no input line.
if (!pendingCookedRead || gci.CookedReadData().VisibleCharCount() == 0 || !isVisible)
{
return false;
}
const auto& cookedRead = gci.CookedReadData();
const COORD coordStart = cookedRead.OriginalCursorPosition();
COORD coordEnd = cookedRead.OriginalCursorPosition();
if (coordEnd.X < 0 && coordEnd.Y < 0)
{
// if the original cursor position from the input line data is invalid, then the buffer cursor position is the final position
coordEnd = textBuffer.GetCursor().GetPosition();
}
else
{
// otherwise, we need to add the number of characters in the input line to the original cursor position
bufferSize.MoveInBounds(cookedRead.VisibleCharCount(), coordEnd);
}
// - 1 so the coordinate is on top of the last position of the text, not one past it.
bufferSize.MoveInBounds(-1, coordEnd);
if (pcoordInputStart != nullptr)
{
pcoordInputStart->X = coordStart.X;
pcoordInputStart->Y = coordStart.Y;
}
if (pcoordInputEnd != nullptr)
{
pcoordInputEnd->X = coordEnd.X;
pcoordInputEnd->Y = coordEnd.Y;
}
return true;
}
// Routine Description:
// - Gets the boundaries of all valid text on the screen.
// Includes the output/back buffer as well as the input line text.
// Arguments:
// - pcoordInputStart - Position of the first character in the buffer
// - pcoordInputEnd - Position of the last character in the buffer
// Return Value:
// - If true, the boundaries returned are valid. If false, they should be discarded.
void Selection::GetValidAreaBoundaries(_Out_opt_ COORD* const pcoordValidStart, _Out_opt_ COORD* const pcoordValidEnd) const
{
const CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
COORD coordEnd;
coordEnd.X = 0;
coordEnd.Y = 0;
const bool fHaveInput = s_GetInputLineBoundaries(nullptr, &coordEnd);
if (!fHaveInput)
{
if (IsInSelectingState() && IsKeyboardMarkSelection())
{
coordEnd = _coordSavedCursorPosition;
}
else
{
coordEnd = gci.GetActiveOutputBuffer().GetTextBuffer().GetCursor().GetPosition();
}
}
if (pcoordValidStart != nullptr)
{
// valid area always starts at 0,0
pcoordValidStart->X = 0;
pcoordValidStart->Y = 0;
}
if (pcoordValidEnd != nullptr)
{
pcoordValidEnd->X = coordEnd.X;
pcoordValidEnd->Y = coordEnd.Y;
}
}
// Routine Description:
// - Determines if a coordinate lies between the start and end positions
// - NOTE: Is inclusive of the edges of the boundary.
// Arguments:
// - coordPosition - The position to test
// - coordFirst - The start or left most edge of the regional boundary.
// - coordSecond - The end or right most edge of the regional boundary.
// Return Value:
// - True if it's within the bounds (inclusive). False otherwise.
bool Selection::s_IsWithinBoundaries(const COORD coordPosition, const COORD coordStart, const COORD coordEnd)
{
bool fInBoundaries = false;
if (Utils::s_CompareCoords(coordStart, coordPosition) <= 0)
{
if (Utils::s_CompareCoords(coordPosition, coordEnd) <= 0)
{
fInBoundaries = true;
}
}
return fInBoundaries;
}
#pragma endregion
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

The new Windows Terminal, and the original Windows console host -- all in the same place!
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/AbstractFactory/Terminal.git
git@gitee.com:AbstractFactory/Terminal.git
AbstractFactory
Terminal
Terminal
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /