File size: 74,649 Bytes
8c763fb | 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 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Globalization;
#pragma warning disable 1634, 1691 // Stops compiler from warning about unknown warnings
namespace System.Management.Automation.Host
{
#region Ancillary types.
// I would have preferred to make these nested types within PSHostRawUserInterface, but that
// is evidently discouraged by the .net design guidelines.
/// <summary>
/// Represents an (x,y) coordinate pair.
/// </summary>
public
struct Coordinates
{
#region DO NOT REMOVE OR RENAME THESE FIELDS - it will break remoting compatibility with Windows PowerShell
private int x;
private int y;
#endregion
/// <summary>
/// Gets and sets the X coordinate.
/// </summary>
public int X
{
get { return x; }
set { x = value; }
}
/// <summary>
/// Gets and sets the Y coordinate.
/// </summary>
public int Y
{
get { return y; }
set { y = value; }
}
/// <summary>
/// Initializes a new instance of the Coordinates class and defines the X and Y values.
/// </summary>
/// <param name="x">
/// The X coordinate
/// </param>
/// <param name="y">
/// The Y coordinate
/// </param>
public
Coordinates(int x, int y)
{
this.x = x;
this.y = y;
}
/// <summary>
/// Overrides <see cref="object.ToString"/>
/// </summary>
/// <returns>
/// "a,b" where a and b are the values of the X and Y properties.
/// </returns>
public override
string
ToString()
{
return string.Create(CultureInfo.InvariantCulture, $"{X},{Y}");
}
/// <summary>
/// Overrides <see cref="object.Equals(object)"/>
/// </summary>
/// <param name="obj">
/// object to be compared for equality.
/// </param>
/// <returns>
/// True if <paramref name="objB"/> is Coordinates and its X and Y values are the same as those of this instance,
/// false if not.
/// </returns>
public override
bool
Equals(object obj)
{
bool result = false;
if (obj is Coordinates)
{
result = this == ((Coordinates)obj);
}
return result;
}
/// <summary>
/// Overrides <see cref="object.GetHashCode"/>
/// </summary>
/// <returns>
/// Hash code for this instance.
/// </returns>
public override
int
GetHashCode()
{
// idea: consider X the high-order part of a 64-bit in, and Y the lower order half. Then use the int64.GetHashCode.
UInt64 i64 = 0;
if (X < 0)
{
if (X == Int32.MinValue)
{
// add one and invert to avoid an overflow.
i64 = (UInt64)(-1 * (X + 1));
}
else
{
i64 = (UInt64)(-X);
}
}
else
{
i64 = (UInt64)X;
}
// rotate 32 bits to the left.
i64 *= 0x100000000U;
// mask in Y
if (Y < 0)
{
if (Y == Int32.MinValue)
{
i64 += (UInt64)(-1 * (Y + 1));
}
else
{
i64 += (UInt64)(-Y);
}
}
else
{
i64 += (UInt64)Y;
}
int result = i64.GetHashCode();
return result;
}
/// <summary>
/// Compares two instances for equality.
/// </summary>
/// <param name="first">
/// The left side operand.
/// </param>
/// <param name="second">
/// The right side operand.
/// </param>
/// <returns>
/// true if the respective X and Y values are the same, false otherwise.
/// </returns>
public static
bool
operator ==(Coordinates first, Coordinates second)
{
bool result = first.X == second.X && first.Y == second.Y;
return result;
}
/// <summary>
/// Compares two instances for inequality.
/// </summary>
/// <param name="first">
/// The left side operand.
/// </param>
/// <param name="second">
/// The right side operand.
/// </param>
/// <returns>
/// true if any of the respective either X or Y field is not the same, false otherwise.
/// </returns>
public static
bool
operator !=(Coordinates first, Coordinates second)
{
return !(first == second);
}
}
/// <summary>
/// Represents a width and height pair.
/// </summary>
public
struct Size
{
#region DO NOT REMOVE OR RENAME THESE FIELDS - it will break remoting compatibility with Windows PowerShell
private int width;
private int height;
#endregion
/// <summary>
/// Gets and sets the Width.
/// </summary>
public int Width
{
get { return width; }
set { width = value; }
}
/// <summary>
/// Gets and sets the Height.
/// </summary>
public int Height
{
get { return height; }
set { height = value; }
}
/// <summary>
/// Initialize a new instance of the Size class and defines the Width and Height values.
/// </summary>
/// <param name="width">
/// The Width
/// </param>
/// <param name="height">
/// The Height
/// </param>
public
Size(int width, int height)
{
this.width = width;
this.height = height;
}
/// <summary>
/// Overloads <see cref="object.ToString"/>
/// </summary>
/// <returns>
/// "a,b" where a and b are the values of the Width and Height properties.
/// </returns>
public override
string
ToString()
{
return string.Create(CultureInfo.InvariantCulture, $"{Width},{Height}");
}
/// <summary>
/// Overrides <see cref="object.Equals(object)"/>
/// </summary>
/// <param name="obj">
/// object to be compared for equality.
/// </param>
/// <returns>
/// True if <paramref name="obj"/> is Size and its Width and Height values are the same as those of this instance,
/// false if not.
/// </returns>
public override
bool
Equals(object obj)
{
bool result = false;
if (obj is Size)
{
result = this == ((Size)obj);
}
return result;
}
/// <summary>
/// Overrides <see cref="object.GetHashCode"/>
/// </summary>
/// <returns>
/// Hash code for this instance.
/// <!--
/// consider Width the high-order part of a 64-bit in, and
/// Height the lower order half. Then use the int64.GetHashCode.-->
/// </returns>
public override
int
GetHashCode()
{
// idea: consider Width the high-order part of a 64-bit in, and Height the lower order half. Then use the int64.GetHashCode.
UInt64 i64 = 0;
if (Width < 0)
{
if (Width == Int32.MinValue)
{
// add one and invert to avoid an overflow.
i64 = (UInt64)(-1 * (Width + 1));
}
else
{
i64 = (UInt64)(-Width);
}
}
else
{
i64 = (UInt64)Width;
}
// rotate 32 bits to the left.
i64 *= 0x100000000U;
// mask in Height
if (Height < 0)
{
if (Height == Int32.MinValue)
{
i64 += (UInt64)(-1 * (Height + 1));
}
else
{
i64 += (UInt64)(-Height);
}
}
else
{
i64 += (UInt64)Height;
}
int result = i64.GetHashCode();
return result;
}
/// <summary>
/// Compares two instances for equality.
/// </summary>
/// <param name="first">
/// The left side operand.
/// </param>
/// <param name="second">
/// The right side operand.
/// </param>
/// <returns>
/// true if the respective Width and Height fields are the same, false otherwise.
/// </returns>
public static
bool
operator ==(Size first, Size second)
{
bool result = first.Width == second.Width && first.Height == second.Height;
return result;
}
/// <summary>
/// Compares two instances for inequality.
/// </summary>
/// <param name="first">
/// The left side operand.
/// </param>
/// <param name="second">
/// The right side operand.
/// </param>
/// <returns>
/// true if any of the respective Width and Height fields are not the same, false otherwise.
/// </returns>
public static
bool
operator !=(Size first, Size second)
{
return !(first == second);
}
}
/// <summary>
/// Governs the behavior of <see cref="System.Management.Automation.Host.PSHostRawUserInterface.ReadKey()"/>
/// and <see cref="System.Management.Automation.Host.PSHostRawUserInterface.ReadKey(System.Management.Automation.Host.ReadKeyOptions)"/>
/// </summary>
[Flags]
public
enum
ReadKeyOptions
{
/// <summary>
/// Allow Ctrl-C to be processed as a keystroke, as opposed to causing a break event.
/// </summary>
AllowCtrlC = 0x0001,
/// <summary>
/// Do not display the character for the key in the window when pressed.
/// </summary>
NoEcho = 0x0002,
/// <summary>
/// Include key down events. Either one of IncludeKeyDown and IncludeKeyUp or both must be specified.
/// </summary>
IncludeKeyDown = 0x0004,
/// <summary>
/// Include key up events. Either one of IncludeKeyDown and IncludeKeyUp or both must be specified.
/// </summary>
IncludeKeyUp = 0x0008
}
/// <summary>
/// Defines the states of Control Key.
/// </summary>
[Flags]
public
enum ControlKeyStates
{
/// <summary>
/// The right alt key is pressed.
/// </summary>
RightAltPressed = 0x0001,
/// <summary>
/// The left alt key is pressed.
/// </summary>
LeftAltPressed = 0x0002,
/// <summary>
/// The right ctrl key is pressed.
/// </summary>
RightCtrlPressed = 0x0004,
/// <summary>
/// The left ctrl key is pressed.
/// </summary>
LeftCtrlPressed = 0x0008,
/// <summary>
/// The shift key is pressed.
/// </summary>
ShiftPressed = 0x0010,
/// <summary>
/// The numlock light is on.
/// </summary>
NumLockOn = 0x0020,
/// <summary>
/// The scrolllock light is on.
/// </summary>
ScrollLockOn = 0x0040,
/// <summary>
/// The capslock light is on.
/// </summary>
CapsLockOn = 0x0080,
/// <summary>
/// The key is enhanced.
/// </summary>
EnhancedKey = 0x0100
}
/// <summary>
/// Represents information of a keystroke.
/// </summary>
public
struct KeyInfo
{
#region DO NOT REMOVE OR RENAME THESE FIELDS - it will break remoting compatibility with Windows PowerShell
private int virtualKeyCode;
private char character;
private ControlKeyStates controlKeyState;
private bool keyDown;
#endregion
/// <summary>
/// Gets and set device-independent key.
/// </summary>
public int VirtualKeyCode
{
get { return virtualKeyCode; }
set { virtualKeyCode = value; }
}
/// <summary>
/// Gets and set unicode Character of the key.
/// </summary>
public char Character
{
get { return character; }
set { character = value; }
}
/// <summary>
/// State of the control keys.
/// </summary>
public ControlKeyStates ControlKeyState
{
get { return controlKeyState; }
set { controlKeyState = value; }
}
/// <summary>
/// Gets and set the status of whether this instance is generated by a key pressed or released.
/// </summary>
public bool KeyDown
{
get { return keyDown; }
set { keyDown = value; }
}
/// <summary>
/// Initialize a new instance of the KeyInfo class and defines the VirtualKeyCode,
/// Character, ControlKeyState and KeyDown values.
/// </summary>
/// <param name="virtualKeyCode">
/// The virtual key code
/// </param>
/// <param name="ch">
/// The character
/// </param>
/// <param name="controlKeyState">
/// The control key state
/// </param>
/// <param name="keyDown">
/// Whether the key is pressed or released
/// </param>
public
KeyInfo
(
int virtualKeyCode,
char ch,
ControlKeyStates controlKeyState,
bool keyDown
)
{
this.virtualKeyCode = virtualKeyCode;
this.character = ch;
this.controlKeyState = controlKeyState;
this.keyDown = keyDown;
}
/// <summary>
/// Overloads <see cref="object.ToString"/>
/// </summary>
/// <returns>
/// "a,b,c,d" where a, b, c, and d are the values of the VirtualKeyCode, Character, ControlKeyState, and KeyDown properties.
/// </returns>
public override
string
ToString()
{
return string.Create(CultureInfo.InvariantCulture, $"{VirtualKeyCode},{Character},{ControlKeyState},{KeyDown}");
}
/// <summary>
/// Overrides <see cref="object.Equals(object)"/>
/// </summary>
/// <param name="obj">
/// object to be compared for equality.
/// </param>
/// <returns>
/// True if <paramref name="obj"/> is KeyInfo and its VirtualKeyCode, Character, ControlKeyState, and KeyDown values are the
/// same as those of this instance, false if not.
/// </returns>
public override
bool
Equals(object obj)
{
bool result = false;
if (obj is KeyInfo)
{
result = this == ((KeyInfo)obj);
}
return result;
}
/// <summary>
/// Overrides <see cref="object.GetHashCode"/>
/// </summary>
/// <returns>
/// Hash code for this instance.
/// <!--consider KeyDown (true == 1, false == 0) the highest-order nibble,
/// ControlKeyState the second to fourth highest-order nibbles
/// VirtualKeyCode the lower-order nibbles of a 32-bit int,
/// Then use the UInt32.GetHashCode.-->
/// </returns>
public override
int
GetHashCode()
{
// idea: consider KeyDown (true == 1, false == 0) the highest-order nibble,
// ControlKeyState the second to fourth highest-order nibbles
// VirtualKeyCode the lower-order nibbles of a 32-bit int,
// Then use the UInt32.GetHashCode.
UInt32 i32 = KeyDown ? 0x10000000U : 0;
// mask in ControlKeyState
i32 |= ((uint)ControlKeyState) << 16;
// mask in the VirtualKeyCode
i32 |= (UInt32)VirtualKeyCode;
return i32.GetHashCode();
}
/// <summary>
/// Compares two instances for equality.
/// </summary>
/// <param name="first">
/// The left side operand.
/// </param>
/// <param name="second">
/// The right side operand.
/// </param>
/// <returns>
/// true if the respective Character, ControlKeyStates , KeyDown, and VirtualKeyCode fields
/// are the same, false otherwise.
/// </returns>
/// <exception/>
public static
bool
operator ==(KeyInfo first, KeyInfo second)
{
bool result = first.Character == second.Character && first.ControlKeyState == second.ControlKeyState &&
first.KeyDown == second.KeyDown && first.VirtualKeyCode == second.VirtualKeyCode;
return result;
}
/// <summary>
/// Compares two instances for inequality.
/// </summary>
/// <param name="first">
/// The left side operand.
/// </param>
/// <param name="second">
/// The right side operand.
/// </param>
/// <returns>
/// true if any of the respective Character, ControlKeyStates , KeyDown, or VirtualKeyCode fields
/// are the different, false otherwise.
/// </returns>
/// <exception/>
public static
bool
operator !=(KeyInfo first, KeyInfo second)
{
return !(first == second);
}
}
/// <summary>
/// Represents a rectangular region of the screen.
/// <!--We use this structure instead of System.Drawing.Rectangle because S.D.R
/// is way overkill and would bring in another assembly.-->
/// </summary>
public
struct Rectangle
{
#region DO NOT REMOVE OR RENAME THESE FIELDS - it will break remoting compatibility with Windows PowerShell
private int left;
private int top;
private int right;
private int bottom;
#endregion
/// <summary>
/// Gets and sets the left side of the rectangle.
/// </summary>
public int Left
{
get { return left; }
set { left = value; }
}
/// <summary>
/// Gets and sets the top of the rectangle.
/// </summary>
public int Top
{
get { return top; }
set { top = value; }
}
/// <summary>
/// Gets and sets the right side of the rectangle.
/// </summary>
public int Right
{
get { return right; }
set { right = value; }
}
/// <summary>
/// Gets and sets the bottom of the rectangle.
/// </summary>
public int Bottom
{
get { return bottom; }
set { bottom = value; }
}
/// <summary>
/// Initialize a new instance of the Rectangle class and defines the Left, Top, Right, and Bottom values.
/// </summary>
/// <param name="left">
/// The left side of the rectangle
/// </param>
/// <param name="top">
/// The top of the rectangle
/// </param>
/// <param name="right">
/// The right side of the rectangle
/// </param>
/// <param name="bottom">
/// The bottom of the rectangle
/// </param>
/// <exception cref="ArgumentException">
/// <paramref name="right"/> is less than <paramref name="left"/>;
/// <paramref name="bottom"/> is less than <paramref name="top"/>
/// </exception>
public
Rectangle(int left, int top, int right, int bottom)
{
if (right < left)
{
// "right" and "left" are not localizable
throw PSTraceSource.NewArgumentException(nameof(right), MshHostRawUserInterfaceStrings.LessThanErrorTemplate, "right", "left");
}
if (bottom < top)
{
// "bottom" and "top" are not localizable
throw PSTraceSource.NewArgumentException(nameof(bottom), MshHostRawUserInterfaceStrings.LessThanErrorTemplate, "bottom", "top");
}
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
/// <summary>
/// Initializes a new instance of the Rectangle class and defines the Left, Top, Right, and Bottom values
/// by <paramref name="upperLeft"/>, the upper left corner and <paramref name="lowerRight"/>, the lower
/// right corner.
/// <!--
/// Added based on feedback from review with BCL PM.
/// -->
/// </summary>
/// <param name="upperLeft">
/// The Coordinates of the upper left corner of the Rectangle
/// </param>
/// <param name="lowerRight">
/// The Coordinates of the lower right corner of the Rectangle
/// </param>
/// <exception/>
public
Rectangle(Coordinates upperLeft, Coordinates lowerRight)
: this(upperLeft.X, upperLeft.Y, lowerRight.X, lowerRight.Y)
{
}
/// <summary>
/// Overloads <see cref="object.ToString"/>
/// </summary>
/// <returns>
/// "a,b ; c,d" where a, b, c, and d are values of the Left, Top, Right, and Bottom properties.
/// </returns>
public override
string
ToString()
{
return string.Create(CultureInfo.InvariantCulture, $"{Left},{Top} ; {Right},{Bottom}");
}
/// <summary>
/// Overrides <see cref="object.Equals(object)"/>
/// </summary>
/// <param name="obj">
/// object to be compared for equality.
/// </param>
/// <returns>
/// True if <paramref name="obj"/> is Rectangle and its Left, Top, Right, and Bottom values are the same as those of this instance,
/// false if not.
/// </returns>
public override
bool
Equals(object obj)
{
bool result = false;
if (obj is Rectangle)
{
result = this == ((Rectangle)obj);
}
return result;
}
/// <summary>
/// Overrides <see cref="object.GetHashCode"/>
/// </summary>
/// <returns>
/// Hash code for this instance.
/// <!-- consider (Top XOR Bottom) the high-order part of a 64-bit int,
/// (Left XOR Right) the lower order half. Then use the int64.GetHashCode.-->
/// </returns>
/// <exception/>
public override
int
GetHashCode()
{
// idea: consider (Top XOR Bottom) the high-order part of a 64-bit int,
// (Left XOR Right) the lower order half. Then use the int64.GetHashCode.
UInt64 i64 = 0;
int upper = Top ^ Bottom;
if (upper < 0)
{
if (upper == Int32.MinValue)
{
// add one and invert to avoid an overflow.
i64 = (UInt64)(-1 * (upper + 1));
}
else
{
i64 = (UInt64)(-upper);
}
}
else
{
i64 = (UInt64)upper;
}
// rotate 32 bits to the left.
i64 *= 0x100000000U;
// mask in lower
int lower = Left ^ Right;
if (lower < 0)
{
if (lower == Int32.MinValue)
{
i64 += (UInt64)(-1 * (lower + 1));
}
else
{
i64 += (UInt64)(-upper);
}
}
else
{
i64 += (UInt64)lower;
}
int result = i64.GetHashCode();
return result;
}
/// <summary>
/// Compares two instances for equality.
/// </summary>
/// <param name="first">
/// The left side operand.
/// </param>
/// <param name="second">
/// The right side operand.
/// </param>
/// <returns>
/// true if the respective Top, Left, Bottom, and Right fields are the same, false otherwise.
/// </returns>
public static
bool
operator ==(Rectangle first, Rectangle second)
{
bool result = first.Top == second.Top && first.Left == second.Left &&
first.Bottom == second.Bottom && first.Right == second.Right;
return result;
}
/// <summary>
/// Compares two instances for inequality.
/// </summary>
/// <param name="first">
/// The left side operand.
/// </param>
/// <param name="second">
/// The right side operand.
/// </param>
/// <returns>
/// true if any of the respective Top, Left, Bottom, and Right fields are not the same, false otherwise.
/// </returns>
/// <exception/>
public static
bool
operator !=(Rectangle first, Rectangle second)
{
return !(first == second);
}
}
/// <summary>
/// Represents a character, a foregroundColor color, and background color.
/// </summary>
public
struct BufferCell
{
#region DO NOT REMOVE OR RENAME THESE FIELDS - it will break remoting compatibility with Windows PowerShell
private char character;
private ConsoleColor foregroundColor;
private ConsoleColor backgroundColor;
private BufferCellType bufferCellType;
#endregion
/// <summary>
/// Gets and sets the character value.
/// </summary>
public char Character
{
get { return character; }
set { character = value; }
}
// we reuse System.ConsoleColor - it's in the core assembly, and I think it would be confusing to create another
// essentially identical enum
/// <summary>
/// Gets and sets the foreground color.
/// </summary>
public ConsoleColor ForegroundColor
{
get { return foregroundColor; }
set { foregroundColor = value; }
}
/// <summary>
/// Gets and sets the background color.
/// </summary>
public ConsoleColor BackgroundColor
{
get { return backgroundColor; }
set { backgroundColor = value; }
}
/// <summary>
/// Gets and sets the type value.
/// </summary>
public BufferCellType BufferCellType
{
get { return bufferCellType; }
set { bufferCellType = value; }
}
/// <summary>
/// Initializes a new instance of the BufferCell class and defines the
/// Character, ForegroundColor, BackgroundColor and Type values.
/// </summary>
/// <param name="character">
/// The character in this BufferCell object
/// </param>
/// <param name="foreground">
/// The foreground color of this BufferCell object
/// </param>
/// <param name="background">
/// The foreground color of this BufferCell object
/// </param>
/// <param name="bufferCellType">
/// The type of this BufferCell object
/// </param>
public
BufferCell(char character, ConsoleColor foreground, ConsoleColor background, BufferCellType bufferCellType)
{
this.character = character;
this.foregroundColor = foreground;
this.backgroundColor = background;
this.bufferCellType = bufferCellType;
}
/// <summary>
/// Overloads <see cref="object.ToString"/>
/// </summary>
/// <returns>
/// "'a' b c d" where a, b, c, and d are the values of the Character, ForegroundColor, BackgroundColor, and Type properties.
/// </returns>
public override
string
ToString()
{
return string.Create(CultureInfo.InvariantCulture, $"'{Character}' {ForegroundColor} {BackgroundColor} {BufferCellType}");
}
/// <summary>
/// Overrides <see cref="object.Equals(object)"/>
/// </summary>
/// <param name="obj">
/// object to be compared for equality.
/// </param>
/// <returns>
/// True if <paramref name="obj"/> is BufferCell and its Character, ForegroundColor, BackgroundColor, and BufferCellType values
/// are the same as those of this instance, false if not.
/// </returns>
public override
bool
Equals(object obj)
{
bool result = false;
if (obj is BufferCell)
{
result = this == ((BufferCell)obj);
}
return result;
}
/// <summary>
/// Overrides <see cref="object.GetHashCode"/>
/// <!-- consider (ForegroundColor XOR BackgroundColor) the high-order part of a 32-bit int,
/// and Character the lower order half. Then use the int32.GetHashCode.-->
/// </summary>
/// <returns>
/// Hash code for this instance.
///
/// </returns>
public override
int
GetHashCode()
{
// idea: consider (ForegroundColor XOR BackgroundColor) the high-order part of a 32-bit int,
// and Character the lower order half. Then use the int32.GetHashCode.
UInt32 i32 = ((uint)(ForegroundColor ^ BackgroundColor)) << 16;
// mask in Height
i32 |= (UInt16)Character;
int result = i32.GetHashCode();
return result;
}
/// <summary>
/// Compares two instances for equality.
/// </summary>
/// <param name="first">
/// The left side operand.
/// </param>
/// <param name="second">
/// The right side operand.
/// </param>
/// <returns>
/// true if the respective Character, ForegroundColor, BackgroundColor, and BufferCellType values are the same, false otherwise.
/// </returns>
public static
bool
operator ==(BufferCell first, BufferCell second)
{
bool result = first.Character == second.Character &&
first.BackgroundColor == second.BackgroundColor &&
first.ForegroundColor == second.ForegroundColor &&
first.BufferCellType == second.BufferCellType;
return result;
}
/// <summary>
/// Compares two instances for inequality.
/// </summary>
/// <param name="first">
/// The left side operand.
/// </param>
/// <param name="second">
/// The right side operand.
/// </param>
/// <returns>
/// true if any of the respective Character, ForegroundColor, BackgroundColor, and BufferCellType values are not the same,
/// false otherwise.
/// </returns>
public static
bool
operator !=(BufferCell first, BufferCell second)
{
return !(first == second);
}
private const string StringsBaseName = "MshHostRawUserInterfaceStrings";
}
/// <summary>
/// Defines three types of BufferCells to accommodate for hosts that use up to two cells
/// to display a character in some languages such as Chinese and Japanese.
/// </summary>
public enum
BufferCellType
{
/// <summary>
/// Character occupies one BufferCell.
/// </summary>
Complete,
/// <summary>
/// Character occupies two BufferCells and this is the leading one.
/// </summary>
Leading,
/// <summary>
/// Preceded by a Leading BufferCell.
/// </summary>
Trailing
}
#endregion Ancillary types
/// <summary>
/// Defines the lowest-level user interface functions that an interactive application hosting PowerShell
/// <see cref="System.Management.Automation.Runspaces.Runspace"/> can choose to implement if it wants to
/// support any cmdlet that does character-mode interaction with the user.
/// </summary>
/// <remarks>
/// It models an 2-dimensional grid of cells called a Buffer. A buffer has a visible rectangular region, called a window.
/// Each cell of the grid has a character, a foreground color, and a background color. When the buffer has input focus, it
/// shows a cursor positioned in one cell. Keystrokes can be read from the buffer and optionally echoed at the current
/// cursor position.
/// </remarks>
/// <seealso cref="System.Management.Automation.Host.PSHost"/>
/// <seealso cref="System.Management.Automation.Host.PSHostUserInterface"/>
public abstract
class PSHostRawUserInterface
{
/// <summary>
/// Protected constructor which does nothing. Provided per .Net design guidelines section 4.3.1.
/// </summary>
protected
PSHostRawUserInterface()
{
// do nothing
}
/// <summary>
/// Gets or sets the color used to render characters on the screen buffer. Each character cell in the screen buffer can
/// have a separate foreground color.
/// </summary>
/// <!--Design note: we separate Foreground and Background colors into separate properties rather than having a single
/// property that is a ColorAttribute. While a single property that takes a struct is consistent with all of our
/// other properties that take structs (e.g. -Position, -Size), I anticipate that the more common use-case for color
/// is to just change the foreground color.-->
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.BackgroundColor"/>
public abstract
ConsoleColor
ForegroundColor
{
get;
set;
}
/// <summary>
/// Gets or sets the color used to render the background behind characters on the screen buffer. Each character cell in
/// the screen buffer can have a separate background color.
/// </summary>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.ForegroundColor"/>
public abstract
ConsoleColor
BackgroundColor
{
get;
set;
}
/// <summary>
/// Gets or sets the cursor position in the screen buffer. The view window always adjusts it's location over the screen
/// buffer such that the cursor is always visible.
/// </summary>
/// <remarks>
/// To write to the screen buffer without updating the cursor position, use
/// <see cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Rectangle, BufferCell)"/> or
/// <see cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Coordinates, BufferCell[,])"/>
/// </remarks>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.MaxPhysicalWindowSize"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.WindowSize"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.WindowPosition"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.MaxWindowSize"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Rectangle, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Coordinates, BufferCell[,])"/>
public abstract
Coordinates
CursorPosition
{
get;
set;
}
/// <summary>
/// Gets or sets position of the view window relative to the screen buffer, in characters. (0,0) is the upper left of the screen
/// buffer.
/// </summary>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.MaxPhysicalWindowSize"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.WindowSize"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.CursorPosition"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.MaxWindowSize"/>
public abstract
Coordinates
WindowPosition
{
get;
set;
}
/// <summary>
/// Gets or sets the cursor size as a percentage 0..100.
/// </summary>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.CursorPosition"/>
public abstract
int
CursorSize
{
get;
set;
}
/// <summary>
/// Gets or sets the current size of the screen buffer, measured in character cells.
/// </summary>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.MaxPhysicalWindowSize"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.WindowSize"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.CursorPosition"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.MaxWindowSize"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.WindowPosition"/>
public abstract
Size
BufferSize
{
get;
set;
}
/// <summary>
/// Gets or sets the current view window size, measured in character cells. The window size cannot be larger than the
/// dimensions returned by <see cref="System.Management.Automation.Host.PSHostRawUserInterface.MaxPhysicalWindowSize"/>.
/// </summary>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.MaxPhysicalWindowSize"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.BufferSize"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.CursorPosition"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.MaxWindowSize"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.WindowPosition"/>
public abstract
Size
WindowSize
{
get;
set;
}
/// <summary>
/// Gets the size of the largest window possible for the current buffer, current font, and current display hardware.
/// The view window cannot be larger than the screen buffer or the current display (the display the window is rendered on).
/// </summary>
/// <value>
/// The largest dimensions the window can be resized to without resizing the screen buffer.
/// </value>
/// <remarks>
/// Always returns a value less than or equal to
/// <see cref="System.Management.Automation.Host.PSHostRawUserInterface.MaxPhysicalWindowSize"/>.
/// </remarks>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.MaxPhysicalWindowSize"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.BufferSize"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.CursorPosition"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.WindowSize"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.WindowPosition"/>
public abstract
Size
MaxWindowSize
{
get;
}
/// <summary>
/// Gets the largest window possible for the current font and display hardware, ignoring the current buffer dimensions. In
/// other words, the dimensions of the largest window that could be rendered in the current display, if the buffer was
/// at least as large.
/// </summary>
/// <remarks>
/// To resize the window to this dimension, use <see cref="System.Management.Automation.Host.PSHostRawUserInterface.BufferSize"/>
/// to first check and, if necessary, adjust, the screen buffer size.
/// </remarks>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.MaxWindowSize"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.BufferSize"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.CursorPosition"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.WindowSize"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.WindowPosition"/>
public abstract
Size
MaxPhysicalWindowSize
{
get;
}
/// <summary>
/// Reads a key stroke from the keyboard device, blocking until a keystroke is typed.
/// Same as ReadKey(ReadKeyOptions.IncludeKeyDown)
/// </summary>
/// <returns>
/// Key stroke when a key is pressed.
/// </returns>
/// <example>
/// <code>
/// $Host.UI.RawUI.ReadKey()
/// </code>
/// </example>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.ReadKey(ReadKeyOptions)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.FlushInputBuffer"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.KeyAvailable"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.WindowPosition"/>
public
KeyInfo
ReadKey()
{
return ReadKey(ReadKeyOptions.IncludeKeyDown);
}
/// <summary>
/// Reads a key stroke from the keyboard device, blocking until a keystroke is typed.
/// Either one of ReadKeyOptions.IncludeKeyDown and ReadKeyOptions.IncludeKeyUp or both must be specified.
/// </summary>
/// <param name="options">
/// A bit mask of the options to be used to read the keyboard. Constants defined by
/// <see cref="System.Management.Automation.Host.ReadKeyOptions"/>
/// </param>
/// <returns>
/// Key stroke depending on the value of <paramref name="options"/>.
/// </returns>
/// <exception cref="System.ArgumentException">
/// Neither ReadKeyOptions.IncludeKeyDown nor ReadKeyOptions.IncludeKeyUp is specified.
/// </exception>
/// <example>
/// <code>
/// $option = [System.Management.Automation.Host.ReadKeyOptions]"IncludeKeyDown";
/// $host.UI.RawUI.ReadKey($option)
/// </code>
/// </example>
/// <seealso cref="System.Management.Automation.Host.ReadKeyOptions"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.ReadKey()"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.ReadKey(System.Management.Automation.Host.ReadKeyOptions)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.FlushInputBuffer"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.KeyAvailable"/>
public abstract
KeyInfo
ReadKey(ReadKeyOptions options);
/// <summary>
/// Resets the keyboard input buffer.
/// </summary>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.ReadKey()"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.ReadKey(System.Management.Automation.Host.ReadKeyOptions)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.KeyAvailable"/>
public abstract
void
FlushInputBuffer();
/// <summary>
/// A non-blocking call to examine if a keystroke is waiting in the input buffer.
/// </summary>
/// <value>
/// True if a keystroke is waiting in the input buffer, false if not.
/// </value>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.ReadKey()"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.ReadKey(ReadKeyOptions)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.FlushInputBuffer"/>
public abstract
bool
KeyAvailable
{
get;
}
/// <summary>
/// Gets or sets the titlebar text of the current view window.
/// </summary>
public abstract
string
WindowTitle
{
get;
set;
}
/// <summary>
/// Copies the <see cref="System.Management.Automation.Host.BufferCell"/> array into the screen buffer at the
/// given origin, clipping such that cells in the array that would fall outside the screen buffer are ignored.
/// </summary>
/// <param name="origin">
/// The top left corner of the rectangular screen area to which <paramref name="contents"/> is copied.
/// </param>
/// <param name="contents">
/// A rectangle of <see cref="System.Management.Automation.Host.BufferCell"/> objects to be copied to the
/// screen buffer.
/// </param>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(int, int, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(Size, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(string[], ConsoleColor, ConsoleColor)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.LengthInBufferCells(char)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.LengthInBufferCells(string)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Rectangle, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.GetBufferContents"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.ScrollBufferContents"/>
public abstract
void
SetBufferContents(Coordinates origin, BufferCell[,] contents);
/// <summary>
/// Copies a given character to all of the character cells in the screen buffer with the indicated colors.
/// </summary>
/// <param name="rectangle">
/// The rectangle on the screen buffer to which <paramref name="fill"/> is copied.
/// If all elements are -1, the entire screen buffer will be copied with <paramref name="fill"/>.
/// </param>
/// <param name="fill">
/// The character and attributes used to fill <paramref name="rectangle"/>.
/// </param>
/// <remarks>
/// Provided for clearing regions -- less chatty than passing an array of cells.
/// </remarks>
/// <example>
/// <code>
/// using System;
/// using System.Management.Automation;
/// using System.Management.Automation.Host;
/// namespace Microsoft.Samples.Cmdlet
/// {
/// [Cmdlet("Clear","Screen")]
/// public class ClearScreen : PSCmdlet
/// {
/// protected override void BeginProcessing()
/// {
/// Host.UI.RawUI.SetBufferContents(new Rectangle(-1, -1, -1, -1),
/// new BufferCell(' ', Host.UI.RawUI.ForegroundColor, Host.UI.RawUI.BackgroundColor))
/// }
/// }
/// }
/// </code>
/// </example>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(int, int, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(Size, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(string[], ConsoleColor, ConsoleColor)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.LengthInBufferCells(char)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.LengthInBufferCells(string)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Coordinates, BufferCell[,])"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.GetBufferContents"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.ScrollBufferContents"/>
public abstract
void
SetBufferContents(Rectangle rectangle, BufferCell fill);
/// <summary>
/// Extracts a rectangular region of the screen buffer.
/// </summary>
/// <param name="rectangle">
/// The rectangle on the screen buffer to extract.
/// </param>
/// <returns>
/// An array of <see cref="System.Management.Automation.Host.BufferCell"/> objects extracted from
/// the rectangular region of the screen buffer specified by <paramref name="rectangle"/>
/// </returns>
/// <remarks>
/// If the rectangle is completely outside of the screen buffer, a BufferCell array of zero rows and column will be
/// returned.
///
/// If the rectangle is partially outside of the screen buffer, the area where the screen buffer and rectangle overlap
/// will be read and returned. The size of the returned array is the same as that of r. Each BufferCell in the
/// non-overlapping area of this array is set as follows:
///
/// Character is the space (' ')
/// ForegroundColor to the current foreground color, given by the ForegroundColor property of this class.
/// BackgroundColor to the current background color, given by the BackgroundColor property of this class.
///
/// The resulting array is organized in row-major order for performance reasons. The screen buffer, however, is
/// organized in column-major order -- e.g. you specify the column index first, then the row index second, as in (x, y).
/// This means that a cell at screen buffer position (x, y) is in the array element [y, x].
/// </remarks>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(int, int, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(Size, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(string[], ConsoleColor, ConsoleColor)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.LengthInBufferCells(char)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.LengthInBufferCells(string)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Rectangle, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Coordinates, BufferCell[,])"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.ScrollBufferContents"/>
public abstract
BufferCell[,]
GetBufferContents(Rectangle rectangle);
/// <summary>
/// Scroll a region of the screen buffer.
/// </summary>
/// <param name="source">
/// Indicates the region of the screen to be scrolled.
/// </param>
/// <param name="destination">
/// Indicates the upper left coordinates of the region of the screen to receive the source region contents. The target
/// region is the same size as the source region.
/// </param>
/// <param name="clip">
/// Indicates the region of the screen to include in the operation. If a cell would be changed by the operation but
/// does not fall within the clip region, it will be unchanged.
/// </param>
/// <param name="fill">
/// The character and attributes to be used to fill any cells within the intersection of the source rectangle and
/// clipping rectangle that are left "empty" by the move.
/// </param>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(int, int, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(Size, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(string[], ConsoleColor, ConsoleColor)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.LengthInBufferCells(char)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.LengthInBufferCells(string)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Rectangle, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Coordinates, BufferCell[,])"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.GetBufferContents"/>
public abstract
void
ScrollBufferContents
(
Rectangle source,
Coordinates destination,
Rectangle clip,
BufferCell fill
);
/// <summary>
/// Determines the number of BufferCells a substring of a string occupies.
/// </summary>
/// <param name="source">
/// The string whose substring length we want to know.
/// </param>
/// <param name="offset">
/// Offset where the substring begins in <paramref name="source"/>
/// </param>
/// <returns>
/// The default implementation calls <see cref="PSHostRawUserInterface.LengthInBufferCells(string)"/> method
/// with the substring extracted from the <paramref name="source"/> string
/// starting at the offset <paramref name="offset"/>
/// </returns>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(int, int, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(Size, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(string[], ConsoleColor, ConsoleColor)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.LengthInBufferCells(string)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.LengthInBufferCells(char)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Rectangle, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Coordinates, BufferCell[,])"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.GetBufferContents"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.ScrollBufferContents"/>
public virtual
int
LengthInBufferCells
(
string source,
int offset
)
{
if (source == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(source));
}
// this implementation is inefficient
// it is here to help with backcompatibility
// it preserves the old behavior from the times
// when there was only Length(string) overload
string substring = offset == 0 ? source : source.Substring(offset);
return this.LengthInBufferCells(substring);
}
/// <summary>
/// Determines the number of BufferCells a string occupies.
/// </summary>
/// <param name="source">
/// The string whose length we want to know.
/// </param>
/// <returns>
/// The default implementation returns the length of <paramref name="source"/>
/// </returns>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(int, int, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(Size, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(string[], ConsoleColor, ConsoleColor)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.LengthInBufferCells(char)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.LengthInBufferCells(string, int)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Rectangle, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Coordinates, BufferCell[,])"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.GetBufferContents"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.ScrollBufferContents"/>
public virtual
int
LengthInBufferCells
(
string source
)
{
if (source == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(source));
}
return source.Length;
}
/// <summary>
/// Determines the number of BufferCells a character occupies.
/// </summary>
/// <param name="source">
/// The character whose length we want to know.
/// </param>
/// <returns>
/// The default implementation returns 1.
/// </returns>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(int, int, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(Size, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(string[], ConsoleColor, ConsoleColor)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.LengthInBufferCells(char)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Rectangle, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Coordinates, BufferCell[,])"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.GetBufferContents"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.ScrollBufferContents"/>
public virtual
int
LengthInBufferCells
(
char source
)
{
return 1;
}
/// <summary>
/// Creates a two dimensional array of BufferCells by examining each character in <paramref name="contents"/>.
/// </summary>
/// <param name="contents">
/// String array based on which the two dimensional array of BufferCells will be created.
/// </param>
/// <param name="foregroundColor">
/// Foreground color of the buffer cells in the resulting array.
/// </param>
/// <param name="backgroundColor">
/// Background color of the buffer cells in the resulting array.
/// </param>
/// <returns>
/// A two dimensional array of BufferCells whose characters are the same as those in <paramref name="contents"/>
/// and whose foreground and background colors set to <paramref name="foregroundColor"/> and
/// <paramref name="backgroundColor"/>
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="contents"/> is null;
/// Any string in <paramref name="contents"/> is null or empty
/// </exception>
/// <remarks>
/// If a character C takes one BufferCell to display as determined by LengthInBufferCells,
/// one BufferCell is allocated with its Character set to C and BufferCellType to BufferCell.Complete.
/// On the other hand, if C takes two BufferCell, two adjacent BufferCells on a row in
/// the returned array will be allocated: the first has Character set to C and BufferCellType to
/// <see cref="System.Management.Automation.Host.BufferCellType.Leading"/> and the second
/// Character set to (char)0 and Type to
/// <see cref="System.Management.Automation.Host.BufferCellType.Trailing"/>. Hence, the returned
/// BufferCell array has <paramref name="contents"/>.Length number of rows and number of columns
/// equal to the largest number of cells a string in <paramref name="contents"/> takes. The
/// foreground and background colors of the cells are initialized to
/// <paramref name="foregroundColor"/> and <paramref name="backgroundColor"/>, respectively.
/// The resulting array is suitable for use with <see cref="PSHostRawUserInterface.SetBufferContents(Rectangle, BufferCell)"/>
/// and <see cref="PSHostRawUserInterface.SetBufferContents(Coordinates, BufferCell[,])"/>.
/// </remarks>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(int, int, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(Size, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.LengthInBufferCells(char)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.LengthInBufferCells(string)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Rectangle, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Coordinates, BufferCell[,])"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.GetBufferContents"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.ScrollBufferContents"/>
#pragma warning disable 56506
public
BufferCell[,]
NewBufferCellArray(string[] contents, ConsoleColor foregroundColor, ConsoleColor backgroundColor)
{
#pragma warning disable 56506
if (contents == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(contents));
}
byte[][] charLengths = new byte[contents.Length][];
int maxStringLengthInBufferCells = 0;
for (int i = 0; i < contents.Length; i++)
{
if (string.IsNullOrEmpty(contents[i]))
{
continue;
}
int lengthInBufferCells = 0;
charLengths[i] = new byte[contents[i].Length];
for (int j = 0; j < contents[i].Length; j++)
{
charLengths[i][j] = (byte)LengthInBufferCells(contents[i][j]);
lengthInBufferCells += charLengths[i][j];
}
if (maxStringLengthInBufferCells < lengthInBufferCells)
{
maxStringLengthInBufferCells = lengthInBufferCells;
}
}
if (maxStringLengthInBufferCells <= 0)
{
throw PSTraceSource.NewArgumentException(nameof(contents), MshHostRawUserInterfaceStrings.AllNullOrEmptyStringsErrorTemplate);
}
BufferCell[,] results = new BufferCell[contents.Length, maxStringLengthInBufferCells];
for (int i = 0; i < contents.Length; i++)
{
int resultJ = 0;
for (int j = 0; j < contents[i].Length; j++, resultJ++)
{
if (charLengths[i][j] == 1)
{
results[i, resultJ] =
new BufferCell(contents[i][j], foregroundColor, backgroundColor, BufferCellType.Complete);
}
else if (charLengths[i][j] == 2)
{
results[i, resultJ] =
new BufferCell(contents[i][j], foregroundColor, backgroundColor, BufferCellType.Leading);
resultJ++;
results[i, resultJ] =
new BufferCell((char)0, foregroundColor, backgroundColor, BufferCellType.Trailing);
}
}
while (resultJ < maxStringLengthInBufferCells)
{
results[i, resultJ] = new BufferCell(' ', foregroundColor, backgroundColor, BufferCellType.Complete);
resultJ++;
}
}
return results;
#pragma warning restore 56506
}
#pragma warning restore 56506
/// <summary>
/// Creates a 2D array of BufferCells by examining <paramref name="contents"/>.Character.
/// <see cref="PSHostRawUserInterface"/>
/// </summary>
/// <param name="width">
/// The number of columns of the resulting array
/// </param>
/// <param name="height">
/// The number of rows of the resulting array
/// </param>
/// <param name="contents">
/// The cell to be copied to each of the elements of the resulting array.
/// </param>
/// <returns>
/// A <paramref name="width"/> by <paramref name="height"/> array of BufferCells where each cell's value is
/// based on <paramref name="contents"/>
/// <paramref name="backgroundColor"/>
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="width"/> is less than 1;
/// <paramref name="height"/> is less than 1.
/// </exception>
/// <remarks>
/// If the character takes one BufferCell to display as determined by LengthInBufferCells,
/// one BufferCell is allocated with its Character set to the character and BufferCellType to
/// BufferCell.Complete.
/// On the other hand, if it takes two BufferCells, two adjacent BufferCells on a row
/// in the returned array will be allocated: the first has Character
/// set to the character and BufferCellType to BufferCellType.Leading and the second Character
/// set to (char)0 and BufferCellType to BufferCellType.Trailing. Moreover, if <paramref name="width"/>
/// is odd, the last column will just contain the leading cell.
/// <paramref name="prototype"/>.BufferCellType is not used in creating the array.
/// The resulting array is suitable for use with the PSHostRawUserInterface.SetBufferContents method.
/// </remarks>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(Size, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(string[], ConsoleColor, ConsoleColor)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.LengthInBufferCells(char)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.LengthInBufferCells(string)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Rectangle, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Coordinates, BufferCell[,])"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.GetBufferContents"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.ScrollBufferContents"/>
public
BufferCell[,]
NewBufferCellArray(int width, int height, BufferCell contents)
{
if (width <= 0)
{
// "width" is not localizable
throw PSTraceSource.NewArgumentOutOfRangeException(nameof(width), width,
MshHostRawUserInterfaceStrings.NonPositiveNumberErrorTemplate, "width");
}
if (height <= 0)
{
// "height" is not localizable
throw PSTraceSource.NewArgumentOutOfRangeException(nameof(height), height,
MshHostRawUserInterfaceStrings.NonPositiveNumberErrorTemplate, "height");
}
BufferCell[,] buffer = new BufferCell[height, width];
int charLength = LengthInBufferCells(contents.Character);
if (charLength == 1)
{
for (int r = 0; r < buffer.GetLength(0); ++r)
{
for (int c = 0; c < buffer.GetLength(1); ++c)
{
buffer[r, c] = contents;
buffer[r, c].BufferCellType = BufferCellType.Complete;
}
}
}
else if (charLength == 2)
{
int normalizedWidth = width % 2 == 0 ? width : width - 1;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < normalizedWidth; j++)
{
buffer[i, j] = contents;
buffer[i, j].BufferCellType = BufferCellType.Leading;
j++;
buffer[i, j] = new BufferCell((char)0,
contents.ForegroundColor, contents.BackgroundColor,
BufferCellType.Trailing);
}
if (normalizedWidth < width)
{
buffer[i, normalizedWidth] = contents;
buffer[i, normalizedWidth].BufferCellType = BufferCellType.Leading;
}
}
}
return buffer;
}
/// <summary>
/// Same as <see cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(int, int, BufferCell)"/>
/// </summary>
/// <param name="size">
/// The width and height of the resulting array.
/// </param>
/// <param name="contents">
/// The cell to be copied to each of the elements of the resulting array.
/// </param>
/// <returns>
/// An array of BufferCells whose size is <paramref name="size"/> and where each cell's value is
/// based on <paramref name="contents"/>
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">
/// If <paramref name="size"/>.Width or <paramref name="size"/>.Height is less than 1.
/// </exception>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(int, int, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.NewBufferCellArray(string[], ConsoleColor, ConsoleColor)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.LengthInBufferCells(char)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.LengthInBufferCells(string)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Rectangle, BufferCell)"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.SetBufferContents(Coordinates, BufferCell[,])"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.GetBufferContents"/>
/// <seealso cref="System.Management.Automation.Host.PSHostRawUserInterface.ScrollBufferContents"/>
public
BufferCell[,]
NewBufferCellArray(Size size, BufferCell contents)
{
return NewBufferCellArray(size.Width, size.Height, contents);
}
}
}
|