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 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
//! The textwrap library provides functions for word wrapping and //! indenting text. //! //! # Wrapping Text //! //! Wrapping text can be very useful in command-line programs where //! you want to format dynamic output nicely so it looks good in a //! terminal. A quick example: //! //! ```no_run //! fn main() { //! let text = "textwrap: a small library for wrapping text."; //! println!("{}", textwrap::fill(text, 18)); //! } //! ``` //! //! When you run this program, it will display the following output: //! //! ```text //! textwrap: a small //! library for //! wrapping text. //! ``` //! //! If you enable the `hyphenation` Cargo feature, you can get //! automatic hyphenation for a number of languages: //! //! ```no_run //! # #[cfg(feature = "hyphenation")] //! use hyphenation::{Language, Load, Standard}; //! use textwrap::{fill, Options}; //! //! # #[cfg(feature = "hyphenation")] //! fn main() { //! let text = "textwrap: a small library for wrapping text."; //! let dictionary = Standard::from_embedded(Language::EnglishUS).unwrap(); //! let options = Options::new(18).word_splitter(dictionary); //! println!("{}", fill(text, &options)); //! } //! //! # #[cfg(not(feature = "hyphenation"))] //! # fn main() { } //! ``` //! //! The program will now output: //! //! ```text //! textwrap: a small //! library for wrap- //! ping text. //! ``` //! //! See also the [`unfill`] and [`refill`] functions which allow you to //! manipulate already wrapped text. //! //! ## Wrapping Strings at Compile Time //! //! If your strings are known at compile time, please take a look at //! the procedural macros from the [textwrap-macros] crate. //! //! ## Displayed Width vs Byte Size //! //! To word wrap text, one must know the width of each word so one can //! know when to break lines. This library will by default measure the //! width of text using the _displayed width_, not the size in bytes. //! The `unicode-width` Cargo feature controls this. //! //! This is important for non-ASCII text. ASCII characters such as `a` //! and `!` are simple and take up one column each. This means that //! the displayed width is equal to the string length in bytes. //! However, non-ASCII characters and symbols take up more than one //! byte when UTF-8 encoded: `é` is `0xc3 0xa9` (two bytes) and `⚙` is //! `0xe2 0x9a 0x99` (three bytes) in UTF-8, respectively. //! //! This is why we take care to use the displayed width instead of the //! byte count when computing line lengths. All functions in this //! library handle Unicode characters like this when the //! `unicode-width` Cargo feature is enabled (it is enabled by //! default). //! //! # Indentation and Dedentation //! //! The textwrap library also offers functions for adding a prefix to //! every line of a string and to remove leading whitespace. As an //! example, the [`indent`] function allows you to turn lines of text //! into a bullet list: //! //! ``` //! let before = "\ //! foo //! bar //! baz //! "; //! let after = "\ //! * foo //! * bar //! * baz //! "; //! assert_eq!(textwrap::indent(before, "* "), after); //! ``` //! //! Removing leading whitespace is done with [`dedent`]: //! //! ``` //! let before = " //! Some //! indented //! text //! "; //! let after = " //! Some //! indented //! text //! "; //! assert_eq!(textwrap::dedent(before), after); //! ``` //! //! # Cargo Features //! //! The textwrap library can be slimmed down as needed via a number of //! Cargo features. This means you only pay for the features you //! actually use. //! //! The full dependency graph, where dashed lines indicate optional //! dependencies, is shown below: //! //! <img src="https://raw.githubusercontent.com/mgeisler/textwrap/master/images/textwrap-0.14.2.svg"> //! //! ## Default Features //! //! These features are enabled by default: //! //! * `unicode-linebreak`: enables finding words using the //! [unicode-linebreak] crate, which implements the line breaking //! algorithm described in [Unicode Standard Annex //! #14](https://www.unicode.org/reports/tr14/). //! //! This feature can be disabled if you are happy to find words //! separated by ASCII space characters only. People wrapping text //! with emojis or East-Asian characters will want most likely want //! to enable this feature. See the //! [`word_separators::WordSeparator`] trait for details. //! //! * `unicode-width`: enables correct width computation of non-ASCII //! characters via the [unicode-width] crate. Without this feature, //! every [`char`] is 1 column wide, except for emojis which are 2 //! columns wide. See the [`core::display_width`] function for //! details. //! //! This feature can be disabled if you only need to wrap ASCII //! text, or if the functions in [`core`] are used directly with //! [`core::Fragment`]s for which the widths have been computed in //! other ways. //! //! * `smawk`: enables linear-time wrapping of the whole paragraph via //! the [smawk] crate. See the [`wrap_algorithms::wrap_optimal_fit`] //! function for details on the optimal-fit algorithm. //! //! This feature can be disabled if you only ever intend to use //! [`wrap_algorithms::wrap_first_fit`]. //! //! ## Optional Features //! //! These Cargo features enable new functionality: //! //! * `terminal_size`: enables automatic detection of the terminal //! width via the [terminal_size] crate. See the //! [`Options::with_termwidth`] constructor for details. //! //! * `hyphenation`: enables language-sensitive hyphenation via the //! [hyphenation] crate. See the [`word_splitters::WordSplitter`] trait for details. //! //! [unicode-linebreak]: https://docs.rs/unicode-linebreak/ //! [unicode-width]: https://docs.rs/unicode-width/ //! [smawk]: https://docs.rs/smawk/ //! [textwrap-macros]: https://docs.rs/textwrap-macros/ //! [terminal_size]: https://docs.rs/terminal_size/ //! [hyphenation]: https://docs.rs/hyphenation/ #![doc(html_root_url = "https://docs.rs/textwrap/0.14.2")] #![forbid(unsafe_code)] // See https://github.com/mgeisler/textwrap/issues/210 #![deny(missing_docs)] #![deny(missing_debug_implementations)] #![allow(clippy::redundant_field_names)] use std::borrow::Cow; mod indentation; pub use crate::indentation::dedent; pub use crate::indentation::indent; pub mod word_separators; pub mod word_splitters; pub mod wrap_algorithms; pub mod core; // These private macros lets us hide the actual WrapAlgorithm and // WordSeperator used in the function signatures below. #[cfg(feature = "smawk")] macro_rules! DefaultWrapAlgorithm { () => { wrap_algorithms::OptimalFit }; } #[cfg(not(feature = "smawk"))] macro_rules! DefaultWrapAlgorithm { () => { wrap_algorithms::FirstFit }; } #[cfg(feature = "unicode-linebreak")] macro_rules! DefaultWordSeparator { () => { word_separators::UnicodeBreakProperties }; } #[cfg(not(feature = "unicode-linebreak"))] macro_rules! DefaultWordSeparator { () => { word_separators::AsciiSpace }; } /// Holds settings for wrapping and filling text. #[derive(Debug, Clone)] pub struct Options< 'a, WrapAlgo = Box<dyn wrap_algorithms::WrapAlgorithm>, WordSep = Box<dyn word_separators::WordSeparator>, WordSplit = Box<dyn word_splitters::WordSplitter>, > { /// The width in columns at which the text will be wrapped. pub width: usize, /// Indentation used for the first line of output. See the /// [`Options::initial_indent`] method. pub initial_indent: &'a str, /// Indentation used for subsequent lines of output. See the /// [`Options::subsequent_indent`] method. pub subsequent_indent: &'a str, /// Allow long words to be broken if they cannot fit on a line. /// When set to `false`, some lines may be longer than /// `self.width`. See the [`Options::break_words`] method. pub break_words: bool, /// Wrapping algorithm to use, see the implementations of the /// [`wrap_algorithms::WrapAlgorithm`] trait for details. pub wrap_algorithm: WrapAlgo, /// The line breaking algorithm to use, see /// [`word_separators::WordSeparator`] trait for an overview and /// possible implementations. pub word_separator: WordSep, /// The method for splitting words. This can be used to prohibit /// splitting words on hyphens, or it can be used to implement /// language-aware machine hyphenation. Please see the /// [`word_splitters::WordSplitter`] trait for details. pub word_splitter: WordSplit, } impl<'a, WrapAlgo, WordSep, WordSplit> From<&'a Options<'a, WrapAlgo, WordSep, WordSplit>> for Options<'a, WrapAlgo, WordSep, WordSplit> where WrapAlgo: Clone, WordSep: Clone, WordSplit: Clone, { fn from(options: &'a Options<'a, WrapAlgo, WordSep, WordSplit>) -> Self { Self { width: options.width, initial_indent: options.initial_indent, subsequent_indent: options.subsequent_indent, break_words: options.break_words, word_separator: options.word_separator.clone(), wrap_algorithm: options.wrap_algorithm.clone(), word_splitter: options.word_splitter.clone(), } } } impl<'a> From<usize> for Options< 'a, DefaultWrapAlgorithm!(), DefaultWordSeparator!(), word_splitters::HyphenSplitter, > { fn from(width: usize) -> Self { Options::new(width) } } /// Constructors for boxed Options, specifically. impl<'a> Options<'a, DefaultWrapAlgorithm!(), DefaultWordSeparator!(), word_splitters::HyphenSplitter> { /// Creates a new [`Options`] with the specified width and static /// dispatch using the [`word_splitters::HyphenSplitter`]. /// Equivalent to /// /// ``` /// # use textwrap::word_splitters::{HyphenSplitter, WordSplitter}; /// # use textwrap::Options; /// # let width = 80; /// # let actual = Options::new(width); /// # let expected = /// Options { /// width: width, /// initial_indent: "", /// subsequent_indent: "", /// break_words: true, /// #[cfg(feature = "unicode-linebreak")] /// word_separator: textwrap::word_separators::UnicodeBreakProperties, /// #[cfg(not(feature = "unicode-linebreak"))] /// word_separator: textwrap::word_separators::AsciiSpace, /// #[cfg(feature = "smawk")] /// wrap_algorithm: textwrap::wrap_algorithms::OptimalFit, /// #[cfg(not(feature = "smawk"))] /// wrap_algorithm: textwrap::wrap_algorithms::FirstFit, /// word_splitter: textwrap::word_splitters::HyphenSplitter, /// } /// # ; /// # assert_eq!(actual.width, expected.width); /// # assert_eq!(actual.initial_indent, expected.initial_indent); /// # assert_eq!(actual.subsequent_indent, expected.subsequent_indent); /// # assert_eq!(actual.break_words, expected.break_words); /// ``` /// /// Note that the default word separator and wrap algorithms /// changes based on the available Cargo features. The best /// available algorithm is used by default. /// /// Static dispatch means here, that the word splitter is stored as-is /// and the type is known at compile-time. Thus the returned value /// is actually a `Options<AsciiSpace, HyphenSplitter>`. /// /// Dynamic dispatch on the other hand, means that the word /// separator and/or word splitter is stored as a trait object /// such as a `Box<dyn word_splitters::WordSplitter>`. This way /// the word splitter's inner type can be changed without changing /// the type of this struct, which then would be just `Options` as /// a short cut for `Options<Box<dyn /// word_separators::WordSeparator>, Box<dyn /// word_splitters::WordSplitter>>`. /// /// The value and type of the word splitter can be choose from the /// start using the [`Options::with_word_splitter`] constructor or /// changed afterwards using the [`Options::word_splitter`] /// method. Whether static or dynamic dispatch is used, depends on /// whether these functions are given a boxed /// [`word_splitters::WordSplitter`] or not. Take for example: /// /// ``` /// use textwrap::Options; /// use textwrap::word_splitters::{HyphenSplitter, NoHyphenation}; /// # use textwrap::word_splitters::WordSplitter; /// # use textwrap::word_separators::AsciiSpace; /// # let width = 80; /// /// // uses HyphenSplitter with static dispatch /// // the actual type: Options<AsciiSpace, HyphenSplitter> /// let opt = Options::new(width); /// /// // uses NoHyphenation with static dispatch /// // the actual type: Options<AsciiSpace, NoHyphenation> /// let opt = Options::new(width).word_splitter(NoHyphenation); /// /// // uses HyphenSplitter with dynamic dispatch /// // the actual type: Options<AsciiSpace, Box<dyn word_splitters::WordSplitter>> /// let opt: Options<_, _, _> = Options::new(width).word_splitter(Box::new(HyphenSplitter)); /// /// // uses NoHyphenation with dynamic dispatch /// // the actual type: Options<AsciiSpace, Box<dyn word_splitters::WordSplitter>> /// let opt: Options<_, _, _> = Options::new(width).word_splitter(Box::new(NoHyphenation)); /// ``` /// /// Notice that the last two variables have the same type, despite /// the different `WordSplitter` in use. Thus dynamic dispatch /// allows to change the word splitter at run-time without /// changing the variables type. pub const fn new(width: usize) -> Self { Options::with_word_splitter(width, word_splitters::HyphenSplitter) } /// Creates a new [`Options`] with `width` set to the current /// terminal width. If the terminal width cannot be determined /// (typically because the standard input and output is not /// connected to a terminal), a width of 80 characters will be /// used. Other settings use the same defaults as /// [`Options::new`]. /// /// Equivalent to: /// /// ```no_run /// use textwrap::{termwidth, Options}; /// /// let options = Options::new(termwidth()); /// ``` /// /// **Note:** Only available when the `terminal_size` feature is /// enabled. #[cfg(feature = "terminal_size")] pub fn with_termwidth() -> Self { Self::new(termwidth()) } } impl<'a, WordSplit> Options<'a, DefaultWrapAlgorithm!(), DefaultWordSeparator!(), WordSplit> { /// Creates a new [`Options`] with the specified width and /// word splitter. Equivalent to /// /// ``` /// # use textwrap::Options; /// # use textwrap::word_splitters::{NoHyphenation, HyphenSplitter}; /// # const word_splitter: NoHyphenation = NoHyphenation; /// # const width: usize = 80; /// # let actual = Options::with_word_splitter(width, word_splitter); /// # let expected = /// Options { /// width: width, /// initial_indent: "", /// subsequent_indent: "", /// break_words: true, /// #[cfg(feature = "unicode-linebreak")] /// word_separator: textwrap::word_separators::UnicodeBreakProperties, /// #[cfg(not(feature = "unicode-linebreak"))] /// word_separator: textwrap::word_separators::AsciiSpace, /// #[cfg(feature = "smawk")] /// wrap_algorithm: textwrap::wrap_algorithms::OptimalFit, /// #[cfg(not(feature = "smawk"))] /// wrap_algorithm: textwrap::wrap_algorithms::FirstFit, /// word_splitter: word_splitter, /// } /// # ; /// # assert_eq!(actual.width, expected.width); /// # assert_eq!(actual.initial_indent, expected.initial_indent); /// # assert_eq!(actual.subsequent_indent, expected.subsequent_indent); /// # assert_eq!(actual.break_words, expected.break_words); /// ``` /// /// This constructor allows to specify the word splitter to be /// used. It is like a short-cut for /// `Options::new(w).word_splitter(s)`, but this function is a /// `const fn`. The given word splitter may be in a [`Box`], which /// then can be coerced into a trait object for dynamic dispatch: /// /// ``` /// use textwrap::Options; /// use textwrap::word_splitters::{HyphenSplitter, NoHyphenation, WordSplitter}; /// # const width: usize = 80; /// /// // This opt contains a boxed trait object as splitter. /// // The type annotation is important, otherwise it will be not a trait object /// let mut opt: Options<_, _, Box<dyn WordSplitter>> /// = Options::with_word_splitter(width, Box::new(NoHyphenation)); /// // Its type is actually: `Options<AsciiSpace, Box<dyn word_splitters::WordSplitter>>`: /// let opt_coerced: Options<_, _, Box<dyn WordSplitter>> = opt; /// /// // Thus, it can be overridden with a different word splitter. /// opt = Options::with_word_splitter(width, Box::new(HyphenSplitter)); /// // Now, containing a `HyphenSplitter` instead. /// ``` /// /// Since the word splitter is given by value, which determines /// the generic type parameter, it can be used to produce both an /// [`Options`] with static and dynamic dispatch, respectively. /// While dynamic dispatch allows to change the type of the inner /// word splitter at run time as seen above, static dispatch /// especially can store the word splitter directly, without the /// need for a box. This in turn allows it to be used in constant /// and static context: /// /// ``` /// use textwrap::word_splitters::HyphenSplitter; use textwrap::{ Options}; /// use textwrap::word_separators::AsciiSpace; /// use textwrap::wrap_algorithms::FirstFit; /// # const width: usize = 80; /// /// # #[cfg(all(not(feature = "smawk"), not(feature = "unicode-linebreak")))] { /// const FOO: Options<FirstFit, AsciiSpace, HyphenSplitter> = /// Options::with_word_splitter(width, HyphenSplitter); /// static BAR: Options<FirstFit, AsciiSpace, HyphenSplitter> = FOO; /// # } /// ``` pub const fn with_word_splitter(width: usize, word_splitter: WordSplit) -> Self { Options { width, initial_indent: "", subsequent_indent: "", break_words: true, word_separator: DefaultWordSeparator!(), wrap_algorithm: DefaultWrapAlgorithm!(), word_splitter: word_splitter, } } } impl<'a, WrapAlgo, WordSep, WordSplit> Options<'a, WrapAlgo, WordSep, WordSplit> { /// Change [`self.initial_indent`]. The initial indentation is /// used on the very first line of output. /// /// # Examples /// /// Classic paragraph indentation can be achieved by specifying an /// initial indentation and wrapping each paragraph by itself: /// /// ``` /// use textwrap::{Options, wrap}; /// /// let options = Options::new(16).initial_indent(" "); /// assert_eq!(wrap("This is a little example.", options), /// vec![" This is a", /// "little example."]); /// ``` /// /// [`self.initial_indent`]: #structfield.initial_indent pub fn initial_indent(self, indent: &'a str) -> Self { Options { initial_indent: indent, ..self } } /// Change [`self.subsequent_indent`]. The subsequent indentation /// is used on lines following the first line of output. /// /// # Examples /// /// Combining initial and subsequent indentation lets you format a /// single paragraph as a bullet list: /// /// ``` /// use textwrap::{Options, wrap}; /// /// let options = Options::new(12) /// .initial_indent("* ") /// .subsequent_indent(" "); /// #[cfg(feature = "smawk")] /// assert_eq!(wrap("This is a little example.", options), /// vec!["* This is", /// " a little", /// " example."]); /// /// // Without the `smawk` feature, the wrapping is a little different: /// #[cfg(not(feature = "smawk"))] /// assert_eq!(wrap("This is a little example.", options), /// vec!["* This is a", /// " little", /// " example."]); /// ``` /// /// [`self.subsequent_indent`]: #structfield.subsequent_indent pub fn subsequent_indent(self, indent: &'a str) -> Self { Options { subsequent_indent: indent, ..self } } /// Change [`self.break_words`]. This controls if words longer /// than `self.width` can be broken, or if they will be left /// sticking out into the right margin. /// /// # Examples /// /// ``` /// use textwrap::{wrap, Options}; /// /// let options = Options::new(4).break_words(true); /// assert_eq!(wrap("This is a little example.", options), /// vec!["This", /// "is a", /// "litt", /// "le", /// "exam", /// "ple."]); /// ``` /// /// [`self.break_words`]: #structfield.break_words pub fn break_words(self, setting: bool) -> Self { Options { break_words: setting, ..self } } /// Change [`self.word_separator`]. /// /// See [`word_separators::WordSeparator`] for details on the choices. /// /// [`self.word_separator`]: #structfield.word_separator pub fn word_separator<NewWordSep>( self, word_separator: NewWordSep, ) -> Options<'a, WrapAlgo, NewWordSep, WordSplit> { Options { width: self.width, initial_indent: self.initial_indent, subsequent_indent: self.subsequent_indent, break_words: self.break_words, word_separator: word_separator, wrap_algorithm: self.wrap_algorithm, word_splitter: self.word_splitter, } } /// Change [`self.wrap_algorithm`]. /// /// See the [`wrap_algorithms::WrapAlgorithm`] trait for details on /// the choices. /// /// [`self.wrap_algorithm`]: #structfield.wrap_algorithm pub fn wrap_algorithm<NewWrapAlgo>( self, wrap_algorithm: NewWrapAlgo, ) -> Options<'a, NewWrapAlgo, WordSep, WordSplit> { Options { width: self.width, initial_indent: self.initial_indent, subsequent_indent: self.subsequent_indent, break_words: self.break_words, word_separator: self.word_separator, wrap_algorithm: wrap_algorithm, word_splitter: self.word_splitter, } } /// Change [`self.word_splitter`]. The /// [`word_splitters::WordSplitter`] is used to fit part of a word /// into the current line when wrapping text. /// /// This function may return a different type than `Self`. That is /// the case when the given `splitter` is of a different type the /// the currently stored one in the `splitter` field. Take for /// example: /// /// ``` /// use textwrap::word_splitters::{HyphenSplitter, NoHyphenation}; /// use textwrap::Options; /// // The default type returned by `new`: /// let opt: Options<_, _, HyphenSplitter> = Options::new(80); /// // Setting a different word splitter changes the type /// let opt: Options<_, _, NoHyphenation> = opt.word_splitter(NoHyphenation); /// ``` /// /// [`self.word_splitter`]: #structfield.word_splitter pub fn word_splitter<NewWordSplit>( self, word_splitter: NewWordSplit, ) -> Options<'a, WrapAlgo, WordSep, NewWordSplit> { Options { width: self.width, initial_indent: self.initial_indent, subsequent_indent: self.subsequent_indent, break_words: self.break_words, word_separator: self.word_separator, wrap_algorithm: self.wrap_algorithm, word_splitter, } } } /// Return the current terminal width. /// /// If the terminal width cannot be determined (typically because the /// standard output is not connected to a terminal), a default width /// of 80 characters will be used. /// /// # Examples /// /// Create an [`Options`] for wrapping at the current terminal width /// with a two column margin to the left and the right: /// /// ```no_run /// use textwrap::{termwidth, Options}; /// use textwrap::word_splitters::NoHyphenation; /// /// let width = termwidth() - 4; // Two columns on each side. /// let options = Options::new(width) /// .word_splitter(NoHyphenation) /// .initial_indent(" ") /// .subsequent_indent(" "); /// ``` /// /// **Note:** Only available when the `terminal_size` Cargo feature is /// enabled. #[cfg(feature = "terminal_size")] pub fn termwidth() -> usize { terminal_size::terminal_size().map_or(80, |(terminal_size::Width(w), _)| w.into()) } /// Fill a line of text at a given width. /// /// The result is a [`String`], complete with newlines between each /// line. Use the [`wrap`] function if you need access to the /// individual lines. /// /// The easiest way to use this function is to pass an integer for /// `width_or_options`: /// /// ``` /// use textwrap::fill; /// /// assert_eq!( /// fill("Memory safety without garbage collection.", 15), /// "Memory safety\nwithout garbage\ncollection." /// ); /// ``` /// /// If you need to customize the wrapping, you can pass an [`Options`] /// instead of an `usize`: /// /// ``` /// use textwrap::{fill, Options}; /// /// let options = Options::new(15) /// .initial_indent("- ") /// .subsequent_indent(" "); /// assert_eq!( /// fill("Memory safety without garbage collection.", &options), /// "- Memory safety\n without\n garbage\n collection." /// ); /// ``` pub fn fill<'a, WrapAlgo, WordSep, WordSplit, Opt>(text: &str, width_or_options: Opt) -> String where WrapAlgo: wrap_algorithms::WrapAlgorithm, WordSep: word_separators::WordSeparator, WordSplit: word_splitters::WordSplitter, Opt: Into<Options<'a, WrapAlgo, WordSep, WordSplit>>, { // This will avoid reallocation in simple cases (no // indentation, no hyphenation). let mut result = String::with_capacity(text.len()); for (i, line) in wrap(text, width_or_options).iter().enumerate() { if i > 0 { result.push('\n'); } result.push_str(&line); } result } /// Unpack a paragraph of already-wrapped text. /// /// This function attempts to recover the original text from a single /// paragraph of text produced by the [`fill`] function. This means /// that it turns /// /// ```text /// textwrap: a small /// library for /// wrapping text. /// ``` /// /// back into /// /// ```text /// textwrap: a small library for wrapping text. /// ``` /// /// In addition, it will recognize a common prefix among the lines. /// The prefix of the first line is returned in /// [`Options::initial_indent`] and the prefix (if any) of the the /// other lines is returned in [`Options::subsequent_indent`]. /// /// In addition to `' '`, the prefixes can consist of characters used /// for unordered lists (`'-'`, `'+'`, and `'*'`) and block quotes /// (`'>'`) in Markdown as well as characters often used for inline /// comments (`'#'` and `'/'`). /// /// The text must come from a single wrapped paragraph. This means /// that there can be no `"\n\n"` within the text. /// /// # Examples /// /// ``` /// use textwrap::unfill; /// /// let (text, options) = unfill("\ /// * This is an /// example of /// a list item. /// "); /// /// assert_eq!(text, "This is an example of a list item.\n"); /// assert_eq!(options.initial_indent, "* "); /// assert_eq!(options.subsequent_indent, " "); /// ``` pub fn unfill( text: &str, ) -> ( String, Options<'_, DefaultWrapAlgorithm!(), DefaultWordSeparator!(), word_splitters::HyphenSplitter>, ) { let trimmed = text.trim_end_matches('\n'); let prefix_chars: &[_] = &[' ', '-', '+', '*', '>', '#', '/']; let mut options = Options::new(0); for (idx, line) in trimmed.split('\n').enumerate() { options.width = std::cmp::max(options.width, core::display_width(line)); let without_prefix = line.trim_start_matches(prefix_chars); let prefix = &line[..line.len() - without_prefix.len()]; if idx == 0 { options.initial_indent = prefix; } else if idx == 1 { options.subsequent_indent = prefix; } else if idx > 1 { for ((idx, x), y) in prefix.char_indices().zip(options.subsequent_indent.chars()) { if x != y { options.subsequent_indent = &prefix[..idx]; break; } } if prefix.len() < options.subsequent_indent.len() { options.subsequent_indent = prefix; } } } let mut unfilled = String::with_capacity(text.len()); for (idx, line) in trimmed.split('\n').enumerate() { if idx == 0 { unfilled.push_str(&line[options.initial_indent.len()..]); } else { unfilled.push(' '); unfilled.push_str(&line[options.subsequent_indent.len()..]); } } unfilled.push_str(&text[trimmed.len()..]); (unfilled, options) } /// Refill a paragraph of wrapped text with a new width. /// /// This function will first use the [`unfill`] function to remove /// newlines from the text. Afterwards the text is filled again using /// the [`fill`] function. /// /// The `new_width_or_options` argument specify the new width and can /// specify other options as well — except for /// [`Options::initial_indent`] and [`Options::subsequent_indent`], /// which are deduced from `filled_text`. /// /// # Examples /// /// ``` /// use textwrap::refill; /// /// // Some loosely wrapped text. The "> " prefix is recognized automatically. /// let text = "\ /// > Memory /// > safety without garbage /// > collection. /// "; /// /// assert_eq!(refill(text, 20), "\ /// > Memory safety /// > without garbage /// > collection. /// "); /// /// assert_eq!(refill(text, 40), "\ /// > Memory safety without garbage /// > collection. /// "); /// /// assert_eq!(refill(text, 60), "\ /// > Memory safety without garbage collection. /// "); /// ``` /// /// You can also reshape bullet points: /// /// ``` /// use textwrap::refill; /// /// let text = "\ /// - This is my /// list item. /// "; /// /// assert_eq!(refill(text, 20), "\ /// - This is my list /// item. /// "); /// ``` pub fn refill<'a, WrapAlgo, WordSep, WordSplit, Opt>( filled_text: &str, new_width_or_options: Opt, ) -> String where WrapAlgo: wrap_algorithms::WrapAlgorithm, WordSep: word_separators::WordSeparator, WordSplit: word_splitters::WordSplitter, Opt: Into<Options<'a, WrapAlgo, WordSep, WordSplit>>, { let trimmed = filled_text.trim_end_matches('\n'); let (text, options) = unfill(trimmed); let mut new_options = new_width_or_options.into(); new_options.initial_indent = options.initial_indent; new_options.subsequent_indent = options.subsequent_indent; let mut refilled = fill(&text, new_options); refilled.push_str(&filled_text[trimmed.len()..]); refilled } /// Wrap a line of text at a given width. /// /// The result is a vector of lines, each line is of type [`Cow<'_, /// str>`](Cow), which means that the line will borrow from the input /// `&str` if possible. The lines do not have trailing whitespace, /// including a final `'\n'`. Please use the [`fill`] function if you /// need a [`String`] instead. /// /// The easiest way to use this function is to pass an integer for /// `width_or_options`: /// /// ``` /// use textwrap::wrap; /// /// let lines = wrap("Memory safety without garbage collection.", 15); /// assert_eq!(lines, &[ /// "Memory safety", /// "without garbage", /// "collection.", /// ]); /// ``` /// /// If you need to customize the wrapping, you can pass an [`Options`] /// instead of an `usize`: /// /// ``` /// use textwrap::{wrap, Options}; /// /// let options = Options::new(15) /// .initial_indent("- ") /// .subsequent_indent(" "); /// let lines = wrap("Memory safety without garbage collection.", &options); /// assert_eq!(lines, &[ /// "- Memory safety", /// " without", /// " garbage", /// " collection.", /// ]); /// ``` /// /// # Optimal-Fit Wrapping /// /// By default, `wrap` will try to ensure an even right margin by /// finding breaks which avoid short lines. We call this an /// “optimal-fit algorithm” since the line breaks are computed by /// considering all possible line breaks. The alternative is a /// “first-fit algorithm” which simply accumulates words until they no /// longer fit on the line. /// /// As an example, using the first-fit algorithm to wrap the famous /// Hamlet quote “To be, or not to be: that is the question” in a /// narrow column with room for only 10 characters looks like this: /// /// ``` /// # use textwrap::{wrap_algorithms::FirstFit, Options, wrap}; /// # /// # let lines = wrap("To be, or not to be: that is the question", /// # Options::new(10).wrap_algorithm(FirstFit)); /// # assert_eq!(lines.join("\n") + "\n", "\ /// To be, or /// not to be: /// that is /// the /// question /// # "); /// ``` /// /// Notice how the second to last line is quite narrow because /// “question” was too large to fit? The greedy first-fit algorithm /// doesn’t look ahead, so it has no other option than to put /// “question” onto its own line. /// /// With the optimal-fit wrapping algorithm, the previous lines are /// shortened slightly in order to make the word “is” go into the /// second last line: /// /// ``` /// # #[cfg(feature = "smawk")] { /// # use textwrap::{Options, wrap}; /// # use textwrap::wrap_algorithms::OptimalFit; /// # /// # let lines = wrap("To be, or not to be: that is the question", /// # Options::new(10).wrap_algorithm(OptimalFit)); /// # assert_eq!(lines.join("\n") + "\n", "\ /// To be, /// or not to /// be: that /// is the /// question /// # "); } /// ``` /// /// Please see the [`wrap_algorithms::WrapAlgorithm`] trait for details. /// /// # Examples /// /// The returned iterator yields lines of type `Cow<'_, str>`. If /// possible, the wrapped lines will borrow from the input string. As /// an example, a hanging indentation, the first line can borrow from /// the input, but the subsequent lines become owned strings: /// /// ``` /// use std::borrow::Cow::{Borrowed, Owned}; /// use textwrap::{wrap, Options}; /// /// let options = Options::new(15).subsequent_indent("...."); /// let lines = wrap("Wrapping text all day long.", &options); /// let annotated = lines /// .iter() /// .map(|line| match line { /// Borrowed(text) => format!("[Borrowed] {}", text), /// Owned(text) => format!("[Owned] {}", text), /// }) /// .collect::<Vec<_>>(); /// assert_eq!( /// annotated, /// &[ /// "[Borrowed] Wrapping text", /// "[Owned] ....all day", /// "[Owned] ....long.", /// ] /// ); /// ``` /// /// ## Leading and Trailing Whitespace /// /// As a rule, leading whitespace (indentation) is preserved and /// trailing whitespace is discarded. /// /// In more details, when wrapping words into lines, words are found /// by splitting the input text on space characters. One or more /// spaces (shown here as “␣”) are attached to the end of each word: /// /// ```text /// "Foo␣␣␣bar␣baz" -> ["Foo␣␣␣", "bar␣", "baz"] /// ``` /// /// These words are then put into lines. The interword whitespace is /// preserved, unless the lines are wrapped so that the `"Foo␣␣␣"` /// word falls at the end of a line: /// /// ``` /// use textwrap::wrap; /// /// assert_eq!(wrap("Foo bar baz", 10), vec!["Foo bar", "baz"]); /// assert_eq!(wrap("Foo bar baz", 8), vec!["Foo", "bar baz"]); /// ``` /// /// Notice how the trailing whitespace is removed in both case: in the /// first example, `"bar␣"` becomes `"bar"` and in the second case /// `"Foo␣␣␣"` becomes `"Foo"`. /// /// Leading whitespace is preserved when the following word fits on /// the first line. To understand this, consider how words are found /// in a text with leading spaces: /// /// ```text /// "␣␣foo␣bar" -> ["␣␣", "foo␣", "bar"] /// ``` /// /// When put into lines, the indentation is preserved if `"foo"` fits /// on the first line, otherwise you end up with an empty line: /// /// ``` /// use textwrap::wrap; /// /// assert_eq!(wrap(" foo bar", 8), vec![" foo", "bar"]); /// assert_eq!(wrap(" foo bar", 4), vec!["", "foo", "bar"]); /// ``` pub fn wrap<'a, WrapAlgo, WordSep, WordSplit, Opt>( text: &str, width_or_options: Opt, ) -> Vec<Cow<'_, str>> where WrapAlgo: wrap_algorithms::WrapAlgorithm, WordSep: word_separators::WordSeparator, WordSplit: word_splitters::WordSplitter, Opt: Into<Options<'a, WrapAlgo, WordSep, WordSplit>>, { let options = width_or_options.into(); let initial_width = options .width .saturating_sub(core::display_width(options.initial_indent)); let subsequent_width = options .width .saturating_sub(core::display_width(options.subsequent_indent)); let mut lines = Vec::new(); for line in text.split('\n') { let words = options.word_separator.find_words(line); let split_words = word_splitters::split_words(words, &options.word_splitter); let broken_words = if options.break_words { let mut broken_words = core::break_words(split_words, subsequent_width); if !options.initial_indent.is_empty() { // Without this, the first word will always go into // the first line. However, since we break words based // on the _second_ line width, it can be wrong to // unconditionally put the first word onto the first // line. An empty zero-width word fixed this. broken_words.insert(0, core::Word::from("")); } broken_words } else { split_words.collect::<Vec<_>>() }; let line_widths = [initial_width, subsequent_width]; let wrapped_words = options.wrap_algorithm.wrap(&broken_words, &line_widths); let mut idx = 0; for words in wrapped_words { let last_word = match words.last() { None => { lines.push(Cow::from("")); continue; } Some(word) => word, }; // We assume here that all words are contiguous in `line`. // That is, the sum of their lengths should add up to the // length of `line`. let len = words .iter() .map(|word| word.len() + word.whitespace.len()) .sum::<usize>() - last_word.whitespace.len(); // The result is owned if we have indentation, otherwise // we can simply borrow an empty string. let mut result = if lines.is_empty() && !options.initial_indent.is_empty() { Cow::Owned(options.initial_indent.to_owned()) } else if !lines.is_empty() && !options.subsequent_indent.is_empty() { Cow::Owned(options.subsequent_indent.to_owned()) } else { // We can use an empty string here since string // concatenation for `Cow` preserves a borrowed value // when either side is empty. Cow::from("") }; result += &line[idx..idx + len]; if !last_word.penalty.is_empty() { result.to_mut().push_str(&last_word.penalty); } lines.push(result); // Advance by the length of `result`, plus the length of // `last_word.whitespace` -- even if we had a penalty, we // need to skip over the whitespace. idx += len + last_word.whitespace.len(); } } lines } /// Wrap text into columns with a given total width. /// /// The `left_gap`, `middle_gap` and `right_gap` arguments specify the /// strings to insert before, between, and after the columns. The /// total width of all columns and all gaps is specified using the /// `total_width_or_options` argument. This argument can simply be an /// integer if you want to use default settings when wrapping, or it /// can be a [`Options`] value if you want to customize the wrapping. /// /// If the columns are narrow, it is recommended to set /// [`Options::break_words`] to `true` to prevent words from /// protruding into the margins. /// /// The per-column width is computed like this: /// /// ``` /// # let (left_gap, middle_gap, right_gap) = ("", "", ""); /// # let columns = 2; /// # let options = textwrap::Options::new(80); /// let inner_width = options.width /// - textwrap::core::display_width(left_gap) /// - textwrap::core::display_width(right_gap) /// - textwrap::core::display_width(middle_gap) * (columns - 1); /// let column_width = inner_width / columns; /// ``` /// /// The `text` is wrapped using [`wrap`] and the given `options` /// argument, but the width is overwritten to the computed /// `column_width`. /// /// # Panics /// /// Panics if `columns` is zero. /// /// # Examples /// /// ``` /// use textwrap::wrap_columns; /// /// let text = "\ /// This is an example text, which is wrapped into three columns. \ /// Notice how the final column can be shorter than the others."; /// /// #[cfg(feature = "smawk")] /// assert_eq!(wrap_columns(text, 3, 50, "| ", " | ", " |"), /// vec!["| This is | into three | column can be |", /// "| an example | columns. | shorter than |", /// "| text, which | Notice how | the others. |", /// "| is wrapped | the final | |"]); /// /// // Without the `smawk` feature, the middle column is a little more uneven: /// #[cfg(not(feature = "smawk"))] /// assert_eq!(wrap_columns(text, 3, 50, "| ", " | ", " |"), /// vec!["| This is an | three | column can be |", /// "| example text, | columns. | shorter than |", /// "| which is | Notice how | the others. |", /// "| wrapped into | the final | |"]); pub fn wrap_columns<'a, WrapAlgo, WordSep, WordSplit, Opt>( text: &str, columns: usize, total_width_or_options: Opt, left_gap: &str, middle_gap: &str, right_gap: &str, ) -> Vec<String> where WrapAlgo: wrap_algorithms::WrapAlgorithm, WordSep: word_separators::WordSeparator, WordSplit: word_splitters::WordSplitter, Opt: Into<Options<'a, WrapAlgo, WordSep, WordSplit>>, { assert!(columns > 0); let mut options = total_width_or_options.into(); let inner_width = options .width .saturating_sub(core::display_width(left_gap)) .saturating_sub(core::display_width(right_gap)) .saturating_sub(core::display_width(middle_gap) * (columns - 1)); let column_width = std::cmp::max(inner_width / columns, 1); options.width = column_width; let last_column_padding = " ".repeat(inner_width % column_width); let wrapped_lines = wrap(text, options); let lines_per_column = wrapped_lines.len() / columns + usize::from(wrapped_lines.len() % columns > 0); let mut lines = Vec::new(); for line_no in 0..lines_per_column { let mut line = String::from(left_gap); for column_no in 0..columns { match wrapped_lines.get(line_no + column_no * lines_per_column) { Some(column_line) => { line.push_str(&column_line); line.push_str(&" ".repeat(column_width - core::display_width(&column_line))); } None => { line.push_str(&" ".repeat(column_width)); } } if column_no == columns - 1 { line.push_str(&last_column_padding); } else { line.push_str(middle_gap); } } line.push_str(right_gap); lines.push(line); } lines } /// Fill `text` in-place without reallocating the input string. /// /// This function works by modifying the input string: some `' '` /// characters will be replaced by `'\n'` characters. The rest of the /// text remains untouched. /// /// Since we can only replace existing whitespace in the input with /// `'\n'`, we cannot do hyphenation nor can we split words longer /// than the line width. We also need to use `AsciiSpace` as the word /// separator since we need `' '` characters between words in order to /// replace some of them with a `'\n'`. Indentation is also ruled out. /// In other words, `fill_inplace(width)` behaves as if you had called /// [`fill`] with these options: /// /// ``` /// # use textwrap::{core, Options}; /// # use textwrap::{word_separators, word_splitters, wrap_algorithms}; /// # let width = 80; /// Options { /// width: width, /// initial_indent: "", /// subsequent_indent: "", /// break_words: false, /// word_separator: word_separators::AsciiSpace, /// wrap_algorithm: wrap_algorithms::FirstFit, /// word_splitter: word_splitters::NoHyphenation, /// }; /// ``` /// /// The wrap algorithm is [`wrap_algorithms::FirstFit`] since this /// is the fastest algorithm — and the main reason to use /// `fill_inplace` is to get the string broken into newlines as fast /// as possible. /// /// A last difference is that (unlike [`fill`]) `fill_inplace` can /// leave trailing whitespace on lines. This is because we wrap by /// inserting a `'\n'` at the final whitespace in the input string: /// /// ``` /// let mut text = String::from("Hello World!"); /// textwrap::fill_inplace(&mut text, 10); /// assert_eq!(text, "Hello \nWorld!"); /// ``` /// /// If we didn't do this, the word `World!` would end up being /// indented. You can avoid this if you make sure that your input text /// has no double spaces. /// /// # Performance /// /// In benchmarks, `fill_inplace` is about twice as fast as [`fill`]. /// Please see the [`linear` /// benchmark](https://github.com/mgeisler/textwrap/blob/master/benches/linear.rs) /// for details. pub fn fill_inplace(text: &mut String, width: usize) { use word_separators::WordSeparator; let mut indices = Vec::new(); let mut offset = 0; for line in text.split('\n') { let words = word_separators::AsciiSpace .find_words(line) .collect::<Vec<_>>(); let wrapped_words = wrap_algorithms::wrap_first_fit(&words, &[width]); let mut line_offset = offset; for words in &wrapped_words[..wrapped_words.len() - 1] { let line_len = words .iter() .map(|word| word.len() + word.whitespace.len()) .sum::<usize>(); line_offset += line_len; // We've advanced past all ' ' characters -- want to move // one ' ' backwards and insert our '\n' there. indices.push(line_offset - 1); } // Advance past entire line, plus the '\n' which was removed // by the split call above. offset += line.len() + 1; } let mut bytes = std::mem::take(text).into_bytes(); for idx in indices { bytes[idx] = b'\n'; } *text = String::from_utf8(bytes).unwrap(); } #[cfg(test)] mod tests { use super::*; use crate::word_splitters::WordSplitter; use crate::{word_splitters, wrap_algorithms}; #[cfg(feature = "hyphenation")] use hyphenation::{Language, Load, Standard}; #[test] fn options_agree_with_usize() { let opt_usize = Options::from(42_usize); let opt_options = Options::new(42); assert_eq!(opt_usize.width, opt_options.width); assert_eq!(opt_usize.initial_indent, opt_options.initial_indent); assert_eq!(opt_usize.subsequent_indent, opt_options.subsequent_indent); assert_eq!(opt_usize.break_words, opt_options.break_words); assert_eq!( opt_usize.word_splitter.split_points("hello-world"), opt_options.word_splitter.split_points("hello-world") ); } #[test] fn no_wrap() { assert_eq!(wrap("foo", 10), vec!["foo"]); } #[test] fn wrap_simple() { assert_eq!(wrap("foo bar baz", 5), vec!["foo", "bar", "baz"]); } #[test] fn to_be_or_not() { assert_eq!( wrap( "To be, or not to be, that is the question.", Options::new(10).wrap_algorithm(wrap_algorithms::FirstFit) ), vec!["To be, or", "not to be,", "that is", "the", "question."] ); } #[test] fn multiple_words_on_first_line() { assert_eq!(wrap("foo bar baz", 10), vec!["foo bar", "baz"]); } #[test] fn long_word() { assert_eq!(wrap("foo", 0), vec!["f", "o", "o"]); } #[test] fn long_words() { assert_eq!(wrap("foo bar", 0), vec!["f", "o", "o", "b", "a", "r"]); } #[test] fn max_width() { assert_eq!(wrap("foo bar", usize::max_value()), vec!["foo bar"]); } #[test] fn leading_whitespace() { assert_eq!(wrap(" foo bar", 6), vec![" foo", "bar"]); } #[test] fn leading_whitespace_empty_first_line() { // If there is no space for the first word, the first line // will be empty. This is because the string is split into // words like [" ", "foobar ", "baz"], which puts "foobar " on // the second line. We never output trailing whitespace assert_eq!(wrap(" foobar baz", 6), vec!["", "foobar", "baz"]); } #[test] fn trailing_whitespace() { // Whitespace is only significant inside a line. After a line // gets too long and is broken, the first word starts in // column zero and is not indented. assert_eq!(wrap("foo bar baz ", 5), vec!["foo", "bar", "baz"]); } #[test] fn issue_99() { // We did not reset the in_whitespace flag correctly and did // not handle single-character words after a line break. assert_eq!( wrap("aaabbbccc x yyyzzzwww", 9), vec!["aaabbbccc", "x", "yyyzzzwww"] ); } #[test] fn issue_129() { // The dash is an em-dash which takes up four bytes. We used // to panic since we tried to index into the character. let options = Options::new(1).word_separator(word_separators::AsciiSpace); assert_eq!(wrap("x – x", options), vec!["x", "–", "x"]); } #[test] #[cfg(feature = "unicode-width")] fn wide_character_handling() { assert_eq!(wrap("Hello, World!", 15), vec!["Hello, World!"]); assert_eq!( wrap( "Hello, World!", Options::new(15).word_separator(word_separators::AsciiSpace) ), vec!["Hello,", "World!"] ); // Wide characters are allowed to break if the // unicode-linebreak feature is enabled. #[cfg(feature = "unicode-linebreak")] assert_eq!( wrap( "Hello, World!", Options::new(15).word_separator(word_separators::UnicodeBreakProperties) ), vec!["Hello, W", "orld!"] ); } #[test] fn empty_line_is_indented() { // Previously, indentation was not applied to empty lines. // However, this is somewhat inconsistent and undesirable if // the indentation is something like a border ("| ") which you // want to apply to all lines, empty or not. let options = Options::new(10).initial_indent("!!!"); assert_eq!(fill("", &options), "!!!"); } #[test] fn indent_single_line() { let options = Options::new(10).initial_indent(">>>"); // No trailing space assert_eq!(fill("foo", &options), ">>>foo"); } #[test] #[cfg(feature = "unicode-width")] fn indent_first_emoji() { let options = Options::new(10).initial_indent("👉👉"); assert_eq!( wrap("x x x x x x x x x x x x x", &options), vec!["👉👉x x x", "x x x x x", "x x x x x"] ); } #[test] fn indent_multiple_lines() { let options = Options::new(6).initial_indent("* ").subsequent_indent(" "); assert_eq!( wrap("foo bar baz", &options), vec!["* foo", " bar", " baz"] ); } #[test] fn indent_break_words() { let options = Options::new(5).initial_indent("* ").subsequent_indent(" "); assert_eq!(wrap("foobarbaz", &options), vec!["* foo", " bar", " baz"]); } #[test] fn initial_indent_break_words() { // This is a corner-case showing how the long word is broken // according to the width of the subsequent lines. The first // fragment of the word no longer fits on the first line, // which ends up being pure indentation. let options = Options::new(5).initial_indent("-->"); assert_eq!(wrap("foobarbaz", &options), vec!["-->", "fooba", "rbaz"]); } #[test] fn hyphens() { assert_eq!(wrap("foo-bar", 5), vec!["foo-", "bar"]); } #[test] fn trailing_hyphen() { let options = Options::new(5).break_words(false); assert_eq!(wrap("foobar-", &options), vec!["foobar-"]); } #[test] fn multiple_hyphens() { assert_eq!(wrap("foo-bar-baz", 5), vec!["foo-", "bar-", "baz"]); } #[test] fn hyphens_flag() { let options = Options::new(5).break_words(false); assert_eq!( wrap("The --foo-bar flag.", &options), vec!["The", "--foo-", "bar", "flag."] ); } #[test] fn repeated_hyphens() { let options = Options::new(4).break_words(false); assert_eq!(wrap("foo--bar", &options), vec!["foo--bar"]); } #[test] fn hyphens_alphanumeric() { assert_eq!(wrap("Na2-CH4", 5), vec!["Na2-", "CH4"]); } #[test] fn hyphens_non_alphanumeric() { let options = Options::new(5).break_words(false); assert_eq!(wrap("foo(-)bar", &options), vec!["foo(-)bar"]); } #[test] fn multiple_splits() { assert_eq!(wrap("foo-bar-baz", 9), vec!["foo-bar-", "baz"]); } #[test] fn forced_split() { let options = Options::new(5).break_words(false); assert_eq!(wrap("foobar-baz", &options), vec!["foobar-", "baz"]); } #[test] fn multiple_unbroken_words_issue_193() { let options = Options::new(3).break_words(false); assert_eq!( wrap("small large tiny", &options), vec!["small", "large", "tiny"] ); assert_eq!( wrap("small large tiny", &options), vec!["small", "large", "tiny"] ); } #[test] fn very_narrow_lines_issue_193() { let options = Options::new(1).break_words(false); assert_eq!(wrap("fooo x y", &options), vec!["fooo", "x", "y"]); assert_eq!(wrap("fooo x y", &options), vec!["fooo", "x", "y"]); } #[test] fn simple_hyphens_static() { let options = Options::new(8).word_splitter(word_splitters::HyphenSplitter); assert_eq!(wrap("foo bar-baz", &options), vec!["foo bar-", "baz"]); } #[test] fn simple_hyphens_dynamic() { let options: Options<_, _> = Options::new(8).word_splitter(Box::new(word_splitters::HyphenSplitter)); assert_eq!(wrap("foo bar-baz", &options), vec!["foo bar-", "baz"]); } #[test] fn no_hyphenation_static() { let options = Options::new(8).word_splitter(word_splitters::NoHyphenation); assert_eq!(wrap("foo bar-baz", &options), vec!["foo", "bar-baz"]); } #[test] fn no_hyphenation_dynamic() { let options: Options<_, _> = Options::new(8).word_splitter(Box::new(word_splitters::NoHyphenation)); assert_eq!(wrap("foo bar-baz", &options), vec!["foo", "bar-baz"]); } #[test] #[cfg(feature = "hyphenation")] fn auto_hyphenation_double_hyphenation_static() { let dictionary = Standard::from_embedded(Language::EnglishUS).unwrap(); let options = Options::new(10); assert_eq!( wrap("Internationalization", &options), vec!["Internatio", "nalization"] ); let options = Options::new(10).word_splitter(dictionary); assert_eq!( wrap("Internationalization", &options), vec!["Interna-", "tionaliza-", "tion"] ); } #[test] #[cfg(feature = "hyphenation")] fn auto_hyphenation_double_hyphenation_dynamic() { let dictionary = Standard::from_embedded(Language::EnglishUS).unwrap(); let mut options: Options<_, _, Box<dyn word_splitters::WordSplitter>> = Options::new(10).word_splitter(Box::new(word_splitters::HyphenSplitter)); assert_eq!( wrap("Internationalization", &options), vec!["Internatio", "nalization"] ); options = Options::new(10).word_splitter(Box::new(dictionary)); assert_eq!( wrap("Internationalization", &options), vec!["Interna-", "tionaliza-", "tion"] ); } #[test] #[cfg(feature = "hyphenation")] fn auto_hyphenation_issue_158() { let dictionary = Standard::from_embedded(Language::EnglishUS).unwrap(); let options = Options::new(10); assert_eq!( wrap("participation is the key to success", &options), vec!["participat", "ion is", "the key to", "success"] ); let options = Options::new(10).word_splitter(dictionary); assert_eq!( wrap("participation is the key to success", &options), vec!["partici-", "pation is", "the key to", "success"] ); } #[test] #[cfg(feature = "hyphenation")] fn split_len_hyphenation() { // Test that hyphenation takes the width of the wihtespace // into account. let dictionary = Standard::from_embedded(Language::EnglishUS).unwrap(); let options = Options::new(15).word_splitter(dictionary); assert_eq!( wrap("garbage collection", &options), vec!["garbage col-", "lection"] ); } #[test] #[cfg(feature = "hyphenation")] fn borrowed_lines() { // Lines that end with an extra hyphen are owned, the final // line is borrowed. use std::borrow::Cow::{Borrowed, Owned}; let dictionary = Standard::from_embedded(Language::EnglishUS).unwrap(); let options = Options::new(10).word_splitter(dictionary); let lines = wrap("Internationalization", &options); if let Borrowed(s) = lines[0] { assert!(false, "should not have been borrowed: {:?}", s); } if let Borrowed(s) = lines[1] { assert!(false, "should not have been borrowed: {:?}", s); } if let Owned(ref s) = lines[2] { assert!(false, "should not have been owned: {:?}", s); } } #[test] #[cfg(feature = "hyphenation")] fn auto_hyphenation_with_hyphen() { let dictionary = Standard::from_embedded(Language::EnglishUS).unwrap(); let options = Options::new(8).break_words(false); assert_eq!( wrap("over-caffinated", &options), vec!["over-", "caffinated"] ); let options = options.word_splitter(dictionary); assert_eq!( wrap("over-caffinated", &options), vec!["over-", "caffi-", "nated"] ); } #[test] fn break_words() { assert_eq!(wrap("foobarbaz", 3), vec!["foo", "bar", "baz"]); } #[test] fn break_words_wide_characters() { // Even the poor man's version of `ch_width` counts these // characters as wide. let options = Options::new(5).word_separator(word_separators::AsciiSpace); assert_eq!(wrap("Hello", options), vec!["He", "ll", "o"]); } #[test] fn break_words_zero_width() { assert_eq!(wrap("foobar", 0), vec!["f", "o", "o", "b", "a", "r"]); } #[test] fn break_long_first_word() { assert_eq!(wrap("testx y", 4), vec!["test", "x y"]); } #[test] fn break_words_line_breaks() { assert_eq!(fill("ab\ncdefghijkl", 5), "ab\ncdefg\nhijkl"); assert_eq!(fill("abcdefgh\nijkl", 5), "abcde\nfgh\nijkl"); } #[test] fn break_words_empty_lines() { assert_eq!( fill("foo\nbar", &Options::new(2).break_words(false)), "foo\nbar" ); } #[test] fn preserve_line_breaks() { assert_eq!(fill("", 80), ""); assert_eq!(fill("\n", 80), "\n"); assert_eq!(fill("\n\n\n", 80), "\n\n\n"); assert_eq!(fill("test\n", 80), "test\n"); assert_eq!(fill("test\n\na\n\n", 80), "test\n\na\n\n"); assert_eq!( fill( "1 3 5 7\n1 3 5 7", Options::new(7).wrap_algorithm(wrap_algorithms::FirstFit) ), "1 3 5 7\n1 3 5 7" ); assert_eq!( fill( "1 3 5 7\n1 3 5 7", Options::new(5).wrap_algorithm(wrap_algorithms::FirstFit) ), "1 3 5\n7\n1 3 5\n7" ); } #[test] fn preserve_line_breaks_with_whitespace() { assert_eq!(fill(" ", 80), ""); assert_eq!(fill(" \n ", 80), "\n"); assert_eq!(fill(" \n \n \n ", 80), "\n\n\n"); } #[test] fn non_breaking_space() { let options = Options::new(5).break_words(false); assert_eq!(fill("foo bar baz", &options), "foo bar baz"); } #[test] fn non_breaking_hyphen() { let options = Options::new(5).break_words(false); assert_eq!(fill("foo‑bar‑baz", &options), "foo‑bar‑baz"); } #[test] fn fill_simple() { assert_eq!(fill("foo bar baz", 10), "foo bar\nbaz"); } #[test] fn fill_colored_text() { // The words are much longer than 6 bytes, but they remain // intact after filling the text. let green_hello = "\u{1b}[0m\u{1b}[32mHello\u{1b}[0m"; let blue_world = "\u{1b}[0m\u{1b}[34mWorld!\u{1b}[0m"; assert_eq!( fill(&(String::from(green_hello) + " " + &blue_world), 6), String::from(green_hello) + "\n" + &blue_world ); } #[test] fn fill_unicode_boundary() { // https://github.com/mgeisler/textwrap/issues/390 fill("\u{1b}!Ͽ", 10); } #[test] #[cfg(not(feature = "smawk"))] #[cfg(not(feature = "unicode-linebreak"))] fn cloning_works() { static OPT: Options< wrap_algorithms::FirstFit, word_separators::AsciiSpace, word_splitters::HyphenSplitter, > = Options::with_word_splitter(80, word_splitters::HyphenSplitter); #[allow(clippy::clone_on_copy)] let opt = OPT.clone(); assert_eq!(opt.width, 80); } #[test] fn fill_inplace_empty() { let mut text = String::from(""); fill_inplace(&mut text, 80); assert_eq!(text, ""); } #[test] fn fill_inplace_simple() { let mut text = String::from("foo bar baz"); fill_inplace(&mut text, 10); assert_eq!(text, "foo bar\nbaz"); } #[test] fn fill_inplace_multiple_lines() { let mut text = String::from("Some text to wrap over multiple lines"); fill_inplace(&mut text, 12); assert_eq!(text, "Some text to\nwrap over\nmultiple\nlines"); } #[test] fn fill_inplace_long_word() { let mut text = String::from("Internationalization is hard"); fill_inplace(&mut text, 10); assert_eq!(text, "Internationalization\nis hard"); } #[test] fn fill_inplace_no_hyphen_splitting() { let mut text = String::from("A well-chosen example"); fill_inplace(&mut text, 10); assert_eq!(text, "A\nwell-chosen\nexample"); } #[test] fn fill_inplace_newlines() { let mut text = String::from("foo bar\n\nbaz\n\n\n"); fill_inplace(&mut text, 10); assert_eq!(text, "foo bar\n\nbaz\n\n\n"); } #[test] fn fill_inplace_newlines_reset_line_width() { let mut text = String::from("1 3 5\n1 3 5 7 9\n1 3 5 7 9 1 3"); fill_inplace(&mut text, 10); assert_eq!(text, "1 3 5\n1 3 5 7 9\n1 3 5 7 9\n1 3"); } #[test] fn fill_inplace_leading_whitespace() { let mut text = String::from(" foo bar baz"); fill_inplace(&mut text, 10); assert_eq!(text, " foo bar\nbaz"); } #[test] fn fill_inplace_trailing_whitespace() { let mut text = String::from("foo bar baz "); fill_inplace(&mut text, 10); assert_eq!(text, "foo bar\nbaz "); } #[test] fn fill_inplace_interior_whitespace() { // To avoid an unwanted indentation of "baz", it is important // to replace the final ' ' with '\n'. let mut text = String::from("foo bar baz"); fill_inplace(&mut text, 10); assert_eq!(text, "foo bar \nbaz"); } #[test] fn unfill_simple() { let (text, options) = unfill("foo\nbar"); assert_eq!(text, "foo bar"); assert_eq!(options.width, 3); } #[test] fn unfill_trailing_newlines() { let (text, options) = unfill("foo\nbar\n\n\n"); assert_eq!(text, "foo bar\n\n\n"); assert_eq!(options.width, 3); } #[test] fn unfill_initial_indent() { let (text, options) = unfill(" foo\nbar\nbaz"); assert_eq!(text, "foo bar baz"); assert_eq!(options.width, 5); assert_eq!(options.initial_indent, " "); } #[test] fn unfill_differing_indents() { let (text, options) = unfill(" foo\n bar\n baz"); assert_eq!(text, "foo bar baz"); assert_eq!(options.width, 7); assert_eq!(options.initial_indent, " "); assert_eq!(options.subsequent_indent, " "); } #[test] fn unfill_list_item() { let (text, options) = unfill("* foo\n bar\n baz"); assert_eq!(text, "foo bar baz"); assert_eq!(options.width, 5); assert_eq!(options.initial_indent, "* "); assert_eq!(options.subsequent_indent, " "); } #[test] fn unfill_multiple_char_prefix() { let (text, options) = unfill(" // foo bar\n // baz\n // quux"); assert_eq!(text, "foo bar baz quux"); assert_eq!(options.width, 14); assert_eq!(options.initial_indent, " // "); assert_eq!(options.subsequent_indent, " // "); } #[test] fn unfill_block_quote() { let (text, options) = unfill("> foo\n> bar\n> baz"); assert_eq!(text, "foo bar baz"); assert_eq!(options.width, 5); assert_eq!(options.initial_indent, "> "); assert_eq!(options.subsequent_indent, "> "); } #[test] fn unfill_whitespace() { assert_eq!(unfill("foo bar").0, "foo bar"); } #[test] fn trait_object_vec() { // Create a vector of Options containing trait-objects. let mut vector: Vec< Options< _, Box<dyn word_separators::WordSeparator>, Box<dyn word_splitters::WordSplitter>, >, > = Vec::new(); // Expected result from each options let mut results = Vec::new(); let opt_full_type: Options< _, Box<dyn word_separators::WordSeparator>, Box<dyn word_splitters::WordSplitter>, > = Options::new(10) .word_splitter(Box::new(word_splitters::HyphenSplitter) as Box<dyn word_splitters::WordSplitter>) .word_separator(Box::new(word_separators::AsciiSpace) as Box<dyn word_separators::WordSeparator>); vector.push(opt_full_type); results.push(vec!["over-", "caffinated"]); // Actually: Options<Box<AsciiSpace>, Box<dyn word_splitters::WordSplitter>> let opt_abbreviated_type = Options::new(10) .break_words(false) .word_splitter(Box::new(word_splitters::NoHyphenation) as Box<dyn word_splitters::WordSplitter>) .word_separator(Box::new(word_separators::AsciiSpace) as Box<dyn word_separators::WordSeparator>); vector.push(opt_abbreviated_type); results.push(vec!["over-caffinated"]); #[cfg(feature = "hyphenation")] { let dictionary = Standard::from_embedded(Language::EnglishUS).unwrap(); let opt_hyp = Options::new(8) .word_splitter(Box::new(dictionary) as Box<dyn word_splitters::WordSplitter>) .word_separator(Box::new(word_separators::AsciiSpace) as Box<dyn word_separators::WordSeparator>); vector.push(opt_hyp); results.push(vec!["over-", "caffi-", "nated"]); } // Test each entry for (opt, expected) in vector.into_iter().zip(results) { assert_eq!(wrap("over-caffinated", opt), expected); } } #[test] fn wrap_columns_empty_text() { assert_eq!(wrap_columns("", 1, 10, "| ", "", " |"), vec!["| |"]); } #[test] fn wrap_columns_single_column() { assert_eq!( wrap_columns("Foo", 3, 30, "| ", " | ", " |"), vec!["| Foo | | |"] ); } #[test] fn wrap_columns_uneven_columns() { // The gaps take up a total of 5 columns, so the columns are // (21 - 5)/4 = 4 columns wide: assert_eq!( wrap_columns("Foo Bar Baz Quux", 4, 21, "|", "|", "|"), vec!["|Foo |Bar |Baz |Quux|"] ); // As the total width increases, the last column absorbs the // excess width: assert_eq!( wrap_columns("Foo Bar Baz Quux", 4, 24, "|", "|", "|"), vec!["|Foo |Bar |Baz |Quux |"] ); // Finally, when the width is 25, the columns can be resized // to a width of (25 - 5)/4 = 5 columns: assert_eq!( wrap_columns("Foo Bar Baz Quux", 4, 25, "|", "|", "|"), vec!["|Foo |Bar |Baz |Quux |"] ); } #[test] #[cfg(feature = "unicode-width")] fn wrap_columns_with_emojis() { assert_eq!( wrap_columns( "Words and a few emojis 😍 wrapped in ⓶ columns", 2, 30, "✨ ", " ⚽ ", " 👀" ), vec![ "✨ Words ⚽ wrapped in 👀", "✨ and a few ⚽ ⓶ columns 👀", "✨ emojis 😍 ⚽ 👀" ] ); } #[test] fn wrap_columns_big_gaps() { // The column width shrinks to 1 because the gaps take up all // the space. assert_eq!( wrap_columns("xyz", 2, 10, "----> ", " !!! ", " <----"), vec![ "----> x !!! z <----", // "----> y !!! <----" ] ); } #[test] #[should_panic] fn wrap_columns_panic_with_zero_columns() { wrap_columns("", 0, 10, "", "", ""); } }