mirror of
https://github.com/nushell/nushell.git
synced 2025-07-01 07:00:37 +02:00
Compare commits
866 Commits
Author | SHA1 | Date | |
---|---|---|---|
c74cff213e | |||
2ea5819b02 | |||
1115190a49 | |||
7132c5ad02 | |||
1920ece759 | |||
6f59abaf43 | |||
5f7425a7b4 | |||
1ab9ec3ebc | |||
f2095ed0cc | |||
7e26b4fcc2 | |||
ad95e4cc27 | |||
ee5a18167c | |||
75c9e3e5df | |||
77f10eb270 | |||
42bb42a2e1 | |||
f597380112 | |||
b92b4120dc | |||
de5ad5de19 | |||
64695cd67c | |||
21b3eeed99 | |||
a86a7e6c29 | |||
15421dc45e | |||
9522052063 | |||
ba880277bf | |||
40241e9be6 | |||
34f3da7150 | |||
534287ed65 | |||
913c2b8d1c | |||
aeffa188f0 | |||
543a25599c | |||
7ad0e5541e | |||
c1cc1c82cc | |||
b0b4c3dffd | |||
f3de373c59 | |||
df1fecd2cb | |||
9620e27e4f | |||
072ecec8ca | |||
748d82cec1 | |||
5e5d1ea81b | |||
bb570e42be | |||
3a050864df | |||
8cfa96b4c0 | |||
6f384da57e | |||
f8e63328d8 | |||
5d98a727ca | |||
109f629cb6 | |||
ff6a67d293 | |||
03ae01f11e | |||
697f3c03f1 | |||
1cecb37628 | |||
89436e978b | |||
cd0a52cf00 | |||
c6043eb500 | |||
729373aba0 | |||
f59a6990dc | |||
0f9094ead2 | |||
70f7db14d4 | |||
0cba269d80 | |||
ec2593efb8 | |||
c2283596ac | |||
c9399f5142 | |||
c9c93f5b4d | |||
2264682443 | |||
247c33b6d6 | |||
a6da8ce769 | |||
020e121391 | |||
7d5bd0d6be | |||
87717b9ddd | |||
3c6fac059e | |||
9092fc1b12 | |||
5b557a888e | |||
398b756aee | |||
533c1a89af | |||
84742275a1 | |||
92d968b8c8 | |||
50102bf69b | |||
156232fe08 | |||
0a3761a594 | |||
44dc890124 | |||
6ead98effb | |||
d5f76c02f0 | |||
fd77114d82 | |||
78f52e8b66 | |||
5b01685fc3 | |||
c2b684464f | |||
fd56768fdc | |||
070afa4528 | |||
23fec8eb0d | |||
c9841f67e9 | |||
76482cc1b2 | |||
d43f4253e8 | |||
0fba08808c | |||
b3a52a247f | |||
4763801cb2 | |||
ecb3b3a364 | |||
3e5f81ae14 | |||
ca05553fc6 | |||
fa5d7babb9 | |||
94b27267fd | |||
d1390ac95b | |||
d717e8faeb | |||
a95a4505ef | |||
b03f1efac4 | |||
5d5088b5d5 | |||
c1c73811d5 | |||
51bf8d9f6a | |||
858c93d2e5 | |||
31146a7591 | |||
05d7d6d6ad | |||
fb3350ebc3 | |||
2ffe30ecf0 | |||
f8dc3421b0 | |||
c1a30ac60f | |||
fc06afd051 | |||
c9aa6ba0f3 | |||
f8c82588b6 | |||
67eec92e76 | |||
b227eea668 | |||
58d002d469 | |||
5d283755e3 | |||
35e8db160d | |||
7d8df4ba9e | |||
f36c055c50 | |||
76bdda1178 | |||
5c07e82fc0 | |||
6ea5bdcf47 | |||
15c7e1b725 | |||
e5d9f405db | |||
80881c75f9 | |||
250ee62e16 | |||
54d73748e4 | |||
d08e254d16 | |||
7f771babb7 | |||
b9b9eaaca5 | |||
0303d709e6 | |||
0e1322e6d6 | |||
e290fa0e68 | |||
112306aab5 | |||
98952082ae | |||
8386bc0919 | |||
182b0ab4fb | |||
fa83458a6d | |||
54398f9546 | |||
077d1c8125 | |||
1ff8c2d81d | |||
d77f1753c2 | |||
85c6047b71 | |||
d37893cca0 | |||
95ac436d26 | |||
c2a46070aa | |||
57808ca7cc | |||
6cfe35eb7e | |||
b2734db015 | |||
823e578c46 | |||
95a745e622 | |||
776df7cd93 | |||
d5677625a7 | |||
64288b4350 | |||
a42fd3611a | |||
e36f69bf3c | |||
5ad7b8f029 | |||
ffb80b8873 | |||
177e800a07 | |||
a7e8970383 | |||
a324a50bb7 | |||
0578cf85ac | |||
1b54dd5418 | |||
8ad5d8bb6a | |||
869b01205c | |||
f5b2f5a9ee | |||
adfa4d00c0 | |||
12effd9b4e | |||
c26fca7419 | |||
da59dfe7d2 | |||
08715e6308 | |||
07d7899a97 | |||
494a5a5286 | |||
f41c93b2d3 | |||
5063e01c12 | |||
d1137cc700 | |||
3966c0a9fd | |||
dbdb1f6600 | |||
84cdc0d521 | |||
ab59dab129 | |||
e0c8a3d14c | |||
e93e51d672 | |||
4205edbc70 | |||
80bee40807 | |||
461837773b | |||
52d4259f58 | |||
274a8366c6 | |||
a1dfc35968 | |||
5886a74ccc | |||
4367aa9f58 | |||
e9c298713e | |||
c110ddff66 | |||
a806717f35 | |||
2b5f1ee5b3 | |||
77a1c3c7b2 | |||
82b3ae826f | |||
1b3092ae7c | |||
942ff7df4d | |||
51abe4c262 | |||
e8e0526f57 | |||
0b25385109 | |||
415b1273b4 | |||
6bee80dcd7 | |||
588a078872 | |||
93096a07aa | |||
523d0bca16 | |||
fe92051bb3 | |||
ee648ecb7d | |||
91920373b5 | |||
33a7bc405f | |||
cd75640a90 | |||
0f600bc3f5 | |||
aed4b626b8 | |||
92503e6571 | |||
44c0db46e1 | |||
1fd3bc1ba6 | |||
59ea28cf06 | |||
435abadd8a | |||
86cd387439 | |||
edbf3aaccb | |||
b03ef56bcb | |||
55316a9f27 | |||
d3ec3dc66b | |||
7ebae0b5f7 | |||
f45aed257f | |||
60da7abbc7 | |||
7a3cbf43e8 | |||
45b02ce2ab | |||
c039e4b3d0 | |||
9b202d560d | |||
1874082a2c | |||
1359b26da2 | |||
b9bc527d27 | |||
51cdd9fbb2 | |||
d838871063 | |||
4d16d92847 | |||
fc01701a41 | |||
51d5d0eac8 | |||
81d00f71a9 | |||
77fbf3e2d2 | |||
f565661f42 | |||
1a864ea6f4 | |||
c1738620e3 | |||
56e35fc3f9 | |||
29591c97a7 | |||
697dee6750 | |||
0ca8fcf58c | |||
a46048f362 | |||
0569a9c92e | |||
c1ca10ffd1 | |||
15c22db8f4 | |||
1c52b112c8 | |||
275dba82d5 | |||
cf7040a215 | |||
72cb4b6032 | |||
d4cbab454e | |||
3645178ff1 | |||
005180f269 | |||
72f7b9b7cc | |||
3f61ca19f0 | |||
d8c59eddb3 | |||
ac43372618 | |||
dccb2b48f3 | |||
2e68e6ddbf | |||
3dfe1a4f0e | |||
c87bac04c0 | |||
4b301710d3 | |||
7d67ca3652 | |||
01d8961eb7 | |||
7ac5a01e2f | |||
e2fb0e5b82 | |||
a11e41332c | |||
f3656f7822 | |||
38f4ab0bc9 | |||
f35741d50e | |||
d93315d8f5 | |||
8429aec57f | |||
f043a8a8ff | |||
c6016d7659 | |||
78b4472b32 | |||
cb754befe9 | |||
0588a4fc19 | |||
ff3a0a0de3 | |||
c799f77577 | |||
d3182a6737 | |||
b5e09b8a30 | |||
05efd735b9 | |||
5e0499fcf9 | |||
74d3f3c1d6 | |||
de6edf18d9 | |||
a35ecb4837 | |||
a01ef85bda | |||
6445c4e7de | |||
066c9118ae | |||
52e8b0afb2 | |||
db3f3eaf5a | |||
878f0cf6e1 | |||
7caf27b665 | |||
22b375ccd4 | |||
6a2539534f | |||
f310a9be8c | |||
d0dc6986dd | |||
11480c77be | |||
4fd2b702ee | |||
030e55acbf | |||
c5e1b64b40 | |||
999f7b229f | |||
de1c7bb39f | |||
54bc662e0e | |||
5f2089a15b | |||
adb99938f7 | |||
b907939916 | |||
27e6271402 | |||
0a8f27f6f2 | |||
1662e61ecb | |||
b58819d51e | |||
9692240b4f | |||
d204defb68 | |||
9e7f84afb0 | |||
ed8dee04b6 | |||
4171c654a5 | |||
22ee041002 | |||
7162d4d9aa | |||
9c70c68914 | |||
93b4aa5fcf | |||
71b0e12b57 | |||
09b3dab35d | |||
88a87158c2 | |||
faf84e69b4 | |||
5d8763ed1d | |||
58124e66a4 | |||
25af32d3a8 | |||
5a746c0ed6 | |||
89ccdc2b06 | |||
76ee00e013 | |||
4e5a1ced13 | |||
1f62024a15 | |||
6181ea5fc1 | |||
1751ac12f4 | |||
6cff54ed0d | |||
ec3e4ce120 | |||
f97443aff6 | |||
c925537c48 | |||
c5545c59c6 | |||
55044aa7d6 | |||
4be7004289 | |||
814a5caf9a | |||
81ece18d5e | |||
0ba81f1d51 | |||
c81fa397b6 | |||
8c36e9df44 | |||
1402508416 | |||
dc3c34275d | |||
f77fe04425 | |||
20ac30b6e2 | |||
4007256cfd | |||
b634f1b010 | |||
8a77d1ed92 | |||
69a17fb247 | |||
9f144798d3 | |||
0b651b6372 | |||
ce09186e2e | |||
0c67d742f0 | |||
2ef34a3b4b | |||
2d72f892fe | |||
ee4e0a933b | |||
765b303689 | |||
e427c68731 | |||
ff6c0fcb81 | |||
bcf3537395 | |||
4efccb2b1c | |||
67b5e1bde9 | |||
eb4fd144eb | |||
d51e82f33b | |||
7827b1fb87 | |||
7dbda76fad | |||
0dbd014d8b | |||
d064d187ab | |||
399319476a | |||
4f4e8c984e | |||
129ae0bf3e | |||
471c58448e | |||
a03c1c266c | |||
afdb68dc71 | |||
bc1b2fa5bd | |||
8c507dc984 | |||
4a82ee6c11 | |||
e8da57b05e | |||
f481879ed3 | |||
7c1487e18d | |||
2cc4191ec9 | |||
844cb1213b | |||
783f2a9342 | |||
eb6870cab5 | |||
0d367af24a | |||
5c15a4dd6e | |||
eeade99452 | |||
679879f79b | |||
c4bbc3edad | |||
6c6d215197 | |||
4b9ec03110 | |||
b9ecfeb890 | |||
4dbbacc35d | |||
28ef14399c | |||
20aaaaf90c | |||
7c274ad4d8 | |||
30c331e882 | |||
fa2e6e5d53 | |||
7eaa6d01ab | |||
d34581db4a | |||
85d6529f0d | |||
50039164f1 | |||
c64017de7b | |||
9e445fd4c5 | |||
cc4f8bbd82 | |||
16453b6986 | |||
d6b9153ac5 | |||
80a183dde2 | |||
f2af12af2c | |||
7ad4c679b3 | |||
9a0c6f2e02 | |||
78d0e1d0b8 | |||
dc739f703a | |||
ac7263d957 | |||
9c52b93975 | |||
80220b722b | |||
d1dc610769 | |||
cc767463e6 | |||
8f4ea69c22 | |||
6c026242d4 | |||
4a26719b0c | |||
d2f513da36 | |||
feef612388 | |||
65074ec449 | |||
a19cac2673 | |||
5326e51e4f | |||
b39cca91e5 | |||
57825a5c45 | |||
19cee5fda1 | |||
95a4649cc9 | |||
e96039fb1b | |||
65e2733571 | |||
bc437da5c7 | |||
65c4083ae6 | |||
6a2fd91a01 | |||
b6d31e0e45 | |||
d2c87ad4b4 | |||
a26a01c8d0 | |||
414216edfa | |||
6df001f72d | |||
4880721b73 | |||
1bb953877e | |||
ef7ade59f3 | |||
1c677c9577 | |||
1072bd06c3 | |||
4aa9102523 | |||
0c7a8e3634 | |||
addf8ca942 | |||
7cfd4d2cfa | |||
4ae53d93fb | |||
29e809ad77 | |||
e1c6be0682 | |||
bf40f035f6 | |||
989a14709c | |||
8d8b44342b | |||
7980ad9f7f | |||
af15f794b4 | |||
a6f62e05ae | |||
f8939de14f | |||
01ade02ac1 | |||
39d93b536a | |||
32f67557af | |||
f05eed8e8d | |||
f0a265dbee | |||
bc7736bc99 | |||
19d732f313 | |||
a9a82de5c4 | |||
9074015d1c | |||
2b77544e58 | |||
5ee74b6ab5 | |||
3a04bd9154 | |||
2c176a7f14 | |||
026e18399e | |||
bbf0b45c59 | |||
5bd7300cd5 | |||
ffb5051f6c | |||
ce4ea16c08 | |||
48c94c75fc | |||
73d3708006 | |||
bbea7da669 | |||
7f39609d9a | |||
a14e9e0a2e | |||
3e14dc3eb8 | |||
ba6d8ad261 | |||
2a08865851 | |||
0a3bfe7f73 | |||
451a9c64d3 | |||
88d79c84cd | |||
abcb0877e2 | |||
9e1e2a4320 | |||
d53b0a99d0 | |||
1fb4f9e455 | |||
6e9b6f22c9 | |||
e90b099622 | |||
84c10de864 | |||
d618b60d9e | |||
c761f7f844 | |||
7b89fab327 | |||
eddff46155 | |||
baa50ec9b2 | |||
513186c390 | |||
0c37463bfa | |||
94fc33bbee | |||
ce378a68a6 | |||
fa40740e77 | |||
762fdb98ac | |||
5f795b1aec | |||
6811700b90 | |||
248aca7a44 | |||
17abbdf6e0 | |||
40eca52ed5 | |||
7907dda8f7 | |||
8501024546 | |||
21d30d1e4d | |||
eeaa65c8af | |||
6754b8534e | |||
2ffff959fc | |||
fed4233db4 | |||
2f47263380 | |||
4d5386635e | |||
872eb2c3df | |||
e62a77a885 | |||
9bca63ebef | |||
ae54dc862c | |||
5e951b2be9 | |||
f78d57a703 | |||
f021be623e | |||
b6189879e3 | |||
c7c6445b03 | |||
535aec0648 | |||
664dd291eb | |||
a9216deaa4 | |||
99caad7d60 | |||
7486850357 | |||
bb06661d24 | |||
6a374182a7 | |||
f433b3102f | |||
456e2a8ee3 | |||
1ee3bf784c | |||
54394fe9af | |||
7a728340de | |||
5f1e8a6af8 | |||
e566a073dc | |||
9a4dad6ca1 | |||
eca9f461da | |||
08aaa9494c | |||
352f913c39 | |||
aeeb5dd405 | |||
278bf7ffa9 | |||
b15c824932 | |||
5ad3bfa31b | |||
e5145358eb | |||
3a20fbfe94 | |||
dac32557cd | |||
fedd879b2e | |||
a46c21cffb | |||
6cdfee3573 | |||
af79eb2943 | |||
e9d4730099 | |||
844f541719 | |||
d28f728787 | |||
f35808cb89 | |||
7d6b23ee2f | |||
e68ae4c8d1 | |||
faad6ca355 | |||
c77c1bd297 | |||
5b4b4446b7 | |||
93f20b406e | |||
02318cf3a7 | |||
35fc387505 | |||
fd4ba0443d | |||
b943cbedff | |||
3fd1a26ec0 | |||
3f2c76df28 | |||
7d3312e96e | |||
c59d9dc306 | |||
1f06f8405c | |||
38f454d5ab | |||
487f1a97ea | |||
0f05475e2e | |||
cc805f3f01 | |||
5ac5b90aed | |||
3d73287ea4 | |||
7ebdced256 | |||
ad12018199 | |||
27dcc3ecc3 | |||
e25a795cf6 | |||
16c15e83a3 | |||
cea67cb30b | |||
1e3e034021 | |||
8da27a1a09 | |||
030e749fe7 | |||
a785e64bc9 | |||
d4eeef4bd1 | |||
c8a07d477f | |||
af82eeca72 | |||
3d698b74d8 | |||
d2abb8603a | |||
894e0f7658 | |||
a5087c4966 | |||
ac4ab452d4 | |||
5378727049 | |||
0786ddddbd | |||
7617084ca3 | |||
28941f1a06 | |||
43ceb3edec | |||
bffd8e4dd2 | |||
66023f6243 | |||
10fc32e3ef | |||
3148acd3a4 | |||
e6ce8a89be | |||
74f8081290 | |||
2ae1de2470 | |||
028a327ce8 | |||
318862aad6 | |||
9f4510f2e1 | |||
98c7ab96b6 | |||
13114e972b | |||
4a1b3e26ef | |||
1e3248dfe8 | |||
2aa4cd5cc5 | |||
fb908df17d | |||
cdf09abcc0 | |||
fe2c498a81 | |||
fe7122280d | |||
2e0fb7c1a6 | |||
f33b60c001 | |||
7e48607820 | |||
68a821c84a | |||
c5e59efa4d | |||
ec5b9b9f37 | |||
e88a51e930 | |||
35f8d8548a | |||
7a123d3eb1 | |||
3ed45c7ba8 | |||
8b160f9850 | |||
696b2cda4a | |||
435348aa61 | |||
0a5f41abc2 | |||
2b97bc54c5 | |||
ad49c17eba | |||
f1e88d95c1 | |||
6eac9bfd0f | |||
5d94b16d71 | |||
3bd46fe27a | |||
7b1c7debcb | |||
e77a0a48aa | |||
aa37572ddc | |||
a0cecf7658 | |||
23170ff368 | |||
f9ffd9ae29 | |||
d5fa7b8a55 | |||
f94df58486 | |||
6f5bd62a97 | |||
bb3cc9e153 | |||
202dfdaee2 | |||
0674d4960b | |||
85c2035016 | |||
02be83efbf | |||
b964347895 | |||
56ed1eb807 | |||
570175f95d | |||
839010b89d | |||
7694d09d14 | |||
4accc67843 | |||
c070e2d6f7 | |||
d302d63030 | |||
a2e117f8b0 | |||
b1974fae39 | |||
7248de1167 | |||
a9582e1c62 | |||
bb6335830a | |||
c8f3799c20 | |||
bd3a61a2f7 | |||
077643cadf | |||
fa2d9a8a58 | |||
58f98a4260 | |||
066790552a | |||
dcb1a1996c | |||
6b4d06d8a7 | |||
f615038938 | |||
71b74a284a | |||
2b431f994f | |||
7e096e61d7 | |||
9d7a1097f2 | |||
a98b3124c5 | |||
572698bf3e | |||
7162289d77 | |||
14bf25da14 | |||
a455e2e5eb | |||
840b4b854b | |||
ec4941c8ac | |||
dd86f14a5a | |||
63103580d2 | |||
d25df9c00b | |||
778a00efa1 | |||
fea822792f | |||
f6033ac5af | |||
583ef8674e | |||
94bec72079 | |||
28ed21864d | |||
e16ce1df36 | |||
87abfee268 | |||
ba0f069c31 | |||
154856066f | |||
f91713b714 | |||
f1bf485b2a | |||
955de76116 | |||
bf5bd3ff10 | |||
6ac3351fd1 | |||
b6dafa6e67 | |||
152a541696 | |||
ff8c3aa356 | |||
99ed8e42a3 | |||
6a7a23e3fa | |||
9448225690 | |||
28b99bfaf7 | |||
8403fff345 | |||
aa08e81370 | |||
831d1da256 | |||
3481c7e242 | |||
78e29af423 | |||
3ef5e90b64 | |||
6aa30132aa | |||
5d2ef0faf1 | |||
b2e191f836 | |||
d9230a76f3 | |||
f8d325dbfe | |||
88a890c11f | |||
5a28371b18 | |||
a33b5fe6ce | |||
2080719162 | |||
9db0d6bd34 | |||
d7ebe5fdc3 | |||
4d0b6d8821 | |||
b9acb61d9d | |||
2dcd1c5dbe | |||
2aeb77bd3e | |||
17f8ad7210 | |||
4dbdb1fe54 | |||
79359598db | |||
75180d07de | |||
cdc4fb1011 | |||
7e1b922ea7 | |||
0b1e368cea | |||
3b9a0ac7c6 | |||
a1f989caf9 | |||
c01f2ee0e9 | |||
693cb5c142 | |||
d104efdf68 | |||
bd9d865912 | |||
c62cbcd5f8 | |||
ba4723cc9f | |||
488002f4bc | |||
5115260366 | |||
2d557bce5d | |||
17caa31325 | |||
1ba2269aa9 | |||
36030cab8a | |||
345c00aa1e | |||
cc202e2199 | |||
a5a79a7d95 | |||
656f707a0b | |||
48271d8c3e | |||
60bb984e6e | |||
eeb3b38fba | |||
79d9a0542f | |||
5bfec20244 | |||
57d96c09fa | |||
b693db16cc | |||
3ffafa7196 | |||
53ae03bd63 | |||
ba766de5d1 | |||
8c52b7a23a | |||
2fc9506bc7 | |||
4804e6a151 | |||
786ba3bf91 | |||
e66139e6bb | |||
30904bd095 | |||
99329f14a3 | |||
3c583c9a20 | |||
9a6a3a731e | |||
b2043135ed | |||
545697c0b2 | |||
556852ded4 | |||
ca794f6adb | |||
39b43d1e4b | |||
026335fff0 | |||
7c9edbd9ee | |||
341fa7b196 | |||
bd0032898f | |||
ad11e25fc5 | |||
942c66a9f3 | |||
e10d84b72f | |||
cf36f052c4 | |||
266c9ae9e2 | |||
a3702e1eb7 | |||
92354a817c | |||
79a9751a58 | |||
47979651f3 | |||
667502e8da | |||
fabc0a3f45 | |||
ad125abf6a | |||
8e38596bc9 | |||
440a0e960a | |||
85d47c299e | |||
628a47e6dc | |||
f38657e6f3 | |||
504eff73f0 | |||
fbc1408913 | |||
544c46e0e4 | |||
6c8adac0d9 | |||
687b0e16f7 | |||
881c3495c1 | |||
406b606398 | |||
168e7f7b16 | |||
53cd4df806 | |||
65a163357d | |||
1bdec1cbb6 | |||
7e39179b7f | |||
2bb0c1c618 | |||
9c7e37f728 | |||
227d1d9508 | |||
9998fa50a3 | |||
9d76bf97a3 | |||
9cd494cdfa | |||
5d9e2455f7 | |||
b70cce47e2 | |||
4acf21cdcf | |||
9ef1203ef9 | |||
7c80067900 | |||
49a1e22ba3 | |||
ebd89d8b21 | |||
9547c106d3 | |||
88b22a9248 | |||
4af24363c2 | |||
11132f7353 | |||
cd56b97587 | |||
e6b4e59c0f | |||
18fdc5a229 | |||
3699188586 | |||
88acc11501 | |||
d3919a311f | |||
b52e31fac2 | |||
b44926d597 | |||
3fd92b6437 | |||
d80abb20a4 | |||
9068093081 | |||
9d247387ea | |||
3ed44992e6 | |||
283589dc2f | |||
088e6dffbe |
@ -26,3 +26,8 @@ rustflags = ["-C", "link-args=-stack:10000000", "-C", "target-feature=+crt-stati
|
|||||||
# [target.aarch64-apple-darwin]
|
# [target.aarch64-apple-darwin]
|
||||||
# linker = "clang"
|
# linker = "clang"
|
||||||
# rustflags = ["-C", "link-arg=-fuse-ld=mold"]
|
# rustflags = ["-C", "link-arg=-fuse-ld=mold"]
|
||||||
|
|
||||||
|
[target.aarch64-apple-darwin]
|
||||||
|
# We can guarantee that this target will always run on a CPU with _at least_
|
||||||
|
# these capabilities, so let's optimize for them
|
||||||
|
rustflags = ["-Ctarget-cpu=apple-m1"]
|
2
.typos.toml → .github/.typos.toml
vendored
2
.typos.toml → .github/.typos.toml
vendored
@ -11,3 +11,5 @@ Plasticos = "Plasticos"
|
|||||||
IIF = "IIF"
|
IIF = "IIF"
|
||||||
numer = "numer"
|
numer = "numer"
|
||||||
ratatui = "ratatui"
|
ratatui = "ratatui"
|
||||||
|
doas = "doas"
|
||||||
|
wheres = "wheres"
|
6
.github/pull_request_template.md
vendored
6
.github/pull_request_template.md
vendored
@ -24,9 +24,9 @@ Don't forget to add tests that cover your changes.
|
|||||||
Make sure you've run and fixed any issues with these commands:
|
Make sure you've run and fixed any issues with these commands:
|
||||||
|
|
||||||
- `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes)
|
- `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes)
|
||||||
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style
|
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style
|
||||||
- `cargo test --workspace` to check that all tests pass
|
- `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
|
||||||
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the standard library
|
- `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library
|
||||||
|
|
||||||
> **Note**
|
> **Note**
|
||||||
> from `nushell` you can also use the `toolkit` as follows
|
> from `nushell` you can also use the `toolkit` as follows
|
||||||
|
25
.github/workflows/audit.yml
vendored
Normal file
25
.github/workflows/audit.yml
vendored
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
name: Security audit
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
paths:
|
||||||
|
- '**/Cargo.toml'
|
||||||
|
- '**/Cargo.lock'
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
|
||||||
|
env:
|
||||||
|
RUST_BACKTRACE: 1
|
||||||
|
CARGO_TERM_COLOR: always
|
||||||
|
CLICOLOR: 1
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
security_audit:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
# Prevent sudden announcement of a new advisory from failing ci:
|
||||||
|
continue-on-error: true
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- uses: rustsec/audit-check@v1.4.1
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.GITHUB_TOKEN }}
|
189
.github/workflows/ci.yml
vendored
189
.github/workflows/ci.yml
vendored
@ -1,13 +1,19 @@
|
|||||||
on:
|
on:
|
||||||
pull_request:
|
pull_request:
|
||||||
push: # Run CI on the main branch after every merge. This is important to fill the GitHub Actions cache in a way that pull requests can see it
|
push:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
|
|
||||||
name: continuous-integration
|
name: continuous-integration
|
||||||
|
|
||||||
|
env:
|
||||||
|
NUSHELL_CARGO_PROFILE: ci
|
||||||
|
NU_LOG_LEVEL: DEBUG
|
||||||
|
# If changing these settings also change toolkit.nu
|
||||||
|
CLIPPY_OPTIONS: "-D warnings -D clippy::unwrap_used"
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
nu-fmt-clippy:
|
fmt-clippy:
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: true
|
fail-fast: true
|
||||||
matrix:
|
matrix:
|
||||||
@ -15,216 +21,165 @@ jobs:
|
|||||||
# builds to link against a too-new-for-many-Linux-installs glibc version. Consider
|
# builds to link against a too-new-for-many-Linux-installs glibc version. Consider
|
||||||
# revisiting this when 20.04 is closer to EOL (April 2025)
|
# revisiting this when 20.04 is closer to EOL (April 2025)
|
||||||
platform: [windows-latest, macos-latest, ubuntu-20.04]
|
platform: [windows-latest, macos-latest, ubuntu-20.04]
|
||||||
style: [default, dataframe, extra]
|
feature: [default, dataframe, extra]
|
||||||
rust:
|
|
||||||
- stable
|
|
||||||
include:
|
include:
|
||||||
- style: default
|
- feature: default
|
||||||
flags: ""
|
flags: ""
|
||||||
- style: dataframe
|
- feature: dataframe
|
||||||
flags: "--features=dataframe "
|
flags: "--features=dataframe"
|
||||||
- style: extra
|
- feature: extra
|
||||||
flags: "--features=extra "
|
flags: "--features=extra"
|
||||||
exclude:
|
exclude:
|
||||||
# only test dataframes on Ubuntu (the fastest platform)
|
|
||||||
- platform: windows-latest
|
- platform: windows-latest
|
||||||
style: dataframe
|
feature: dataframe
|
||||||
- platform: macos-latest
|
- platform: macos-latest
|
||||||
style: dataframe
|
feature: dataframe
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
runs-on: ${{ matrix.platform }}
|
runs-on: ${{ matrix.platform }}
|
||||||
env:
|
|
||||||
NUSHELL_CARGO_TARGET: ci
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Setup Rust toolchain and cache
|
- name: Setup Rust toolchain and cache
|
||||||
uses: actions-rust-lang/setup-rust-toolchain@v1.5.0
|
uses: actions-rust-lang/setup-rust-toolchain@v1.6.0
|
||||||
with:
|
with:
|
||||||
rustflags: ""
|
rustflags: ""
|
||||||
|
|
||||||
- name: cargo fmt
|
- name: cargo fmt
|
||||||
run: cargo fmt --all -- --check
|
run: cargo fmt --all -- --check
|
||||||
|
|
||||||
|
# If changing these settings also change toolkit.nu
|
||||||
- name: Clippy
|
- name: Clippy
|
||||||
run: cargo clippy --workspace ${{ matrix.flags }}--exclude nu_plugin_* -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err
|
run: cargo clippy --workspace ${{ matrix.flags }} --exclude nu_plugin_* -- $CLIPPY_OPTIONS
|
||||||
|
|
||||||
nu-tests:
|
# In tests we don't have to deny unwrap
|
||||||
env:
|
- name: Clippy of tests
|
||||||
NUSHELL_CARGO_TARGET: ci
|
run: cargo clippy --tests --workspace ${{ matrix.flags }} --exclude nu_plugin_* -- -D warnings
|
||||||
|
|
||||||
|
tests:
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: true
|
fail-fast: true
|
||||||
matrix:
|
matrix:
|
||||||
platform: [windows-latest, macos-latest, ubuntu-20.04]
|
platform: [windows-latest, macos-latest, ubuntu-20.04]
|
||||||
style: [default, dataframe, extra]
|
feature: [default, dataframe, extra]
|
||||||
rust:
|
|
||||||
- stable
|
|
||||||
include:
|
include:
|
||||||
- style: default
|
- feature: default
|
||||||
flags: ""
|
flags: ""
|
||||||
- style: dataframe
|
- feature: dataframe
|
||||||
flags: "--features=dataframe"
|
flags: "--features=dataframe"
|
||||||
- style: extra
|
- feature: extra
|
||||||
flags: "--features=extra"
|
flags: "--features=extra"
|
||||||
|
|
||||||
exclude:
|
exclude:
|
||||||
# only test dataframes and extra on Ubuntu (the fastest platform)
|
|
||||||
- platform: windows-latest
|
- platform: windows-latest
|
||||||
style: dataframe
|
feature: dataframe
|
||||||
- platform: macos-latest
|
- platform: macos-latest
|
||||||
style: dataframe
|
feature: dataframe
|
||||||
- platform: windows-latest
|
- platform: windows-latest
|
||||||
style: extra
|
feature: extra
|
||||||
- platform: macos-latest
|
- platform: macos-latest
|
||||||
style: extra
|
feature: extra
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
runs-on: ${{ matrix.platform }}
|
runs-on: ${{ matrix.platform }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Setup Rust toolchain and cache
|
- name: Setup Rust toolchain and cache
|
||||||
uses: actions-rust-lang/setup-rust-toolchain@v1.5.0
|
uses: actions-rust-lang/setup-rust-toolchain@v1.6.0
|
||||||
with:
|
with:
|
||||||
rustflags: ""
|
rustflags: ""
|
||||||
|
|
||||||
- name: Tests
|
- name: Tests
|
||||||
run: cargo test --workspace --profile ci --exclude nu_plugin_* ${{ matrix.flags }}
|
run: cargo test --workspace --profile ci --exclude nu_plugin_* ${{ matrix.flags }}
|
||||||
|
|
||||||
std-lib-and-python-virtualenv:
|
- name: Check for clean repo
|
||||||
env:
|
shell: bash
|
||||||
NU_LOG_LEVEL: DEBUG
|
run: |
|
||||||
|
if [ -n "$(git status --porcelain)" ]; then
|
||||||
|
echo "there are changes";
|
||||||
|
git status --porcelain
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
echo "no changes in working directory";
|
||||||
|
fi
|
||||||
|
|
||||||
|
std-lib-and-python-virtualenv:
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: true
|
fail-fast: true
|
||||||
matrix:
|
matrix:
|
||||||
platform: [ubuntu-20.04, macos-latest, windows-latest]
|
platform: [ubuntu-20.04, macos-latest, windows-latest]
|
||||||
rust:
|
|
||||||
- stable
|
|
||||||
py:
|
py:
|
||||||
- py
|
- py
|
||||||
|
|
||||||
runs-on: ${{ matrix.platform }}
|
runs-on: ${{ matrix.platform }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Setup Rust toolchain and cache
|
- name: Setup Rust toolchain and cache
|
||||||
uses: actions-rust-lang/setup-rust-toolchain@v1.5.0
|
uses: actions-rust-lang/setup-rust-toolchain@v1.6.0
|
||||||
with:
|
with:
|
||||||
rustflags: ""
|
rustflags: ""
|
||||||
|
|
||||||
- name: Install Nushell
|
- name: Install Nushell
|
||||||
# prior to [*standard library: bring the tests into the main CI*](#8525)
|
|
||||||
# there was a `--profile ci` here in the `cargo install`, as well as
|
|
||||||
# `NUSHELL_CARGO_TARGET: ci` in the prelude above.
|
|
||||||
#
|
|
||||||
# this caused a "stackoverflow" error in the CI on windows,
|
|
||||||
# see [this failing job](https://github.com/nushell/nushell/actions/runs/4512034615/jobs/7944945590)
|
|
||||||
#
|
|
||||||
# the CI profile has been removed in 00b820de9021227d1910a9ea388297ee7aee308e
|
|
||||||
# as part of #8525.
|
|
||||||
run: cargo install --path . --locked --no-default-features
|
run: cargo install --path . --locked --no-default-features
|
||||||
|
|
||||||
- name: Standard library tests
|
- name: Standard library tests
|
||||||
run: nu -c 'use std testing; testing run-tests --path crates/nu-std'
|
run: nu -c 'use std testing; testing run-tests --path crates/nu-std'
|
||||||
|
|
||||||
- name: Setup Python
|
- name: Setup Python
|
||||||
uses: actions/setup-python@v4
|
uses: actions/setup-python@v5
|
||||||
with:
|
with:
|
||||||
python-version: "3.10"
|
python-version: "3.10"
|
||||||
|
|
||||||
- run: python -m pip install tox
|
|
||||||
|
|
||||||
# Get only the latest tagged version for stability reasons
|
|
||||||
- name: Install virtualenv
|
- name: Install virtualenv
|
||||||
run: git clone https://github.com/pypa/virtualenv.git
|
run: pip install virtualenv
|
||||||
shell: bash
|
shell: bash
|
||||||
|
|
||||||
- name: Test Nushell in virtualenv
|
- name: Test Nushell in virtualenv
|
||||||
run: |
|
run: nu scripts/test_virtualenv.nu
|
||||||
cd virtualenv
|
|
||||||
# if we encounter problems with bleeding edge tests pin to the latest tag
|
|
||||||
# git checkout $(git describe --tags | cut -d - -f 1)
|
|
||||||
# We need to disable failing on coverage levels.
|
|
||||||
nu -c "open pyproject.toml | upsert tool.coverage.report.fail_under 1 | save patchproject.toml"
|
|
||||||
mv patchproject.toml pyproject.toml
|
|
||||||
tox -e ${{ matrix.py }} -- -k nushell
|
|
||||||
shell: bash
|
shell: bash
|
||||||
|
|
||||||
# Build+test plugins on their own, without the rest of Nu. This helps with CI parallelization and
|
- name: Check for clean repo
|
||||||
# also helps test that the plugins build without any feature unification shenanigans
|
shell: bash
|
||||||
plugins:
|
run: |
|
||||||
env:
|
if [ -n "$(git status --porcelain)" ]; then
|
||||||
NUSHELL_CARGO_TARGET: ci
|
echo "there are changes";
|
||||||
|
git status --porcelain
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
echo "no changes in working directory";
|
||||||
|
fi
|
||||||
|
|
||||||
|
plugins:
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: true
|
fail-fast: true
|
||||||
matrix:
|
matrix:
|
||||||
platform: [windows-latest, macos-latest, ubuntu-20.04]
|
platform: [windows-latest, macos-latest, ubuntu-20.04]
|
||||||
rust:
|
|
||||||
- stable
|
|
||||||
|
|
||||||
runs-on: ${{ matrix.platform }}
|
runs-on: ${{ matrix.platform }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Setup Rust toolchain and cache
|
- name: Setup Rust toolchain and cache
|
||||||
uses: actions-rust-lang/setup-rust-toolchain@v1.5.0
|
uses: actions-rust-lang/setup-rust-toolchain@v1.6.0
|
||||||
with:
|
with:
|
||||||
rustflags: ""
|
rustflags: ""
|
||||||
|
|
||||||
- name: Clippy
|
- name: Clippy
|
||||||
run: cargo clippy --package nu_plugin_* ${{ matrix.flags }} -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err
|
run: cargo clippy --package nu_plugin_* ${{ matrix.flags }} -- $CLIPPY_OPTIONS
|
||||||
|
|
||||||
- name: Tests
|
- name: Tests
|
||||||
run: cargo test --profile ci --package nu_plugin_*
|
run: cargo test --profile ci --package nu_plugin_*
|
||||||
|
|
||||||
|
- name: Check for clean repo
|
||||||
nu-coverage:
|
|
||||||
if: false
|
|
||||||
env:
|
|
||||||
NUSHELL_CARGO_TARGET: ci
|
|
||||||
|
|
||||||
strategy:
|
|
||||||
fail-fast: true
|
|
||||||
matrix:
|
|
||||||
# disabled mac due to problems with merging coverage and similarity to linux
|
|
||||||
# disabled windows due to running out of disk space when having too many crates or tests
|
|
||||||
platform: [ubuntu-20.04] # windows-latest
|
|
||||||
rust:
|
|
||||||
- stable
|
|
||||||
|
|
||||||
runs-on: ${{ matrix.platform }}
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
|
|
||||||
- name: Setup Rust toolchain and cache
|
|
||||||
uses: actions-rust-lang/setup-rust-toolchain@v1.5.0
|
|
||||||
with:
|
|
||||||
rustflags: ""
|
|
||||||
|
|
||||||
- name: Install cargo-llvm-cov
|
|
||||||
uses: taiki-e/install-action@cargo-llvm-cov
|
|
||||||
|
|
||||||
- name: Tests
|
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
source <(cargo llvm-cov show-env --export-prefix) # Set the environment variables needed to get coverage.
|
if [ -n "$(git status --porcelain)" ]; then
|
||||||
cargo llvm-cov clean --workspace # Remove artifacts that may affect the coverage results.
|
echo "there are changes";
|
||||||
cargo build --workspace --profile ci
|
git status --porcelain
|
||||||
cargo test --workspace --profile ci
|
exit 1
|
||||||
cargo llvm-cov report --profile ci --lcov --output-path lcov.info
|
else
|
||||||
|
echo "no changes in working directory";
|
||||||
- name: Upload coverage reports to Codecov with GitHub Action
|
fi
|
||||||
uses: codecov/codecov-action@v3
|
|
||||||
with:
|
|
||||||
files: lcov.info
|
|
||||||
|
152
.github/workflows/nightly-build.yml
vendored
152
.github/workflows/nightly-build.yml
vendored
@ -13,7 +13,7 @@ on:
|
|||||||
- nightly # Just for test purpose only with the nightly repo
|
- nightly # Just for test purpose only with the nightly repo
|
||||||
# This schedule will run only from the default branch
|
# This schedule will run only from the default branch
|
||||||
schedule:
|
schedule:
|
||||||
- cron: '15 1 * * *' # run at 01:15 AM UTC
|
- cron: '15 0 * * *' # run at 00:15 AM UTC
|
||||||
|
|
||||||
defaults:
|
defaults:
|
||||||
run:
|
run:
|
||||||
@ -27,7 +27,7 @@ jobs:
|
|||||||
# if: github.repository == 'nushell/nightly'
|
# if: github.repository == 'nushell/nightly'
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v4
|
||||||
if: github.repository == 'nushell/nightly'
|
if: github.repository == 'nushell/nightly'
|
||||||
with:
|
with:
|
||||||
ref: main
|
ref: main
|
||||||
@ -36,10 +36,10 @@ jobs:
|
|||||||
token: ${{ secrets.WORKFLOW_TOKEN }}
|
token: ${{ secrets.WORKFLOW_TOKEN }}
|
||||||
|
|
||||||
- name: Setup Nushell
|
- name: Setup Nushell
|
||||||
uses: hustcer/setup-nu@v3
|
uses: hustcer/setup-nu@v3.8
|
||||||
if: github.repository == 'nushell/nightly'
|
if: github.repository == 'nushell/nightly'
|
||||||
with:
|
with:
|
||||||
version: 0.81.0
|
version: 0.86.0
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
@ -53,22 +53,21 @@ jobs:
|
|||||||
# We can't push if no user name and email are configured
|
# We can't push if no user name and email are configured
|
||||||
git config user.name 'hustcer'
|
git config user.name 'hustcer'
|
||||||
git config user.email 'hustcer@outlook.com'
|
git config user.email 'hustcer@outlook.com'
|
||||||
git fetch origin main
|
git pull origin main
|
||||||
git remote add src https://github.com/nushell/nushell.git
|
git remote add src https://github.com/nushell/nushell.git
|
||||||
git fetch src main
|
git fetch src main
|
||||||
# git pull --rebase src main
|
|
||||||
# All the changes will be overwritten by the upstream main branch
|
# All the changes will be overwritten by the upstream main branch
|
||||||
git reset --hard src/main
|
git reset --hard src/main
|
||||||
git push origin main -f
|
git push origin main -f
|
||||||
let sha_short = (git rev-parse --short src/main | str trim | str substring 0..7)
|
let sha_short = (git rev-parse --short origin/main | str trim | str substring 0..7)
|
||||||
let tag_name = $'nightly-($sha_short)'
|
let tag_name = $'nightly-($sha_short)'
|
||||||
if (git ls-remote --tags origin $tag_name | is-empty) {
|
if (git ls-remote --tags origin $tag_name | is-empty) {
|
||||||
git tag -a $tag_name -m $'Nightly build from ($sha_short)'
|
git tag -a $tag_name -m $'Nightly build from ($sha_short)'
|
||||||
git push origin --tags
|
git push origin --tags
|
||||||
}
|
}
|
||||||
|
|
||||||
release:
|
standard:
|
||||||
name: Release
|
name: Std
|
||||||
needs: prepare
|
needs: prepare
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
@ -120,29 +119,31 @@ jobs:
|
|||||||
os: ubuntu-20.04
|
os: ubuntu-20.04
|
||||||
target_rustflags: ''
|
target_rustflags: ''
|
||||||
- target: riscv64gc-unknown-linux-gnu
|
- target: riscv64gc-unknown-linux-gnu
|
||||||
os: ubuntu-20.04
|
os: ubuntu-latest
|
||||||
target_rustflags: ''
|
target_rustflags: ''
|
||||||
|
|
||||||
runs-on: ${{matrix.os}}
|
runs-on: ${{matrix.os}}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
ref: main
|
ref: main
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: Update Rust Toolchain Target
|
- name: Update Rust Toolchain Target
|
||||||
run: |
|
run: |
|
||||||
echo "targets = ['${{matrix.target}}']" >> rust-toolchain.toml
|
echo "targets = ['${{matrix.target}}']" >> rust-toolchain.toml
|
||||||
|
|
||||||
- name: Setup Rust toolchain and cache
|
- name: Setup Rust toolchain and cache
|
||||||
uses: actions-rust-lang/setup-rust-toolchain@v1.5.0
|
uses: actions-rust-lang/setup-rust-toolchain@v1.6.0
|
||||||
|
# WARN: Keep the rustflags to prevent from the winget submission error: `CAQuietExec: Error 0xc0000135`
|
||||||
with:
|
with:
|
||||||
rustflags: ''
|
rustflags: ''
|
||||||
|
|
||||||
- name: Setup Nushell
|
- name: Setup Nushell
|
||||||
uses: hustcer/setup-nu@v3
|
uses: hustcer/setup-nu@v3.8
|
||||||
with:
|
with:
|
||||||
version: 0.81.0
|
version: 0.86.0
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
@ -150,6 +151,7 @@ jobs:
|
|||||||
id: nu
|
id: nu
|
||||||
run: nu .github/workflows/release-pkg.nu
|
run: nu .github/workflows/release-pkg.nu
|
||||||
env:
|
env:
|
||||||
|
RELEASE_TYPE: standard
|
||||||
OS: ${{ matrix.os }}
|
OS: ${{ matrix.os }}
|
||||||
REF: ${{ github.ref }}
|
REF: ${{ github.ref }}
|
||||||
TARGET: ${{ matrix.target }}
|
TARGET: ${{ matrix.target }}
|
||||||
@ -176,7 +178,121 @@ jobs:
|
|||||||
# REF: https://github.com/marketplace/actions/gh-release
|
# REF: https://github.com/marketplace/actions/gh-release
|
||||||
# Create a release only in nushell/nightly repo
|
# Create a release only in nushell/nightly repo
|
||||||
- name: Publish Archive
|
- name: Publish Archive
|
||||||
uses: softprops/action-gh-release@v0.1.13
|
uses: softprops/action-gh-release@v0.1.15
|
||||||
|
if: ${{ startsWith(github.repository, 'nushell/nightly') }}
|
||||||
|
with:
|
||||||
|
prerelease: true
|
||||||
|
files: ${{ steps.nu.outputs.archive }}
|
||||||
|
tag_name: nightly-${{ steps.vars.outputs.sha_short }}
|
||||||
|
name: Nu-nightly-${{ steps.vars.outputs.date }}-${{ steps.vars.outputs.sha_short }}
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
full:
|
||||||
|
name: Full
|
||||||
|
needs: prepare
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
target:
|
||||||
|
- aarch64-apple-darwin
|
||||||
|
- x86_64-apple-darwin
|
||||||
|
- x86_64-pc-windows-msvc
|
||||||
|
- aarch64-pc-windows-msvc
|
||||||
|
- x86_64-unknown-linux-gnu
|
||||||
|
- x86_64-unknown-linux-musl
|
||||||
|
- aarch64-unknown-linux-gnu
|
||||||
|
extra: ['bin']
|
||||||
|
include:
|
||||||
|
- target: aarch64-apple-darwin
|
||||||
|
os: macos-latest
|
||||||
|
target_rustflags: '--features=dataframe,extra'
|
||||||
|
- target: x86_64-apple-darwin
|
||||||
|
os: macos-latest
|
||||||
|
target_rustflags: '--features=dataframe,extra'
|
||||||
|
- target: x86_64-pc-windows-msvc
|
||||||
|
extra: 'bin'
|
||||||
|
os: windows-latest
|
||||||
|
target_rustflags: '--features=dataframe,extra'
|
||||||
|
- target: x86_64-pc-windows-msvc
|
||||||
|
extra: msi
|
||||||
|
os: windows-latest
|
||||||
|
target_rustflags: '--features=dataframe,extra'
|
||||||
|
- target: aarch64-pc-windows-msvc
|
||||||
|
extra: 'bin'
|
||||||
|
os: windows-latest
|
||||||
|
target_rustflags: '--features=dataframe,extra'
|
||||||
|
- target: aarch64-pc-windows-msvc
|
||||||
|
extra: msi
|
||||||
|
os: windows-latest
|
||||||
|
target_rustflags: '--features=dataframe,extra'
|
||||||
|
- target: x86_64-unknown-linux-gnu
|
||||||
|
os: ubuntu-20.04
|
||||||
|
target_rustflags: '--features=dataframe,extra'
|
||||||
|
- target: x86_64-unknown-linux-musl
|
||||||
|
os: ubuntu-20.04
|
||||||
|
target_rustflags: '--features=dataframe,extra'
|
||||||
|
- target: aarch64-unknown-linux-gnu
|
||||||
|
os: ubuntu-20.04
|
||||||
|
target_rustflags: '--features=dataframe,extra'
|
||||||
|
|
||||||
|
runs-on: ${{matrix.os}}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
ref: main
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Update Rust Toolchain Target
|
||||||
|
run: |
|
||||||
|
echo "targets = ['${{matrix.target}}']" >> rust-toolchain.toml
|
||||||
|
|
||||||
|
- name: Setup Rust toolchain and cache
|
||||||
|
uses: actions-rust-lang/setup-rust-toolchain@v1.6.0
|
||||||
|
# WARN: Keep the rustflags to prevent from the winget submission error: `CAQuietExec: Error 0xc0000135`
|
||||||
|
with:
|
||||||
|
rustflags: ''
|
||||||
|
|
||||||
|
- name: Setup Nushell
|
||||||
|
uses: hustcer/setup-nu@v3.8
|
||||||
|
with:
|
||||||
|
version: 0.86.0
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Release Nu Binary
|
||||||
|
id: nu
|
||||||
|
run: nu .github/workflows/release-pkg.nu
|
||||||
|
env:
|
||||||
|
RELEASE_TYPE: full
|
||||||
|
OS: ${{ matrix.os }}
|
||||||
|
REF: ${{ github.ref }}
|
||||||
|
TARGET: ${{ matrix.target }}
|
||||||
|
_EXTRA_: ${{ matrix.extra }}
|
||||||
|
TARGET_RUSTFLAGS: ${{ matrix.target_rustflags }}
|
||||||
|
|
||||||
|
- name: Create an Issue for Release Failure
|
||||||
|
if: ${{ failure() }}
|
||||||
|
uses: JasonEtco/create-an-issue@v2.9.1
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
with:
|
||||||
|
update_existing: true
|
||||||
|
search_existing: open
|
||||||
|
filename: .github/AUTO_ISSUE_TEMPLATE/nightly-build-fail.md
|
||||||
|
|
||||||
|
- name: Set Outputs of Short SHA
|
||||||
|
id: vars
|
||||||
|
run: |
|
||||||
|
echo "date=$(date -u +'%Y-%m-%d')" >> $GITHUB_OUTPUT
|
||||||
|
sha_short=$(git rev-parse --short HEAD)
|
||||||
|
echo "sha_short=${sha_short:0:7}" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
# REF: https://github.com/marketplace/actions/gh-release
|
||||||
|
# Create a release only in nushell/nightly repo
|
||||||
|
- name: Publish Archive
|
||||||
|
uses: softprops/action-gh-release@v0.1.15
|
||||||
if: ${{ startsWith(github.repository, 'nushell/nightly') }}
|
if: ${{ startsWith(github.repository, 'nushell/nightly') }}
|
||||||
with:
|
with:
|
||||||
draft: false
|
draft: false
|
||||||
@ -200,14 +316,14 @@ jobs:
|
|||||||
- name: Waiting for Release
|
- name: Waiting for Release
|
||||||
run: sleep 1800
|
run: sleep 1800
|
||||||
|
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
ref: main
|
ref: main
|
||||||
|
|
||||||
- name: Setup Nushell
|
- name: Setup Nushell
|
||||||
uses: hustcer/setup-nu@v3
|
uses: hustcer/setup-nu@v3.8
|
||||||
with:
|
with:
|
||||||
version: 0.81.0
|
version: 0.86.0
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
106
.github/workflows/release-pkg.nu
vendored
106
.github/workflows/release-pkg.nu
vendored
@ -9,34 +9,36 @@
|
|||||||
# Instructions for manually creating an MSI for Winget Releases when they fail
|
# Instructions for manually creating an MSI for Winget Releases when they fail
|
||||||
# Added 2022-11-29 when Windows packaging wouldn't work
|
# Added 2022-11-29 when Windows packaging wouldn't work
|
||||||
# Updated again on 2023-02-23 because msis are still failing validation
|
# Updated again on 2023-02-23 because msis are still failing validation
|
||||||
|
# Update on 2023-10-18 to use RELEASE_TYPE env var to determine if full or not
|
||||||
# To run this manual for windows here are the steps I take
|
# To run this manual for windows here are the steps I take
|
||||||
# checkout the release you want to publish
|
# checkout the release you want to publish
|
||||||
# 1. git checkout 0.76.0
|
# 1. git checkout 0.86.0
|
||||||
# unset CARGO_TARGET_DIR if set (I have to do this in the parent shell to get it to work)
|
# unset CARGO_TARGET_DIR if set (I have to do this in the parent shell to get it to work)
|
||||||
# 2. $env:CARGO_TARGET_DIR = ""
|
# 2. $env:CARGO_TARGET_DIR = ""
|
||||||
# 2. hide-env CARGO_TARGET_DIR
|
# 2. hide-env CARGO_TARGET_DIR
|
||||||
# 3. let-env TARGET = 'x86_64-pc-windows-msvc'
|
# 3. $env.TARGET = 'x86_64-pc-windows-msvc'
|
||||||
# 4. let-env TARGET_RUSTFLAGS = ''
|
# 4. $env.TARGET_RUSTFLAGS = ''
|
||||||
# 5. let-env GITHUB_WORKSPACE = 'C:\Users\dschroeder\source\repos\forks\nushell'
|
# 5. $env.GITHUB_WORKSPACE = 'D:\nushell'
|
||||||
# 6. let-env GITHUB_OUTPUT = 'C:\Users\dschroeder\source\repos\forks\nushell\output\out.txt'
|
# 6. $env.GITHUB_OUTPUT = 'D:\nushell\output\out.txt'
|
||||||
# 7. let-env OS = 'windows-latest'
|
# 7. $env.OS = 'windows-latest'
|
||||||
|
# 8. $env.RELEASE_TYPE = '' # There is full and '' for normal releases
|
||||||
# make sure 7z.exe is in your path https://www.7-zip.org/download.html
|
# make sure 7z.exe is in your path https://www.7-zip.org/download.html
|
||||||
# 8. let-env Path = ($env.Path | append 'c:\apps\7-zip')
|
# 9. $env.Path = ($env.Path | append 'c:\apps\7-zip')
|
||||||
# make sure aria2c.exe is in your path https://github.com/aria2/aria2
|
# make sure aria2c.exe is in your path https://github.com/aria2/aria2
|
||||||
# 9. let-env Path = ($env.Path | append 'c:\path\to\aria2c')
|
# 10. $env.Path = ($env.Path | append 'c:\path\to\aria2c')
|
||||||
# make sure you have the wixtools installed https://wixtoolset.org/
|
# make sure you have the wixtools installed https://wixtoolset.org/
|
||||||
# 10. let-env Path = ($env.Path | append 'C:\Users\dschroeder\AppData\Local\tauri\WixTools')
|
# 11. $env.Path = ($env.Path | append 'C:\Users\dschroeder\AppData\Local\tauri\WixTools')
|
||||||
# You need to run the release-pkg twice. The first pass, with _EXTRA_ as 'bin', makes the output
|
# You need to run the release-pkg twice. The first pass, with _EXTRA_ as 'bin', makes the output
|
||||||
# folder and builds everything. The second pass, that generates the msi file, with _EXTRA_ as 'msi'
|
# folder and builds everything. The second pass, that generates the msi file, with _EXTRA_ as 'msi'
|
||||||
# 11. let-env _EXTRA_ = 'bin'
|
# 12. $env._EXTRA_ = 'bin'
|
||||||
# 12. source .github\workflows\release-pkg.nu
|
# 13. source .github\workflows\release-pkg.nu
|
||||||
# 13. cd ..
|
# 14. cd ..
|
||||||
# 14. let-env _EXTRA_ = 'msi'
|
# 15. $env._EXTRA_ = 'msi'
|
||||||
# 15. source .github\workflows\release-pkg.nu
|
# 16. source .github\workflows\release-pkg.nu
|
||||||
# After msi is generated, you have to update winget-pkgs repo, you'll need to patch the release
|
# After msi is generated, you have to update winget-pkgs repo, you'll need to patch the release
|
||||||
# by deleting the existing msi and uploading this new msi. Then you'll need to update the hash
|
# by deleting the existing msi and uploading this new msi. Then you'll need to update the hash
|
||||||
# on the winget-pkgs PR. To generate the hash, run this command
|
# on the winget-pkgs PR. To generate the hash, run this command
|
||||||
# 16. open target\wix\nu-0.74.0-x86_64-pc-windows-msvc.msi | hash sha256
|
# 17. open target\wix\nu-0.74.0-x86_64-pc-windows-msvc.msi | hash sha256
|
||||||
# Then, just take the output and put it in the winget-pkgs PR for the hash on the msi
|
# Then, just take the output and put it in the winget-pkgs PR for the hash on the msi
|
||||||
|
|
||||||
|
|
||||||
@ -51,11 +53,26 @@ let dist = $'($env.GITHUB_WORKSPACE)/output'
|
|||||||
let version = (open Cargo.toml | get package.version)
|
let version = (open Cargo.toml | get package.version)
|
||||||
|
|
||||||
print $'Debugging info:'
|
print $'Debugging info:'
|
||||||
print { version: $version, bin: $bin, os: $os, target: $target, src: $src, flags: $flags, dist: $dist }; hr-line -b
|
print { version: $version, bin: $bin, os: $os, releaseType: $env.RELEASE_TYPE, target: $target, src: $src, flags: $flags, dist: $dist }; hr-line -b
|
||||||
|
|
||||||
|
# Rename the full release name so that we won't break the existing scripts for standard release downloading, such as:
|
||||||
|
# curl -s https://api.github.com/repos/chmln/sd/releases/latest | grep browser_download_url | cut -d '"' -f 4 | grep x86_64-unknown-linux-musl
|
||||||
|
const FULL_RLS_NAMING = {
|
||||||
|
x86_64-apple-darwin: 'x86_64-darwin-full',
|
||||||
|
aarch64-apple-darwin: 'aarch64-darwin-full',
|
||||||
|
x86_64-unknown-linux-gnu: 'x86_64-linux-gnu-full',
|
||||||
|
x86_64-pc-windows-msvc: 'x86_64-windows-msvc-full',
|
||||||
|
x86_64-unknown-linux-musl: 'x86_64-linux-musl-full',
|
||||||
|
aarch64-unknown-linux-gnu: 'aarch64-linux-gnu-full',
|
||||||
|
aarch64-pc-windows-msvc: 'aarch64-windows-msvc-full',
|
||||||
|
riscv64gc-unknown-linux-gnu: 'riscv64-linux-gnu-full',
|
||||||
|
armv7-unknown-linux-gnueabihf: 'armv7-linux-gnueabihf-full',
|
||||||
|
}
|
||||||
|
|
||||||
# $env
|
# $env
|
||||||
|
|
||||||
let USE_UBUNTU = 'ubuntu-20.04'
|
let USE_UBUNTU = 'ubuntu-20.04'
|
||||||
|
let FULL_NAME = $FULL_RLS_NAMING | get -i $target | default 'unknown-target-full'
|
||||||
|
|
||||||
print $'(char nl)Packaging ($bin) v($version) for ($target) in ($src)...'; hr-line -b
|
print $'(char nl)Packaging ($bin) v($version) for ($target) in ($src)...'; hr-line -b
|
||||||
if not ('Cargo.lock' | path exists) { cargo generate-lockfile }
|
if not ('Cargo.lock' | path exists) { cargo generate-lockfile }
|
||||||
@ -65,31 +82,31 @@ print $'Start building ($bin)...'; hr-line
|
|||||||
# ----------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------
|
||||||
# Build for Ubuntu and macOS
|
# Build for Ubuntu and macOS
|
||||||
# ----------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------
|
||||||
if $os in [$USE_UBUNTU, 'macos-latest'] {
|
if $os in [$USE_UBUNTU, 'macos-latest', 'ubuntu-latest'] {
|
||||||
if $os == $USE_UBUNTU {
|
if $os starts-with ubuntu {
|
||||||
sudo apt update
|
sudo apt update
|
||||||
sudo apt-get install libxcb-composite0-dev -y
|
sudo apt-get install libxcb-composite0-dev -y
|
||||||
}
|
}
|
||||||
match $target {
|
match $target {
|
||||||
'aarch64-unknown-linux-gnu' => {
|
'aarch64-unknown-linux-gnu' => {
|
||||||
sudo apt-get install gcc-aarch64-linux-gnu -y
|
sudo apt-get install gcc-aarch64-linux-gnu -y
|
||||||
let-env CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER = 'aarch64-linux-gnu-gcc'
|
$env.CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER = 'aarch64-linux-gnu-gcc'
|
||||||
cargo-build-nu $flags
|
cargo-build-nu $flags
|
||||||
}
|
}
|
||||||
'riscv64gc-unknown-linux-gnu' => {
|
'riscv64gc-unknown-linux-gnu' => {
|
||||||
sudo apt-get install gcc-riscv64-linux-gnu -y
|
sudo apt-get install gcc-riscv64-linux-gnu -y
|
||||||
let-env CARGO_TARGET_RISCV64GC_UNKNOWN_LINUX_GNU_LINKER = 'riscv64-linux-gnu-gcc'
|
$env.CARGO_TARGET_RISCV64GC_UNKNOWN_LINUX_GNU_LINKER = 'riscv64-linux-gnu-gcc'
|
||||||
cargo-build-nu $flags
|
cargo-build-nu $flags
|
||||||
}
|
}
|
||||||
'armv7-unknown-linux-gnueabihf' => {
|
'armv7-unknown-linux-gnueabihf' => {
|
||||||
sudo apt-get install pkg-config gcc-arm-linux-gnueabihf -y
|
sudo apt-get install pkg-config gcc-arm-linux-gnueabihf -y
|
||||||
let-env CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER = 'arm-linux-gnueabihf-gcc'
|
$env.CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER = 'arm-linux-gnueabihf-gcc'
|
||||||
cargo-build-nu $flags
|
cargo-build-nu $flags
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
# musl-tools to fix 'Failed to find tool. Is `musl-gcc` installed?'
|
# musl-tools to fix 'Failed to find tool. Is `musl-gcc` installed?'
|
||||||
# Actually just for x86_64-unknown-linux-musl target
|
# Actually just for x86_64-unknown-linux-musl target
|
||||||
if $os == $USE_UBUNTU { sudo apt install musl-tools -y }
|
if $os starts-with ubuntu { sudo apt install musl-tools -y }
|
||||||
cargo-build-nu $flags
|
cargo-build-nu $flags
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -99,12 +116,7 @@ if $os in [$USE_UBUNTU, 'macos-latest'] {
|
|||||||
# Build for Windows without static-link-openssl feature
|
# Build for Windows without static-link-openssl feature
|
||||||
# ----------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------
|
||||||
if $os in ['windows-latest'] {
|
if $os in ['windows-latest'] {
|
||||||
# let-env CARGO_BUILD_TARGET = $target
|
cargo-build-nu $flags
|
||||||
if ($flags | str trim | is-empty) {
|
|
||||||
cargo build --release --all --target $target
|
|
||||||
} else {
|
|
||||||
cargo build --release --all --target $target $flags
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------
|
||||||
@ -124,10 +136,8 @@ print (ls -f $executable); sleep 1sec
|
|||||||
print $'(char nl)Copying release files...'; hr-line
|
print $'(char nl)Copying release files...'; hr-line
|
||||||
"To use Nu plugins, use the register command to tell Nu where to find the plugin. For example:
|
"To use Nu plugins, use the register command to tell Nu where to find the plugin. For example:
|
||||||
|
|
||||||
> register ./nu_plugin_query" | save $'($dist)/README.txt'
|
> register ./nu_plugin_query" | save $'($dist)/README.txt' -f
|
||||||
[LICENSE $executable] | each {|it| cp -rv $it $dist } | flatten
|
[LICENSE $executable] | each {|it| cp -rv $it $dist } | flatten
|
||||||
# Sleep a few seconds to make sure the cp process finished successfully
|
|
||||||
sleep 3sec
|
|
||||||
|
|
||||||
print $'(char nl)Check binary release version detail:'; hr-line
|
print $'(char nl)Check binary release version detail:'; hr-line
|
||||||
let ver = if $os == 'windows-latest' {
|
let ver = if $os == 'windows-latest' {
|
||||||
@ -136,17 +146,17 @@ let ver = if $os == 'windows-latest' {
|
|||||||
(do -i { ./output/nu -c 'version' }) | str join
|
(do -i { ./output/nu -c 'version' }) | str join
|
||||||
}
|
}
|
||||||
if ($ver | str trim | is-empty) {
|
if ($ver | str trim | is-empty) {
|
||||||
print $'(ansi r)Incompatible nu binary...(ansi reset)'
|
print $'(ansi r)Incompatible Nu binary: The binary cross compiled is not runnable on current arch...(ansi reset)'
|
||||||
} else { print $ver }
|
} else { print $ver }
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------
|
||||||
# Create a release archive and send it to output for the following steps
|
# Create a release archive and send it to output for the following steps
|
||||||
# ----------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------
|
||||||
cd $dist; print $'(char nl)Creating release archive...'; hr-line
|
cd $dist; print $'(char nl)Creating release archive...'; hr-line
|
||||||
if $os in [$USE_UBUNTU, 'macos-latest'] {
|
if $os in [$USE_UBUNTU, 'macos-latest', 'ubuntu-latest'] {
|
||||||
|
|
||||||
let files = (ls | get name)
|
let files = (ls | get name)
|
||||||
let dest = $'($bin)-($version)-($target)'
|
let dest = if $env.RELEASE_TYPE == 'full' { $'($bin)-($version)-($FULL_NAME)' } else { $'($bin)-($version)-($target)' }
|
||||||
let archive = $'($dist)/($dest).tar.gz'
|
let archive = $'($dist)/($dest).tar.gz'
|
||||||
|
|
||||||
mkdir $dest
|
mkdir $dest
|
||||||
@ -161,7 +171,7 @@ if $os in [$USE_UBUNTU, 'macos-latest'] {
|
|||||||
|
|
||||||
} else if $os == 'windows-latest' {
|
} else if $os == 'windows-latest' {
|
||||||
|
|
||||||
let releaseStem = $'($bin)-($version)-($target)'
|
let releaseStem = if $env.RELEASE_TYPE == 'full' { $'($bin)-($version)-($FULL_NAME)' } else { $'($bin)-($version)-($target)' }
|
||||||
|
|
||||||
print $'(char nl)Download less related stuffs...'; hr-line
|
print $'(char nl)Download less related stuffs...'; hr-line
|
||||||
aria2c https://github.com/jftuga/less-Windows/releases/download/less-v608/less.exe -o less.exe
|
aria2c https://github.com/jftuga/less-Windows/releases/download/less-v608/less.exe -o less.exe
|
||||||
@ -177,33 +187,45 @@ if $os in [$USE_UBUNTU, 'macos-latest'] {
|
|||||||
cp -r $'($dist)/*' target/release/
|
cp -r $'($dist)/*' target/release/
|
||||||
cargo install cargo-wix --version 0.3.4
|
cargo install cargo-wix --version 0.3.4
|
||||||
cargo wix --no-build --nocapture --package nu --output $wixRelease
|
cargo wix --no-build --nocapture --package nu --output $wixRelease
|
||||||
print $'archive: ---> ($wixRelease)';
|
# Workaround for https://github.com/softprops/action-gh-release/issues/280
|
||||||
echo $"archive=($wixRelease)" | save --append $env.GITHUB_OUTPUT
|
let archive = ($wixRelease | str replace --all '\' '/')
|
||||||
|
print $'archive: ---> ($archive)';
|
||||||
|
echo $"archive=($archive)" | save --append $env.GITHUB_OUTPUT
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
print $'(char nl)(ansi g)Archive contents:(ansi reset)'; hr-line; ls
|
print $'(char nl)(ansi g)Archive contents:(ansi reset)'; hr-line; ls
|
||||||
let archive = $'($dist)/($releaseStem).zip'
|
let archive = $'($dist)/($releaseStem).zip'
|
||||||
7z a $archive *
|
7z a $archive *
|
||||||
print $'archive: ---> ($archive)';
|
|
||||||
let pkg = (ls -f $archive | get name)
|
let pkg = (ls -f $archive | get name)
|
||||||
if not ($pkg | is-empty) {
|
if not ($pkg | is-empty) {
|
||||||
echo $"archive=($pkg | get 0)" | save --append $env.GITHUB_OUTPUT
|
# Workaround for https://github.com/softprops/action-gh-release/issues/280
|
||||||
|
let archive = ($pkg | get 0 | str replace --all '\' '/')
|
||||||
|
print $'archive: ---> ($archive)'
|
||||||
|
echo $"archive=($archive)" | save --append $env.GITHUB_OUTPUT
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def 'cargo-build-nu' [ options: string ] {
|
def 'cargo-build-nu' [ options: string ] {
|
||||||
if ($options | str trim | is-empty) {
|
if ($options | str trim | is-empty) {
|
||||||
cargo build --release --all --target $target --features=static-link-openssl
|
if $os == 'windows-latest' {
|
||||||
|
cargo build --release --all --target $target
|
||||||
|
} else {
|
||||||
|
cargo build --release --all --target $target --features=static-link-openssl
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
cargo build --release --all --target $target --features=static-link-openssl $options
|
if $os == 'windows-latest' {
|
||||||
|
cargo build --release --all --target $target $options
|
||||||
|
} else {
|
||||||
|
cargo build --release --all --target $target --features=static-link-openssl $options
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Print a horizontal line marker
|
# Print a horizontal line marker
|
||||||
def 'hr-line' [
|
def 'hr-line' [
|
||||||
--blank-line(-b): bool
|
--blank-line(-b)
|
||||||
] {
|
] {
|
||||||
print $'(ansi g)---------------------------------------------------------------------------->(ansi reset)'
|
print $'(ansi g)---------------------------------------------------------------------------->(ansi reset)'
|
||||||
if $blank_line { char nl }
|
if $blank_line { char nl }
|
||||||
|
115
.github/workflows/release.yml
vendored
115
.github/workflows/release.yml
vendored
@ -14,8 +14,8 @@ defaults:
|
|||||||
shell: bash
|
shell: bash
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
all:
|
standard:
|
||||||
name: All
|
name: Std
|
||||||
|
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
@ -27,6 +27,8 @@ jobs:
|
|||||||
- x86_64-unknown-linux-gnu
|
- x86_64-unknown-linux-gnu
|
||||||
- x86_64-unknown-linux-musl
|
- x86_64-unknown-linux-musl
|
||||||
- aarch64-unknown-linux-gnu
|
- aarch64-unknown-linux-gnu
|
||||||
|
- armv7-unknown-linux-gnueabihf
|
||||||
|
- riscv64gc-unknown-linux-gnu
|
||||||
extra: ['bin']
|
extra: ['bin']
|
||||||
include:
|
include:
|
||||||
- target: aarch64-apple-darwin
|
- target: aarch64-apple-darwin
|
||||||
@ -60,25 +62,32 @@ jobs:
|
|||||||
- target: aarch64-unknown-linux-gnu
|
- target: aarch64-unknown-linux-gnu
|
||||||
os: ubuntu-20.04
|
os: ubuntu-20.04
|
||||||
target_rustflags: ''
|
target_rustflags: ''
|
||||||
|
- target: armv7-unknown-linux-gnueabihf
|
||||||
|
os: ubuntu-20.04
|
||||||
|
target_rustflags: ''
|
||||||
|
- target: riscv64gc-unknown-linux-gnu
|
||||||
|
os: ubuntu-latest
|
||||||
|
target_rustflags: ''
|
||||||
|
|
||||||
runs-on: ${{matrix.os}}
|
runs-on: ${{matrix.os}}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Update Rust Toolchain Target
|
- name: Update Rust Toolchain Target
|
||||||
run: |
|
run: |
|
||||||
echo "targets = ['${{matrix.target}}']" >> rust-toolchain.toml
|
echo "targets = ['${{matrix.target}}']" >> rust-toolchain.toml
|
||||||
|
|
||||||
- name: Setup Rust toolchain and cache
|
- name: Setup Rust toolchain and cache
|
||||||
uses: actions-rust-lang/setup-rust-toolchain@v1.5.0
|
uses: actions-rust-lang/setup-rust-toolchain@v1.6.0
|
||||||
|
# WARN: Keep the rustflags to prevent from the winget submission error: `CAQuietExec: Error 0xc0000135`
|
||||||
with:
|
with:
|
||||||
rustflags: ''
|
rustflags: ''
|
||||||
|
|
||||||
- name: Setup Nushell
|
- name: Setup Nushell
|
||||||
uses: hustcer/setup-nu@v3
|
uses: hustcer/setup-nu@v3.8
|
||||||
with:
|
with:
|
||||||
version: 0.81.0
|
version: 0.86.0
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
@ -86,6 +95,7 @@ jobs:
|
|||||||
id: nu
|
id: nu
|
||||||
run: nu .github/workflows/release-pkg.nu
|
run: nu .github/workflows/release-pkg.nu
|
||||||
env:
|
env:
|
||||||
|
RELEASE_TYPE: standard
|
||||||
OS: ${{ matrix.os }}
|
OS: ${{ matrix.os }}
|
||||||
REF: ${{ github.ref }}
|
REF: ${{ github.ref }}
|
||||||
TARGET: ${{ matrix.target }}
|
TARGET: ${{ matrix.target }}
|
||||||
@ -94,7 +104,98 @@ jobs:
|
|||||||
|
|
||||||
# REF: https://github.com/marketplace/actions/gh-release
|
# REF: https://github.com/marketplace/actions/gh-release
|
||||||
- name: Publish Archive
|
- name: Publish Archive
|
||||||
uses: softprops/action-gh-release@v0.1.13
|
uses: softprops/action-gh-release@v0.1.15
|
||||||
|
if: ${{ startsWith(github.ref, 'refs/tags/') }}
|
||||||
|
with:
|
||||||
|
draft: true
|
||||||
|
files: ${{ steps.nu.outputs.archive }}
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
full:
|
||||||
|
name: Full
|
||||||
|
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
target:
|
||||||
|
- aarch64-apple-darwin
|
||||||
|
- x86_64-apple-darwin
|
||||||
|
- x86_64-pc-windows-msvc
|
||||||
|
- aarch64-pc-windows-msvc
|
||||||
|
- x86_64-unknown-linux-gnu
|
||||||
|
- x86_64-unknown-linux-musl
|
||||||
|
- aarch64-unknown-linux-gnu
|
||||||
|
extra: ['bin']
|
||||||
|
include:
|
||||||
|
- target: aarch64-apple-darwin
|
||||||
|
os: macos-latest
|
||||||
|
target_rustflags: '--features=dataframe,extra'
|
||||||
|
- target: x86_64-apple-darwin
|
||||||
|
os: macos-latest
|
||||||
|
target_rustflags: '--features=dataframe,extra'
|
||||||
|
- target: x86_64-pc-windows-msvc
|
||||||
|
extra: 'bin'
|
||||||
|
os: windows-latest
|
||||||
|
target_rustflags: '--features=dataframe,extra'
|
||||||
|
- target: x86_64-pc-windows-msvc
|
||||||
|
extra: msi
|
||||||
|
os: windows-latest
|
||||||
|
target_rustflags: '--features=dataframe,extra'
|
||||||
|
- target: aarch64-pc-windows-msvc
|
||||||
|
extra: 'bin'
|
||||||
|
os: windows-latest
|
||||||
|
target_rustflags: '--features=dataframe,extra'
|
||||||
|
- target: aarch64-pc-windows-msvc
|
||||||
|
extra: msi
|
||||||
|
os: windows-latest
|
||||||
|
target_rustflags: '--features=dataframe,extra'
|
||||||
|
- target: x86_64-unknown-linux-gnu
|
||||||
|
os: ubuntu-20.04
|
||||||
|
target_rustflags: '--features=dataframe,extra'
|
||||||
|
- target: x86_64-unknown-linux-musl
|
||||||
|
os: ubuntu-20.04
|
||||||
|
target_rustflags: '--features=dataframe,extra'
|
||||||
|
- target: aarch64-unknown-linux-gnu
|
||||||
|
os: ubuntu-20.04
|
||||||
|
target_rustflags: '--features=dataframe,extra'
|
||||||
|
|
||||||
|
runs-on: ${{matrix.os}}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Update Rust Toolchain Target
|
||||||
|
run: |
|
||||||
|
echo "targets = ['${{matrix.target}}']" >> rust-toolchain.toml
|
||||||
|
|
||||||
|
- name: Setup Rust toolchain and cache
|
||||||
|
uses: actions-rust-lang/setup-rust-toolchain@v1.6.0
|
||||||
|
# WARN: Keep the rustflags to prevent from the winget submission error: `CAQuietExec: Error 0xc0000135`
|
||||||
|
with:
|
||||||
|
rustflags: ''
|
||||||
|
|
||||||
|
- name: Setup Nushell
|
||||||
|
uses: hustcer/setup-nu@v3.8
|
||||||
|
with:
|
||||||
|
version: 0.86.0
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Release Nu Binary
|
||||||
|
id: nu
|
||||||
|
run: nu .github/workflows/release-pkg.nu
|
||||||
|
env:
|
||||||
|
RELEASE_TYPE: full
|
||||||
|
OS: ${{ matrix.os }}
|
||||||
|
REF: ${{ github.ref }}
|
||||||
|
TARGET: ${{ matrix.target }}
|
||||||
|
_EXTRA_: ${{ matrix.extra }}
|
||||||
|
TARGET_RUSTFLAGS: ${{ matrix.target_rustflags }}
|
||||||
|
|
||||||
|
# REF: https://github.com/marketplace/actions/gh-release
|
||||||
|
- name: Publish Archive
|
||||||
|
uses: softprops/action-gh-release@v0.1.15
|
||||||
if: ${{ startsWith(github.ref, 'refs/tags/') }}
|
if: ${{ startsWith(github.ref, 'refs/tags/') }}
|
||||||
with:
|
with:
|
||||||
draft: true
|
draft: true
|
||||||
|
6
.github/workflows/typos.yml
vendored
6
.github/workflows/typos.yml
vendored
@ -7,7 +7,9 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout Actions Repository
|
- name: Checkout Actions Repository
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Check spelling
|
- name: Check spelling
|
||||||
uses: crate-ci/typos@master
|
uses: crate-ci/typos@v1.17.0
|
||||||
|
with:
|
||||||
|
config: ./.github/.typos.toml
|
||||||
|
7
.github/workflows/winget-submission.yml
vendored
7
.github/workflows/winget-submission.yml
vendored
@ -7,19 +7,22 @@ on:
|
|||||||
inputs:
|
inputs:
|
||||||
tag_name:
|
tag_name:
|
||||||
description: 'Specific tag name'
|
description: 'Specific tag name'
|
||||||
required: true
|
required: true
|
||||||
type: string
|
type: string
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
|
||||||
winget:
|
winget:
|
||||||
name: Publish winget package
|
name: Publish winget package
|
||||||
runs-on: windows-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Submit package to Windows Package Manager Community Repository
|
- name: Submit package to Windows Package Manager Community Repository
|
||||||
uses: vedantmgoyal2009/winget-releaser@v2
|
uses: vedantmgoyal2009/winget-releaser@v2
|
||||||
with:
|
with:
|
||||||
identifier: Nushell.Nushell
|
identifier: Nushell.Nushell
|
||||||
|
# Exclude all `*-msvc-full.msi` full release files,
|
||||||
|
# and only the default `*msvc.msi` files will be included
|
||||||
|
installers-regex: 'msvc\.msi$'
|
||||||
version: ${{ inputs.tag_name || github.event.release.tag_name }}
|
version: ${{ inputs.tag_name || github.event.release.tag_name }}
|
||||||
release-tag: ${{ inputs.tag_name || github.event.release.tag_name }}
|
release-tag: ${{ inputs.tag_name || github.event.release.tag_name }}
|
||||||
token: ${{ secrets.NUSHELL_PAT }}
|
token: ${{ secrets.NUSHELL_PAT }}
|
||||||
|
@ -2,7 +2,25 @@
|
|||||||
|
|
||||||
Welcome to Nushell and thank you for considering contributing!
|
Welcome to Nushell and thank you for considering contributing!
|
||||||
|
|
||||||
## Review Process
|
## Table of contents
|
||||||
|
- [Proposing design changes](#proposing-design-changes)
|
||||||
|
- [Developing](#developing)
|
||||||
|
- [Setup](#setup)
|
||||||
|
- [Tests](#tests)
|
||||||
|
- [Useful commands](#useful-commands)
|
||||||
|
- [Debugging tips](#debugging-tips)
|
||||||
|
- [Git etiquette](#git-etiquette)
|
||||||
|
- [License](#license)
|
||||||
|
|
||||||
|
## Other helpful resources
|
||||||
|
|
||||||
|
More resources can be found in the nascent [developer documentation](devdocs/README.md) in this repo.
|
||||||
|
|
||||||
|
- [Developer FAQ](FAQ.md)
|
||||||
|
- [Platform support policy](PLATFORM_SUPPORT.md)
|
||||||
|
- [Our Rust style](devdocs/rust_style.md)
|
||||||
|
|
||||||
|
## Proposing design changes
|
||||||
|
|
||||||
First of all, before diving into the code, if you want to create a new feature, change something significantly, and especially if the change is user-facing, it is a good practice to first get an approval from the core team before starting to work on it.
|
First of all, before diving into the code, if you want to create a new feature, change something significantly, and especially if the change is user-facing, it is a good practice to first get an approval from the core team before starting to work on it.
|
||||||
This saves both your and our time if we realize the change needs to go another direction before spending time on it.
|
This saves both your and our time if we realize the change needs to go another direction before spending time on it.
|
||||||
@ -33,7 +51,7 @@ cargo build
|
|||||||
|
|
||||||
### Tests
|
### Tests
|
||||||
|
|
||||||
It is a good practice to cover your changes with a test. Also, try to think about corner cases and various ways how your changes could break. Cover those in the tests as well.
|
It is good practice to cover your changes with a test. Also, try to think about corner cases and various ways how your changes could break. Cover those in the tests as well.
|
||||||
|
|
||||||
Tests can be found in different places:
|
Tests can be found in different places:
|
||||||
* `/tests`
|
* `/tests`
|
||||||
@ -41,80 +59,83 @@ Tests can be found in different places:
|
|||||||
* command examples
|
* command examples
|
||||||
* crate-specific tests
|
* crate-specific tests
|
||||||
|
|
||||||
The most comprehensive test suite we have is the `nu-test-support` crate. For testing specific features, such as running Nushell in a REPL mode, we have so called "testbins". For simple tests, you can find `run_test()` and `fail_test()` functions.
|
Most of the tests are built upon the `nu-test-support` crate. For testing specific features, such as running Nushell in a REPL mode, we have so called "testbins". For simple tests, you can find `run_test()` and `fail_test()` functions.
|
||||||
|
|
||||||
### Useful Commands
|
### Useful Commands
|
||||||
|
|
||||||
|
As Nushell is built using a cargo workspace consisting of multiple crates keep in mind that you may need to pass additional flags compared to how you may be used to it from a single crate project.
|
||||||
|
Read cargo's documentation for more details: https://doc.rust-lang.org/cargo/reference/workspaces.html
|
||||||
|
|
||||||
- Build and run Nushell:
|
- Build and run Nushell:
|
||||||
|
|
||||||
```shell
|
```nushell
|
||||||
cargo run
|
cargo run
|
||||||
```
|
```
|
||||||
|
|
||||||
- Build and run with dataframe support.
|
- Build and run with dataframe support.
|
||||||
```shell
|
```nushell
|
||||||
cargo run --features=dataframe
|
cargo run --features=dataframe
|
||||||
```
|
```
|
||||||
|
|
||||||
- Run Clippy on Nushell:
|
- Run Clippy on Nushell:
|
||||||
|
|
||||||
```shell
|
```nushell
|
||||||
cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err
|
cargo clippy --workspace -- -D warnings -D clippy::unwrap_used
|
||||||
```
|
```
|
||||||
or via the `toolkit.nu` command:
|
or via the `toolkit.nu` command:
|
||||||
```shell
|
```nushell
|
||||||
use toolkit.nu clippy
|
use toolkit.nu clippy
|
||||||
clippy
|
clippy
|
||||||
```
|
```
|
||||||
|
|
||||||
- Run all tests:
|
- Run all tests:
|
||||||
|
|
||||||
```shell
|
```nushell
|
||||||
cargo test --workspace
|
cargo test --workspace
|
||||||
```
|
```
|
||||||
|
|
||||||
along with dataframe tests
|
along with dataframe tests
|
||||||
|
|
||||||
```shell
|
```nushell
|
||||||
cargo test --workspace --features=dataframe
|
cargo test --workspace --features=dataframe
|
||||||
```
|
```
|
||||||
or via the `toolkit.nu` command:
|
or via the `toolkit.nu` command:
|
||||||
```shell
|
```nushell
|
||||||
use toolkit.nu test
|
use toolkit.nu test
|
||||||
test
|
test
|
||||||
```
|
```
|
||||||
|
|
||||||
- Run all tests for a specific command
|
- Run all tests for a specific command
|
||||||
|
|
||||||
```shell
|
```nushell
|
||||||
cargo test --package nu-cli --test main -- commands::<command_name_here>
|
cargo test --package nu-cli --test main -- commands::<command_name_here>
|
||||||
```
|
```
|
||||||
|
|
||||||
- Check to see if there are code formatting issues
|
- Check to see if there are code formatting issues
|
||||||
|
|
||||||
```shell
|
```nushell
|
||||||
cargo fmt --all -- --check
|
cargo fmt --all -- --check
|
||||||
```
|
```
|
||||||
or via the `toolkit.nu` command:
|
or via the `toolkit.nu` command:
|
||||||
```shell
|
```nushell
|
||||||
use toolkit.nu fmt
|
use toolkit.nu fmt
|
||||||
fmt --check
|
fmt --check
|
||||||
```
|
```
|
||||||
|
|
||||||
- Format the code in the project
|
- Format the code in the project
|
||||||
|
|
||||||
```shell
|
```nushell
|
||||||
cargo fmt --all
|
cargo fmt --all
|
||||||
```
|
```
|
||||||
or via the `toolkit.nu` command:
|
or via the `toolkit.nu` command:
|
||||||
```shell
|
```nushell
|
||||||
use toolkit.nu fmt
|
use toolkit.nu fmt
|
||||||
fmt
|
fmt
|
||||||
```
|
```
|
||||||
|
|
||||||
- Set up `git` hooks to check formatting and run `clippy` before committing and pushing:
|
- Set up `git` hooks to check formatting and run `clippy` before committing and pushing:
|
||||||
|
|
||||||
```shell
|
```nushell
|
||||||
use toolkit.nu setup-git-hooks
|
use toolkit.nu setup-git-hooks
|
||||||
setup-git-hooks
|
setup-git-hooks
|
||||||
```
|
```
|
||||||
@ -124,12 +145,12 @@ The most comprehensive test suite we have is the `nu-test-support` crate. For te
|
|||||||
|
|
||||||
- To view verbose logs when developing, enable the `trace` log level.
|
- To view verbose logs when developing, enable the `trace` log level.
|
||||||
|
|
||||||
```shell
|
```nushell
|
||||||
cargo run --release -- --log-level trace
|
cargo run --release -- --log-level trace
|
||||||
```
|
```
|
||||||
|
|
||||||
- To redirect trace logs to a file, enable the `--log-target file` switch.
|
- To redirect trace logs to a file, enable the `--log-target file` switch.
|
||||||
```shell
|
```nushell
|
||||||
cargo run --release -- --log-level trace --log-target file
|
cargo run --release -- --log-level trace --log-target file
|
||||||
open $"($nu.temp-path)/nu-($nu.pid).log"
|
open $"($nu.temp-path)/nu-($nu.pid).log"
|
||||||
```
|
```
|
||||||
@ -220,7 +241,7 @@ You can help us to make the review process a smooth experience:
|
|||||||
- Choose what simplifies having confidence in the conflict resolution and the review. **Merge commits in your branch are OK** in the squash model.
|
- Choose what simplifies having confidence in the conflict resolution and the review. **Merge commits in your branch are OK** in the squash model.
|
||||||
- Feel free to notify your reviewers or affected PR authors if your change might cause larger conflicts with another change.
|
- Feel free to notify your reviewers or affected PR authors if your change might cause larger conflicts with another change.
|
||||||
- During the rollup of multiple PRs, we may choose to resolve merge conflicts and CI failures ourselves. (Allow maintainers to push to your branch to enable us to do this quickly.)
|
- During the rollup of multiple PRs, we may choose to resolve merge conflicts and CI failures ourselves. (Allow maintainers to push to your branch to enable us to do this quickly.)
|
||||||
|
|
||||||
### License
|
## License
|
||||||
|
|
||||||
We use the [MIT License](https://github.com/nushell/nushell/blob/main/LICENSE) in all of our Nushell projects. If you are including or referencing a crate that uses the [GPL License](https://www.gnu.org/licenses/gpl-3.0.en.html#license-text) unfortunately we will not be able to accept your PR.
|
We use the [MIT License](https://github.com/nushell/nushell/blob/main/LICENSE) in all of our Nushell projects. If you are including or referencing a crate that uses the [GPL License](https://www.gnu.org/licenses/gpl-3.0.en.html#license-text) unfortunately we will not be able to accept your PR.
|
||||||
|
3434
Cargo.lock
generated
3434
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
81
Cargo.toml
81
Cargo.toml
@ -10,8 +10,8 @@ homepage = "https://www.nushell.sh"
|
|||||||
license = "MIT"
|
license = "MIT"
|
||||||
name = "nu"
|
name = "nu"
|
||||||
repository = "https://github.com/nushell/nushell"
|
repository = "https://github.com/nushell/nushell"
|
||||||
rust-version = "1.60"
|
rust-version = "1.72.1"
|
||||||
version = "0.82.0"
|
version = "0.88.2"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
@ -33,6 +33,11 @@ members = [
|
|||||||
"crates/nu-cmd-lang",
|
"crates/nu-cmd-lang",
|
||||||
"crates/nu-cmd-dataframe",
|
"crates/nu-cmd-dataframe",
|
||||||
"crates/nu-command",
|
"crates/nu-command",
|
||||||
|
"crates/nu-color-config",
|
||||||
|
"crates/nu-explore",
|
||||||
|
"crates/nu-json",
|
||||||
|
"crates/nu-lsp",
|
||||||
|
"crates/nu-pretty-hex",
|
||||||
"crates/nu-protocol",
|
"crates/nu-protocol",
|
||||||
"crates/nu-plugin",
|
"crates/nu-plugin",
|
||||||
"crates/nu_plugin_inc",
|
"crates/nu_plugin_inc",
|
||||||
@ -42,39 +47,42 @@ members = [
|
|||||||
"crates/nu_plugin_custom_values",
|
"crates/nu_plugin_custom_values",
|
||||||
"crates/nu_plugin_formats",
|
"crates/nu_plugin_formats",
|
||||||
"crates/nu-std",
|
"crates/nu-std",
|
||||||
|
"crates/nu-table",
|
||||||
|
"crates/nu-term-grid",
|
||||||
"crates/nu-utils",
|
"crates/nu-utils",
|
||||||
]
|
]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
nu-cli = { path = "./crates/nu-cli", version = "0.82.0" }
|
nu-cli = { path = "./crates/nu-cli", version = "0.88.2" }
|
||||||
nu-color-config = { path = "./crates/nu-color-config", version = "0.82.0" }
|
nu-color-config = { path = "./crates/nu-color-config", version = "0.88.2" }
|
||||||
nu-cmd-base = { path = "./crates/nu-cmd-base", version = "0.82.0" }
|
nu-cmd-base = { path = "./crates/nu-cmd-base", version = "0.88.2" }
|
||||||
nu-cmd-lang = { path = "./crates/nu-cmd-lang", version = "0.82.0" }
|
nu-cmd-lang = { path = "./crates/nu-cmd-lang", version = "0.88.2" }
|
||||||
nu-cmd-dataframe = { path = "./crates/nu-cmd-dataframe", version = "0.82.0", optional = true }
|
nu-cmd-dataframe = { path = "./crates/nu-cmd-dataframe", version = "0.88.2", features = ["dataframe"], optional = true }
|
||||||
nu-cmd-extra = { path = "./crates/nu-cmd-extra", version = "0.82.0", optional = true }
|
nu-cmd-extra = { path = "./crates/nu-cmd-extra", version = "0.88.2", optional = true }
|
||||||
nu-command = { path = "./crates/nu-command", version = "0.82.0" }
|
nu-command = { path = "./crates/nu-command", version = "0.88.2" }
|
||||||
nu-engine = { path = "./crates/nu-engine", version = "0.82.0" }
|
nu-engine = { path = "./crates/nu-engine", version = "0.88.2" }
|
||||||
nu-explore = { path = "./crates/nu-explore", version = "0.82.0" }
|
nu-explore = { path = "./crates/nu-explore", version = "0.88.2" }
|
||||||
nu-json = { path = "./crates/nu-json", version = "0.82.0" }
|
nu-json = { path = "./crates/nu-json", version = "0.88.2" }
|
||||||
nu-parser = { path = "./crates/nu-parser", version = "0.82.0" }
|
nu-lsp = { path = "./crates/nu-lsp/", version = "0.88.2" }
|
||||||
nu-path = { path = "./crates/nu-path", version = "0.82.0" }
|
nu-parser = { path = "./crates/nu-parser", version = "0.88.2" }
|
||||||
nu-plugin = { path = "./crates/nu-plugin", optional = true, version = "0.82.0" }
|
nu-path = { path = "./crates/nu-path", version = "0.88.2" }
|
||||||
nu-pretty-hex = { path = "./crates/nu-pretty-hex", version = "0.82.0" }
|
nu-plugin = { path = "./crates/nu-plugin", optional = true, version = "0.88.2" }
|
||||||
nu-protocol = { path = "./crates/nu-protocol", version = "0.82.0" }
|
nu-pretty-hex = { path = "./crates/nu-pretty-hex", version = "0.88.2" }
|
||||||
nu-system = { path = "./crates/nu-system", version = "0.82.0" }
|
nu-protocol = { path = "./crates/nu-protocol", version = "0.88.2" }
|
||||||
nu-table = { path = "./crates/nu-table", version = "0.82.0" }
|
nu-system = { path = "./crates/nu-system", version = "0.88.2" }
|
||||||
nu-term-grid = { path = "./crates/nu-term-grid", version = "0.82.0" }
|
nu-table = { path = "./crates/nu-table", version = "0.88.2" }
|
||||||
nu-std = { path = "./crates/nu-std", version = "0.82.0" }
|
nu-term-grid = { path = "./crates/nu-term-grid", version = "0.88.2" }
|
||||||
nu-utils = { path = "./crates/nu-utils", version = "0.82.0" }
|
nu-std = { path = "./crates/nu-std", version = "0.88.2" }
|
||||||
nu-ansi-term = "0.47.0"
|
nu-utils = { path = "./crates/nu-utils", version = "0.88.2" }
|
||||||
reedline = { version = "0.21.0", features = ["bashisms", "sqlite"]}
|
|
||||||
|
|
||||||
mimalloc = { version = "0.1.37", default-features = false, optional = true}
|
nu-ansi-term = "0.49.0"
|
||||||
|
reedline = { version = "0.27.0", features = ["bashisms", "sqlite"] }
|
||||||
|
|
||||||
crossterm = "0.26"
|
crossterm = "0.27"
|
||||||
ctrlc = "3.4"
|
ctrlc = "3.4"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
miette = { version = "5.9", features = ["fancy-no-backtrace"] }
|
miette = { version = "5.10", features = ["fancy-no-backtrace"] }
|
||||||
|
mimalloc = { version = "0.1.37", default-features = false, optional = true }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
simplelog = "0.12"
|
simplelog = "0.12"
|
||||||
time = "0.3"
|
time = "0.3"
|
||||||
@ -88,22 +96,21 @@ signal-hook = { version = "0.3", default-features = false }
|
|||||||
winresource = "0.1"
|
winresource = "0.1"
|
||||||
|
|
||||||
[target.'cfg(target_family = "unix")'.dependencies]
|
[target.'cfg(target_family = "unix")'.dependencies]
|
||||||
nix = { version = "0.26", default-features = false, features = [
|
nix = { version = "0.27", default-features = false, features = [
|
||||||
"signal",
|
"signal",
|
||||||
"process",
|
"process",
|
||||||
"fs",
|
"fs",
|
||||||
"term",
|
"term",
|
||||||
] }
|
] }
|
||||||
atty = "0.2"
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
nu-test-support = { path = "./crates/nu-test-support", version = "0.82.0" }
|
nu-test-support = { path = "./crates/nu-test-support", version = "0.88.2" }
|
||||||
tempfile = "3.5"
|
|
||||||
assert_cmd = "2.0"
|
assert_cmd = "2.0"
|
||||||
criterion = "0.5"
|
criterion = "0.5"
|
||||||
pretty_assertions = "1.0"
|
pretty_assertions = "1.4"
|
||||||
|
rstest = { version = "0.18", default-features = false }
|
||||||
serial_test = "2.0"
|
serial_test = "2.0"
|
||||||
rstest = { version = "0.17", default-features = false }
|
tempfile = "3.8"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
plugin = [
|
plugin = [
|
||||||
@ -114,12 +121,13 @@ plugin = [
|
|||||||
"nu-protocol/plugin",
|
"nu-protocol/plugin",
|
||||||
"nu-engine/plugin",
|
"nu-engine/plugin",
|
||||||
]
|
]
|
||||||
default = ["plugin", "which-support", "trash-support", "sqlite"]
|
default = ["plugin", "which-support", "trash-support", "sqlite", "mimalloc"]
|
||||||
stable = ["default"]
|
stable = ["default"]
|
||||||
wasi = ["nu-cmd-lang/wasi"]
|
wasi = ["nu-cmd-lang/wasi"]
|
||||||
# NOTE: individual features are also passed to `nu-cmd-lang` that uses them to generate the feature matrix in the `version` command
|
# NOTE: individual features are also passed to `nu-cmd-lang` that uses them to generate the feature matrix in the `version` command
|
||||||
|
|
||||||
# Enable to statically link OpenSSL; otherwise the system version will be used. Not enabled by default because it takes a while to build
|
# Enable to statically link OpenSSL (perl is required, to build OpenSSL https://docs.rs/openssl/latest/openssl/);
|
||||||
|
# otherwise the system version will be used. Not enabled by default because it takes a while to build
|
||||||
static-link-openssl = ["dep:openssl", "nu-cmd-lang/static-link-openssl"]
|
static-link-openssl = ["dep:openssl", "nu-cmd-lang/static-link-openssl"]
|
||||||
|
|
||||||
mimalloc = ["nu-cmd-lang/mimalloc", "dep:mimalloc"]
|
mimalloc = ["nu-cmd-lang/mimalloc", "dep:mimalloc"]
|
||||||
@ -165,8 +173,9 @@ bench = false
|
|||||||
# To use a development version of a dependency please use a global override here
|
# To use a development version of a dependency please use a global override here
|
||||||
# changing versions in each sub-crate of the workspace is tedious
|
# changing versions in each sub-crate of the workspace is tedious
|
||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
# reedline = { git = "https://github.com/nushell/reedline.git", branch = "main"}
|
reedline = { git = "https://github.com/nushell/reedline.git", branch = "main" }
|
||||||
# nu-ansi-term = {git = "https://github.com/nushell/nu-ansi-term.git", branch = "main"}
|
# nu-ansi-term = {git = "https://github.com/nushell/nu-ansi-term.git", branch = "main"}
|
||||||
|
# uu_cp = { git = "https://github.com/uutils/coreutils.git", branch = "main" }
|
||||||
|
|
||||||
# Criterion benchmarking setup
|
# Criterion benchmarking setup
|
||||||
# Run all benchmarks with `cargo bench`
|
# Run all benchmarks with `cargo bench`
|
||||||
|
15
Cross.toml
15
Cross.toml
@ -1,9 +1,18 @@
|
|||||||
# Configuration for cross-rs: https://github.com/cross-rs/cross
|
# Configuration for cross-rs: https://github.com/cross-rs/cross
|
||||||
# Run cross-rs like this:
|
# Run cross-rs like this:
|
||||||
# cross build --target aarch64-unknown-linux-musl --release
|
# cross build --target aarch64-unknown-linux-gnu --release
|
||||||
|
# or
|
||||||
|
# cross build --target aarch64-unknown-linux-musl --release --features=static-link-openssl
|
||||||
|
|
||||||
[target.aarch64-unknown-linux-gnu]
|
[target.aarch64-unknown-linux-gnu]
|
||||||
dockerfile = "./docker/cross-rs/aarch64-unknown-linux-gnu.dockerfile"
|
pre-build = [
|
||||||
|
"dpkg --add-architecture $CROSS_DEB_ARCH",
|
||||||
|
"apt-get update && apt-get install --assume-yes libssl-dev:$CROSS_DEB_ARCH clang"
|
||||||
|
]
|
||||||
|
|
||||||
|
# NOTE: for musl you will need to build with --features=static-link-openssl
|
||||||
[target.aarch64-unknown-linux-musl]
|
[target.aarch64-unknown-linux-musl]
|
||||||
dockerfile = "./docker/cross-rs/aarch64-unknown-linux-musl.dockerfile"
|
pre-build = [
|
||||||
|
"dpkg --add-architecture $CROSS_DEB_ARCH",
|
||||||
|
"apt-get update && apt-get install --assume-yes clang"
|
||||||
|
]
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
[](https://twitter.com/nu_shell)
|
[](https://twitter.com/nu_shell)
|
||||||
[](https://github.com/nushell/nushell/graphs/commit-activity)
|
[](https://github.com/nushell/nushell/graphs/commit-activity)
|
||||||
[](https://github.com/nushell/nushell/graphs/contributors)
|
[](https://github.com/nushell/nushell/graphs/contributors)
|
||||||
[](https://codecov.io/gh/nushell/nushell)
|
|
||||||
|
|
||||||
A new type of shell.
|
A new type of shell.
|
||||||
|
|
||||||
@ -55,6 +54,7 @@ Detailed installation instructions can be found in the [installation chapter of
|
|||||||
|
|
||||||
[](https://repology.org/project/nushell/versions)
|
[](https://repology.org/project/nushell/versions)
|
||||||
|
|
||||||
|
For details about which platforms the Nushell team actively supports, see [our platform support policy](devdocs/PLATFORM_SUPPORT.md).
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
@ -199,7 +199,7 @@ topics that have been presented.
|
|||||||
|
|
||||||
Nu adheres closely to a set of goals that make up its design philosophy. As features are added, they are checked against these goals.
|
Nu adheres closely to a set of goals that make up its design philosophy. As features are added, they are checked against these goals.
|
||||||
|
|
||||||
- First and foremost, Nu is cross-platform. Commands and techniques should work across platforms and Nu has first-class support for Windows, macOS, and Linux.
|
- First and foremost, Nu is cross-platform. Commands and techniques should work across platforms and Nu has [first-class support for Windows, macOS, and Linux](devdocs/PLATFORM_SUPPORT.md).
|
||||||
|
|
||||||
- Nu ensures compatibility with existing platform-specific executables.
|
- Nu ensures compatibility with existing platform-specific executables.
|
||||||
|
|
||||||
@ -220,13 +220,15 @@ Please submit an issue or PR to be added to this list.
|
|||||||
- [virtualenv](https://github.com/pypa/virtualenv)
|
- [virtualenv](https://github.com/pypa/virtualenv)
|
||||||
- [atuin](https://github.com/ellie/atuin)
|
- [atuin](https://github.com/ellie/atuin)
|
||||||
- [clap](https://github.com/clap-rs/clap/tree/master/clap_complete_nushell)
|
- [clap](https://github.com/clap-rs/clap/tree/master/clap_complete_nushell)
|
||||||
|
- [Dorothy](http://github.com/bevry/dorothy)
|
||||||
|
- [Direnv](https://github.com/direnv/direnv/blob/master/docs/hook.md#nushell)
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
See [Contributing](CONTRIBUTING.md) for details. Thanks to all the people who already contributed!
|
See [Contributing](CONTRIBUTING.md) for details. Thanks to all the people who already contributed!
|
||||||
|
|
||||||
<a href="https://github.com/nushell/nushell/graphs/contributors">
|
<a href="https://github.com/nushell/nushell/graphs/contributors">
|
||||||
<img src="https://contributors-img.web.app/image?repo=nushell/nushell&max=500" />
|
<img src="https://contributors-img.web.app/image?repo=nushell/nushell&max=600" />
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
@ -4,10 +4,31 @@ use nu_parser::parse;
|
|||||||
use nu_plugin::{EncodingType, PluginResponse};
|
use nu_plugin::{EncodingType, PluginResponse};
|
||||||
use nu_protocol::{engine::EngineState, PipelineData, Span, Value};
|
use nu_protocol::{engine::EngineState, PipelineData, Span, Value};
|
||||||
use nu_utils::{get_default_config, get_default_env};
|
use nu_utils::{get_default_config, get_default_env};
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
fn load_bench_commands() -> EngineState {
|
fn load_bench_commands() -> EngineState {
|
||||||
nu_command::add_shell_command_context(nu_cmd_lang::create_default_context())
|
nu_command::add_shell_command_context(nu_cmd_lang::create_default_context())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn canonicalize_path(engine_state: &EngineState, path: &Path) -> PathBuf {
|
||||||
|
let cwd = engine_state.current_work_dir();
|
||||||
|
|
||||||
|
if path.exists() {
|
||||||
|
match nu_path::canonicalize_with(path, cwd) {
|
||||||
|
Ok(canon_path) => canon_path,
|
||||||
|
Err(_) => path.to_owned(),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
path.to_owned()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_home_path(engine_state: &EngineState) -> PathBuf {
|
||||||
|
nu_path::home_dir()
|
||||||
|
.map(|path| canonicalize_path(engine_state, &path))
|
||||||
|
.unwrap_or_default()
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME: All benchmarks live in this 1 file to speed up build times when benchmarking.
|
// FIXME: All benchmarks live in this 1 file to speed up build times when benchmarking.
|
||||||
// When the *_benchmarks functions were in different files, `cargo bench` would build
|
// When the *_benchmarks functions were in different files, `cargo bench` would build
|
||||||
// an executable for every single one - incredibly slowly. Would be nice to figure out
|
// an executable for every single one - incredibly slowly. Would be nice to figure out
|
||||||
@ -15,10 +36,12 @@ fn load_bench_commands() -> EngineState {
|
|||||||
|
|
||||||
fn parser_benchmarks(c: &mut Criterion) {
|
fn parser_benchmarks(c: &mut Criterion) {
|
||||||
let mut engine_state = load_bench_commands();
|
let mut engine_state = load_bench_commands();
|
||||||
// parsing config.nu breaks without PWD set
|
let home_path = get_home_path(&engine_state);
|
||||||
|
|
||||||
|
// parsing config.nu breaks without PWD set, so set a valid path
|
||||||
engine_state.add_env_var(
|
engine_state.add_env_var(
|
||||||
"PWD".into(),
|
"PWD".into(),
|
||||||
Value::string("/some/dir".to_string(), Span::test_data()),
|
Value::string(home_path.to_string_lossy(), Span::test_data()),
|
||||||
);
|
);
|
||||||
|
|
||||||
let default_env = get_default_env().as_bytes();
|
let default_env = get_default_env().as_bytes();
|
||||||
@ -41,7 +64,6 @@ fn parser_benchmarks(c: &mut Criterion) {
|
|||||||
|
|
||||||
c.bench_function("eval default_env.nu", |b| {
|
c.bench_function("eval default_env.nu", |b| {
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
let mut engine_state = load_bench_commands();
|
|
||||||
let mut stack = nu_protocol::engine::Stack::new();
|
let mut stack = nu_protocol::engine::Stack::new();
|
||||||
eval_source(
|
eval_source(
|
||||||
&mut engine_state,
|
&mut engine_state,
|
||||||
@ -56,12 +78,6 @@ fn parser_benchmarks(c: &mut Criterion) {
|
|||||||
|
|
||||||
c.bench_function("eval default_config.nu", |b| {
|
c.bench_function("eval default_config.nu", |b| {
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
let mut engine_state = load_bench_commands();
|
|
||||||
// parsing config.nu breaks without PWD set
|
|
||||||
engine_state.add_env_var(
|
|
||||||
"PWD".into(),
|
|
||||||
Value::string("/some/dir".to_string(), Span::test_data()),
|
|
||||||
);
|
|
||||||
let mut stack = nu_protocol::engine::Stack::new();
|
let mut stack = nu_protocol::engine::Stack::new();
|
||||||
eval_source(
|
eval_source(
|
||||||
&mut engine_state,
|
&mut engine_state,
|
||||||
@ -76,9 +92,17 @@ fn parser_benchmarks(c: &mut Criterion) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn eval_benchmarks(c: &mut Criterion) {
|
fn eval_benchmarks(c: &mut Criterion) {
|
||||||
|
let mut engine_state = load_bench_commands();
|
||||||
|
let home_path = get_home_path(&engine_state);
|
||||||
|
|
||||||
|
// parsing config.nu breaks without PWD set, so set a valid path
|
||||||
|
engine_state.add_env_var(
|
||||||
|
"PWD".into(),
|
||||||
|
Value::string(home_path.to_string_lossy(), Span::test_data()),
|
||||||
|
);
|
||||||
|
|
||||||
c.bench_function("eval default_env.nu", |b| {
|
c.bench_function("eval default_env.nu", |b| {
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
let mut engine_state = load_bench_commands();
|
|
||||||
let mut stack = nu_protocol::engine::Stack::new();
|
let mut stack = nu_protocol::engine::Stack::new();
|
||||||
eval_source(
|
eval_source(
|
||||||
&mut engine_state,
|
&mut engine_state,
|
||||||
@ -93,12 +117,6 @@ fn eval_benchmarks(c: &mut Criterion) {
|
|||||||
|
|
||||||
c.bench_function("eval default_config.nu", |b| {
|
c.bench_function("eval default_config.nu", |b| {
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
let mut engine_state = load_bench_commands();
|
|
||||||
// parsing config.nu breaks without PWD set
|
|
||||||
engine_state.add_env_var(
|
|
||||||
"PWD".into(),
|
|
||||||
Value::string("/some/dir".to_string(), Span::test_data()),
|
|
||||||
);
|
|
||||||
let mut stack = nu_protocol::engine::Stack::new();
|
let mut stack = nu_protocol::engine::Stack::new();
|
||||||
eval_source(
|
eval_source(
|
||||||
&mut engine_state,
|
&mut engine_state,
|
||||||
@ -114,15 +132,13 @@ fn eval_benchmarks(c: &mut Criterion) {
|
|||||||
|
|
||||||
// generate a new table data with `row_cnt` rows, `col_cnt` columns.
|
// generate a new table data with `row_cnt` rows, `col_cnt` columns.
|
||||||
fn encoding_test_data(row_cnt: usize, col_cnt: usize) -> Value {
|
fn encoding_test_data(row_cnt: usize, col_cnt: usize) -> Value {
|
||||||
let columns: Vec<String> = (0..col_cnt).map(|x| format!("col_{x}")).collect();
|
let record = Value::test_record(
|
||||||
let vals: Vec<Value> = (0..col_cnt as i64).map(Value::test_int).collect();
|
(0..col_cnt)
|
||||||
|
.map(|x| (format!("col_{x}"), Value::test_int(x as i64)))
|
||||||
Value::List {
|
|
||||||
vals: (0..row_cnt)
|
|
||||||
.map(|_| Value::test_record(columns.clone(), vals.clone()))
|
|
||||||
.collect(),
|
.collect(),
|
||||||
span: Span::test_data(),
|
);
|
||||||
}
|
|
||||||
|
Value::list(vec![record; row_cnt], Span::test_data())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encoding_benchmarks(c: &mut Criterion) {
|
fn encoding_benchmarks(c: &mut Criterion) {
|
||||||
|
17
codecov.yml
17
codecov.yml
@ -1,17 +0,0 @@
|
|||||||
coverage:
|
|
||||||
status:
|
|
||||||
project:
|
|
||||||
default:
|
|
||||||
target: 55%
|
|
||||||
threshold: 2%
|
|
||||||
patch:
|
|
||||||
default:
|
|
||||||
informational: true
|
|
||||||
|
|
||||||
comment:
|
|
||||||
layout: reach, diff, files
|
|
||||||
behavior: default
|
|
||||||
require_base: yes
|
|
||||||
require_head: yes
|
|
||||||
after_n_builds: 1 # Disabled windows else: 2
|
|
||||||
|
|
@ -5,40 +5,42 @@ repository = "https://github.com/nushell/nushell/tree/main/crates/nu-cli"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
name = "nu-cli"
|
name = "nu-cli"
|
||||||
version = "0.82.0"
|
version = "0.88.2"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
bench = false
|
bench = false
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
nu-cmd-lang = { path = "../nu-cmd-lang", version = "0.82.0" }
|
nu-cmd-lang = { path = "../nu-cmd-lang", version = "0.88.2" }
|
||||||
nu-test-support = { path = "../nu-test-support", version = "0.82.0" }
|
nu-command = { path = "../nu-command", version = "0.88.2" }
|
||||||
rstest = { version = "0.17.0", default-features = false }
|
nu-test-support = { path = "../nu-test-support", version = "0.88.2" }
|
||||||
|
rstest = { version = "0.18.1", default-features = false }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
nu-cmd-base = { path = "../nu-cmd-base", version = "0.82.0" }
|
nu-cmd-base = { path = "../nu-cmd-base", version = "0.88.2" }
|
||||||
nu-command = { path = "../nu-command", version = "0.82.0" }
|
nu-engine = { path = "../nu-engine", version = "0.88.2" }
|
||||||
nu-engine = { path = "../nu-engine", version = "0.82.0" }
|
nu-path = { path = "../nu-path", version = "0.88.2" }
|
||||||
nu-path = { path = "../nu-path", version = "0.82.0" }
|
nu-parser = { path = "../nu-parser", version = "0.88.2" }
|
||||||
nu-parser = { path = "../nu-parser", version = "0.82.0" }
|
nu-protocol = { path = "../nu-protocol", version = "0.88.2" }
|
||||||
nu-protocol = { path = "../nu-protocol", version = "0.82.0" }
|
nu-utils = { path = "../nu-utils", version = "0.88.2" }
|
||||||
nu-utils = { path = "../nu-utils", version = "0.82.0" }
|
nu-color-config = { path = "../nu-color-config", version = "0.88.2" }
|
||||||
nu-color-config = { path = "../nu-color-config", version = "0.82.0" }
|
nu-ansi-term = "0.49.0"
|
||||||
nu-ansi-term = "0.47.0"
|
reedline = { version = "0.27.0", features = ["bashisms", "sqlite"] }
|
||||||
reedline = { version = "0.21.0", features = ["bashisms", "sqlite"]}
|
|
||||||
|
|
||||||
atty = "0.2"
|
|
||||||
chrono = { default-features = false, features = ["std"], version = "0.4" }
|
chrono = { default-features = false, features = ["std"], version = "0.4" }
|
||||||
crossterm = "0.26"
|
crossterm = "0.27"
|
||||||
fancy-regex = "0.11"
|
fancy-regex = "0.12"
|
||||||
fuzzy-matcher = "0.3"
|
fuzzy-matcher = "0.3"
|
||||||
is_executable = "1.0"
|
is_executable = "1.0"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
miette = { version = "5.9", features = ["fancy-no-backtrace"] }
|
miette = { version = "5.10", features = ["fancy-no-backtrace"] }
|
||||||
once_cell = "1.18"
|
once_cell = "1.18"
|
||||||
percent-encoding = "2"
|
percent-encoding = "2"
|
||||||
sysinfo = "0.29"
|
pathdiff = "0.2"
|
||||||
|
sysinfo = "0.30"
|
||||||
unicode-segmentation = "1.10"
|
unicode-segmentation = "1.10"
|
||||||
|
uuid = { version = "1.6.0", features = ["v4"] }
|
||||||
|
which = "5.0.0"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
plugin = []
|
plugin = []
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
use nu_engine::CallExt;
|
use nu_engine::CallExt;
|
||||||
use nu_protocol::ast::Call;
|
use nu_protocol::{
|
||||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
ast::Call,
|
||||||
use nu_protocol::Category;
|
engine::{Command, EngineState, Stack},
|
||||||
use nu_protocol::IntoPipelineData;
|
Category, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
|
||||||
use nu_protocol::{PipelineData, ShellError, Signature, SyntaxShape, Type, Value};
|
};
|
||||||
use unicode_segmentation::UnicodeSegmentation;
|
use unicode_segmentation::UnicodeSegmentation;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@ -16,12 +16,20 @@ impl Command for Commandline {
|
|||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build("commandline")
|
Signature::build("commandline")
|
||||||
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
|
.input_output_types(vec![
|
||||||
|
(Type::Nothing, Type::Nothing),
|
||||||
|
(Type::String, Type::String),
|
||||||
|
])
|
||||||
.switch(
|
.switch(
|
||||||
"cursor",
|
"cursor",
|
||||||
"Set or get the current cursor position",
|
"Set or get the current cursor position",
|
||||||
Some('c'),
|
Some('c'),
|
||||||
)
|
)
|
||||||
|
.switch(
|
||||||
|
"cursor-end",
|
||||||
|
"Set the current cursor position to the end of the buffer",
|
||||||
|
Some('e'),
|
||||||
|
)
|
||||||
.switch(
|
.switch(
|
||||||
"append",
|
"append",
|
||||||
"appends the string to the end of the buffer",
|
"appends the string to the end of the buffer",
|
||||||
@ -81,9 +89,9 @@ impl Command for Commandline {
|
|||||||
return Err(ShellError::CantConvert {
|
return Err(ShellError::CantConvert {
|
||||||
to_type: "int".to_string(),
|
to_type: "int".to_string(),
|
||||||
from_type: "string".to_string(),
|
from_type: "string".to_string(),
|
||||||
span: cmd.span()?,
|
span: cmd.span(),
|
||||||
help: Some(format!(
|
help: Some(format!(
|
||||||
r#"string "{cmd_str}" does not represent a valid integer"#
|
r#"string "{cmd_str}" does not represent a valid int"#
|
||||||
)),
|
)),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -99,27 +107,22 @@ impl Command for Commandline {
|
|||||||
repl.buffer = cmd.as_string()?;
|
repl.buffer = cmd.as_string()?;
|
||||||
repl.cursor_pos = repl.buffer.len();
|
repl.cursor_pos = repl.buffer.len();
|
||||||
}
|
}
|
||||||
Ok(Value::Nothing { span: call.head }.into_pipeline_data())
|
Ok(Value::nothing(call.head).into_pipeline_data())
|
||||||
} else {
|
} else {
|
||||||
let repl = engine_state.repl_state.lock().expect("repl state mutex");
|
let mut repl = engine_state.repl_state.lock().expect("repl state mutex");
|
||||||
if call.has_flag("cursor") {
|
if call.has_flag("cursor-end") {
|
||||||
|
repl.cursor_pos = repl.buffer.graphemes(true).count();
|
||||||
|
Ok(Value::nothing(call.head).into_pipeline_data())
|
||||||
|
} else if call.has_flag("cursor") {
|
||||||
let char_pos = repl
|
let char_pos = repl
|
||||||
.buffer
|
.buffer
|
||||||
.grapheme_indices(true)
|
.grapheme_indices(true)
|
||||||
.chain(std::iter::once((repl.buffer.len(), "")))
|
.chain(std::iter::once((repl.buffer.len(), "")))
|
||||||
.position(|(i, _c)| i == repl.cursor_pos)
|
.position(|(i, _c)| i == repl.cursor_pos)
|
||||||
.expect("Cursor position isn't on a grapheme boundary");
|
.expect("Cursor position isn't on a grapheme boundary");
|
||||||
Ok(Value::String {
|
Ok(Value::string(char_pos.to_string(), call.head).into_pipeline_data())
|
||||||
val: char_pos.to_string(),
|
|
||||||
span: call.head,
|
|
||||||
}
|
|
||||||
.into_pipeline_data())
|
|
||||||
} else {
|
} else {
|
||||||
Ok(Value::String {
|
Ok(Value::string(repl.buffer.to_string(), call.head).into_pipeline_data())
|
||||||
val: repl.buffer.to_string(),
|
|
||||||
span: call.head,
|
|
||||||
}
|
|
||||||
.into_pipeline_data())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
use nu_protocol::ast::Call;
|
use nu_protocol::ast::Call;
|
||||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
Category, Example, HistoryFileFormat, IntoInterruptiblePipelineData, PipelineData, ShellError,
|
record, Category, Example, HistoryFileFormat, IntoInterruptiblePipelineData, PipelineData,
|
||||||
Signature, Span, Type, Value,
|
ShellError, Signature, Span, Type, Value,
|
||||||
};
|
};
|
||||||
use reedline::{
|
use reedline::{
|
||||||
FileBackedHistory, History as ReedlineHistory, HistoryItem, SearchDirection, SearchQuery,
|
FileBackedHistory, History as ReedlineHistory, HistoryItem, SearchDirection, SearchQuery,
|
||||||
@ -34,7 +34,7 @@ impl Command for History {
|
|||||||
"Show long listing of entries for sqlite history",
|
"Show long listing of entries for sqlite history",
|
||||||
Some('l'),
|
Some('l'),
|
||||||
)
|
)
|
||||||
.category(Category::Misc)
|
.category(Category::History)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
@ -70,12 +70,14 @@ impl Command for History {
|
|||||||
} else {
|
} else {
|
||||||
let history_reader: Option<Box<dyn ReedlineHistory>> =
|
let history_reader: Option<Box<dyn ReedlineHistory>> =
|
||||||
match engine_state.config.history_file_format {
|
match engine_state.config.history_file_format {
|
||||||
HistoryFileFormat::Sqlite => SqliteBackedHistory::with_file(history_path)
|
HistoryFileFormat::Sqlite => {
|
||||||
.map(|inner| {
|
SqliteBackedHistory::with_file(history_path, None, None)
|
||||||
let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
|
.map(|inner| {
|
||||||
boxed
|
let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
|
||||||
})
|
boxed
|
||||||
.ok(),
|
})
|
||||||
|
.ok()
|
||||||
|
}
|
||||||
|
|
||||||
HistoryFileFormat::PlainText => FileBackedHistory::with_file(
|
HistoryFileFormat::PlainText => FileBackedHistory::with_file(
|
||||||
engine_state.config.max_history_size as usize,
|
engine_state.config.max_history_size as usize,
|
||||||
@ -95,22 +97,17 @@ impl Command for History {
|
|||||||
.ok()
|
.ok()
|
||||||
})
|
})
|
||||||
.map(move |entries| {
|
.map(move |entries| {
|
||||||
entries
|
entries.into_iter().enumerate().map(move |(idx, entry)| {
|
||||||
.into_iter()
|
Value::record(
|
||||||
.enumerate()
|
record! {
|
||||||
.map(move |(idx, entry)| Value::Record {
|
"command" => Value::string(entry.command_line, head),
|
||||||
cols: vec!["command".to_string(), "index".to_string()],
|
"index" => Value::int(idx as i64, head),
|
||||||
vals: vec![
|
},
|
||||||
Value::String {
|
head,
|
||||||
val: entry.command_line,
|
)
|
||||||
span: head,
|
})
|
||||||
},
|
|
||||||
Value::int(idx as i64, head),
|
|
||||||
],
|
|
||||||
span: head,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
.ok_or(ShellError::FileNotFound(head))?
|
.ok_or(ShellError::FileNotFound { span: head })?
|
||||||
.into_pipeline_data(ctrlc)),
|
.into_pipeline_data(ctrlc)),
|
||||||
HistoryFileFormat::Sqlite => Ok(history_reader
|
HistoryFileFormat::Sqlite => Ok(history_reader
|
||||||
.and_then(|h| {
|
.and_then(|h| {
|
||||||
@ -122,12 +119,12 @@ impl Command for History {
|
|||||||
create_history_record(idx, entry, long, head)
|
create_history_record(idx, entry, long, head)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.ok_or(ShellError::FileNotFound(head))?
|
.ok_or(ShellError::FileNotFound { span: head })?
|
||||||
.into_pipeline_data(ctrlc)),
|
.into_pipeline_data(ctrlc)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Err(ShellError::FileNotFound(head))
|
Err(ShellError::FileNotFound { span: head })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,7 +141,7 @@ impl Command for History {
|
|||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
example: "history | wrap cmd | where cmd =~ cargo",
|
example: "history | where command =~ cargo | get command",
|
||||||
description: "Search all the commands from history that contains 'cargo'",
|
description: "Search all the commands from history that contains 'cargo'",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
@ -156,8 +153,8 @@ fn create_history_record(idx: usize, entry: HistoryItem, long: bool, head: Span)
|
|||||||
//1. Format all the values
|
//1. Format all the values
|
||||||
//2. Create a record of either short or long columns and values
|
//2. Create a record of either short or long columns and values
|
||||||
|
|
||||||
let item_id_value = Value::Int {
|
let item_id_value = Value::int(
|
||||||
val: match entry.id {
|
match entry.id {
|
||||||
Some(id) => {
|
Some(id) => {
|
||||||
let ids = id.to_string();
|
let ids = id.to_string();
|
||||||
match ids.parse::<i64>() {
|
match ids.parse::<i64>() {
|
||||||
@ -167,21 +164,18 @@ fn create_history_record(idx: usize, entry: HistoryItem, long: bool, head: Span)
|
|||||||
}
|
}
|
||||||
None => 0i64,
|
None => 0i64,
|
||||||
},
|
},
|
||||||
span: head,
|
head,
|
||||||
};
|
);
|
||||||
let start_timestamp_value = Value::String {
|
let start_timestamp_value = Value::string(
|
||||||
val: match entry.start_timestamp {
|
match entry.start_timestamp {
|
||||||
Some(time) => time.to_string(),
|
Some(time) => time.to_string(),
|
||||||
None => "".into(),
|
None => "".into(),
|
||||||
},
|
},
|
||||||
span: head,
|
head,
|
||||||
};
|
);
|
||||||
let command_value = Value::String {
|
let command_value = Value::string(entry.command_line, head);
|
||||||
val: entry.command_line,
|
let session_id_value = Value::int(
|
||||||
span: head,
|
match entry.session_id {
|
||||||
};
|
|
||||||
let session_id_value = Value::Int {
|
|
||||||
val: match entry.session_id {
|
|
||||||
Some(sid) => {
|
Some(sid) => {
|
||||||
let sids = sid.to_string();
|
let sids = sid.to_string();
|
||||||
match sids.parse::<i64>() {
|
match sids.parse::<i64>() {
|
||||||
@ -191,74 +185,56 @@ fn create_history_record(idx: usize, entry: HistoryItem, long: bool, head: Span)
|
|||||||
}
|
}
|
||||||
None => 0i64,
|
None => 0i64,
|
||||||
},
|
},
|
||||||
span: head,
|
head,
|
||||||
};
|
);
|
||||||
let hostname_value = Value::String {
|
let hostname_value = Value::string(
|
||||||
val: match entry.hostname {
|
match entry.hostname {
|
||||||
Some(host) => host,
|
Some(host) => host,
|
||||||
None => "".into(),
|
None => "".into(),
|
||||||
},
|
},
|
||||||
span: head,
|
head,
|
||||||
};
|
);
|
||||||
let cwd_value = Value::String {
|
let cwd_value = Value::string(
|
||||||
val: match entry.cwd {
|
match entry.cwd {
|
||||||
Some(cwd) => cwd,
|
Some(cwd) => cwd,
|
||||||
None => "".into(),
|
None => "".into(),
|
||||||
},
|
},
|
||||||
span: head,
|
head,
|
||||||
};
|
);
|
||||||
let duration_value = Value::Duration {
|
let duration_value = Value::duration(
|
||||||
val: match entry.duration {
|
match entry.duration {
|
||||||
Some(d) => d.as_nanos().try_into().unwrap_or(0),
|
Some(d) => d.as_nanos().try_into().unwrap_or(0),
|
||||||
None => 0,
|
None => 0,
|
||||||
},
|
},
|
||||||
span: head,
|
head,
|
||||||
};
|
);
|
||||||
let exit_status_value = Value::int(entry.exit_status.unwrap_or(0), head);
|
let exit_status_value = Value::int(entry.exit_status.unwrap_or(0), head);
|
||||||
let index_value = Value::int(idx as i64, head);
|
let index_value = Value::int(idx as i64, head);
|
||||||
if long {
|
if long {
|
||||||
Value::Record {
|
Value::record(
|
||||||
cols: vec![
|
record! {
|
||||||
"item_id".into(),
|
"item_id" => item_id_value,
|
||||||
"start_timestamp".into(),
|
"start_timestamp" => start_timestamp_value,
|
||||||
"command".to_string(),
|
"command" => command_value,
|
||||||
"session_id".into(),
|
"session_id" => session_id_value,
|
||||||
"hostname".into(),
|
"hostname" => hostname_value,
|
||||||
"cwd".into(),
|
"cwd" => cwd_value,
|
||||||
"duration".into(),
|
"duration" => duration_value,
|
||||||
"exit_status".into(),
|
"exit_status" => exit_status_value,
|
||||||
"idx".to_string(),
|
"idx" => index_value,
|
||||||
],
|
},
|
||||||
vals: vec![
|
head,
|
||||||
item_id_value,
|
)
|
||||||
start_timestamp_value,
|
|
||||||
command_value,
|
|
||||||
session_id_value,
|
|
||||||
hostname_value,
|
|
||||||
cwd_value,
|
|
||||||
duration_value,
|
|
||||||
exit_status_value,
|
|
||||||
index_value,
|
|
||||||
],
|
|
||||||
span: head,
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
Value::Record {
|
Value::record(
|
||||||
cols: vec![
|
record! {
|
||||||
"start_timestamp".into(),
|
"start_timestamp" => start_timestamp_value,
|
||||||
"command".to_string(),
|
"command" => command_value,
|
||||||
"cwd".into(),
|
"cwd" => cwd_value,
|
||||||
"duration".into(),
|
"duration" => duration_value,
|
||||||
"exit_status".into(),
|
"exit_status" => exit_status_value,
|
||||||
],
|
},
|
||||||
vals: vec![
|
head,
|
||||||
start_timestamp_value,
|
)
|
||||||
command_value,
|
|
||||||
cwd_value,
|
|
||||||
duration_value,
|
|
||||||
exit_status_value,
|
|
||||||
],
|
|
||||||
span: head,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -18,7 +18,7 @@ impl Command for HistorySession {
|
|||||||
|
|
||||||
fn signature(&self) -> nu_protocol::Signature {
|
fn signature(&self) -> nu_protocol::Signature {
|
||||||
Signature::build("history session")
|
Signature::build("history session")
|
||||||
.category(Category::Misc)
|
.category(Category::History)
|
||||||
.input_output_types(vec![(Type::Nothing, Type::Int)])
|
.input_output_types(vec![(Type::Nothing, Type::Int)])
|
||||||
}
|
}
|
||||||
|
|
5
crates/nu-cli/src/commands/history/mod.rs
Normal file
5
crates/nu-cli/src/commands/history/mod.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
mod history_;
|
||||||
|
mod history_session;
|
||||||
|
|
||||||
|
pub use history_::History;
|
||||||
|
pub use history_session::HistorySession;
|
@ -24,7 +24,10 @@ impl Command for Keybindings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
"You must use one of the following subcommands. Using this command as-is will only produce this help message."
|
r#"You must use one of the following subcommands. Using this command as-is will only produce this help message.
|
||||||
|
|
||||||
|
For more information on input and keybindings, check:
|
||||||
|
https://www.nushell.sh/book/line_editor.html"#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
@ -38,16 +41,16 @@ impl Command for Keybindings {
|
|||||||
call: &Call,
|
call: &Call,
|
||||||
_input: PipelineData,
|
_input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
Ok(Value::String {
|
Ok(Value::string(
|
||||||
val: get_full_help(
|
get_full_help(
|
||||||
&Keybindings.signature(),
|
&Keybindings.signature(),
|
||||||
&Keybindings.examples(),
|
&Keybindings.examples(),
|
||||||
engine_state,
|
engine_state,
|
||||||
stack,
|
stack,
|
||||||
self.is_parser_keyword(),
|
self.is_parser_keyword(),
|
||||||
),
|
),
|
||||||
span: call.head,
|
call.head,
|
||||||
}
|
)
|
||||||
.into_pipeline_data())
|
.into_pipeline_data())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
ast::Call,
|
ast::Call,
|
||||||
engine::{Command, EngineState, Stack},
|
engine::{Command, EngineState, Stack},
|
||||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
|
record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
|
||||||
};
|
};
|
||||||
use reedline::get_reedline_default_keybindings;
|
use reedline::get_reedline_default_keybindings;
|
||||||
|
|
||||||
@ -41,43 +41,18 @@ impl Command for KeybindingsDefault {
|
|||||||
let records = get_reedline_default_keybindings()
|
let records = get_reedline_default_keybindings()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(mode, modifier, code, event)| {
|
.map(|(mode, modifier, code, event)| {
|
||||||
let mode = Value::String {
|
Value::record(
|
||||||
val: mode,
|
record! {
|
||||||
span: call.head,
|
"mode" => Value::string(mode, call.head),
|
||||||
};
|
"modifier" => Value::string(modifier, call.head),
|
||||||
|
"code" => Value::string(code, call.head),
|
||||||
let modifier = Value::String {
|
"event" => Value::string(event, call.head),
|
||||||
val: modifier,
|
},
|
||||||
span: call.head,
|
call.head,
|
||||||
};
|
)
|
||||||
|
|
||||||
let code = Value::String {
|
|
||||||
val: code,
|
|
||||||
span: call.head,
|
|
||||||
};
|
|
||||||
|
|
||||||
let event = Value::String {
|
|
||||||
val: event,
|
|
||||||
span: call.head,
|
|
||||||
};
|
|
||||||
|
|
||||||
Value::Record {
|
|
||||||
cols: vec![
|
|
||||||
"mode".to_string(),
|
|
||||||
"modifier".to_string(),
|
|
||||||
"code".to_string(),
|
|
||||||
"event".to_string(),
|
|
||||||
],
|
|
||||||
vals: vec![mode, modifier, code, event],
|
|
||||||
span: call.head,
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
Ok(Value::List {
|
Ok(Value::list(records, call.head).into_pipeline_data())
|
||||||
vals: records,
|
|
||||||
span: call.head,
|
|
||||||
}
|
|
||||||
.into_pipeline_data())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
ast::Call,
|
ast::Call,
|
||||||
engine::{Command, EngineState, Stack},
|
engine::{Command, EngineState, Stack},
|
||||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type, Value,
|
record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type,
|
||||||
|
Value,
|
||||||
};
|
};
|
||||||
use reedline::{
|
use reedline::{
|
||||||
get_reedline_edit_commands, get_reedline_keybinding_modifiers, get_reedline_keycodes,
|
get_reedline_edit_commands, get_reedline_keybinding_modifiers, get_reedline_keycodes,
|
||||||
@ -35,7 +36,7 @@ impl Command for KeybindingsList {
|
|||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
description: "Get list of key modifiers",
|
description: "Get list of key modifiers",
|
||||||
example: "keybindings list -m",
|
example: "keybindings list --modifiers",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
@ -59,26 +60,22 @@ impl Command for KeybindingsList {
|
|||||||
_input: PipelineData,
|
_input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let records = if call.named_len() == 0 {
|
let records = if call.named_len() == 0 {
|
||||||
let all_options = vec!["modifiers", "keycodes", "edits", "modes", "events"];
|
let all_options = ["modifiers", "keycodes", "edits", "modes", "events"];
|
||||||
all_options
|
all_options
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|argument| get_records(argument, &call.head))
|
.flat_map(|argument| get_records(argument, call.head))
|
||||||
.collect()
|
.collect()
|
||||||
} else {
|
} else {
|
||||||
call.named_iter()
|
call.named_iter()
|
||||||
.flat_map(|(argument, _, _)| get_records(argument.item.as_str(), &call.head))
|
.flat_map(|(argument, _, _)| get_records(argument.item.as_str(), call.head))
|
||||||
.collect()
|
.collect()
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Value::List {
|
Ok(Value::list(records, call.head).into_pipeline_data())
|
||||||
vals: records,
|
|
||||||
span: call.head,
|
|
||||||
}
|
|
||||||
.into_pipeline_data())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_records(entry_type: &str, span: &Span) -> Vec<Value> {
|
fn get_records(entry_type: &str, span: Span) -> Vec<Value> {
|
||||||
let values = match entry_type {
|
let values = match entry_type {
|
||||||
"modifiers" => get_reedline_keybinding_modifiers().sorted(),
|
"modifiers" => get_reedline_keybinding_modifiers().sorted(),
|
||||||
"keycodes" => get_reedline_keycodes().sorted(),
|
"keycodes" => get_reedline_keycodes().sorted(),
|
||||||
@ -95,16 +92,14 @@ fn get_records(entry_type: &str, span: &Span) -> Vec<Value> {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn convert_to_record(edit: &str, entry_type: &str, span: &Span) -> Value {
|
fn convert_to_record(edit: &str, entry_type: &str, span: Span) -> Value {
|
||||||
let entry_type = Value::string(entry_type, *span);
|
Value::record(
|
||||||
|
record! {
|
||||||
let name = Value::string(edit, *span);
|
"type" => Value::string(entry_type, span),
|
||||||
|
"name" => Value::string(edit, span),
|
||||||
Value::Record {
|
},
|
||||||
cols: vec!["type".to_string(), "name".to_string()],
|
span,
|
||||||
vals: vec![entry_type, name],
|
)
|
||||||
span: *span,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper to sort a vec and return a vec
|
// Helper to sort a vec and return a vec
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
|
use crossterm::execute;
|
||||||
use crossterm::QueueableCommand;
|
use crossterm::QueueableCommand;
|
||||||
use crossterm::{event::Event, event::KeyCode, event::KeyEvent, terminal};
|
use crossterm::{event::Event, event::KeyCode, event::KeyEvent, terminal};
|
||||||
use nu_protocol::ast::Call;
|
use nu_protocol::ast::Call;
|
||||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type, Value,
|
record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type,
|
||||||
|
Value,
|
||||||
};
|
};
|
||||||
use std::io::{stdout, Write};
|
use std::io::{stdout, Write};
|
||||||
|
|
||||||
@ -19,6 +21,10 @@ impl Command for KeybindingsListen {
|
|||||||
"Get input from the user."
|
"Get input from the user."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn extra_usage(&self) -> &str {
|
||||||
|
"This is an internal debugging tool. For better output, try `input listen --types [key]`"
|
||||||
|
}
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.category(Category::Platform)
|
.category(Category::Platform)
|
||||||
@ -39,13 +45,13 @@ impl Command for KeybindingsListen {
|
|||||||
Ok(v) => Ok(v.into_pipeline_data()),
|
Ok(v) => Ok(v.into_pipeline_data()),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
terminal::disable_raw_mode()?;
|
terminal::disable_raw_mode()?;
|
||||||
Err(ShellError::GenericError(
|
Err(ShellError::GenericError {
|
||||||
"Error with input".to_string(),
|
error: "Error with input".into(),
|
||||||
"".to_string(),
|
msg: "".into(),
|
||||||
None,
|
span: None,
|
||||||
Some(e.to_string()),
|
help: Some(e.to_string()),
|
||||||
Vec::new(),
|
inner: vec![],
|
||||||
))
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -64,6 +70,32 @@ pub fn print_events(engine_state: &EngineState) -> Result<Value, ShellError> {
|
|||||||
|
|
||||||
stdout().flush()?;
|
stdout().flush()?;
|
||||||
terminal::enable_raw_mode()?;
|
terminal::enable_raw_mode()?;
|
||||||
|
|
||||||
|
if config.use_kitty_protocol {
|
||||||
|
if let Ok(false) = crossterm::terminal::supports_keyboard_enhancement() {
|
||||||
|
println!("WARN: The terminal doesn't support use_kitty_protocol config.\r");
|
||||||
|
}
|
||||||
|
|
||||||
|
// enable kitty protocol
|
||||||
|
//
|
||||||
|
// Note that, currently, only the following support this protocol:
|
||||||
|
// * [kitty terminal](https://sw.kovidgoyal.net/kitty/)
|
||||||
|
// * [foot terminal](https://codeberg.org/dnkl/foot/issues/319)
|
||||||
|
// * [WezTerm terminal](https://wezfurlong.org/wezterm/config/lua/config/enable_kitty_keyboard.html)
|
||||||
|
// * [notcurses library](https://github.com/dankamongmen/notcurses/issues/2131)
|
||||||
|
// * [neovim text editor](https://github.com/neovim/neovim/pull/18181)
|
||||||
|
// * [kakoune text editor](https://github.com/mawww/kakoune/issues/4103)
|
||||||
|
// * [dte text editor](https://gitlab.com/craigbarnes/dte/-/issues/138)
|
||||||
|
//
|
||||||
|
// Refer to https://sw.kovidgoyal.net/kitty/keyboard-protocol/ if you're curious.
|
||||||
|
let _ = execute!(
|
||||||
|
stdout(),
|
||||||
|
crossterm::event::PushKeyboardEnhancementFlags(
|
||||||
|
crossterm::event::KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
let mut stdout = std::io::BufWriter::new(std::io::stderr());
|
let mut stdout = std::io::BufWriter::new(std::io::stderr());
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@ -78,9 +110,8 @@ pub fn print_events(engine_state: &EngineState) -> Result<Value, ShellError> {
|
|||||||
let v = print_events_helper(event)?;
|
let v = print_events_helper(event)?;
|
||||||
// Print out the record
|
// Print out the record
|
||||||
let o = match v {
|
let o = match v {
|
||||||
Value::Record { cols, vals, .. } => cols
|
Value::Record { val, .. } => val
|
||||||
.iter()
|
.iter()
|
||||||
.zip(vals.iter())
|
|
||||||
.map(|(x, y)| format!("{}: {}", x, y.into_string("", config)))
|
.map(|(x, y)| format!("{}: {}", x, y.into_string("", config)))
|
||||||
.collect::<Vec<String>>()
|
.collect::<Vec<String>>()
|
||||||
.join(", "),
|
.join(", "),
|
||||||
@ -91,6 +122,14 @@ pub fn print_events(engine_state: &EngineState) -> Result<Value, ShellError> {
|
|||||||
stdout.queue(crossterm::style::Print("\r\n"))?;
|
stdout.queue(crossterm::style::Print("\r\n"))?;
|
||||||
stdout.flush()?;
|
stdout.flush()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.use_kitty_protocol {
|
||||||
|
let _ = execute!(
|
||||||
|
std::io::stdout(),
|
||||||
|
crossterm::event::PopKeyboardEnhancementFlags
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
terminal::disable_raw_mode()?;
|
terminal::disable_raw_mode()?;
|
||||||
|
|
||||||
Ok(Value::nothing(Span::unknown()))
|
Ok(Value::nothing(Span::unknown()))
|
||||||
@ -111,46 +150,29 @@ fn print_events_helper(event: Event) -> Result<Value, ShellError> {
|
|||||||
{
|
{
|
||||||
match code {
|
match code {
|
||||||
KeyCode::Char(c) => {
|
KeyCode::Char(c) => {
|
||||||
let record = Value::Record {
|
let record = record! {
|
||||||
cols: vec![
|
"char" => Value::string(format!("{c}"), Span::unknown()),
|
||||||
"char".into(),
|
"code" => Value::string(format!("{:#08x}", u32::from(c)), Span::unknown()),
|
||||||
"code".into(),
|
"modifier" => Value::string(format!("{modifiers:?}"), Span::unknown()),
|
||||||
"modifier".into(),
|
"flags" => Value::string(format!("{modifiers:#08b}"), Span::unknown()),
|
||||||
"flags".into(),
|
"kind" => Value::string(format!("{kind:?}"), Span::unknown()),
|
||||||
"kind".into(),
|
"state" => Value::string(format!("{state:?}"), Span::unknown()),
|
||||||
"state".into(),
|
|
||||||
],
|
|
||||||
vals: vec![
|
|
||||||
Value::string(format!("{c}"), Span::unknown()),
|
|
||||||
Value::string(format!("{:#08x}", u32::from(c)), Span::unknown()),
|
|
||||||
Value::string(format!("{modifiers:?}"), Span::unknown()),
|
|
||||||
Value::string(format!("{modifiers:#08b}"), Span::unknown()),
|
|
||||||
Value::string(format!("{kind:?}"), Span::unknown()),
|
|
||||||
Value::string(format!("{state:?}"), Span::unknown()),
|
|
||||||
],
|
|
||||||
span: Span::unknown(),
|
|
||||||
};
|
};
|
||||||
Ok(record)
|
Ok(Value::record(record, Span::unknown()))
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let record = Value::Record {
|
let record = record! {
|
||||||
cols: vec!["code".into(), "modifier".into(), "flags".into()],
|
"code" => Value::string(format!("{code:?}"), Span::unknown()),
|
||||||
vals: vec![
|
"modifier" => Value::string(format!("{modifiers:?}"), Span::unknown()),
|
||||||
Value::string(format!("{code:?}"), Span::unknown()),
|
"flags" => Value::string(format!("{modifiers:#08b}"), Span::unknown()),
|
||||||
Value::string(format!("{modifiers:?}"), Span::unknown()),
|
"kind" => Value::string(format!("{kind:?}"), Span::unknown()),
|
||||||
Value::string(format!("{modifiers:#08b}"), Span::unknown()),
|
"state" => Value::string(format!("{state:?}"), Span::unknown()),
|
||||||
],
|
|
||||||
span: Span::unknown(),
|
|
||||||
};
|
};
|
||||||
Ok(record)
|
Ok(Value::record(record, Span::unknown()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let record = Value::Record {
|
let record = record! { "event" => Value::string(format!("{event:?}"), Span::unknown()) };
|
||||||
cols: vec!["event".into()],
|
Ok(Value::record(record, Span::unknown()))
|
||||||
vals: vec![Value::string(format!("{event:?}"), Span::unknown())],
|
|
||||||
span: Span::unknown(),
|
|
||||||
};
|
|
||||||
Ok(record)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,13 @@
|
|||||||
mod commandline;
|
mod commandline;
|
||||||
mod default_context;
|
mod default_context;
|
||||||
mod history;
|
mod history;
|
||||||
mod history_session;
|
|
||||||
mod keybindings;
|
mod keybindings;
|
||||||
mod keybindings_default;
|
mod keybindings_default;
|
||||||
mod keybindings_list;
|
mod keybindings_list;
|
||||||
mod keybindings_listen;
|
mod keybindings_listen;
|
||||||
|
|
||||||
pub use commandline::Commandline;
|
pub use commandline::Commandline;
|
||||||
pub use history::History;
|
pub use history::{History, HistorySession};
|
||||||
pub use history_session::HistorySession;
|
|
||||||
pub use keybindings::Keybindings;
|
pub use keybindings::Keybindings;
|
||||||
pub use keybindings_default::KeybindingsDefault;
|
pub use keybindings_default::KeybindingsDefault;
|
||||||
pub use keybindings_list::KeybindingsList;
|
pub use keybindings_list::KeybindingsList;
|
||||||
|
@ -89,7 +89,7 @@ impl CommandCompletion {
|
|||||||
let filter_predicate = |command: &[u8]| match_algorithm.matches_u8(command, partial);
|
let filter_predicate = |command: &[u8]| match_algorithm.matches_u8(command, partial);
|
||||||
|
|
||||||
let mut results = working_set
|
let mut results = working_set
|
||||||
.find_commands_by_predicate(filter_predicate)
|
.find_commands_by_predicate(filter_predicate, true)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(move |x| Suggestion {
|
.map(move |x| Suggestion {
|
||||||
value: String::from_utf8_lossy(&x.0).to_string(),
|
value: String::from_utf8_lossy(&x.0).to_string(),
|
||||||
@ -205,10 +205,7 @@ impl Completer for CommandCompletion {
|
|||||||
vec![]
|
vec![]
|
||||||
};
|
};
|
||||||
|
|
||||||
subcommands
|
subcommands.into_iter().chain(commands).collect::<Vec<_>>()
|
||||||
.into_iter()
|
|
||||||
.chain(commands.into_iter())
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_sort_by(&self) -> SortBy {
|
fn get_sort_by(&self) -> SortBy {
|
||||||
@ -237,7 +234,7 @@ pub fn is_passthrough_command(working_set_file_contents: &[(Vec<u8>, usize, usiz
|
|||||||
let cur_pos = find_non_whitespace_index(contents, last_pipe_pos);
|
let cur_pos = find_non_whitespace_index(contents, last_pipe_pos);
|
||||||
|
|
||||||
let result = match contents.get(cur_pos..) {
|
let result = match contents.get(cur_pos..) {
|
||||||
Some(contents) => contents.starts_with(b"sudo "),
|
Some(contents) => contents.starts_with(b"sudo ") || contents.starts_with(b"doas "),
|
||||||
None => false,
|
None => false,
|
||||||
};
|
};
|
||||||
if result {
|
if result {
|
||||||
@ -266,7 +263,7 @@ mod command_completions_tests {
|
|||||||
(" hello sud", 1),
|
(" hello sud", 1),
|
||||||
];
|
];
|
||||||
for (idx, ele) in commands.iter().enumerate() {
|
for (idx, ele) in commands.iter().enumerate() {
|
||||||
let index = find_non_whitespace_index(&Vec::from(ele.0.as_bytes()), 0);
|
let index = find_non_whitespace_index(ele.0.as_bytes(), 0);
|
||||||
assert_eq!(index, ele.1, "Failed on index {}", idx);
|
assert_eq!(index, ele.1, "Failed on index {}", idx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use crate::completions::{
|
use crate::completions::{
|
||||||
CommandCompletion, Completer, CompletionOptions, CustomCompletion, DirectoryCompletion,
|
CommandCompletion, Completer, CompletionOptions, CustomCompletion, DirectoryCompletion,
|
||||||
DotNuCompletion, FileCompletion, FlagCompletion, MatchAlgorithm, VariableCompletion,
|
DotNuCompletion, FileCompletion, FlagCompletion, VariableCompletion,
|
||||||
};
|
};
|
||||||
use nu_engine::eval_block;
|
use nu_engine::eval_block;
|
||||||
use nu_parser::{flatten_expression, parse, FlatShape};
|
use nu_parser::{flatten_expression, parse, FlatShape};
|
||||||
@ -39,15 +39,12 @@ impl NuCompleter {
|
|||||||
) -> Vec<Suggestion> {
|
) -> Vec<Suggestion> {
|
||||||
let config = self.engine_state.get_config();
|
let config = self.engine_state.get_config();
|
||||||
|
|
||||||
let mut options = CompletionOptions {
|
let options = CompletionOptions {
|
||||||
case_sensitive: config.case_sensitive_completions,
|
case_sensitive: config.case_sensitive_completions,
|
||||||
|
match_algorithm: config.completion_algorithm.into(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
if config.completion_algorithm == "fuzzy" {
|
|
||||||
options.match_algorithm = MatchAlgorithm::Fuzzy;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch
|
// Fetch
|
||||||
let mut suggestions =
|
let mut suggestions =
|
||||||
completer.fetch(working_set, prefix.clone(), new_span, offset, pos, &options);
|
completer.fetch(working_set, prefix.clone(), new_span, offset, pos, &options);
|
||||||
@ -67,20 +64,20 @@ impl NuCompleter {
|
|||||||
) -> Option<Vec<Suggestion>> {
|
) -> Option<Vec<Suggestion>> {
|
||||||
let stack = self.stack.clone();
|
let stack = self.stack.clone();
|
||||||
let block = self.engine_state.get_block(block_id);
|
let block = self.engine_state.get_block(block_id);
|
||||||
let mut callee_stack = stack.gather_captures(&block.captures);
|
let mut callee_stack = stack.gather_captures(&self.engine_state, &block.captures);
|
||||||
|
|
||||||
// Line
|
// Line
|
||||||
if let Some(pos_arg) = block.signature.required_positional.get(0) {
|
if let Some(pos_arg) = block.signature.required_positional.first() {
|
||||||
if let Some(var_id) = pos_arg.var_id {
|
if let Some(var_id) = pos_arg.var_id {
|
||||||
callee_stack.add_var(
|
callee_stack.add_var(
|
||||||
var_id,
|
var_id,
|
||||||
Value::List {
|
Value::list(
|
||||||
vals: spans
|
spans
|
||||||
.iter()
|
.iter()
|
||||||
.map(|it| Value::string(it, Span::unknown()))
|
.map(|it| Value::string(it, Span::unknown()))
|
||||||
.collect(),
|
.collect(),
|
||||||
span: Span::unknown(),
|
Span::unknown(),
|
||||||
},
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -97,7 +94,7 @@ impl NuCompleter {
|
|||||||
match result {
|
match result {
|
||||||
Ok(pd) => {
|
Ok(pd) => {
|
||||||
let value = pd.into_value(span);
|
let value = pd.into_value(span);
|
||||||
if let Value::List { vals, span: _ } = value {
|
if let Value::List { vals, .. } = value {
|
||||||
let result =
|
let result =
|
||||||
map_value_completions(vals.iter(), Span::new(span.start, span.end), offset);
|
map_value_completions(vals.iter(), Span::new(span.start, span.end), offset);
|
||||||
|
|
||||||
@ -125,35 +122,45 @@ impl NuCompleter {
|
|||||||
for pipeline_element in pipeline.elements {
|
for pipeline_element in pipeline.elements {
|
||||||
match pipeline_element {
|
match pipeline_element {
|
||||||
PipelineElement::Expression(_, expr)
|
PipelineElement::Expression(_, expr)
|
||||||
| PipelineElement::Redirection(_, _, expr)
|
| PipelineElement::Redirection(_, _, expr, _)
|
||||||
| PipelineElement::And(_, expr)
|
| PipelineElement::And(_, expr)
|
||||||
| PipelineElement::Or(_, expr)
|
| PipelineElement::Or(_, expr)
|
||||||
| PipelineElement::SameTargetRedirection { cmd: (_, expr), .. }
|
| PipelineElement::SameTargetRedirection { cmd: (_, expr), .. }
|
||||||
| PipelineElement::SeparateRedirection { out: (_, expr), .. } => {
|
| PipelineElement::SeparateRedirection {
|
||||||
|
out: (_, expr, _), ..
|
||||||
|
} => {
|
||||||
let flattened: Vec<_> = flatten_expression(&working_set, &expr);
|
let flattened: Vec<_> = flatten_expression(&working_set, &expr);
|
||||||
let mut spans: Vec<String> = vec![];
|
let mut spans: Vec<String> = vec![];
|
||||||
|
|
||||||
for (flat_idx, flat) in flattened.iter().enumerate() {
|
for (flat_idx, flat) in flattened.iter().enumerate() {
|
||||||
let is_passthrough_command = spans
|
let is_passthrough_command = spans
|
||||||
.first()
|
.first()
|
||||||
.filter(|content| *content == &String::from("sudo"))
|
.filter(|content| {
|
||||||
|
content.as_str() == "sudo" || content.as_str() == "doas"
|
||||||
|
})
|
||||||
.is_some();
|
.is_some();
|
||||||
// Read the current spam to string
|
// Read the current spam to string
|
||||||
let current_span = working_set.get_span_contents(flat.0).to_vec();
|
let current_span = working_set.get_span_contents(flat.0).to_vec();
|
||||||
let current_span_str = String::from_utf8_lossy(¤t_span);
|
let current_span_str = String::from_utf8_lossy(¤t_span);
|
||||||
|
|
||||||
|
let is_last_span = pos >= flat.0.start && pos < flat.0.end;
|
||||||
|
|
||||||
// Skip the last 'a' as span item
|
// Skip the last 'a' as span item
|
||||||
if flat_idx == flattened.len() - 1 {
|
if is_last_span {
|
||||||
let mut chars = current_span_str.chars();
|
let offset = pos - flat.0.start;
|
||||||
chars.next_back();
|
if offset == 0 {
|
||||||
let current_span_str = chars.as_str().to_owned();
|
spans.push(String::new())
|
||||||
spans.push(current_span_str.to_string());
|
} else {
|
||||||
|
let mut current_span_str = current_span_str.to_string();
|
||||||
|
current_span_str.remove(offset);
|
||||||
|
spans.push(current_span_str);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
spans.push(current_span_str.to_string());
|
spans.push(current_span_str.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complete based on the last span
|
// Complete based on the last span
|
||||||
if pos >= flat.0.start && pos < flat.0.end {
|
if is_last_span {
|
||||||
// Context variables
|
// Context variables
|
||||||
let most_left_var =
|
let most_left_var =
|
||||||
most_left_variable(flat_idx, &working_set, flattened.clone());
|
most_left_variable(flat_idx, &working_set, flattened.clone());
|
||||||
@ -243,7 +250,9 @@ impl NuCompleter {
|
|||||||
working_set.get_span_contents(previous_expr.0).to_vec();
|
working_set.get_span_contents(previous_expr.0).to_vec();
|
||||||
|
|
||||||
// Completion for .nu files
|
// Completion for .nu files
|
||||||
if prev_expr_str == b"use" || prev_expr_str == b"source-env"
|
if prev_expr_str == b"use"
|
||||||
|
|| prev_expr_str == b"overlay use"
|
||||||
|
|| prev_expr_str == b"source-env"
|
||||||
{
|
{
|
||||||
let mut completer =
|
let mut completer =
|
||||||
DotNuCompletion::new(self.engine_state.clone());
|
DotNuCompletion::new(self.engine_state.clone());
|
||||||
@ -454,7 +463,7 @@ pub fn map_value_completions<'a>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Match for record values
|
// Match for record values
|
||||||
if let Ok((cols, vals)) = x.as_record() {
|
if let Ok(record) = x.as_record() {
|
||||||
let mut suggestion = Suggestion {
|
let mut suggestion = Suggestion {
|
||||||
value: String::from(""), // Initialize with empty string
|
value: String::from(""), // Initialize with empty string
|
||||||
description: None,
|
description: None,
|
||||||
@ -467,7 +476,7 @@ pub fn map_value_completions<'a>(
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Iterate the cols looking for `value` and `description`
|
// Iterate the cols looking for `value` and `description`
|
||||||
cols.iter().zip(vals).for_each(|it| {
|
record.iter().for_each(|it| {
|
||||||
// Match `value` column
|
// Match `value` column
|
||||||
if it.0 == "value" {
|
if it.0 == "value" {
|
||||||
// Convert the value to string
|
// Convert the value to string
|
||||||
|
199
crates/nu-cli/src/completions/completion_common.rs
Normal file
199
crates/nu-cli/src/completions/completion_common.rs
Normal file
@ -0,0 +1,199 @@
|
|||||||
|
use crate::completions::{matches, CompletionOptions};
|
||||||
|
use nu_path::home_dir;
|
||||||
|
use nu_protocol::{engine::StateWorkingSet, Span};
|
||||||
|
use std::path::{is_separator, Component, Path, PathBuf, MAIN_SEPARATOR as SEP};
|
||||||
|
|
||||||
|
fn complete_rec(
|
||||||
|
partial: &[String],
|
||||||
|
cwd: &Path,
|
||||||
|
options: &CompletionOptions,
|
||||||
|
dir: bool,
|
||||||
|
isdir: bool,
|
||||||
|
) -> Vec<PathBuf> {
|
||||||
|
let mut completions = vec![];
|
||||||
|
|
||||||
|
if let Ok(result) = cwd.read_dir() {
|
||||||
|
for entry in result.filter_map(|e| e.ok()) {
|
||||||
|
let entry_name = entry.file_name().to_string_lossy().into_owned();
|
||||||
|
let path = entry.path();
|
||||||
|
|
||||||
|
if !dir || path.is_dir() {
|
||||||
|
match partial.first() {
|
||||||
|
Some(base) if matches(base, &entry_name, options) => {
|
||||||
|
let partial = &partial[1..];
|
||||||
|
if !partial.is_empty() || isdir {
|
||||||
|
completions.extend(complete_rec(partial, &path, options, dir, isdir));
|
||||||
|
if entry_name.eq(base) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
completions.push(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => completions.push(path),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
completions
|
||||||
|
}
|
||||||
|
|
||||||
|
enum OriginalCwd {
|
||||||
|
None,
|
||||||
|
Home(PathBuf),
|
||||||
|
Some(PathBuf),
|
||||||
|
// referencing a single local file
|
||||||
|
Local(PathBuf),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OriginalCwd {
|
||||||
|
fn apply(&self, p: &Path) -> String {
|
||||||
|
let mut ret = match self {
|
||||||
|
Self::None => p.to_string_lossy().into_owned(),
|
||||||
|
Self::Some(base) => pathdiff::diff_paths(p, base)
|
||||||
|
.unwrap_or(p.to_path_buf())
|
||||||
|
.to_string_lossy()
|
||||||
|
.into_owned(),
|
||||||
|
Self::Home(home) => match p.strip_prefix(home) {
|
||||||
|
Ok(suffix) => format!("~{}{}", SEP, suffix.to_string_lossy()),
|
||||||
|
_ => p.to_string_lossy().into_owned(),
|
||||||
|
},
|
||||||
|
Self::Local(base) => Path::new(".")
|
||||||
|
.join(pathdiff::diff_paths(p, base).unwrap_or(p.to_path_buf()))
|
||||||
|
.to_string_lossy()
|
||||||
|
.into_owned(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if p.is_dir() {
|
||||||
|
ret.push(SEP);
|
||||||
|
}
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn surround_remove(partial: &str) -> String {
|
||||||
|
for c in ['`', '"', '\''] {
|
||||||
|
if partial.starts_with(c) {
|
||||||
|
let ret = partial.strip_prefix(c).unwrap_or(partial);
|
||||||
|
return match ret.split(c).collect::<Vec<_>>()[..] {
|
||||||
|
[inside] => inside.to_string(),
|
||||||
|
[inside, outside] if inside.ends_with(is_separator) => format!("{inside}{outside}"),
|
||||||
|
_ => ret.to_string(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
partial.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn complete_item(
|
||||||
|
want_directory: bool,
|
||||||
|
span: nu_protocol::Span,
|
||||||
|
partial: &str,
|
||||||
|
cwd: &str,
|
||||||
|
options: &CompletionOptions,
|
||||||
|
) -> Vec<(nu_protocol::Span, String)> {
|
||||||
|
let partial = surround_remove(partial);
|
||||||
|
let isdir = partial.ends_with(is_separator);
|
||||||
|
let cwd_pathbuf = Path::new(cwd).to_path_buf();
|
||||||
|
let mut original_cwd = OriginalCwd::None;
|
||||||
|
let mut components = Path::new(&partial).components().peekable();
|
||||||
|
let mut cwd = match components.peek().cloned() {
|
||||||
|
Some(c @ Component::Prefix(..)) => {
|
||||||
|
// windows only by definition
|
||||||
|
components.next();
|
||||||
|
if let Some(Component::RootDir) = components.peek().cloned() {
|
||||||
|
components.next();
|
||||||
|
};
|
||||||
|
[c, Component::RootDir].iter().collect()
|
||||||
|
}
|
||||||
|
Some(c @ Component::RootDir) => {
|
||||||
|
components.next();
|
||||||
|
PathBuf::from(c.as_os_str())
|
||||||
|
}
|
||||||
|
Some(Component::Normal(home)) if home.to_string_lossy() == "~" => {
|
||||||
|
components.next();
|
||||||
|
original_cwd = OriginalCwd::Home(home_dir().unwrap_or(cwd_pathbuf.clone()));
|
||||||
|
home_dir().unwrap_or(cwd_pathbuf)
|
||||||
|
}
|
||||||
|
Some(Component::CurDir) => {
|
||||||
|
components.next();
|
||||||
|
original_cwd = match components.peek().cloned() {
|
||||||
|
Some(Component::Normal(_)) | None => OriginalCwd::Local(cwd_pathbuf.clone()),
|
||||||
|
_ => OriginalCwd::Some(cwd_pathbuf.clone()),
|
||||||
|
};
|
||||||
|
cwd_pathbuf
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
original_cwd = OriginalCwd::Some(cwd_pathbuf.clone());
|
||||||
|
cwd_pathbuf
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut partial = vec![];
|
||||||
|
|
||||||
|
for component in components {
|
||||||
|
match component {
|
||||||
|
Component::Prefix(..) => unreachable!(),
|
||||||
|
Component::RootDir => unreachable!(),
|
||||||
|
Component::CurDir => {}
|
||||||
|
Component::ParentDir => {
|
||||||
|
if partial.pop().is_none() {
|
||||||
|
cwd.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Component::Normal(c) => partial.push(c.to_string_lossy().into_owned()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
complete_rec(partial.as_slice(), &cwd, options, want_directory, isdir)
|
||||||
|
.into_iter()
|
||||||
|
.map(|p| (span, escape_path(original_cwd.apply(&p), want_directory)))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fix files or folders with quotes or hashes
|
||||||
|
pub fn escape_path(path: String, dir: bool) -> String {
|
||||||
|
let filename_contaminated = !dir && path.contains(['\'', '"', ' ', '#', '(', ')']);
|
||||||
|
let dirname_contaminated = dir && path.contains(['\'', '"', ' ', '#']);
|
||||||
|
let maybe_flag = path.starts_with('-');
|
||||||
|
let maybe_number = path.parse::<f64>().is_ok();
|
||||||
|
if filename_contaminated || dirname_contaminated || maybe_flag || maybe_number {
|
||||||
|
format!("`{path}`")
|
||||||
|
} else {
|
||||||
|
path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct AdjustView {
|
||||||
|
pub prefix: String,
|
||||||
|
pub span: Span,
|
||||||
|
pub readjusted: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn adjust_if_intermediate(
|
||||||
|
prefix: &[u8],
|
||||||
|
working_set: &StateWorkingSet,
|
||||||
|
mut span: nu_protocol::Span,
|
||||||
|
) -> AdjustView {
|
||||||
|
let span_contents = String::from_utf8_lossy(working_set.get_span_contents(span)).to_string();
|
||||||
|
let mut prefix = String::from_utf8_lossy(prefix).to_string();
|
||||||
|
|
||||||
|
// A difference of 1 because of the cursor's unicode code point in between.
|
||||||
|
// Using .chars().count() because unicode and Windows.
|
||||||
|
let readjusted = span_contents.chars().count() - prefix.chars().count() > 1;
|
||||||
|
if readjusted {
|
||||||
|
let remnant: String = span_contents
|
||||||
|
.chars()
|
||||||
|
.skip(prefix.chars().count() + 1)
|
||||||
|
.take_while(|&c| !is_separator(c))
|
||||||
|
.collect();
|
||||||
|
prefix.push_str(&remnant);
|
||||||
|
span = Span::new(span.start, span.start + prefix.chars().count() + 1);
|
||||||
|
}
|
||||||
|
AdjustView {
|
||||||
|
prefix,
|
||||||
|
span,
|
||||||
|
readjusted,
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@ use std::fmt::Display;
|
|||||||
|
|
||||||
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
|
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
|
||||||
use nu_parser::trim_quotes_str;
|
use nu_parser::trim_quotes_str;
|
||||||
|
use nu_protocol::CompletionAlgorithm;
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub enum SortBy {
|
pub enum SortBy {
|
||||||
@ -55,6 +56,15 @@ impl MatchAlgorithm {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<CompletionAlgorithm> for MatchAlgorithm {
|
||||||
|
fn from(value: CompletionAlgorithm) -> Self {
|
||||||
|
match value {
|
||||||
|
CompletionAlgorithm::Prefix => MatchAlgorithm::Prefix,
|
||||||
|
CompletionAlgorithm::Fuzzy => MatchAlgorithm::Fuzzy,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl TryFrom<String> for MatchAlgorithm {
|
impl TryFrom<String> for MatchAlgorithm {
|
||||||
type Error = InvalidMatchAlgorithm;
|
type Error = InvalidMatchAlgorithm;
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ use nu_protocol::{
|
|||||||
engine::{EngineState, Stack, StateWorkingSet},
|
engine::{EngineState, Stack, StateWorkingSet},
|
||||||
PipelineData, Span, Type, Value,
|
PipelineData, Span, Type, Value,
|
||||||
};
|
};
|
||||||
|
use nu_utils::IgnoreCaseExt;
|
||||||
use reedline::Suggestion;
|
use reedline::Suggestion;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@ -79,21 +80,20 @@ impl Completer for CustomCompletion {
|
|||||||
.map(|pd| {
|
.map(|pd| {
|
||||||
let value = pd.into_value(span);
|
let value = pd.into_value(span);
|
||||||
match &value {
|
match &value {
|
||||||
Value::Record { .. } => {
|
Value::Record { val, .. } => {
|
||||||
let completions = value
|
let completions = val
|
||||||
.get_data_by_key("completions")
|
.get("completions")
|
||||||
.and_then(|val| {
|
.and_then(|val| {
|
||||||
val.as_list()
|
val.as_list()
|
||||||
.ok()
|
.ok()
|
||||||
.map(|it| map_value_completions(it.iter(), span, offset))
|
.map(|it| map_value_completions(it.iter(), span, offset))
|
||||||
})
|
})
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let options = value.get_data_by_key("options");
|
let options = val.get("options");
|
||||||
|
|
||||||
if let Some(Value::Record { .. }) = &options {
|
if let Some(Value::Record { val: options, .. }) = &options {
|
||||||
let options = options.unwrap_or_default();
|
|
||||||
let should_sort = options
|
let should_sort = options
|
||||||
.get_data_by_key("sort")
|
.get("sort")
|
||||||
.and_then(|val| val.as_bool().ok())
|
.and_then(|val| val.as_bool().ok())
|
||||||
.unwrap_or(false);
|
.unwrap_or(false);
|
||||||
|
|
||||||
@ -103,11 +103,11 @@ impl Completer for CustomCompletion {
|
|||||||
|
|
||||||
custom_completion_options = Some(CompletionOptions {
|
custom_completion_options = Some(CompletionOptions {
|
||||||
case_sensitive: options
|
case_sensitive: options
|
||||||
.get_data_by_key("case_sensitive")
|
.get("case_sensitive")
|
||||||
.and_then(|val| val.as_bool().ok())
|
.and_then(|val| val.as_bool().ok())
|
||||||
.unwrap_or(true),
|
.unwrap_or(true),
|
||||||
positional: options
|
positional: options
|
||||||
.get_data_by_key("positional")
|
.get("positional")
|
||||||
.and_then(|val| val.as_bool().ok())
|
.and_then(|val| val.as_bool().ok())
|
||||||
.unwrap_or(true),
|
.unwrap_or(true),
|
||||||
sort_by: if should_sort {
|
sort_by: if should_sort {
|
||||||
@ -115,9 +115,7 @@ impl Completer for CustomCompletion {
|
|||||||
} else {
|
} else {
|
||||||
SortBy::None
|
SortBy::None
|
||||||
},
|
},
|
||||||
match_algorithm: match options
|
match_algorithm: match options.get("completion_algorithm") {
|
||||||
.get_data_by_key("completion_algorithm")
|
|
||||||
{
|
|
||||||
Some(option) => option
|
Some(option) => option
|
||||||
.as_string()
|
.as_string()
|
||||||
.ok()
|
.ok()
|
||||||
@ -156,8 +154,8 @@ fn filter(prefix: &[u8], items: Vec<Suggestion>, options: &CompletionOptions) ->
|
|||||||
(true, true) => it.value.as_bytes().starts_with(prefix),
|
(true, true) => it.value.as_bytes().starts_with(prefix),
|
||||||
(true, false) => it.value.contains(std::str::from_utf8(prefix).unwrap_or("")),
|
(true, false) => it.value.contains(std::str::from_utf8(prefix).unwrap_or("")),
|
||||||
(false, positional) => {
|
(false, positional) => {
|
||||||
let value = it.value.to_lowercase();
|
let value = it.value.to_folded_case();
|
||||||
let prefix = std::str::from_utf8(prefix).unwrap_or("").to_lowercase();
|
let prefix = std::str::from_utf8(prefix).unwrap_or("").to_folded_case();
|
||||||
if positional {
|
if positional {
|
||||||
value.starts_with(&prefix)
|
value.starts_with(&prefix)
|
||||||
} else {
|
} else {
|
||||||
|
@ -1,17 +1,15 @@
|
|||||||
use crate::completions::{matches, Completer, CompletionOptions};
|
use crate::completions::{
|
||||||
|
completion_common::{adjust_if_intermediate, complete_item, AdjustView},
|
||||||
|
Completer, CompletionOptions, SortBy,
|
||||||
|
};
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
engine::{EngineState, StateWorkingSet},
|
engine::{EngineState, StateWorkingSet},
|
||||||
levenshtein_distance, Span,
|
levenshtein_distance, Span,
|
||||||
};
|
};
|
||||||
use reedline::Suggestion;
|
use reedline::Suggestion;
|
||||||
use std::fs;
|
use std::path::{Path, MAIN_SEPARATOR as SEP};
|
||||||
use std::path::Path;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use super::{partial_from, prepend_base_dir, SortBy};
|
|
||||||
|
|
||||||
const SEP: char = std::path::MAIN_SEPARATOR;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct DirectoryCompletion {
|
pub struct DirectoryCompletion {
|
||||||
engine_state: Arc<EngineState>,
|
engine_state: Arc<EngineState>,
|
||||||
@ -26,30 +24,34 @@ impl DirectoryCompletion {
|
|||||||
impl Completer for DirectoryCompletion {
|
impl Completer for DirectoryCompletion {
|
||||||
fn fetch(
|
fn fetch(
|
||||||
&mut self,
|
&mut self,
|
||||||
_: &StateWorkingSet,
|
working_set: &StateWorkingSet,
|
||||||
prefix: Vec<u8>,
|
prefix: Vec<u8>,
|
||||||
span: Span,
|
span: Span,
|
||||||
offset: usize,
|
offset: usize,
|
||||||
_: usize,
|
_: usize,
|
||||||
options: &CompletionOptions,
|
options: &CompletionOptions,
|
||||||
) -> Vec<Suggestion> {
|
) -> Vec<Suggestion> {
|
||||||
let cwd = self.engine_state.current_work_dir();
|
let AdjustView { prefix, span, .. } = adjust_if_intermediate(&prefix, working_set, span);
|
||||||
let partial = String::from_utf8_lossy(&prefix).to_string();
|
|
||||||
|
|
||||||
// Filter only the folders
|
// Filter only the folders
|
||||||
let output: Vec<_> = directory_completion(span, &partial, &cwd, options)
|
let output: Vec<_> = directory_completion(
|
||||||
.into_iter()
|
span,
|
||||||
.map(move |x| Suggestion {
|
&prefix,
|
||||||
value: x.1,
|
&self.engine_state.current_work_dir(),
|
||||||
description: None,
|
options,
|
||||||
extra: None,
|
)
|
||||||
span: reedline::Span {
|
.into_iter()
|
||||||
start: x.0.start - offset,
|
.map(move |x| Suggestion {
|
||||||
end: x.0.end - offset,
|
value: x.1,
|
||||||
},
|
description: None,
|
||||||
append_whitespace: false,
|
extra: None,
|
||||||
})
|
span: reedline::Span {
|
||||||
.collect();
|
start: x.0.start - offset,
|
||||||
|
end: x.0.end - offset,
|
||||||
|
},
|
||||||
|
append_whitespace: false,
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
@ -63,7 +65,12 @@ impl Completer for DirectoryCompletion {
|
|||||||
|
|
||||||
match self.get_sort_by() {
|
match self.get_sort_by() {
|
||||||
SortBy::Ascending => {
|
SortBy::Ascending => {
|
||||||
sorted_items.sort_by(|a, b| a.value.cmp(&b.value));
|
sorted_items.sort_by(|a, b| {
|
||||||
|
// Ignore trailing slashes in folder names when sorting
|
||||||
|
a.value
|
||||||
|
.trim_end_matches(SEP)
|
||||||
|
.cmp(b.value.trim_end_matches(SEP))
|
||||||
|
});
|
||||||
}
|
}
|
||||||
SortBy::LevenshteinDistance => {
|
SortBy::LevenshteinDistance => {
|
||||||
sorted_items.sort_by(|a, b| {
|
sorted_items.sort_by(|a, b| {
|
||||||
@ -106,60 +113,5 @@ pub fn directory_completion(
|
|||||||
cwd: &str,
|
cwd: &str,
|
||||||
options: &CompletionOptions,
|
options: &CompletionOptions,
|
||||||
) -> Vec<(nu_protocol::Span, String)> {
|
) -> Vec<(nu_protocol::Span, String)> {
|
||||||
let original_input = partial;
|
complete_item(true, span, partial, cwd, options)
|
||||||
|
|
||||||
let (base_dir_name, partial) = partial_from(partial);
|
|
||||||
|
|
||||||
let base_dir = nu_path::expand_path_with(&base_dir_name, cwd);
|
|
||||||
|
|
||||||
// This check is here as base_dir.read_dir() with base_dir == "" will open the current dir
|
|
||||||
// which we don't want in this case (if we did, base_dir would already be ".")
|
|
||||||
if base_dir == Path::new("") {
|
|
||||||
return Vec::new();
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Ok(result) = base_dir.read_dir() {
|
|
||||||
return result
|
|
||||||
.filter_map(|entry| {
|
|
||||||
entry.ok().and_then(|entry| {
|
|
||||||
if let Ok(metadata) = fs::metadata(entry.path()) {
|
|
||||||
if metadata.is_dir() {
|
|
||||||
let mut file_name = entry.file_name().to_string_lossy().into_owned();
|
|
||||||
if matches(&partial, &file_name, options) {
|
|
||||||
let mut path = if prepend_base_dir(original_input, &base_dir_name) {
|
|
||||||
format!("{base_dir_name}{file_name}")
|
|
||||||
} else {
|
|
||||||
file_name.to_string()
|
|
||||||
};
|
|
||||||
|
|
||||||
if entry.path().is_dir() {
|
|
||||||
path.push(SEP);
|
|
||||||
file_name.push(SEP);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fix files or folders with quotes or hash
|
|
||||||
if path.contains('\'')
|
|
||||||
|| path.contains('"')
|
|
||||||
|| path.contains(' ')
|
|
||||||
|| path.contains('#')
|
|
||||||
{
|
|
||||||
path = format!("`{path}`");
|
|
||||||
}
|
|
||||||
|
|
||||||
Some((span, path))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
}
|
|
||||||
|
|
||||||
Vec::new()
|
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
use crate::completions::{
|
use crate::completions::{file_path_completion, Completer, CompletionOptions, SortBy};
|
||||||
file_path_completion, partial_from, Completer, CompletionOptions, SortBy,
|
|
||||||
};
|
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
engine::{EngineState, StateWorkingSet},
|
engine::{EngineState, StateWorkingSet},
|
||||||
Span,
|
Span,
|
||||||
};
|
};
|
||||||
use reedline::Suggestion;
|
use reedline::Suggestion;
|
||||||
use std::sync::Arc;
|
use std::{
|
||||||
const SEP: char = std::path::MAIN_SEPARATOR;
|
path::{is_separator, Path, MAIN_SEPARATOR as SEP, MAIN_SEPARATOR_STR},
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct DotNuCompletion {
|
pub struct DotNuCompletion {
|
||||||
@ -30,9 +30,16 @@ impl Completer for DotNuCompletion {
|
|||||||
_: usize,
|
_: usize,
|
||||||
options: &CompletionOptions,
|
options: &CompletionOptions,
|
||||||
) -> Vec<Suggestion> {
|
) -> Vec<Suggestion> {
|
||||||
let prefix_str = String::from_utf8_lossy(&prefix).to_string();
|
let prefix_str = String::from_utf8_lossy(&prefix).replace('`', "");
|
||||||
let mut search_dirs: Vec<String> = vec![];
|
let mut search_dirs: Vec<String> = vec![];
|
||||||
let (base_dir, mut partial) = partial_from(&prefix_str);
|
|
||||||
|
// If prefix_str is only a word we want to search in the current dir
|
||||||
|
let (base, partial) = prefix_str
|
||||||
|
.rsplit_once(is_separator)
|
||||||
|
.unwrap_or((".", &prefix_str));
|
||||||
|
let base_dir = base.replace(is_separator, MAIN_SEPARATOR_STR);
|
||||||
|
let mut partial = partial.to_string();
|
||||||
|
// On windows, this standardizes paths to use \
|
||||||
let mut is_current_folder = false;
|
let mut is_current_folder = false;
|
||||||
|
|
||||||
// Fetch the lib dirs
|
// Fetch the lib dirs
|
||||||
@ -58,7 +65,8 @@ impl Completer for DotNuCompletion {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Check if the base_dir is a folder
|
// Check if the base_dir is a folder
|
||||||
if base_dir != format!(".{SEP}") {
|
// rsplit_once removes the separator
|
||||||
|
if base_dir != "." {
|
||||||
// Add the base dir into the directories to be searched
|
// Add the base dir into the directories to be searched
|
||||||
search_dirs.push(base_dir.clone());
|
search_dirs.push(base_dir.clone());
|
||||||
|
|
||||||
@ -83,16 +91,21 @@ impl Completer for DotNuCompletion {
|
|||||||
// and transform them into suggestions
|
// and transform them into suggestions
|
||||||
let output: Vec<Suggestion> = search_dirs
|
let output: Vec<Suggestion> = search_dirs
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.flat_map(|it| {
|
.flat_map(|search_dir| {
|
||||||
file_path_completion(span, &partial, &it, options)
|
let completions = file_path_completion(span, &partial, &search_dir, options);
|
||||||
|
completions
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|it| {
|
.filter(move |it| {
|
||||||
// Different base dir, so we list the .nu files or folders
|
// Different base dir, so we list the .nu files or folders
|
||||||
if !is_current_folder {
|
if !is_current_folder {
|
||||||
it.1.ends_with(".nu") || it.1.ends_with(SEP)
|
it.1.ends_with(".nu") || it.1.ends_with(SEP)
|
||||||
} else {
|
} else {
|
||||||
// Lib dirs, so we filter only the .nu files
|
// Lib dirs, so we filter only the .nu files or directory modules
|
||||||
it.1.ends_with(".nu")
|
if it.1.ends_with(SEP) {
|
||||||
|
Path::new(&search_dir).join(&it.1).join("mod.nu").exists()
|
||||||
|
} else {
|
||||||
|
it.1.ends_with(".nu")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.map(move |x| Suggestion {
|
.map(move |x| Suggestion {
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
use crate::completions::{Completer, CompletionOptions};
|
use crate::completions::{
|
||||||
|
completion_common::{adjust_if_intermediate, complete_item, AdjustView},
|
||||||
|
Completer, CompletionOptions, SortBy,
|
||||||
|
};
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
engine::{EngineState, StateWorkingSet},
|
engine::{EngineState, StateWorkingSet},
|
||||||
levenshtein_distance, Span,
|
levenshtein_distance, Span,
|
||||||
};
|
};
|
||||||
|
use nu_utils::IgnoreCaseExt;
|
||||||
use reedline::Suggestion;
|
use reedline::Suggestion;
|
||||||
use std::path::{is_separator, Path};
|
use std::path::{Path, MAIN_SEPARATOR as SEP};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use super::SortBy;
|
|
||||||
|
|
||||||
const SEP: char = std::path::MAIN_SEPARATOR;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct FileCompletion {
|
pub struct FileCompletion {
|
||||||
engine_state: Arc<EngineState>,
|
engine_state: Arc<EngineState>,
|
||||||
@ -25,28 +25,38 @@ impl FileCompletion {
|
|||||||
impl Completer for FileCompletion {
|
impl Completer for FileCompletion {
|
||||||
fn fetch(
|
fn fetch(
|
||||||
&mut self,
|
&mut self,
|
||||||
_: &StateWorkingSet,
|
working_set: &StateWorkingSet,
|
||||||
prefix: Vec<u8>,
|
prefix: Vec<u8>,
|
||||||
span: Span,
|
span: Span,
|
||||||
offset: usize,
|
offset: usize,
|
||||||
_: usize,
|
_: usize,
|
||||||
options: &CompletionOptions,
|
options: &CompletionOptions,
|
||||||
) -> Vec<Suggestion> {
|
) -> Vec<Suggestion> {
|
||||||
let cwd = self.engine_state.current_work_dir();
|
let AdjustView {
|
||||||
let prefix = String::from_utf8_lossy(&prefix).to_string();
|
prefix,
|
||||||
let output: Vec<_> = file_path_completion(span, &prefix, &cwd, options)
|
span,
|
||||||
.into_iter()
|
readjusted,
|
||||||
.map(move |x| Suggestion {
|
} = adjust_if_intermediate(&prefix, working_set, span);
|
||||||
value: x.1,
|
|
||||||
description: None,
|
let output: Vec<_> = complete_item(
|
||||||
extra: None,
|
readjusted,
|
||||||
span: reedline::Span {
|
span,
|
||||||
start: x.0.start - offset,
|
&prefix,
|
||||||
end: x.0.end - offset,
|
&self.engine_state.current_work_dir(),
|
||||||
},
|
options,
|
||||||
append_whitespace: false,
|
)
|
||||||
})
|
.into_iter()
|
||||||
.collect();
|
.map(move |x| Suggestion {
|
||||||
|
value: x.1,
|
||||||
|
description: None,
|
||||||
|
extra: None,
|
||||||
|
span: reedline::Span {
|
||||||
|
start: x.0.start - offset,
|
||||||
|
end: x.0.end - offset,
|
||||||
|
},
|
||||||
|
append_whitespace: false,
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
@ -60,7 +70,12 @@ impl Completer for FileCompletion {
|
|||||||
|
|
||||||
match self.get_sort_by() {
|
match self.get_sort_by() {
|
||||||
SortBy::Ascending => {
|
SortBy::Ascending => {
|
||||||
sorted_items.sort_by(|a, b| a.value.cmp(&b.value));
|
sorted_items.sort_by(|a, b| {
|
||||||
|
// Ignore trailing slashes in folder names when sorting
|
||||||
|
a.value
|
||||||
|
.trim_end_matches(SEP)
|
||||||
|
.cmp(b.value.trim_end_matches(SEP))
|
||||||
|
});
|
||||||
}
|
}
|
||||||
SortBy::LevenshteinDistance => {
|
SortBy::LevenshteinDistance => {
|
||||||
sorted_items.sort_by(|a, b| {
|
sorted_items.sort_by(|a, b| {
|
||||||
@ -97,84 +112,13 @@ impl Completer for FileCompletion {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn partial_from(input: &str) -> (String, String) {
|
|
||||||
let partial = input.replace('`', "");
|
|
||||||
|
|
||||||
// If partial is only a word we want to search in the current dir
|
|
||||||
let (base, rest) = partial.rsplit_once(is_separator).unwrap_or((".", &partial));
|
|
||||||
// On windows, this standardizes paths to use \
|
|
||||||
let mut base = base.replace(is_separator, &SEP.to_string());
|
|
||||||
|
|
||||||
// rsplit_once removes the separator
|
|
||||||
base.push(SEP);
|
|
||||||
|
|
||||||
(base.to_string(), rest.to_string())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn file_path_completion(
|
pub fn file_path_completion(
|
||||||
span: nu_protocol::Span,
|
span: nu_protocol::Span,
|
||||||
partial: &str,
|
partial: &str,
|
||||||
cwd: &str,
|
cwd: &str,
|
||||||
options: &CompletionOptions,
|
options: &CompletionOptions,
|
||||||
) -> Vec<(nu_protocol::Span, String)> {
|
) -> Vec<(nu_protocol::Span, String)> {
|
||||||
let original_input = partial;
|
complete_item(false, span, partial, cwd, options)
|
||||||
let (base_dir_name, partial) = partial_from(partial);
|
|
||||||
|
|
||||||
let base_dir = nu_path::expand_path_with(&base_dir_name, cwd);
|
|
||||||
// This check is here as base_dir.read_dir() with base_dir == "" will open the current dir
|
|
||||||
// which we don't want in this case (if we did, base_dir would already be ".")
|
|
||||||
if base_dir == Path::new("") {
|
|
||||||
return Vec::new();
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Ok(result) = base_dir.read_dir() {
|
|
||||||
return result
|
|
||||||
.filter_map(|entry| {
|
|
||||||
entry.ok().and_then(|entry| {
|
|
||||||
let mut file_name = entry.file_name().to_string_lossy().into_owned();
|
|
||||||
if matches(&partial, &file_name, options) {
|
|
||||||
let mut path = if prepend_base_dir(original_input, &base_dir_name) {
|
|
||||||
format!("{base_dir_name}{file_name}")
|
|
||||||
} else {
|
|
||||||
file_name.to_string()
|
|
||||||
};
|
|
||||||
|
|
||||||
if entry.path().is_dir() {
|
|
||||||
path.push(SEP);
|
|
||||||
file_name.push(SEP);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fix files or folders with quotes or hashes
|
|
||||||
if path.contains('\'')
|
|
||||||
|| path.contains('"')
|
|
||||||
|| path.contains(' ')
|
|
||||||
|| path.contains('#')
|
|
||||||
|| path.contains('(')
|
|
||||||
|| path.contains(')')
|
|
||||||
|| path.starts_with('0')
|
|
||||||
|| path.starts_with('1')
|
|
||||||
|| path.starts_with('2')
|
|
||||||
|| path.starts_with('3')
|
|
||||||
|| path.starts_with('4')
|
|
||||||
|| path.starts_with('5')
|
|
||||||
|| path.starts_with('6')
|
|
||||||
|| path.starts_with('7')
|
|
||||||
|| path.starts_with('8')
|
|
||||||
|| path.starts_with('9')
|
|
||||||
{
|
|
||||||
path = format!("`{path}`");
|
|
||||||
}
|
|
||||||
|
|
||||||
Some((span, path))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
}
|
|
||||||
|
|
||||||
Vec::new()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn matches(partial: &str, from: &str, options: &CompletionOptions) -> bool {
|
pub fn matches(partial: &str, from: &str, options: &CompletionOptions) -> bool {
|
||||||
@ -182,28 +126,8 @@ pub fn matches(partial: &str, from: &str, options: &CompletionOptions) -> bool {
|
|||||||
if !options.case_sensitive {
|
if !options.case_sensitive {
|
||||||
return options
|
return options
|
||||||
.match_algorithm
|
.match_algorithm
|
||||||
.matches_str(&from.to_ascii_lowercase(), &partial.to_ascii_lowercase());
|
.matches_str(&from.to_folded_case(), &partial.to_folded_case());
|
||||||
}
|
}
|
||||||
|
|
||||||
options.match_algorithm.matches_str(from, partial)
|
options.match_algorithm.matches_str(from, partial)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns whether the base_dir should be prepended to the file path
|
|
||||||
pub fn prepend_base_dir(input: &str, base_dir: &str) -> bool {
|
|
||||||
if base_dir == format!(".{SEP}") {
|
|
||||||
// if the current base_dir path is the local folder we only add a "./" prefix if the user
|
|
||||||
// input already includes a local folder prefix.
|
|
||||||
let manually_entered = {
|
|
||||||
let mut chars = input.chars();
|
|
||||||
let first_char = chars.next();
|
|
||||||
let second_char = chars.next();
|
|
||||||
|
|
||||||
first_char == Some('.') && second_char.map(is_separator).unwrap_or(false)
|
|
||||||
};
|
|
||||||
|
|
||||||
manually_entered
|
|
||||||
} else {
|
|
||||||
// always prepend the base dir if it is a subfolder
|
|
||||||
true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
mod base;
|
mod base;
|
||||||
mod command_completions;
|
mod command_completions;
|
||||||
mod completer;
|
mod completer;
|
||||||
|
mod completion_common;
|
||||||
mod completion_options;
|
mod completion_options;
|
||||||
mod custom_completions;
|
mod custom_completions;
|
||||||
mod directory_completions;
|
mod directory_completions;
|
||||||
@ -16,8 +17,6 @@ pub use completion_options::{CompletionOptions, MatchAlgorithm, SortBy};
|
|||||||
pub use custom_completions::CustomCompletion;
|
pub use custom_completions::CustomCompletion;
|
||||||
pub use directory_completions::DirectoryCompletion;
|
pub use directory_completions::DirectoryCompletion;
|
||||||
pub use dotnu_completions::DotNuCompletion;
|
pub use dotnu_completions::DotNuCompletion;
|
||||||
pub use file_completions::{
|
pub use file_completions::{file_path_completion, matches, FileCompletion};
|
||||||
file_path_completion, matches, partial_from, prepend_base_dir, FileCompletion,
|
|
||||||
};
|
|
||||||
pub use flag_completions::FlagCompletion;
|
pub use flag_completions::FlagCompletion;
|
||||||
pub use variable_completions::VariableCompletion;
|
pub use variable_completions::VariableCompletion;
|
||||||
|
@ -43,10 +43,8 @@ impl Completer for VariableCompletion {
|
|||||||
options: &CompletionOptions,
|
options: &CompletionOptions,
|
||||||
) -> Vec<Suggestion> {
|
) -> Vec<Suggestion> {
|
||||||
let mut output = vec![];
|
let mut output = vec![];
|
||||||
let builtins = ["$nu", "$in", "$env", "$nothing"];
|
let builtins = ["$nu", "$in", "$env"];
|
||||||
let var_str = std::str::from_utf8(&self.var_context.0)
|
let var_str = std::str::from_utf8(&self.var_context.0).unwrap_or("");
|
||||||
.unwrap_or("")
|
|
||||||
.to_lowercase();
|
|
||||||
let var_id = working_set.find_variable(&self.var_context.0);
|
let var_id = working_set.find_variable(&self.var_context.0);
|
||||||
let current_span = reedline::Span {
|
let current_span = reedline::Span {
|
||||||
start: span.start - offset,
|
start: span.start - offset,
|
||||||
@ -57,7 +55,7 @@ impl Completer for VariableCompletion {
|
|||||||
// Completions for the given variable
|
// Completions for the given variable
|
||||||
if !var_str.is_empty() {
|
if !var_str.is_empty() {
|
||||||
// Completion for $env.<tab>
|
// Completion for $env.<tab>
|
||||||
if var_str.as_str() == "$env" {
|
if var_str == "$env" {
|
||||||
let env_vars = self.stack.get_env_vars(&self.engine_state);
|
let env_vars = self.stack.get_env_vars(&self.engine_state);
|
||||||
|
|
||||||
// Return nested values
|
// Return nested values
|
||||||
@ -109,7 +107,7 @@ impl Completer for VariableCompletion {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Completions for $nu.<tab>
|
// Completions for $nu.<tab>
|
||||||
if var_str.as_str() == "$nu" {
|
if var_str == "$nu" {
|
||||||
// Eval nu var
|
// Eval nu var
|
||||||
if let Ok(nuval) = eval_variable(
|
if let Ok(nuval) = eval_variable(
|
||||||
&self.engine_state,
|
&self.engine_state,
|
||||||
@ -235,15 +233,11 @@ fn nested_suggestions(
|
|||||||
let value = recursive_value(val, sublevels);
|
let value = recursive_value(val, sublevels);
|
||||||
|
|
||||||
match value {
|
match value {
|
||||||
Value::Record {
|
Value::Record { val, .. } => {
|
||||||
cols,
|
|
||||||
vals: _,
|
|
||||||
span: _,
|
|
||||||
} => {
|
|
||||||
// Add all the columns as completion
|
// Add all the columns as completion
|
||||||
for item in cols {
|
for (col, _) in val.into_iter() {
|
||||||
output.push(Suggestion {
|
output.push(Suggestion {
|
||||||
value: item,
|
value: col,
|
||||||
description: None,
|
description: None,
|
||||||
extra: None,
|
extra: None,
|
||||||
span: current_span,
|
span: current_span,
|
||||||
@ -267,7 +261,7 @@ fn nested_suggestions(
|
|||||||
|
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
Value::List { vals, span: _ } => {
|
Value::List { vals, .. } => {
|
||||||
for column_name in get_columns(vals.as_slice()) {
|
for column_name in get_columns(vals.as_slice()) {
|
||||||
output.push(Suggestion {
|
output.push(Suggestion {
|
||||||
value: column_name,
|
value: column_name,
|
||||||
@ -288,13 +282,10 @@ fn nested_suggestions(
|
|||||||
fn recursive_value(val: Value, sublevels: Vec<Vec<u8>>) -> Value {
|
fn recursive_value(val: Value, sublevels: Vec<Vec<u8>>) -> Value {
|
||||||
// Go to next sublevel
|
// Go to next sublevel
|
||||||
if let Some(next_sublevel) = sublevels.clone().into_iter().next() {
|
if let Some(next_sublevel) = sublevels.clone().into_iter().next() {
|
||||||
|
let span = val.span();
|
||||||
match val {
|
match val {
|
||||||
Value::Record {
|
Value::Record { val, .. } => {
|
||||||
cols,
|
for item in val {
|
||||||
vals,
|
|
||||||
span: _,
|
|
||||||
} => {
|
|
||||||
for item in cols.into_iter().zip(vals.into_iter()) {
|
|
||||||
// Check if index matches with sublevel
|
// Check if index matches with sublevel
|
||||||
if item.0.as_bytes().to_vec() == next_sublevel {
|
if item.0.as_bytes().to_vec() == next_sublevel {
|
||||||
// If matches try to fetch recursively the next
|
// If matches try to fetch recursively the next
|
||||||
@ -303,11 +294,9 @@ fn recursive_value(val: Value, sublevels: Vec<Vec<u8>>) -> Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Current sublevel value not found
|
// Current sublevel value not found
|
||||||
return Value::Nothing {
|
return Value::nothing(span);
|
||||||
span: Span::unknown(),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
Value::LazyRecord { val, span: _ } => {
|
Value::LazyRecord { val, .. } => {
|
||||||
for col in val.column_names() {
|
for col in val.column_names() {
|
||||||
if col.as_bytes().to_vec() == next_sublevel {
|
if col.as_bytes().to_vec() == next_sublevel {
|
||||||
return recursive_value(
|
return recursive_value(
|
||||||
@ -318,15 +307,13 @@ fn recursive_value(val: Value, sublevels: Vec<Vec<u8>>) -> Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Current sublevel value not found
|
// Current sublevel value not found
|
||||||
return Value::Nothing {
|
return Value::nothing(span);
|
||||||
span: Span::unknown(),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
Value::List { vals, span } => {
|
Value::List { vals, .. } => {
|
||||||
for col in get_columns(vals.as_slice()) {
|
for col in get_columns(vals.as_slice()) {
|
||||||
if col.as_bytes().to_vec() == next_sublevel {
|
if col.as_bytes().to_vec() == next_sublevel {
|
||||||
return recursive_value(
|
return recursive_value(
|
||||||
Value::List { vals, span }
|
Value::list(vals, span)
|
||||||
.get_data_by_key(&col)
|
.get_data_by_key(&col)
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
sublevels.into_iter().skip(1).collect(),
|
sublevels.into_iter().skip(1).collect(),
|
||||||
@ -335,9 +322,7 @@ fn recursive_value(val: Value, sublevels: Vec<Vec<u8>>) -> Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Current sublevel value not found
|
// Current sublevel value not found
|
||||||
return Value::Nothing {
|
return Value::nothing(span);
|
||||||
span: Span::unknown(),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
_ => return val,
|
_ => return val,
|
||||||
}
|
}
|
||||||
|
@ -28,13 +28,17 @@ pub fn evaluate_commands(
|
|||||||
let (block, delta) = {
|
let (block, delta) = {
|
||||||
if let Some(ref t_mode) = table_mode {
|
if let Some(ref t_mode) = table_mode {
|
||||||
let mut config = engine_state.get_config().clone();
|
let mut config = engine_state.get_config().clone();
|
||||||
config.table_mode = t_mode.as_string()?;
|
config.table_mode = t_mode.as_string()?.parse().unwrap_or_default();
|
||||||
engine_state.set_config(&config);
|
engine_state.set_config(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut working_set = StateWorkingSet::new(engine_state);
|
let mut working_set = StateWorkingSet::new(engine_state);
|
||||||
|
|
||||||
let output = parse(&mut working_set, None, commands.item.as_bytes(), false);
|
let output = parse(&mut working_set, None, commands.item.as_bytes(), false);
|
||||||
|
if let Some(warning) = working_set.parse_warnings.first() {
|
||||||
|
report_error(&working_set, warning);
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(err) = working_set.parse_errors.first() {
|
if let Some(err) = working_set.parse_errors.first() {
|
||||||
report_error(&working_set, err);
|
report_error(&working_set, err);
|
||||||
|
|
||||||
@ -55,7 +59,7 @@ pub fn evaluate_commands(
|
|||||||
Ok(pipeline_data) => {
|
Ok(pipeline_data) => {
|
||||||
let mut config = engine_state.get_config().clone();
|
let mut config = engine_state.get_config().clone();
|
||||||
if let Some(t_mode) = table_mode {
|
if let Some(t_mode) = table_mode {
|
||||||
config.table_mode = t_mode.as_string()?;
|
config.table_mode = t_mode.as_string()?.parse().unwrap_or_default();
|
||||||
}
|
}
|
||||||
crate::eval_file::print_table_or_error(engine_state, stack, pipeline_data, &mut config)
|
crate::eval_file::print_table_or_error(engine_state, stack, pipeline_data, &mut config)
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ use crate::util::eval_source;
|
|||||||
use log::info;
|
use log::info;
|
||||||
use log::trace;
|
use log::trace;
|
||||||
use miette::{IntoDiagnostic, Result};
|
use miette::{IntoDiagnostic, Result};
|
||||||
|
use nu_engine::eval_block;
|
||||||
use nu_engine::{convert_env_values, current_dir};
|
use nu_engine::{convert_env_values, current_dir};
|
||||||
use nu_parser::parse;
|
use nu_parser::parse;
|
||||||
use nu_path::canonicalize_with;
|
use nu_path::canonicalize_with;
|
||||||
@ -9,7 +10,7 @@ use nu_protocol::report_error;
|
|||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
ast::Call,
|
ast::Call,
|
||||||
engine::{EngineState, Stack, StateWorkingSet},
|
engine::{EngineState, Stack, StateWorkingSet},
|
||||||
Config, PipelineData, ShellError, Span, Type, Value,
|
Config, PipelineData, ShellError, Span, Value,
|
||||||
};
|
};
|
||||||
use nu_utils::stdout_write_all_and_flush;
|
use nu_utils::stdout_write_all_and_flush;
|
||||||
|
|
||||||
@ -34,10 +35,10 @@ pub fn evaluate_file(
|
|||||||
let working_set = StateWorkingSet::new(engine_state);
|
let working_set = StateWorkingSet::new(engine_state);
|
||||||
report_error(
|
report_error(
|
||||||
&working_set,
|
&working_set,
|
||||||
&ShellError::FileNotFoundCustom(
|
&ShellError::FileNotFoundCustom {
|
||||||
format!("Could not access file '{}': {:?}", path, e.to_string()),
|
msg: format!("Could not access file '{}': {:?}", path, e.to_string()),
|
||||||
Span::unknown(),
|
span: Span::unknown(),
|
||||||
),
|
},
|
||||||
);
|
);
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
});
|
});
|
||||||
@ -46,13 +47,13 @@ pub fn evaluate_file(
|
|||||||
let working_set = StateWorkingSet::new(engine_state);
|
let working_set = StateWorkingSet::new(engine_state);
|
||||||
report_error(
|
report_error(
|
||||||
&working_set,
|
&working_set,
|
||||||
&ShellError::NonUtf8Custom(
|
&ShellError::NonUtf8Custom {
|
||||||
format!(
|
msg: format!(
|
||||||
"Input file name '{}' is not valid UTF8",
|
"Input file name '{}' is not valid UTF8",
|
||||||
file_path.to_string_lossy()
|
file_path.to_string_lossy()
|
||||||
),
|
),
|
||||||
Span::unknown(),
|
span: Span::unknown(),
|
||||||
),
|
},
|
||||||
);
|
);
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
});
|
});
|
||||||
@ -63,14 +64,14 @@ pub fn evaluate_file(
|
|||||||
let working_set = StateWorkingSet::new(engine_state);
|
let working_set = StateWorkingSet::new(engine_state);
|
||||||
report_error(
|
report_error(
|
||||||
&working_set,
|
&working_set,
|
||||||
&ShellError::FileNotFoundCustom(
|
&ShellError::FileNotFoundCustom {
|
||||||
format!(
|
msg: format!(
|
||||||
"Could not read file '{}': {:?}",
|
"Could not read file '{}': {:?}",
|
||||||
file_path_str,
|
file_path_str,
|
||||||
e.to_string()
|
e.to_string()
|
||||||
),
|
),
|
||||||
Span::unknown(),
|
span: Span::unknown(),
|
||||||
),
|
},
|
||||||
);
|
);
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
});
|
});
|
||||||
@ -81,10 +82,10 @@ pub fn evaluate_file(
|
|||||||
let working_set = StateWorkingSet::new(engine_state);
|
let working_set = StateWorkingSet::new(engine_state);
|
||||||
report_error(
|
report_error(
|
||||||
&working_set,
|
&working_set,
|
||||||
&ShellError::FileNotFoundCustom(
|
&ShellError::FileNotFoundCustom {
|
||||||
format!("The file path '{file_path_str}' does not have a parent"),
|
msg: format!("The file path '{file_path_str}' does not have a parent"),
|
||||||
Span::unknown(),
|
span: Span::unknown(),
|
||||||
),
|
},
|
||||||
);
|
);
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
});
|
});
|
||||||
@ -97,24 +98,76 @@ pub fn evaluate_file(
|
|||||||
"CURRENT_FILE".to_string(),
|
"CURRENT_FILE".to_string(),
|
||||||
Value::string(file_path.to_string_lossy(), Span::unknown()),
|
Value::string(file_path.to_string_lossy(), Span::unknown()),
|
||||||
);
|
);
|
||||||
|
stack.add_env_var(
|
||||||
|
"PROCESS_PATH".to_string(),
|
||||||
|
Value::string(path, Span::unknown()),
|
||||||
|
);
|
||||||
|
|
||||||
|
let source_filename = file_path
|
||||||
|
.file_name()
|
||||||
|
.expect("internal error: script missing filename");
|
||||||
|
|
||||||
let mut working_set = StateWorkingSet::new(engine_state);
|
let mut working_set = StateWorkingSet::new(engine_state);
|
||||||
trace!("parsing file: {}", file_path_str);
|
trace!("parsing file: {}", file_path_str);
|
||||||
let _ = parse(&mut working_set, Some(file_path_str), &file, false);
|
let block = parse(&mut working_set, Some(file_path_str), &file, false);
|
||||||
|
|
||||||
if working_set.find_decl(b"main", &Type::Any).is_some() {
|
if let Some(err) = working_set.parse_errors.first() {
|
||||||
|
report_error(&working_set, err);
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
for block in &mut working_set.delta.blocks {
|
||||||
|
if block.signature.name == "main" {
|
||||||
|
block.signature.name = source_filename.to_string_lossy().to_string();
|
||||||
|
} else if block.signature.name.starts_with("main ") {
|
||||||
|
block.signature.name =
|
||||||
|
source_filename.to_string_lossy().to_string() + " " + &block.signature.name[5..];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = engine_state.merge_delta(working_set.delta);
|
||||||
|
|
||||||
|
if engine_state.find_decl(b"main", &[]).is_some() {
|
||||||
let args = format!("main {}", args.join(" "));
|
let args = format!("main {}", args.join(" "));
|
||||||
|
|
||||||
if !eval_source(
|
let pipeline_data = eval_block(
|
||||||
engine_state,
|
engine_state,
|
||||||
stack,
|
stack,
|
||||||
&file,
|
&block,
|
||||||
file_path_str,
|
|
||||||
PipelineData::empty(),
|
PipelineData::empty(),
|
||||||
true,
|
false,
|
||||||
) {
|
false,
|
||||||
std::process::exit(1);
|
);
|
||||||
|
let pipeline_data = match pipeline_data {
|
||||||
|
Err(ShellError::Return { .. }) => {
|
||||||
|
// allows early exists before `main` is run.
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
x => x,
|
||||||
}
|
}
|
||||||
|
.unwrap_or_else(|e| {
|
||||||
|
let working_set = StateWorkingSet::new(engine_state);
|
||||||
|
report_error(&working_set, &e);
|
||||||
|
std::process::exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
let result = pipeline_data.print(engine_state, stack, true, false);
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Err(err) => {
|
||||||
|
let working_set = StateWorkingSet::new(engine_state);
|
||||||
|
|
||||||
|
report_error(&working_set, &err);
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
Ok(exit_code) => {
|
||||||
|
if exit_code != 0 {
|
||||||
|
std::process::exit(exit_code as i32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !eval_source(
|
if !eval_source(
|
||||||
engine_state,
|
engine_state,
|
||||||
stack,
|
stack,
|
||||||
@ -146,9 +199,9 @@ pub(crate) fn print_table_or_error(
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Change the engine_state config to use the passed in configuration
|
// Change the engine_state config to use the passed in configuration
|
||||||
engine_state.set_config(config);
|
engine_state.set_config(config.clone());
|
||||||
|
|
||||||
if let PipelineData::Value(Value::Error { error }, ..) = &pipeline_data {
|
if let PipelineData::Value(Value::Error { error, .. }, ..) = &pipeline_data {
|
||||||
let working_set = StateWorkingSet::new(engine_state);
|
let working_set = StateWorkingSet::new(engine_state);
|
||||||
report_error(&working_set, &**error);
|
report_error(&working_set, &**error);
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
@ -195,7 +248,7 @@ pub(crate) fn print_table_or_error(
|
|||||||
|
|
||||||
fn print_or_exit(pipeline_data: PipelineData, engine_state: &mut EngineState, config: &Config) {
|
fn print_or_exit(pipeline_data: PipelineData, engine_state: &mut EngineState, config: &Config) {
|
||||||
for item in pipeline_data {
|
for item in pipeline_data {
|
||||||
if let Value::Error { error } = item {
|
if let Value::Error { error, .. } = item {
|
||||||
let working_set = StateWorkingSet::new(engine_state);
|
let working_set = StateWorkingSet::new(engine_state);
|
||||||
|
|
||||||
report_error(&working_set, &*error);
|
report_error(&working_set, &*error);
|
||||||
|
@ -646,7 +646,10 @@ impl Menu for DescriptionMenu {
|
|||||||
|lb| {
|
|lb| {
|
||||||
lb.replace_range(start..end, replacement);
|
lb.replace_range(start..end, replacement);
|
||||||
let mut offset = lb.insertion_point();
|
let mut offset = lb.insertion_point();
|
||||||
offset += lb.len().saturating_sub(end.saturating_sub(start));
|
offset += lb
|
||||||
|
.len()
|
||||||
|
.saturating_sub(end.saturating_sub(start))
|
||||||
|
.saturating_sub(start);
|
||||||
lb.set_insertion_point(offset);
|
lb.set_insertion_point(offset);
|
||||||
},
|
},
|
||||||
UndoBehavior::CreateUndoPoint,
|
UndoBehavior::CreateUndoPoint,
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use nu_engine::documentation::get_flags_section;
|
use nu_engine::documentation::get_flags_section;
|
||||||
use nu_protocol::{engine::EngineState, levenshtein_distance};
|
use nu_protocol::{engine::EngineState, levenshtein_distance};
|
||||||
|
use nu_utils::IgnoreCaseExt;
|
||||||
use reedline::{Completer, Suggestion};
|
use reedline::{Completer, Suggestion};
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@ -13,21 +14,19 @@ impl NuHelpCompleter {
|
|||||||
|
|
||||||
fn completion_helper(&self, line: &str, pos: usize) -> Vec<Suggestion> {
|
fn completion_helper(&self, line: &str, pos: usize) -> Vec<Suggestion> {
|
||||||
let full_commands = self.0.get_signatures_with_examples(false);
|
let full_commands = self.0.get_signatures_with_examples(false);
|
||||||
|
let folded_line = line.to_folded_case();
|
||||||
|
|
||||||
//Vec<(Signature, Vec<Example>, bool, bool)> {
|
//Vec<(Signature, Vec<Example>, bool, bool)> {
|
||||||
let mut commands = full_commands
|
let mut commands = full_commands
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|(sig, _, _, _, _)| {
|
.filter(|(sig, _, _, _, _)| {
|
||||||
sig.name.to_lowercase().contains(&line.to_lowercase())
|
sig.name.to_folded_case().contains(&folded_line)
|
||||||
|| sig.usage.to_lowercase().contains(&line.to_lowercase())
|
|| sig.usage.to_folded_case().contains(&folded_line)
|
||||||
|| sig
|
|| sig
|
||||||
.search_terms
|
.search_terms
|
||||||
.iter()
|
.iter()
|
||||||
.any(|term| term.to_lowercase().contains(&line.to_lowercase()))
|
.any(|term| term.to_folded_case().contains(&folded_line))
|
||||||
|| sig
|
|| sig.extra_usage.to_folded_case().contains(&folded_line)
|
||||||
.extra_usage
|
|
||||||
.to_lowercase()
|
|
||||||
.contains(&line.to_lowercase())
|
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
@ -57,7 +56,7 @@ impl NuHelpCompleter {
|
|||||||
let _ = write!(long_desc, "Usage:\r\n > {}\r\n", sig.call_signature());
|
let _ = write!(long_desc, "Usage:\r\n > {}\r\n", sig.call_signature());
|
||||||
|
|
||||||
if !sig.named.is_empty() {
|
if !sig.named.is_empty() {
|
||||||
long_desc.push_str(&get_flags_section(sig, |v| {
|
long_desc.push_str(&get_flags_section(Some(&*self.0.clone()), sig, |v| {
|
||||||
v.into_string_parsable(", ", &self.0.config)
|
v.into_string_parsable(", ", &self.0.config)
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
@ -80,24 +80,18 @@ fn convert_to_suggestions(
|
|||||||
only_buffer_difference: bool,
|
only_buffer_difference: bool,
|
||||||
) -> Vec<Suggestion> {
|
) -> Vec<Suggestion> {
|
||||||
match value {
|
match value {
|
||||||
Value::Record { .. } => {
|
Value::Record { val, .. } => {
|
||||||
let text = value
|
let text = val
|
||||||
.get_data_by_key("value")
|
.get("value")
|
||||||
.and_then(|val| val.as_string().ok())
|
.and_then(|val| val.as_string().ok())
|
||||||
.unwrap_or_else(|| "No value key".to_string());
|
.unwrap_or_else(|| "No value key".to_string());
|
||||||
|
|
||||||
let description = value
|
let description = val.get("description").and_then(|val| val.as_string().ok());
|
||||||
.get_data_by_key("description")
|
|
||||||
.and_then(|val| val.as_string().ok());
|
|
||||||
|
|
||||||
let span = match value.get_data_by_key("span") {
|
let span = match val.get("span") {
|
||||||
Some(span @ Value::Record { .. }) => {
|
Some(Value::Record { val: span, .. }) => {
|
||||||
let start = span
|
let start = span.get("start").and_then(|val| val.as_int().ok());
|
||||||
.get_data_by_key("start")
|
let end = span.get("end").and_then(|val| val.as_int().ok());
|
||||||
.and_then(|val| val.as_integer().ok());
|
|
||||||
let end = span
|
|
||||||
.get_data_by_key("end")
|
|
||||||
.and_then(|val| val.as_integer().ok());
|
|
||||||
match (start, end) {
|
match (start, end) {
|
||||||
(Some(start), Some(end)) => {
|
(Some(start), Some(end)) => {
|
||||||
let start = start.min(end);
|
let start = start.min(end);
|
||||||
@ -126,12 +120,12 @@ fn convert_to_suggestions(
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let extra = match value.get_data_by_key("extra") {
|
let extra = match val.get("extra") {
|
||||||
Some(Value::List { vals, .. }) => {
|
Some(Value::List { vals, .. }) => {
|
||||||
let extra: Vec<String> = vals
|
let extra: Vec<String> = vals
|
||||||
.into_iter()
|
.iter()
|
||||||
.filter_map(|extra| match extra {
|
.filter_map(|extra| match extra {
|
||||||
Value::String { val, .. } => Some(val),
|
Value::String { val, .. } => Some(val.clone()),
|
||||||
_ => None,
|
_ => None,
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
@ -48,14 +48,9 @@ impl Command for NuHighlight {
|
|||||||
Ok(line) => {
|
Ok(line) => {
|
||||||
let highlights = highlighter.highlight(&line, line.len());
|
let highlights = highlighter.highlight(&line, line.len());
|
||||||
|
|
||||||
Value::String {
|
Value::string(highlights.render_simple(), head)
|
||||||
val: highlights.render_simple(),
|
|
||||||
span: head,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Err(err) => Value::Error {
|
Err(err) => Value::error(err, head),
|
||||||
error: Box::new(err),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
ctrlc,
|
ctrlc,
|
||||||
)
|
)
|
||||||
|
@ -16,7 +16,11 @@ impl Command for Print {
|
|||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build("print")
|
Signature::build("print")
|
||||||
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
|
.input_output_types(vec![
|
||||||
|
(Type::Nothing, Type::Nothing),
|
||||||
|
(Type::Any, Type::Nothing),
|
||||||
|
])
|
||||||
|
.allow_variants_without_examples(true)
|
||||||
.rest("rest", SyntaxShape::Any, "the values to print")
|
.rest("rest", SyntaxShape::Any, "the values to print")
|
||||||
.switch(
|
.switch(
|
||||||
"no-newline",
|
"no-newline",
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use crate::prompt_update::{POST_PROMPT_MARKER, PRE_PROMPT_MARKER};
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
use nu_utils::enable_vt_processing;
|
use nu_utils::enable_vt_processing;
|
||||||
use reedline::DefaultPrompt;
|
use reedline::DefaultPrompt;
|
||||||
@ -11,6 +12,7 @@ use {
|
|||||||
/// Nushell prompt definition
|
/// Nushell prompt definition
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct NushellPrompt {
|
pub struct NushellPrompt {
|
||||||
|
shell_integration: bool,
|
||||||
left_prompt_string: Option<String>,
|
left_prompt_string: Option<String>,
|
||||||
right_prompt_string: Option<String>,
|
right_prompt_string: Option<String>,
|
||||||
default_prompt_indicator: Option<String>,
|
default_prompt_indicator: Option<String>,
|
||||||
@ -20,15 +22,10 @@ pub struct NushellPrompt {
|
|||||||
render_right_prompt_on_last_line: bool,
|
render_right_prompt_on_last_line: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for NushellPrompt {
|
|
||||||
fn default() -> Self {
|
|
||||||
NushellPrompt::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NushellPrompt {
|
impl NushellPrompt {
|
||||||
pub fn new() -> NushellPrompt {
|
pub fn new(shell_integration: bool) -> NushellPrompt {
|
||||||
NushellPrompt {
|
NushellPrompt {
|
||||||
|
shell_integration,
|
||||||
left_prompt_string: None,
|
left_prompt_string: None,
|
||||||
right_prompt_string: None,
|
right_prompt_string: None,
|
||||||
default_prompt_indicator: None,
|
default_prompt_indicator: None,
|
||||||
@ -109,10 +106,13 @@ impl Prompt for NushellPrompt {
|
|||||||
let prompt = default
|
let prompt = default
|
||||||
.render_prompt_left()
|
.render_prompt_left()
|
||||||
.to_string()
|
.to_string()
|
||||||
.replace('\n', "\r\n")
|
.replace('\n', "\r\n");
|
||||||
+ " ";
|
|
||||||
|
|
||||||
prompt.into()
|
if self.shell_integration {
|
||||||
|
format!("{PRE_PROMPT_MARKER}{prompt}{POST_PROMPT_MARKER}").into()
|
||||||
|
} else {
|
||||||
|
prompt.into()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,11 +144,11 @@ impl Prompt for NushellPrompt {
|
|||||||
PromptEditMode::Vi(vi_mode) => match vi_mode {
|
PromptEditMode::Vi(vi_mode) => match vi_mode {
|
||||||
PromptViMode::Normal => match &self.default_vi_normal_prompt_indicator {
|
PromptViMode::Normal => match &self.default_vi_normal_prompt_indicator {
|
||||||
Some(indicator) => indicator,
|
Some(indicator) => indicator,
|
||||||
None => ": ",
|
None => "> ",
|
||||||
},
|
},
|
||||||
PromptViMode::Insert => match &self.default_vi_insert_prompt_indicator {
|
PromptViMode::Insert => match &self.default_vi_insert_prompt_indicator {
|
||||||
Some(indicator) => indicator,
|
Some(indicator) => indicator,
|
||||||
None => "> ",
|
None => ": ",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
.into(),
|
.into(),
|
||||||
|
@ -15,10 +15,19 @@ pub(crate) const PROMPT_INDICATOR: &str = "PROMPT_INDICATOR";
|
|||||||
pub(crate) const PROMPT_INDICATOR_VI_INSERT: &str = "PROMPT_INDICATOR_VI_INSERT";
|
pub(crate) const PROMPT_INDICATOR_VI_INSERT: &str = "PROMPT_INDICATOR_VI_INSERT";
|
||||||
pub(crate) const PROMPT_INDICATOR_VI_NORMAL: &str = "PROMPT_INDICATOR_VI_NORMAL";
|
pub(crate) const PROMPT_INDICATOR_VI_NORMAL: &str = "PROMPT_INDICATOR_VI_NORMAL";
|
||||||
pub(crate) const PROMPT_MULTILINE_INDICATOR: &str = "PROMPT_MULTILINE_INDICATOR";
|
pub(crate) const PROMPT_MULTILINE_INDICATOR: &str = "PROMPT_MULTILINE_INDICATOR";
|
||||||
|
pub(crate) const TRANSIENT_PROMPT_COMMAND: &str = "TRANSIENT_PROMPT_COMMAND";
|
||||||
|
pub(crate) const TRANSIENT_PROMPT_COMMAND_RIGHT: &str = "TRANSIENT_PROMPT_COMMAND_RIGHT";
|
||||||
|
pub(crate) const TRANSIENT_PROMPT_INDICATOR: &str = "TRANSIENT_PROMPT_INDICATOR";
|
||||||
|
pub(crate) const TRANSIENT_PROMPT_INDICATOR_VI_INSERT: &str =
|
||||||
|
"TRANSIENT_PROMPT_INDICATOR_VI_INSERT";
|
||||||
|
pub(crate) const TRANSIENT_PROMPT_INDICATOR_VI_NORMAL: &str =
|
||||||
|
"TRANSIENT_PROMPT_INDICATOR_VI_NORMAL";
|
||||||
|
pub(crate) const TRANSIENT_PROMPT_MULTILINE_INDICATOR: &str =
|
||||||
|
"TRANSIENT_PROMPT_MULTILINE_INDICATOR";
|
||||||
// According to Daniel Imms @Tyriar, we need to do these this way:
|
// According to Daniel Imms @Tyriar, we need to do these this way:
|
||||||
// <133 A><prompt><133 B><command><133 C><command output>
|
// <133 A><prompt><133 B><command><133 C><command output>
|
||||||
const PRE_PROMPT_MARKER: &str = "\x1b]133;A\x1b\\";
|
pub(crate) const PRE_PROMPT_MARKER: &str = "\x1b]133;A\x1b\\";
|
||||||
const POST_PROMPT_MARKER: &str = "\x1b]133;B\x1b\\";
|
pub(crate) const POST_PROMPT_MARKER: &str = "\x1b]133;B\x1b\\";
|
||||||
|
|
||||||
fn get_prompt_string(
|
fn get_prompt_string(
|
||||||
prompt: &str,
|
prompt: &str,
|
||||||
@ -29,13 +38,9 @@ fn get_prompt_string(
|
|||||||
stack
|
stack
|
||||||
.get_env_var(engine_state, prompt)
|
.get_env_var(engine_state, prompt)
|
||||||
.and_then(|v| match v {
|
.and_then(|v| match v {
|
||||||
Value::Closure {
|
Value::Closure { val, .. } => {
|
||||||
val: block_id,
|
let block = engine_state.get_block(val.block_id);
|
||||||
captures,
|
let mut stack = stack.captures_to_stack(val.captures);
|
||||||
..
|
|
||||||
} => {
|
|
||||||
let block = engine_state.get_block(block_id);
|
|
||||||
let mut stack = stack.captures_to_stack(&captures);
|
|
||||||
// Use eval_subexpression to force a redirection of output, so we can use everything in prompt
|
// Use eval_subexpression to force a redirection of output, so we can use everything in prompt
|
||||||
let ret_val =
|
let ret_val =
|
||||||
eval_subexpression(engine_state, &mut stack, block, PipelineData::empty());
|
eval_subexpression(engine_state, &mut stack, block, PipelineData::empty());
|
||||||
@ -91,12 +96,12 @@ fn get_prompt_string(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn update_prompt<'prompt>(
|
pub(crate) fn update_prompt(
|
||||||
config: &Config,
|
config: &Config,
|
||||||
engine_state: &EngineState,
|
engine_state: &EngineState,
|
||||||
stack: &Stack,
|
stack: &Stack,
|
||||||
nu_prompt: &'prompt mut NushellPrompt,
|
nu_prompt: &mut NushellPrompt,
|
||||||
) -> &'prompt dyn Prompt {
|
) {
|
||||||
let mut stack = stack.clone();
|
let mut stack = stack.clone();
|
||||||
|
|
||||||
let left_prompt_string = get_prompt_string(PROMPT_COMMAND, config, engine_state, &mut stack);
|
let left_prompt_string = get_prompt_string(PROMPT_COMMAND, config, engine_state, &mut stack);
|
||||||
@ -139,9 +144,55 @@ pub(crate) fn update_prompt<'prompt>(
|
|||||||
(prompt_vi_insert_string, prompt_vi_normal_string),
|
(prompt_vi_insert_string, prompt_vi_normal_string),
|
||||||
config.render_right_prompt_on_last_line,
|
config.render_right_prompt_on_last_line,
|
||||||
);
|
);
|
||||||
|
|
||||||
let ret_val = nu_prompt as &dyn Prompt;
|
|
||||||
trace!("update_prompt {}:{}:{}", file!(), line!(), column!());
|
trace!("update_prompt {}:{}:{}", file!(), line!(), column!());
|
||||||
|
}
|
||||||
ret_val
|
|
||||||
|
/// Construct the transient prompt based on the normal nu_prompt
|
||||||
|
pub(crate) fn make_transient_prompt(
|
||||||
|
config: &Config,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
nu_prompt: &NushellPrompt,
|
||||||
|
) -> Box<dyn Prompt> {
|
||||||
|
let mut nu_prompt = nu_prompt.clone();
|
||||||
|
|
||||||
|
if let Some(s) = get_prompt_string(TRANSIENT_PROMPT_COMMAND, config, engine_state, stack) {
|
||||||
|
nu_prompt.update_prompt_left(Some(s))
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(s) = get_prompt_string(TRANSIENT_PROMPT_COMMAND_RIGHT, config, engine_state, stack)
|
||||||
|
{
|
||||||
|
nu_prompt.update_prompt_right(Some(s), config.render_right_prompt_on_last_line)
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(s) = get_prompt_string(TRANSIENT_PROMPT_INDICATOR, config, engine_state, stack) {
|
||||||
|
nu_prompt.update_prompt_indicator(Some(s))
|
||||||
|
}
|
||||||
|
if let Some(s) = get_prompt_string(
|
||||||
|
TRANSIENT_PROMPT_INDICATOR_VI_INSERT,
|
||||||
|
config,
|
||||||
|
engine_state,
|
||||||
|
stack,
|
||||||
|
) {
|
||||||
|
nu_prompt.update_prompt_vi_insert(Some(s))
|
||||||
|
}
|
||||||
|
if let Some(s) = get_prompt_string(
|
||||||
|
TRANSIENT_PROMPT_INDICATOR_VI_NORMAL,
|
||||||
|
config,
|
||||||
|
engine_state,
|
||||||
|
stack,
|
||||||
|
) {
|
||||||
|
nu_prompt.update_prompt_vi_normal(Some(s))
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(s) = get_prompt_string(
|
||||||
|
TRANSIENT_PROMPT_MULTILINE_INDICATOR,
|
||||||
|
config,
|
||||||
|
engine_state,
|
||||||
|
stack,
|
||||||
|
) {
|
||||||
|
nu_prompt.update_prompt_multiline(Some(s))
|
||||||
|
}
|
||||||
|
|
||||||
|
Box::new(nu_prompt)
|
||||||
}
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -7,26 +7,32 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use crossterm::cursor::SetCursorStyle;
|
use crossterm::cursor::SetCursorStyle;
|
||||||
use log::{trace, warn};
|
use log::{trace, warn};
|
||||||
use miette::{IntoDiagnostic, Result};
|
use miette::{ErrReport, IntoDiagnostic, Result};
|
||||||
use nu_cmd_base::util::get_guaranteed_cwd;
|
use nu_cmd_base::util::get_guaranteed_cwd;
|
||||||
|
use nu_cmd_base::{hook::eval_hook, util::get_editor};
|
||||||
use nu_color_config::StyleComputer;
|
use nu_color_config::StyleComputer;
|
||||||
use nu_command::hook::eval_hook;
|
|
||||||
use nu_engine::convert_env_values;
|
use nu_engine::convert_env_values;
|
||||||
use nu_parser::{lex, parse, trim_quotes_str};
|
use nu_parser::{lex, parse, trim_quotes_str};
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
config::NuCursorShape,
|
config::NuCursorShape,
|
||||||
engine::{EngineState, Stack, StateWorkingSet},
|
engine::{EngineState, Stack, StateWorkingSet},
|
||||||
|
eval_const::create_nu_constant,
|
||||||
report_error, report_error_new, HistoryFileFormat, PipelineData, ShellError, Span, Spanned,
|
report_error, report_error_new, HistoryFileFormat, PipelineData, ShellError, Span, Spanned,
|
||||||
Value,
|
Value, NU_VARIABLE_ID,
|
||||||
};
|
};
|
||||||
use nu_utils::utils::perf;
|
use nu_utils::utils::perf;
|
||||||
use reedline::{CursorConfig, DefaultHinter, EditCommand, Emacs, SqliteBackedHistory, Vi};
|
use reedline::{
|
||||||
|
CursorConfig, CwdAwareHinter, EditCommand, Emacs, FileBackedHistory, HistorySessionId,
|
||||||
|
Reedline, SqliteBackedHistory, Vi,
|
||||||
|
};
|
||||||
use std::{
|
use std::{
|
||||||
io::{self, Write},
|
env::temp_dir,
|
||||||
|
io::{self, IsTerminal, Write},
|
||||||
|
path::Path,
|
||||||
sync::atomic::Ordering,
|
sync::atomic::Ordering,
|
||||||
time::Instant,
|
time::Instant,
|
||||||
};
|
};
|
||||||
use sysinfo::SystemExt;
|
use sysinfo::System;
|
||||||
|
|
||||||
// According to Daniel Imms @Tyriar, we need to do these this way:
|
// According to Daniel Imms @Tyriar, we need to do these this way:
|
||||||
// <133 A><prompt><133 B><command><133 C><command output>
|
// <133 A><prompt><133 B><command><133 C><command output>
|
||||||
@ -46,13 +52,14 @@ pub fn evaluate_repl(
|
|||||||
load_std_lib: Option<Spanned<String>>,
|
load_std_lib: Option<Spanned<String>>,
|
||||||
entire_start_time: Instant,
|
entire_start_time: Instant,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
use nu_command::hook;
|
use nu_cmd_base::hook;
|
||||||
use reedline::{FileBackedHistory, Reedline, Signal};
|
use reedline::Signal;
|
||||||
let use_color = engine_state.get_config().use_ansi_coloring;
|
let config = engine_state.get_config();
|
||||||
|
let use_color = config.use_ansi_coloring;
|
||||||
|
|
||||||
// Guard against invocation without a connected terminal.
|
// Guard against invocation without a connected terminal.
|
||||||
// reedline / crossterm event polling will fail without a connected tty
|
// reedline / crossterm event polling will fail without a connected tty
|
||||||
if !atty::is(atty::Stream::Stdin) {
|
if !std::io::stdin().is_terminal() {
|
||||||
return Err(std::io::Error::new(
|
return Err(std::io::Error::new(
|
||||||
std::io::ErrorKind::NotFound,
|
std::io::ErrorKind::NotFound,
|
||||||
"Nushell launched as a REPL, but STDIN is not a TTY; either launch in a valid terminal or provide arguments to invoke a script!",
|
"Nushell launched as a REPL, but STDIN is not a TTY; either launch in a valid terminal or provide arguments to invoke a script!",
|
||||||
@ -62,7 +69,7 @@ pub fn evaluate_repl(
|
|||||||
|
|
||||||
let mut entry_num = 0;
|
let mut entry_num = 0;
|
||||||
|
|
||||||
let mut nu_prompt = NushellPrompt::new();
|
let mut nu_prompt = NushellPrompt::new(config.shell_integration);
|
||||||
|
|
||||||
let start_time = std::time::Instant::now();
|
let start_time = std::time::Instant::now();
|
||||||
// Translate environment variables from Strings to Values
|
// Translate environment variables from Strings to Values
|
||||||
@ -89,13 +96,10 @@ pub fn evaluate_repl(
|
|||||||
|
|
||||||
let mut start_time = std::time::Instant::now();
|
let mut start_time = std::time::Instant::now();
|
||||||
let mut line_editor = Reedline::create();
|
let mut line_editor = Reedline::create();
|
||||||
|
let temp_file = temp_dir().join(format!("{}.nu", uuid::Uuid::new_v4()));
|
||||||
|
|
||||||
// Now that reedline is created, get the history session id and store it in engine_state
|
// Now that reedline is created, get the history session id and store it in engine_state
|
||||||
let hist_sesh = line_editor
|
store_history_id_in_engine(engine_state, &line_editor);
|
||||||
.get_history_session_id()
|
|
||||||
.map(i64::from)
|
|
||||||
.unwrap_or(0);
|
|
||||||
engine_state.history_session_id = hist_sesh;
|
|
||||||
perf(
|
perf(
|
||||||
"setup reedline",
|
"setup reedline",
|
||||||
start_time,
|
start_time,
|
||||||
@ -105,16 +109,8 @@ pub fn evaluate_repl(
|
|||||||
use_color,
|
use_color,
|
||||||
);
|
);
|
||||||
|
|
||||||
let config = engine_state.get_config();
|
|
||||||
if config.bracketed_paste {
|
|
||||||
// try to enable bracketed paste
|
|
||||||
// It doesn't work on windows system: https://github.com/crossterm-rs/crossterm/issues/737
|
|
||||||
#[cfg(not(target_os = "windows"))]
|
|
||||||
let _ = line_editor.enable_bracketed_paste();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setup history_isolation aka "history per session"
|
// Setup history_isolation aka "history per session"
|
||||||
let history_isolation = config.history_isolation;
|
let history_isolation = engine_state.get_config().history_isolation;
|
||||||
let history_session_id = if history_isolation {
|
let history_session_id = if history_isolation {
|
||||||
Reedline::create_history_session_id()
|
Reedline::create_history_session_id()
|
||||||
} else {
|
} else {
|
||||||
@ -127,22 +123,8 @@ pub fn evaluate_repl(
|
|||||||
engine_state.config.history_file_format,
|
engine_state.config.history_file_format,
|
||||||
);
|
);
|
||||||
if let Some(history_path) = history_path.as_deref() {
|
if let Some(history_path) = history_path.as_deref() {
|
||||||
let history: Box<dyn reedline::History> = match engine_state.config.history_file_format {
|
line_editor =
|
||||||
HistoryFileFormat::PlainText => Box::new(
|
update_line_editor_history(engine_state, history_path, line_editor, history_session_id)?
|
||||||
FileBackedHistory::with_file(
|
|
||||||
config.max_history_size as usize,
|
|
||||||
history_path.to_path_buf(),
|
|
||||||
)
|
|
||||||
.into_diagnostic()?,
|
|
||||||
),
|
|
||||||
HistoryFileFormat::Sqlite => Box::new(
|
|
||||||
SqliteBackedHistory::with_file(history_path.to_path_buf()).into_diagnostic()?,
|
|
||||||
),
|
|
||||||
};
|
|
||||||
line_editor = line_editor
|
|
||||||
.with_history_session_id(history_session_id)
|
|
||||||
.with_history_exclusion_prefix(Some(" ".into()))
|
|
||||||
.with_history(history);
|
|
||||||
};
|
};
|
||||||
perf(
|
perf(
|
||||||
"setup history",
|
"setup history",
|
||||||
@ -153,17 +135,6 @@ pub fn evaluate_repl(
|
|||||||
use_color,
|
use_color,
|
||||||
);
|
);
|
||||||
|
|
||||||
start_time = std::time::Instant::now();
|
|
||||||
let sys = sysinfo::System::new();
|
|
||||||
perf(
|
|
||||||
"get sysinfo",
|
|
||||||
start_time,
|
|
||||||
file!(),
|
|
||||||
line!(),
|
|
||||||
column!(),
|
|
||||||
use_color,
|
|
||||||
);
|
|
||||||
|
|
||||||
if let Some(s) = prerun_command {
|
if let Some(s) = prerun_command {
|
||||||
eval_source(
|
eval_source(
|
||||||
engine_state,
|
engine_state,
|
||||||
@ -178,6 +149,10 @@ pub fn evaluate_repl(
|
|||||||
|
|
||||||
engine_state.set_startup_time(entire_start_time.elapsed().as_nanos() as i64);
|
engine_state.set_startup_time(entire_start_time.elapsed().as_nanos() as i64);
|
||||||
|
|
||||||
|
// Regenerate the $nu constant to contain the startup time and any other potential updates
|
||||||
|
let nu_const = create_nu_constant(engine_state, Span::unknown())?;
|
||||||
|
engine_state.set_variable_const_val(NU_VARIABLE_ID, nu_const);
|
||||||
|
|
||||||
if load_std_lib.is_none() && engine_state.get_config().show_banner {
|
if load_std_lib.is_none() && engine_state.get_config().show_banner {
|
||||||
eval_source(
|
eval_source(
|
||||||
engine_state,
|
engine_state,
|
||||||
@ -189,6 +164,10 @@ pub fn evaluate_repl(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if engine_state.get_config().use_kitty_protocol && !reedline::kitty_protocol_available() {
|
||||||
|
warn!("Terminal doesn't support use_kitty_protocol config");
|
||||||
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let loop_start_time = std::time::Instant::now();
|
let loop_start_time = std::time::Instant::now();
|
||||||
|
|
||||||
@ -223,20 +202,6 @@ pub fn evaluate_repl(
|
|||||||
use_color,
|
use_color,
|
||||||
);
|
);
|
||||||
|
|
||||||
start_time = std::time::Instant::now();
|
|
||||||
// Reset the SIGQUIT handler
|
|
||||||
if let Some(sig_quit) = engine_state.get_sig_quit() {
|
|
||||||
sig_quit.store(false, Ordering::SeqCst);
|
|
||||||
}
|
|
||||||
perf(
|
|
||||||
"reset sig_quit",
|
|
||||||
start_time,
|
|
||||||
file!(),
|
|
||||||
line!(),
|
|
||||||
column!(),
|
|
||||||
use_color,
|
|
||||||
);
|
|
||||||
|
|
||||||
start_time = std::time::Instant::now();
|
start_time = std::time::Instant::now();
|
||||||
let config = engine_state.get_config();
|
let config = engine_state.get_config();
|
||||||
|
|
||||||
@ -244,13 +209,9 @@ pub fn evaluate_repl(
|
|||||||
|
|
||||||
// Find the configured cursor shapes for each mode
|
// Find the configured cursor shapes for each mode
|
||||||
let cursor_config = CursorConfig {
|
let cursor_config = CursorConfig {
|
||||||
vi_insert: Some(map_nucursorshape_to_cursorshape(
|
vi_insert: map_nucursorshape_to_cursorshape(config.cursor_shape_vi_insert),
|
||||||
config.cursor_shape_vi_insert,
|
vi_normal: map_nucursorshape_to_cursorshape(config.cursor_shape_vi_normal),
|
||||||
)),
|
emacs: map_nucursorshape_to_cursorshape(config.cursor_shape_emacs),
|
||||||
vi_normal: Some(map_nucursorshape_to_cursorshape(
|
|
||||||
config.cursor_shape_vi_normal,
|
|
||||||
)),
|
|
||||||
emacs: Some(map_nucursorshape_to_cursorshape(config.cursor_shape_emacs)),
|
|
||||||
};
|
};
|
||||||
perf(
|
perf(
|
||||||
"get config/cursor config",
|
"get config/cursor config",
|
||||||
@ -264,6 +225,10 @@ pub fn evaluate_repl(
|
|||||||
start_time = std::time::Instant::now();
|
start_time = std::time::Instant::now();
|
||||||
|
|
||||||
line_editor = line_editor
|
line_editor = line_editor
|
||||||
|
.use_kitty_keyboard_enhancement(config.use_kitty_protocol)
|
||||||
|
// try to enable bracketed paste
|
||||||
|
// It doesn't work on windows system: https://github.com/crossterm-rs/crossterm/issues/737
|
||||||
|
.use_bracketed_paste(cfg!(not(target_os = "windows")) && config.bracketed_paste)
|
||||||
.with_highlighter(Box::new(NuHighlighter {
|
.with_highlighter(Box::new(NuHighlighter {
|
||||||
engine_state: engine_reference.clone(),
|
engine_state: engine_reference.clone(),
|
||||||
config: config.clone(),
|
config: config.clone(),
|
||||||
@ -295,7 +260,7 @@ pub fn evaluate_repl(
|
|||||||
line_editor.with_hinter(Box::new({
|
line_editor.with_hinter(Box::new({
|
||||||
// As of Nov 2022, "hints" color_config closures only get `null` passed in.
|
// As of Nov 2022, "hints" color_config closures only get `null` passed in.
|
||||||
let style = style_computer.compute("hints", &Value::nothing(Span::unknown()));
|
let style = style_computer.compute("hints", &Value::nothing(Span::unknown()));
|
||||||
DefaultHinter::default().with_style(style)
|
CwdAwareHinter::default().with_style(style)
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
line_editor.disable_hints()
|
line_editor.disable_hints()
|
||||||
@ -325,23 +290,17 @@ pub fn evaluate_repl(
|
|||||||
);
|
);
|
||||||
|
|
||||||
start_time = std::time::Instant::now();
|
start_time = std::time::Instant::now();
|
||||||
let buffer_editor = if !config.buffer_editor.is_empty() {
|
let buffer_editor = get_editor(engine_state, stack, Span::unknown());
|
||||||
Some(config.buffer_editor.clone())
|
|
||||||
} else {
|
|
||||||
stack
|
|
||||||
.get_env_var(engine_state, "EDITOR")
|
|
||||||
.map(|v| v.as_string().unwrap_or_default())
|
|
||||||
.filter(|v| !v.is_empty())
|
|
||||||
.or_else(|| {
|
|
||||||
stack
|
|
||||||
.get_env_var(engine_state, "VISUAL")
|
|
||||||
.map(|v| v.as_string().unwrap_or_default())
|
|
||||||
.filter(|v| !v.is_empty())
|
|
||||||
})
|
|
||||||
};
|
|
||||||
|
|
||||||
line_editor = if let Some(buffer_editor) = buffer_editor {
|
line_editor = if let Ok((cmd, args)) = buffer_editor {
|
||||||
line_editor.with_buffer_editor(buffer_editor, "nu".into())
|
let mut command = std::process::Command::new(&cmd);
|
||||||
|
command.args(args).envs(
|
||||||
|
engine_state
|
||||||
|
.render_env_vars()
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|(k, v)| v.as_string().ok().map(|v| (k, v))),
|
||||||
|
);
|
||||||
|
line_editor.with_buffer_editor(command, temp_file.clone())
|
||||||
} else {
|
} else {
|
||||||
line_editor
|
line_editor
|
||||||
};
|
};
|
||||||
@ -404,7 +363,7 @@ pub fn evaluate_repl(
|
|||||||
// Right before we start our prompt and take input from the user,
|
// Right before we start our prompt and take input from the user,
|
||||||
// fire the "pre_prompt" hook
|
// fire the "pre_prompt" hook
|
||||||
if let Some(hook) = config.hooks.pre_prompt.clone() {
|
if let Some(hook) = config.hooks.pre_prompt.clone() {
|
||||||
if let Err(err) = eval_hook(engine_state, stack, None, vec![], &hook) {
|
if let Err(err) = eval_hook(engine_state, stack, None, vec![], &hook, "pre_prompt") {
|
||||||
report_error_new(engine_state, &err);
|
report_error_new(engine_state, &err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -437,7 +396,9 @@ pub fn evaluate_repl(
|
|||||||
|
|
||||||
start_time = std::time::Instant::now();
|
start_time = std::time::Instant::now();
|
||||||
let config = &engine_state.get_config().clone();
|
let config = &engine_state.get_config().clone();
|
||||||
let prompt = prompt_update::update_prompt(config, engine_state, stack, &mut nu_prompt);
|
prompt_update::update_prompt(config, engine_state, stack, &mut nu_prompt);
|
||||||
|
let transient_prompt =
|
||||||
|
prompt_update::make_transient_prompt(config, engine_state, stack, &nu_prompt);
|
||||||
perf(
|
perf(
|
||||||
"update_prompt",
|
"update_prompt",
|
||||||
start_time,
|
start_time,
|
||||||
@ -450,12 +411,13 @@ pub fn evaluate_repl(
|
|||||||
entry_num += 1;
|
entry_num += 1;
|
||||||
|
|
||||||
start_time = std::time::Instant::now();
|
start_time = std::time::Instant::now();
|
||||||
let input = line_editor.read_line(prompt);
|
line_editor = line_editor.with_transient_prompt(transient_prompt);
|
||||||
|
let input = line_editor.read_line(&nu_prompt);
|
||||||
let shell_integration = config.shell_integration;
|
let shell_integration = config.shell_integration;
|
||||||
|
|
||||||
match input {
|
match input {
|
||||||
Ok(Signal::Success(s)) => {
|
Ok(Signal::Success(s)) => {
|
||||||
let hostname = sys.host_name();
|
let hostname = System::host_name();
|
||||||
let history_supports_meta =
|
let history_supports_meta =
|
||||||
matches!(config.history_file_format, HistoryFileFormat::Sqlite);
|
matches!(config.history_file_format, HistoryFileFormat::Sqlite);
|
||||||
if history_supports_meta && !s.is_empty() && line_editor.has_last_command_context()
|
if history_supports_meta && !s.is_empty() && line_editor.has_last_command_context()
|
||||||
@ -479,7 +441,9 @@ pub fn evaluate_repl(
|
|||||||
repl.buffer = s.to_string();
|
repl.buffer = s.to_string();
|
||||||
drop(repl);
|
drop(repl);
|
||||||
|
|
||||||
if let Err(err) = eval_hook(engine_state, stack, None, vec![], &hook) {
|
if let Err(err) =
|
||||||
|
eval_hook(engine_state, stack, None, vec![], &hook, "pre_execution")
|
||||||
|
{
|
||||||
report_error_new(engine_state, &err);
|
report_error_new(engine_state, &err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -513,7 +477,10 @@ pub fn evaluate_repl(
|
|||||||
|
|
||||||
report_error(
|
report_error(
|
||||||
&working_set,
|
&working_set,
|
||||||
&ShellError::DirectoryNotFound(tokens.0[0].span, None),
|
&ShellError::DirectoryNotFound {
|
||||||
|
dir: path.to_string_lossy().to_string(),
|
||||||
|
span: tokens.0[0].span,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let path = nu_path::canonicalize_with(path, &cwd)
|
let path = nu_path::canonicalize_with(path, &cwd)
|
||||||
@ -521,24 +488,12 @@ pub fn evaluate_repl(
|
|||||||
(path.to_string_lossy().to_string(), tokens.0[0].span)
|
(path.to_string_lossy().to_string(), tokens.0[0].span)
|
||||||
};
|
};
|
||||||
|
|
||||||
stack.add_env_var(
|
stack.add_env_var("OLDPWD".into(), Value::string(cwd.clone(), Span::unknown()));
|
||||||
"OLDPWD".into(),
|
|
||||||
Value::String {
|
|
||||||
val: cwd.clone(),
|
|
||||||
span: Span::unknown(),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
//FIXME: this only changes the current scope, but instead this environment variable
|
//FIXME: this only changes the current scope, but instead this environment variable
|
||||||
//should probably be a block that loads the information from the state in the overlay
|
//should probably be a block that loads the information from the state in the overlay
|
||||||
stack.add_env_var(
|
stack.add_env_var("PWD".into(), Value::string(path.clone(), Span::unknown()));
|
||||||
"PWD".into(),
|
let cwd = Value::string(cwd, span);
|
||||||
Value::String {
|
|
||||||
val: path.clone(),
|
|
||||||
span: Span::unknown(),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
let cwd = Value::String { val: cwd, span };
|
|
||||||
|
|
||||||
let shells = stack.get_env_var(engine_state, "NUSHELL_SHELLS");
|
let shells = stack.get_env_var(engine_state, "NUSHELL_SHELLS");
|
||||||
let mut shells = if let Some(v) = shells {
|
let mut shells = if let Some(v) = shells {
|
||||||
@ -551,27 +506,24 @@ pub fn evaluate_repl(
|
|||||||
|
|
||||||
let current_shell = stack.get_env_var(engine_state, "NUSHELL_CURRENT_SHELL");
|
let current_shell = stack.get_env_var(engine_state, "NUSHELL_CURRENT_SHELL");
|
||||||
let current_shell = if let Some(v) = current_shell {
|
let current_shell = if let Some(v) = current_shell {
|
||||||
v.as_integer().unwrap_or_default() as usize
|
v.as_int().unwrap_or_default() as usize
|
||||||
} else {
|
} else {
|
||||||
0
|
0
|
||||||
};
|
};
|
||||||
|
|
||||||
let last_shell = stack.get_env_var(engine_state, "NUSHELL_LAST_SHELL");
|
let last_shell = stack.get_env_var(engine_state, "NUSHELL_LAST_SHELL");
|
||||||
let last_shell = if let Some(v) = last_shell {
|
let last_shell = if let Some(v) = last_shell {
|
||||||
v.as_integer().unwrap_or_default() as usize
|
v.as_int().unwrap_or_default() as usize
|
||||||
} else {
|
} else {
|
||||||
0
|
0
|
||||||
};
|
};
|
||||||
|
|
||||||
shells[current_shell] = Value::String { val: path, span };
|
shells[current_shell] = Value::string(path, span);
|
||||||
|
|
||||||
stack.add_env_var("NUSHELL_SHELLS".into(), Value::List { vals: shells, span });
|
stack.add_env_var("NUSHELL_SHELLS".into(), Value::list(shells, span));
|
||||||
stack.add_env_var(
|
stack.add_env_var(
|
||||||
"NUSHELL_LAST_SHELL".into(),
|
"NUSHELL_LAST_SHELL".into(),
|
||||||
Value::Int {
|
Value::int(last_shell as i64, span),
|
||||||
val: last_shell as i64,
|
|
||||||
span,
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
} else if !s.trim().is_empty() {
|
} else if !s.trim().is_empty() {
|
||||||
trace!("eval source: {}", s);
|
trace!("eval source: {}", s);
|
||||||
@ -597,11 +549,6 @@ pub fn evaluate_repl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// should disable bracketed_paste to avoid strange pasting behavior
|
|
||||||
// while running commands.
|
|
||||||
#[cfg(not(target_os = "windows"))]
|
|
||||||
let _ = line_editor.disable_bracketed_paste();
|
|
||||||
|
|
||||||
eval_source(
|
eval_source(
|
||||||
engine_state,
|
engine_state,
|
||||||
stack,
|
stack,
|
||||||
@ -610,19 +557,12 @@ pub fn evaluate_repl(
|
|||||||
PipelineData::empty(),
|
PipelineData::empty(),
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
if engine_state.get_config().bracketed_paste {
|
|
||||||
#[cfg(not(target_os = "windows"))]
|
|
||||||
let _ = line_editor.enable_bracketed_paste();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
let cmd_duration = start_time.elapsed();
|
let cmd_duration = start_time.elapsed();
|
||||||
|
|
||||||
stack.add_env_var(
|
stack.add_env_var(
|
||||||
"CMD_DURATION_MS".into(),
|
"CMD_DURATION_MS".into(),
|
||||||
Value::String {
|
Value::string(format!("{}", cmd_duration.as_millis()), Span::unknown()),
|
||||||
val: format!("{}", cmd_duration.as_millis()),
|
|
||||||
span: Span::unknown(),
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if history_supports_meta && !s.is_empty() && line_editor.has_last_command_context()
|
if history_supports_meta && !s.is_empty() && line_editor.has_last_command_context()
|
||||||
@ -643,19 +583,29 @@ pub fn evaluate_repl(
|
|||||||
if let Some(cwd) = stack.get_env_var(engine_state, "PWD") {
|
if let Some(cwd) = stack.get_env_var(engine_state, "PWD") {
|
||||||
let path = cwd.as_string()?;
|
let path = cwd.as_string()?;
|
||||||
|
|
||||||
// Communicate the path as OSC 7 (often used for spawning new tabs in the same dir)
|
// Supported escape sequences of Microsoft's Visual Studio Code (vscode)
|
||||||
run_ansi_sequence(&format!(
|
// https://code.visualstudio.com/docs/terminal/shell-integration#_supported-escape-sequences
|
||||||
"\x1b]7;file://{}{}{}\x1b\\",
|
if stack.get_env_var(engine_state, "TERM_PROGRAM")
|
||||||
percent_encoding::utf8_percent_encode(
|
== Some(Value::test_string("vscode"))
|
||||||
&hostname.unwrap_or_else(|| "localhost".to_string()),
|
{
|
||||||
percent_encoding::CONTROLS
|
// If we're in vscode, run their specific ansi escape sequence.
|
||||||
),
|
// This is helpful for ctrl+g to change directories in the terminal.
|
||||||
if path.starts_with('/') { "" } else { "/" },
|
run_ansi_sequence(&format!("\x1b]633;P;Cwd={}\x1b\\", path))?;
|
||||||
percent_encoding::utf8_percent_encode(
|
} else {
|
||||||
&path,
|
// Otherwise, communicate the path as OSC 7 (often used for spawning new tabs in the same dir)
|
||||||
percent_encoding::CONTROLS
|
run_ansi_sequence(&format!(
|
||||||
)
|
"\x1b]7;file://{}{}{}\x1b\\",
|
||||||
))?;
|
percent_encoding::utf8_percent_encode(
|
||||||
|
&hostname.unwrap_or_else(|| "localhost".to_string()),
|
||||||
|
percent_encoding::CONTROLS
|
||||||
|
),
|
||||||
|
if path.starts_with('/') { "" } else { "/" },
|
||||||
|
percent_encoding::utf8_percent_encode(
|
||||||
|
&path,
|
||||||
|
percent_encoding::CONTROLS
|
||||||
|
)
|
||||||
|
))?;
|
||||||
|
}
|
||||||
|
|
||||||
// Try to abbreviate string for windows title
|
// Try to abbreviate string for windows title
|
||||||
let maybe_abbrev_path = if let Some(p) = nu_path::home_dir() {
|
let maybe_abbrev_path = if let Some(p) = nu_path::home_dir() {
|
||||||
@ -734,18 +684,62 @@ pub fn evaluate_repl(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn map_nucursorshape_to_cursorshape(shape: NuCursorShape) -> SetCursorStyle {
|
fn store_history_id_in_engine(engine_state: &mut EngineState, line_editor: &Reedline) {
|
||||||
|
let session_id = line_editor
|
||||||
|
.get_history_session_id()
|
||||||
|
.map(i64::from)
|
||||||
|
.unwrap_or(0);
|
||||||
|
|
||||||
|
engine_state.history_session_id = session_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_line_editor_history(
|
||||||
|
engine_state: &mut EngineState,
|
||||||
|
history_path: &Path,
|
||||||
|
line_editor: Reedline,
|
||||||
|
history_session_id: Option<HistorySessionId>,
|
||||||
|
) -> Result<Reedline, ErrReport> {
|
||||||
|
let config = engine_state.get_config();
|
||||||
|
let history: Box<dyn reedline::History> = match engine_state.config.history_file_format {
|
||||||
|
HistoryFileFormat::PlainText => Box::new(
|
||||||
|
FileBackedHistory::with_file(
|
||||||
|
config.max_history_size as usize,
|
||||||
|
history_path.to_path_buf(),
|
||||||
|
)
|
||||||
|
.into_diagnostic()?,
|
||||||
|
),
|
||||||
|
HistoryFileFormat::Sqlite => Box::new(
|
||||||
|
SqliteBackedHistory::with_file(
|
||||||
|
history_path.to_path_buf(),
|
||||||
|
history_session_id,
|
||||||
|
Some(chrono::Utc::now()),
|
||||||
|
)
|
||||||
|
.into_diagnostic()?,
|
||||||
|
),
|
||||||
|
};
|
||||||
|
let line_editor = line_editor
|
||||||
|
.with_history_session_id(history_session_id)
|
||||||
|
.with_history_exclusion_prefix(Some(" ".into()))
|
||||||
|
.with_history(history);
|
||||||
|
|
||||||
|
store_history_id_in_engine(engine_state, &line_editor);
|
||||||
|
|
||||||
|
Ok(line_editor)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn map_nucursorshape_to_cursorshape(shape: NuCursorShape) -> Option<SetCursorStyle> {
|
||||||
match shape {
|
match shape {
|
||||||
NuCursorShape::Block => SetCursorStyle::SteadyBlock,
|
NuCursorShape::Block => Some(SetCursorStyle::SteadyBlock),
|
||||||
NuCursorShape::UnderScore => SetCursorStyle::SteadyUnderScore,
|
NuCursorShape::UnderScore => Some(SetCursorStyle::SteadyUnderScore),
|
||||||
NuCursorShape::Line => SetCursorStyle::SteadyBar,
|
NuCursorShape::Line => Some(SetCursorStyle::SteadyBar),
|
||||||
NuCursorShape::BlinkBlock => SetCursorStyle::BlinkingBlock,
|
NuCursorShape::BlinkBlock => Some(SetCursorStyle::BlinkingBlock),
|
||||||
NuCursorShape::BlinkUnderScore => SetCursorStyle::BlinkingUnderScore,
|
NuCursorShape::BlinkUnderScore => Some(SetCursorStyle::BlinkingUnderScore),
|
||||||
NuCursorShape::BlinkLine => SetCursorStyle::BlinkingBar,
|
NuCursorShape::BlinkLine => Some(SetCursorStyle::BlinkingBar),
|
||||||
|
NuCursorShape::Inherit => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_command_finished_marker(stack: &Stack, engine_state: &EngineState) -> String {
|
fn get_command_finished_marker(stack: &Stack, engine_state: &EngineState) -> String {
|
||||||
let exit_code = stack
|
let exit_code = stack
|
||||||
.get_env_var(engine_state, "LAST_EXIT_CODE")
|
.get_env_var(engine_state, "LAST_EXIT_CODE")
|
||||||
.and_then(|e| e.as_i64().ok());
|
.and_then(|e| e.as_i64().ok());
|
||||||
@ -754,23 +748,21 @@ pub fn get_command_finished_marker(stack: &Stack, engine_state: &EngineState) ->
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn run_ansi_sequence(seq: &str) -> Result<(), ShellError> {
|
fn run_ansi_sequence(seq: &str) -> Result<(), ShellError> {
|
||||||
io::stdout().write_all(seq.as_bytes()).map_err(|e| {
|
io::stdout()
|
||||||
ShellError::GenericError(
|
.write_all(seq.as_bytes())
|
||||||
"Error writing ansi sequence".into(),
|
.map_err(|e| ShellError::GenericError {
|
||||||
e.to_string(),
|
error: "Error writing ansi sequence".into(),
|
||||||
Some(Span::unknown()),
|
msg: e.to_string(),
|
||||||
None,
|
span: Some(Span::unknown()),
|
||||||
Vec::new(),
|
help: None,
|
||||||
)
|
inner: vec![],
|
||||||
})?;
|
})?;
|
||||||
io::stdout().flush().map_err(|e| {
|
io::stdout().flush().map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error flushing stdio".into(),
|
||||||
"Error flushing stdio".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(Span::unknown()),
|
||||||
Some(Span::unknown()),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -794,14 +786,44 @@ fn looks_like_path(orig: &str) -> bool {
|
|||||||
|| orig.starts_with('~')
|
|| orig.starts_with('~')
|
||||||
|| orig.starts_with('/')
|
|| orig.starts_with('/')
|
||||||
|| orig.starts_with('\\')
|
|| orig.starts_with('\\')
|
||||||
|
|| orig.ends_with(std::path::MAIN_SEPARATOR)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
#[test]
|
||||||
|
fn looks_like_path_windows_drive_path_works() {
|
||||||
|
assert!(looks_like_path("C:"));
|
||||||
|
assert!(looks_like_path("D:\\"));
|
||||||
|
assert!(looks_like_path("E:/"));
|
||||||
|
assert!(looks_like_path("F:\\some_dir"));
|
||||||
|
assert!(looks_like_path("G:/some_dir"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
#[test]
|
||||||
|
fn trailing_slash_looks_like_path() {
|
||||||
|
assert!(looks_like_path("foo\\"))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
#[test]
|
||||||
|
fn trailing_slash_looks_like_path() {
|
||||||
|
assert!(looks_like_path("foo/"))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn looks_like_path_windows_drive_path_works() {
|
fn are_session_ids_in_sync() {
|
||||||
let on_windows = cfg!(windows);
|
let engine_state = &mut EngineState::new();
|
||||||
assert_eq!(looks_like_path("C:"), on_windows);
|
let history_path_o =
|
||||||
assert_eq!(looks_like_path("D:\\"), on_windows);
|
crate::config_files::get_history_path("nushell", engine_state.config.history_file_format);
|
||||||
assert_eq!(looks_like_path("E:/"), on_windows);
|
assert!(history_path_o.is_some());
|
||||||
assert_eq!(looks_like_path("F:\\some_dir"), on_windows);
|
let history_path = history_path_o.as_deref().unwrap();
|
||||||
assert_eq!(looks_like_path("G:/some_dir"), on_windows);
|
let line_editor = reedline::Reedline::create();
|
||||||
|
let history_session_id = reedline::Reedline::create_history_session_id();
|
||||||
|
let line_editor =
|
||||||
|
update_line_editor_history(engine_state, history_path, line_editor, history_session_id);
|
||||||
|
assert_eq!(
|
||||||
|
i64::from(line_editor.unwrap().get_history_session_id().unwrap()),
|
||||||
|
engine_state.history_session_id
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ use log::trace;
|
|||||||
use nu_ansi_term::Style;
|
use nu_ansi_term::Style;
|
||||||
use nu_color_config::{get_matching_brackets_style, get_shape_color};
|
use nu_color_config::{get_matching_brackets_style, get_shape_color};
|
||||||
use nu_parser::{flatten_block, parse, FlatShape};
|
use nu_parser::{flatten_block, parse, FlatShape};
|
||||||
use nu_protocol::ast::{Argument, Block, Expr, Expression, PipelineElement};
|
use nu_protocol::ast::{Argument, Block, Expr, Expression, PipelineElement, RecordItem};
|
||||||
use nu_protocol::engine::{EngineState, StateWorkingSet};
|
use nu_protocol::engine::{EngineState, StateWorkingSet};
|
||||||
use nu_protocol::{Config, Span};
|
use nu_protocol::{Config, Span};
|
||||||
use reedline::{Highlighter, StyledText};
|
use reedline::{Highlighter, StyledText};
|
||||||
@ -17,10 +17,27 @@ impl Highlighter for NuHighlighter {
|
|||||||
fn highlight(&self, line: &str, _cursor: usize) -> StyledText {
|
fn highlight(&self, line: &str, _cursor: usize) -> StyledText {
|
||||||
trace!("highlighting: {}", line);
|
trace!("highlighting: {}", line);
|
||||||
|
|
||||||
|
let highlight_resolved_externals =
|
||||||
|
self.engine_state.get_config().highlight_resolved_externals;
|
||||||
let mut working_set = StateWorkingSet::new(&self.engine_state);
|
let mut working_set = StateWorkingSet::new(&self.engine_state);
|
||||||
let block = parse(&mut working_set, None, line.as_bytes(), false);
|
let block = parse(&mut working_set, None, line.as_bytes(), false);
|
||||||
let (shapes, global_span_offset) = {
|
let (shapes, global_span_offset) = {
|
||||||
let shapes = flatten_block(&working_set, &block);
|
let mut shapes = flatten_block(&working_set, &block);
|
||||||
|
// Highlighting externals has a config point because of concerns that using which to resolve
|
||||||
|
// externals may slow down things too much.
|
||||||
|
if highlight_resolved_externals {
|
||||||
|
for (span, shape) in shapes.iter_mut() {
|
||||||
|
if *shape == FlatShape::External {
|
||||||
|
let str_contents =
|
||||||
|
working_set.get_span_contents(Span::new(span.start, span.end));
|
||||||
|
|
||||||
|
let str_word = String::from_utf8_lossy(str_contents).to_string();
|
||||||
|
if which::which(str_word).ok().is_some() {
|
||||||
|
*shape = FlatShape::ExternalResolved;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
(shapes, self.engine_state.next_span_start())
|
(shapes, self.engine_state.next_span_start())
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -59,7 +76,7 @@ impl Highlighter for NuHighlighter {
|
|||||||
($shape:expr, $span:expr, $text:expr) => {{
|
($shape:expr, $span:expr, $text:expr) => {{
|
||||||
let spans = split_span_by_highlight_positions(
|
let spans = split_span_by_highlight_positions(
|
||||||
line,
|
line,
|
||||||
&$span,
|
$span,
|
||||||
&matching_brackets_pos,
|
&matching_brackets_pos,
|
||||||
global_span_offset,
|
global_span_offset,
|
||||||
);
|
);
|
||||||
@ -91,6 +108,7 @@ impl Highlighter for NuHighlighter {
|
|||||||
FlatShape::InternalCall(_) => add_colored_token(&shape.1, next_token),
|
FlatShape::InternalCall(_) => add_colored_token(&shape.1, next_token),
|
||||||
FlatShape::External => add_colored_token(&shape.1, next_token),
|
FlatShape::External => add_colored_token(&shape.1, next_token),
|
||||||
FlatShape::ExternalArg => add_colored_token(&shape.1, next_token),
|
FlatShape::ExternalArg => add_colored_token(&shape.1, next_token),
|
||||||
|
FlatShape::ExternalResolved => add_colored_token(&shape.1, next_token),
|
||||||
FlatShape::Keyword => add_colored_token(&shape.1, next_token),
|
FlatShape::Keyword => add_colored_token(&shape.1, next_token),
|
||||||
FlatShape::Literal => add_colored_token(&shape.1, next_token),
|
FlatShape::Literal => add_colored_token(&shape.1, next_token),
|
||||||
FlatShape::Operator => add_colored_token(&shape.1, next_token),
|
FlatShape::Operator => add_colored_token(&shape.1, next_token),
|
||||||
@ -143,8 +161,8 @@ impl Highlighter for NuHighlighter {
|
|||||||
|
|
||||||
fn split_span_by_highlight_positions(
|
fn split_span_by_highlight_positions(
|
||||||
line: &str,
|
line: &str,
|
||||||
span: &Span,
|
span: Span,
|
||||||
highlight_positions: &Vec<usize>,
|
highlight_positions: &[usize],
|
||||||
global_span_offset: usize,
|
global_span_offset: usize,
|
||||||
) -> Vec<(Span, bool)> {
|
) -> Vec<(Span, bool)> {
|
||||||
let mut start = span.start;
|
let mut start = span.start;
|
||||||
@ -234,11 +252,11 @@ fn find_matching_block_end_in_block(
|
|||||||
for e in &p.elements {
|
for e in &p.elements {
|
||||||
match e {
|
match e {
|
||||||
PipelineElement::Expression(_, e)
|
PipelineElement::Expression(_, e)
|
||||||
| PipelineElement::Redirection(_, _, e)
|
| PipelineElement::Redirection(_, _, e, _)
|
||||||
| PipelineElement::And(_, e)
|
| PipelineElement::And(_, e)
|
||||||
| PipelineElement::Or(_, e)
|
| PipelineElement::Or(_, e)
|
||||||
| PipelineElement::SameTargetRedirection { cmd: (_, e), .. }
|
| PipelineElement::SameTargetRedirection { cmd: (_, e), .. }
|
||||||
| PipelineElement::SeparateRedirection { out: (_, e), .. } => {
|
| PipelineElement::SeparateRedirection { out: (_, e, _), .. } => {
|
||||||
if e.span.contains(global_cursor_offset) {
|
if e.span.contains(global_cursor_offset) {
|
||||||
if let Some(pos) = find_matching_block_end_in_expr(
|
if let Some(pos) = find_matching_block_end_in_expr(
|
||||||
line,
|
line,
|
||||||
@ -311,10 +329,10 @@ fn find_matching_block_end_in_expr(
|
|||||||
Expr::ImportPattern(_) => None,
|
Expr::ImportPattern(_) => None,
|
||||||
Expr::Overlay(_) => None,
|
Expr::Overlay(_) => None,
|
||||||
Expr::Signature(_) => None,
|
Expr::Signature(_) => None,
|
||||||
Expr::MatchPattern(_) => None,
|
|
||||||
Expr::MatchBlock(_) => None,
|
Expr::MatchBlock(_) => None,
|
||||||
Expr::Nothing => None,
|
Expr::Nothing => None,
|
||||||
Expr::Garbage => None,
|
Expr::Garbage => None,
|
||||||
|
Expr::Spread(_) => None,
|
||||||
|
|
||||||
Expr::Table(hdr, rows) => {
|
Expr::Table(hdr, rows) => {
|
||||||
if expr_last == global_cursor_offset {
|
if expr_last == global_cursor_offset {
|
||||||
@ -346,9 +364,16 @@ fn find_matching_block_end_in_expr(
|
|||||||
Some(expr_last)
|
Some(expr_last)
|
||||||
} else {
|
} else {
|
||||||
// cursor is inside record
|
// cursor is inside record
|
||||||
for (k, v) in exprs {
|
for expr in exprs {
|
||||||
find_in_expr_or_continue!(k);
|
match expr {
|
||||||
find_in_expr_or_continue!(v);
|
RecordItem::Pair(k, v) => {
|
||||||
|
find_in_expr_or_continue!(k);
|
||||||
|
find_in_expr_or_continue!(v);
|
||||||
|
}
|
||||||
|
RecordItem::Spread(_, record) => {
|
||||||
|
find_in_expr_or_continue!(record);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@ -360,6 +385,7 @@ fn find_matching_block_end_in_expr(
|
|||||||
Argument::Named((_, _, opt_expr)) => opt_expr.as_ref(),
|
Argument::Named((_, _, opt_expr)) => opt_expr.as_ref(),
|
||||||
Argument::Positional(inner_expr) => Some(inner_expr),
|
Argument::Positional(inner_expr) => Some(inner_expr),
|
||||||
Argument::Unknown(inner_expr) => Some(inner_expr),
|
Argument::Unknown(inner_expr) => Some(inner_expr),
|
||||||
|
Argument::Spread(inner_expr) => Some(inner_expr),
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(inner_expr) = opt_expr {
|
if let Some(inner_expr) = opt_expr {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use nu_command::hook::eval_hook;
|
use nu_cmd_base::hook::eval_hook;
|
||||||
use nu_engine::{eval_block, eval_block_with_early_return};
|
use nu_engine::{eval_block, eval_block_with_early_return};
|
||||||
use nu_parser::{escape_quote_string, lex, parse, unescape_unquote_string, Token, TokenContents};
|
use nu_parser::{escape_quote_string, lex, parse, unescape_unquote_string, Token, TokenContents};
|
||||||
use nu_protocol::engine::StateWorkingSet;
|
use nu_protocol::engine::StateWorkingSet;
|
||||||
@ -43,13 +43,13 @@ fn gather_env_vars(
|
|||||||
let working_set = StateWorkingSet::new(engine_state);
|
let working_set = StateWorkingSet::new(engine_state);
|
||||||
report_error(
|
report_error(
|
||||||
&working_set,
|
&working_set,
|
||||||
&ShellError::GenericError(
|
&ShellError::GenericError {
|
||||||
format!("Environment variable was not captured: {env_str}"),
|
error: format!("Environment variable was not captured: {env_str}"),
|
||||||
"".to_string(),
|
msg: "".into(),
|
||||||
None,
|
span: None,
|
||||||
Some(msg.into()),
|
help: Some(msg.into()),
|
||||||
Vec::new(),
|
inner: vec![],
|
||||||
),
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,15 +75,15 @@ fn gather_env_vars(
|
|||||||
let working_set = StateWorkingSet::new(engine_state);
|
let working_set = StateWorkingSet::new(engine_state);
|
||||||
report_error(
|
report_error(
|
||||||
&working_set,
|
&working_set,
|
||||||
&ShellError::GenericError(
|
&ShellError::GenericError {
|
||||||
"Current directory is not a valid utf-8 path".to_string(),
|
error: "Current directory is not a valid utf-8 path".into(),
|
||||||
"".to_string(),
|
msg: "".into(),
|
||||||
None,
|
span: None,
|
||||||
Some(format!(
|
help: Some(format!(
|
||||||
"Retrieving current directory failed: {init_cwd:?} not a valid utf-8 path"
|
"Retrieving current directory failed: {init_cwd:?} not a valid utf-8 path"
|
||||||
)),
|
)),
|
||||||
Vec::new(),
|
inner: vec![],
|
||||||
),
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -105,13 +105,13 @@ fn gather_env_vars(
|
|||||||
span: full_span,
|
span: full_span,
|
||||||
} = token
|
} = token
|
||||||
{
|
{
|
||||||
let contents = engine_state.get_span_contents(&full_span);
|
let contents = engine_state.get_span_contents(full_span);
|
||||||
let (parts, _) = lex(contents, full_span.start, &[], &[b'='], true);
|
let (parts, _) = lex(contents, full_span.start, &[], &[b'='], true);
|
||||||
|
|
||||||
let name = if let Some(Token {
|
let name = if let Some(Token {
|
||||||
contents: TokenContents::Item,
|
contents: TokenContents::Item,
|
||||||
span,
|
span,
|
||||||
}) = parts.get(0)
|
}) = parts.first()
|
||||||
{
|
{
|
||||||
let mut working_set = StateWorkingSet::new(engine_state);
|
let mut working_set = StateWorkingSet::new(engine_state);
|
||||||
let bytes = working_set.get_span_contents(*span);
|
let bytes = working_set.get_span_contents(*span);
|
||||||
@ -185,10 +185,7 @@ fn gather_env_vars(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value::String {
|
Value::string(bytes, *span)
|
||||||
val: bytes,
|
|
||||||
span: *span,
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
report_capture_error(
|
report_capture_error(
|
||||||
engine_state,
|
engine_state,
|
||||||
@ -223,6 +220,10 @@ pub fn eval_source(
|
|||||||
source,
|
source,
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
|
if let Some(warning) = working_set.parse_warnings.first() {
|
||||||
|
report_error(&working_set, warning);
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(err) = working_set.parse_errors.first() {
|
if let Some(err) = working_set.parse_errors.first() {
|
||||||
set_last_exit_code(stack, 1);
|
set_last_exit_code(stack, 1);
|
||||||
report_error(&working_set, err);
|
report_error(&working_set, err);
|
||||||
@ -257,7 +258,14 @@ pub fn eval_source(
|
|||||||
{
|
{
|
||||||
result = print_if_stream(stream, stderr_stream, false, exit_code);
|
result = print_if_stream(stream, stderr_stream, false, exit_code);
|
||||||
} else if let Some(hook) = config.hooks.display_output.clone() {
|
} else if let Some(hook) = config.hooks.display_output.clone() {
|
||||||
match eval_hook(engine_state, stack, Some(pipeline_data), vec![], &hook) {
|
match eval_hook(
|
||||||
|
engine_state,
|
||||||
|
stack,
|
||||||
|
Some(pipeline_data),
|
||||||
|
vec![],
|
||||||
|
&hook,
|
||||||
|
"display_output",
|
||||||
|
) {
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
result = Err(err);
|
result = Err(err);
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,16 @@
|
|||||||
pub mod support;
|
pub mod support;
|
||||||
|
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use nu_cli::NuCompleter;
|
use nu_cli::NuCompleter;
|
||||||
use nu_parser::parse;
|
use nu_parser::parse;
|
||||||
use nu_protocol::engine::StateWorkingSet;
|
use nu_protocol::engine::StateWorkingSet;
|
||||||
use reedline::{Completer, Suggestion};
|
use reedline::{Completer, Suggestion};
|
||||||
use rstest::{fixture, rstest};
|
use rstest::{fixture, rstest};
|
||||||
use support::{completions_helpers::new_quote_engine, file, folder, match_suggestions, new_engine};
|
use support::{
|
||||||
|
completions_helpers::{new_partial_engine, new_quote_engine},
|
||||||
|
file, folder, match_suggestions, new_engine,
|
||||||
|
};
|
||||||
|
|
||||||
#[fixture]
|
#[fixture]
|
||||||
fn completer() -> NuCompleter {
|
fn completer() -> NuCompleter {
|
||||||
@ -54,6 +59,29 @@ fn extern_completer() -> NuCompleter {
|
|||||||
NuCompleter::new(std::sync::Arc::new(engine), stack)
|
NuCompleter::new(std::sync::Arc::new(engine), stack)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[fixture]
|
||||||
|
fn custom_completer() -> NuCompleter {
|
||||||
|
// Create a new engine
|
||||||
|
let (dir, _, mut engine, mut stack) = new_engine();
|
||||||
|
|
||||||
|
// Add record value as example
|
||||||
|
let record = r#"
|
||||||
|
let external_completer = {|spans|
|
||||||
|
$spans
|
||||||
|
}
|
||||||
|
|
||||||
|
$env.config.completions.external = {
|
||||||
|
enable: true
|
||||||
|
max_results: 100
|
||||||
|
completer: $external_completer
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok());
|
||||||
|
|
||||||
|
// Instantiate a new completer
|
||||||
|
NuCompleter::new(std::sync::Arc::new(engine), stack)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn variables_dollar_sign_with_varialblecompletion() {
|
fn variables_dollar_sign_with_varialblecompletion() {
|
||||||
let (_, _, engine, stack) = new_engine();
|
let (_, _, engine, stack) = new_engine();
|
||||||
@ -63,7 +91,7 @@ fn variables_dollar_sign_with_varialblecompletion() {
|
|||||||
let target_dir = "$ ";
|
let target_dir = "$ ";
|
||||||
let suggestions = completer.complete(target_dir, target_dir.len());
|
let suggestions = completer.complete(target_dir, target_dir.len());
|
||||||
|
|
||||||
assert_eq!(7, suggestions.len());
|
assert_eq!(8, suggestions.len());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
@ -116,15 +144,34 @@ fn dotnu_completions() {
|
|||||||
let completion_str = "source-env ".to_string();
|
let completion_str = "source-env ".to_string();
|
||||||
let suggestions = completer.complete(&completion_str, completion_str.len());
|
let suggestions = completer.complete(&completion_str, completion_str.len());
|
||||||
|
|
||||||
assert_eq!(1, suggestions.len());
|
assert_eq!(2, suggestions.len());
|
||||||
assert_eq!("custom_completion.nu", suggestions.get(0).unwrap().value);
|
assert_eq!("custom_completion.nu", suggestions.first().unwrap().value);
|
||||||
|
#[cfg(windows)]
|
||||||
|
assert_eq!("directory_completion\\", suggestions.get(1).unwrap().value);
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
assert_eq!("directory_completion/", suggestions.get(1).unwrap().value);
|
||||||
|
|
||||||
// Test use completion
|
// Test use completion
|
||||||
let completion_str = "use ".to_string();
|
let completion_str = "use ".to_string();
|
||||||
let suggestions = completer.complete(&completion_str, completion_str.len());
|
let suggestions = completer.complete(&completion_str, completion_str.len());
|
||||||
|
|
||||||
assert_eq!(1, suggestions.len());
|
assert_eq!(2, suggestions.len());
|
||||||
assert_eq!("custom_completion.nu", suggestions.get(0).unwrap().value);
|
assert_eq!("custom_completion.nu", suggestions.first().unwrap().value);
|
||||||
|
#[cfg(windows)]
|
||||||
|
assert_eq!("directory_completion\\", suggestions.get(1).unwrap().value);
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
assert_eq!("directory_completion/", suggestions.get(1).unwrap().value);
|
||||||
|
|
||||||
|
// Test overlay use completion
|
||||||
|
let completion_str = "overlay use ".to_string();
|
||||||
|
let suggestions = completer.complete(&completion_str, completion_str.len());
|
||||||
|
|
||||||
|
assert_eq!(2, suggestions.len());
|
||||||
|
assert_eq!("custom_completion.nu", suggestions.first().unwrap().value);
|
||||||
|
#[cfg(windows)]
|
||||||
|
assert_eq!("directory_completion\\", suggestions.get(1).unwrap().value);
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
assert_eq!("directory_completion/", suggestions.get(1).unwrap().value);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -136,30 +183,30 @@ fn external_completer_trailing_space() {
|
|||||||
|
|
||||||
let suggestions = run_external_completion(block, &input);
|
let suggestions = run_external_completion(block, &input);
|
||||||
assert_eq!(3, suggestions.len());
|
assert_eq!(3, suggestions.len());
|
||||||
assert_eq!("gh", suggestions.get(0).unwrap().value);
|
assert_eq!("gh", suggestions.first().unwrap().value);
|
||||||
assert_eq!("alias", suggestions.get(1).unwrap().value);
|
assert_eq!("alias", suggestions.get(1).unwrap().value);
|
||||||
assert_eq!("", suggestions.get(2).unwrap().value);
|
assert_eq!("", suggestions.get(2).unwrap().value);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn external_completer_no_trailing_space() {
|
fn external_completer_no_trailing_space() {
|
||||||
let block = "let external_completer = {|spans| $spans}";
|
let block = "{|spans| $spans}";
|
||||||
let input = "gh alias".to_string();
|
let input = "gh alias".to_string();
|
||||||
|
|
||||||
let suggestions = run_external_completion(block, &input);
|
let suggestions = run_external_completion(block, &input);
|
||||||
assert_eq!(2, suggestions.len());
|
assert_eq!(2, suggestions.len());
|
||||||
assert_eq!("gh", suggestions.get(0).unwrap().value);
|
assert_eq!("gh", suggestions.first().unwrap().value);
|
||||||
assert_eq!("alias", suggestions.get(1).unwrap().value);
|
assert_eq!("alias", suggestions.get(1).unwrap().value);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn external_completer_pass_flags() {
|
fn external_completer_pass_flags() {
|
||||||
let block = "let external_completer = {|spans| $spans}";
|
let block = "{|spans| $spans}";
|
||||||
let input = "gh api --".to_string();
|
let input = "gh api --".to_string();
|
||||||
|
|
||||||
let suggestions = run_external_completion(block, &input);
|
let suggestions = run_external_completion(block, &input);
|
||||||
assert_eq!(3, suggestions.len());
|
assert_eq!(3, suggestions.len());
|
||||||
assert_eq!("gh", suggestions.get(0).unwrap().value);
|
assert_eq!("gh", suggestions.first().unwrap().value);
|
||||||
assert_eq!("api", suggestions.get(1).unwrap().value);
|
assert_eq!("api", suggestions.get(1).unwrap().value);
|
||||||
assert_eq!("--", suggestions.get(2).unwrap().value);
|
assert_eq!("--", suggestions.get(2).unwrap().value);
|
||||||
}
|
}
|
||||||
@ -180,6 +227,7 @@ fn file_completions() {
|
|||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
folder(dir.join("another")),
|
folder(dir.join("another")),
|
||||||
file(dir.join("custom_completion.nu")),
|
file(dir.join("custom_completion.nu")),
|
||||||
|
folder(dir.join("directory_completion")),
|
||||||
file(dir.join("nushell")),
|
file(dir.join("nushell")),
|
||||||
folder(dir.join("test_a")),
|
folder(dir.join("test_a")),
|
||||||
folder(dir.join("test_b")),
|
folder(dir.join("test_b")),
|
||||||
@ -201,6 +249,87 @@ fn file_completions() {
|
|||||||
match_suggestions(expected_paths, suggestions);
|
match_suggestions(expected_paths, suggestions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn partial_completions() {
|
||||||
|
// Create a new engine
|
||||||
|
let (dir, _, engine, stack) = new_partial_engine();
|
||||||
|
|
||||||
|
// Instantiate a new completer
|
||||||
|
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
||||||
|
|
||||||
|
// Test completions for a folder's name
|
||||||
|
let target_dir = format!("cd {}", file(dir.join("pa")));
|
||||||
|
let suggestions = completer.complete(&target_dir, target_dir.len());
|
||||||
|
|
||||||
|
// Create the expected values
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
folder(dir.join("partial_a")),
|
||||||
|
folder(dir.join("partial_b")),
|
||||||
|
folder(dir.join("partial_c")),
|
||||||
|
];
|
||||||
|
|
||||||
|
// Match the results
|
||||||
|
match_suggestions(expected_paths, suggestions);
|
||||||
|
|
||||||
|
// Test completions for the files whose name begin with "h"
|
||||||
|
// and are present under directories whose names begin with "pa"
|
||||||
|
let dir_str = file(dir.join("pa").join("h"));
|
||||||
|
let target_dir = format!("cp {dir_str}");
|
||||||
|
let suggestions = completer.complete(&target_dir, target_dir.len());
|
||||||
|
|
||||||
|
// Create the expected values
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
file(dir.join("partial_a").join("hello")),
|
||||||
|
file(dir.join("partial_a").join("hola")),
|
||||||
|
file(dir.join("partial_b").join("hello_b")),
|
||||||
|
file(dir.join("partial_b").join("hi_b")),
|
||||||
|
file(dir.join("partial_c").join("hello_c")),
|
||||||
|
];
|
||||||
|
|
||||||
|
// Match the results
|
||||||
|
match_suggestions(expected_paths, suggestions);
|
||||||
|
|
||||||
|
// Test completion for all files under directories whose names begin with "pa"
|
||||||
|
let dir_str = folder(dir.join("pa"));
|
||||||
|
let target_dir = format!("ls {dir_str}");
|
||||||
|
let suggestions = completer.complete(&target_dir, target_dir.len());
|
||||||
|
|
||||||
|
// Create the expected values
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
file(dir.join("partial_a").join("anotherfile")),
|
||||||
|
file(dir.join("partial_a").join("hello")),
|
||||||
|
file(dir.join("partial_a").join("hola")),
|
||||||
|
file(dir.join("partial_b").join("hello_b")),
|
||||||
|
file(dir.join("partial_b").join("hi_b")),
|
||||||
|
file(dir.join("partial_c").join("hello_c")),
|
||||||
|
];
|
||||||
|
|
||||||
|
// Match the results
|
||||||
|
match_suggestions(expected_paths, suggestions);
|
||||||
|
|
||||||
|
// Test completion for a single file
|
||||||
|
let dir_str = file(dir.join("fi").join("so"));
|
||||||
|
let target_dir = format!("rm {dir_str}");
|
||||||
|
let suggestions = completer.complete(&target_dir, target_dir.len());
|
||||||
|
|
||||||
|
// Create the expected values
|
||||||
|
let expected_paths: Vec<String> = vec![file(dir.join("final_partial").join("somefile"))];
|
||||||
|
|
||||||
|
// Match the results
|
||||||
|
match_suggestions(expected_paths, suggestions);
|
||||||
|
|
||||||
|
// Test completion where there is a sneaky `..` in the path
|
||||||
|
let dir_str = file(dir.join("par").join("..").join("fi").join("so"));
|
||||||
|
let target_dir = format!("rm {dir_str}");
|
||||||
|
let suggestions = completer.complete(&target_dir, target_dir.len());
|
||||||
|
|
||||||
|
// Create the expected values
|
||||||
|
let expected_paths: Vec<String> = vec![file(dir.join("final_partial").join("somefile"))];
|
||||||
|
|
||||||
|
// Match the results
|
||||||
|
match_suggestions(expected_paths, suggestions);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn command_ls_with_filecompletion() {
|
fn command_ls_with_filecompletion() {
|
||||||
let (_, _, engine, stack) = new_engine();
|
let (_, _, engine, stack) = new_engine();
|
||||||
@ -214,6 +343,7 @@ fn command_ls_with_filecompletion() {
|
|||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
"another\\".to_string(),
|
"another\\".to_string(),
|
||||||
"custom_completion.nu".to_string(),
|
"custom_completion.nu".to_string(),
|
||||||
|
"directory_completion\\".to_string(),
|
||||||
"nushell".to_string(),
|
"nushell".to_string(),
|
||||||
"test_a\\".to_string(),
|
"test_a\\".to_string(),
|
||||||
"test_b\\".to_string(),
|
"test_b\\".to_string(),
|
||||||
@ -224,6 +354,7 @@ fn command_ls_with_filecompletion() {
|
|||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
"another/".to_string(),
|
"another/".to_string(),
|
||||||
"custom_completion.nu".to_string(),
|
"custom_completion.nu".to_string(),
|
||||||
|
"directory_completion/".to_string(),
|
||||||
"nushell".to_string(),
|
"nushell".to_string(),
|
||||||
"test_a/".to_string(),
|
"test_a/".to_string(),
|
||||||
"test_b/".to_string(),
|
"test_b/".to_string(),
|
||||||
@ -246,6 +377,7 @@ fn command_open_with_filecompletion() {
|
|||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
"another\\".to_string(),
|
"another\\".to_string(),
|
||||||
"custom_completion.nu".to_string(),
|
"custom_completion.nu".to_string(),
|
||||||
|
"directory_completion\\".to_string(),
|
||||||
"nushell".to_string(),
|
"nushell".to_string(),
|
||||||
"test_a\\".to_string(),
|
"test_a\\".to_string(),
|
||||||
"test_b\\".to_string(),
|
"test_b\\".to_string(),
|
||||||
@ -256,6 +388,7 @@ fn command_open_with_filecompletion() {
|
|||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
"another/".to_string(),
|
"another/".to_string(),
|
||||||
"custom_completion.nu".to_string(),
|
"custom_completion.nu".to_string(),
|
||||||
|
"directory_completion/".to_string(),
|
||||||
"nushell".to_string(),
|
"nushell".to_string(),
|
||||||
"test_a/".to_string(),
|
"test_a/".to_string(),
|
||||||
"test_b/".to_string(),
|
"test_b/".to_string(),
|
||||||
@ -279,6 +412,7 @@ fn command_rm_with_globcompletion() {
|
|||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
"another\\".to_string(),
|
"another\\".to_string(),
|
||||||
"custom_completion.nu".to_string(),
|
"custom_completion.nu".to_string(),
|
||||||
|
"directory_completion\\".to_string(),
|
||||||
"nushell".to_string(),
|
"nushell".to_string(),
|
||||||
"test_a\\".to_string(),
|
"test_a\\".to_string(),
|
||||||
"test_b\\".to_string(),
|
"test_b\\".to_string(),
|
||||||
@ -289,6 +423,7 @@ fn command_rm_with_globcompletion() {
|
|||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
"another/".to_string(),
|
"another/".to_string(),
|
||||||
"custom_completion.nu".to_string(),
|
"custom_completion.nu".to_string(),
|
||||||
|
"directory_completion/".to_string(),
|
||||||
"nushell".to_string(),
|
"nushell".to_string(),
|
||||||
"test_a/".to_string(),
|
"test_a/".to_string(),
|
||||||
"test_b/".to_string(),
|
"test_b/".to_string(),
|
||||||
@ -312,6 +447,7 @@ fn command_cp_with_globcompletion() {
|
|||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
"another\\".to_string(),
|
"another\\".to_string(),
|
||||||
"custom_completion.nu".to_string(),
|
"custom_completion.nu".to_string(),
|
||||||
|
"directory_completion\\".to_string(),
|
||||||
"nushell".to_string(),
|
"nushell".to_string(),
|
||||||
"test_a\\".to_string(),
|
"test_a\\".to_string(),
|
||||||
"test_b\\".to_string(),
|
"test_b\\".to_string(),
|
||||||
@ -322,6 +458,7 @@ fn command_cp_with_globcompletion() {
|
|||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
"another/".to_string(),
|
"another/".to_string(),
|
||||||
"custom_completion.nu".to_string(),
|
"custom_completion.nu".to_string(),
|
||||||
|
"directory_completion/".to_string(),
|
||||||
"nushell".to_string(),
|
"nushell".to_string(),
|
||||||
"test_a/".to_string(),
|
"test_a/".to_string(),
|
||||||
"test_b/".to_string(),
|
"test_b/".to_string(),
|
||||||
@ -345,6 +482,7 @@ fn command_save_with_filecompletion() {
|
|||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
"another\\".to_string(),
|
"another\\".to_string(),
|
||||||
"custom_completion.nu".to_string(),
|
"custom_completion.nu".to_string(),
|
||||||
|
"directory_completion\\".to_string(),
|
||||||
"nushell".to_string(),
|
"nushell".to_string(),
|
||||||
"test_a\\".to_string(),
|
"test_a\\".to_string(),
|
||||||
"test_b\\".to_string(),
|
"test_b\\".to_string(),
|
||||||
@ -355,6 +493,7 @@ fn command_save_with_filecompletion() {
|
|||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
"another/".to_string(),
|
"another/".to_string(),
|
||||||
"custom_completion.nu".to_string(),
|
"custom_completion.nu".to_string(),
|
||||||
|
"directory_completion/".to_string(),
|
||||||
"nushell".to_string(),
|
"nushell".to_string(),
|
||||||
"test_a/".to_string(),
|
"test_a/".to_string(),
|
||||||
"test_b/".to_string(),
|
"test_b/".to_string(),
|
||||||
@ -378,6 +517,7 @@ fn command_touch_with_filecompletion() {
|
|||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
"another\\".to_string(),
|
"another\\".to_string(),
|
||||||
"custom_completion.nu".to_string(),
|
"custom_completion.nu".to_string(),
|
||||||
|
"directory_completion\\".to_string(),
|
||||||
"nushell".to_string(),
|
"nushell".to_string(),
|
||||||
"test_a\\".to_string(),
|
"test_a\\".to_string(),
|
||||||
"test_b\\".to_string(),
|
"test_b\\".to_string(),
|
||||||
@ -388,6 +528,7 @@ fn command_touch_with_filecompletion() {
|
|||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
"another/".to_string(),
|
"another/".to_string(),
|
||||||
"custom_completion.nu".to_string(),
|
"custom_completion.nu".to_string(),
|
||||||
|
"directory_completion/".to_string(),
|
||||||
"nushell".to_string(),
|
"nushell".to_string(),
|
||||||
"test_a/".to_string(),
|
"test_a/".to_string(),
|
||||||
"test_b/".to_string(),
|
"test_b/".to_string(),
|
||||||
@ -411,6 +552,7 @@ fn command_watch_with_filecompletion() {
|
|||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
"another\\".to_string(),
|
"another\\".to_string(),
|
||||||
"custom_completion.nu".to_string(),
|
"custom_completion.nu".to_string(),
|
||||||
|
"directory_completion\\".to_string(),
|
||||||
"nushell".to_string(),
|
"nushell".to_string(),
|
||||||
"test_a\\".to_string(),
|
"test_a\\".to_string(),
|
||||||
"test_b\\".to_string(),
|
"test_b\\".to_string(),
|
||||||
@ -421,6 +563,7 @@ fn command_watch_with_filecompletion() {
|
|||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
"another/".to_string(),
|
"another/".to_string(),
|
||||||
"custom_completion.nu".to_string(),
|
"custom_completion.nu".to_string(),
|
||||||
|
"directory_completion/".to_string(),
|
||||||
"nushell".to_string(),
|
"nushell".to_string(),
|
||||||
"test_a/".to_string(),
|
"test_a/".to_string(),
|
||||||
"test_b/".to_string(),
|
"test_b/".to_string(),
|
||||||
@ -441,10 +584,26 @@ fn file_completion_quoted() {
|
|||||||
let suggestions = completer.complete(target_dir, target_dir.len());
|
let suggestions = completer.complete(target_dir, target_dir.len());
|
||||||
|
|
||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
|
"`--help`".to_string(),
|
||||||
|
"`-42`".to_string(),
|
||||||
|
"`-inf`".to_string(),
|
||||||
|
"`4.2`".to_string(),
|
||||||
"`te st.txt`".to_string(),
|
"`te st.txt`".to_string(),
|
||||||
"`te#st.txt`".to_string(),
|
"`te#st.txt`".to_string(),
|
||||||
"`te'st.txt`".to_string(),
|
"`te'st.txt`".to_string(),
|
||||||
"`te(st).txt`".to_string(),
|
"`te(st).txt`".to_string(),
|
||||||
|
format!("`{}`", folder("test dir".into())),
|
||||||
|
];
|
||||||
|
|
||||||
|
match_suggestions(expected_paths, suggestions);
|
||||||
|
|
||||||
|
let dir: PathBuf = "test dir".into();
|
||||||
|
let target_dir = format!("open '{}'", folder(dir.clone()));
|
||||||
|
let suggestions = completer.complete(&target_dir, target_dir.len());
|
||||||
|
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
format!("`{}`", file(dir.join("double quote"))),
|
||||||
|
format!("`{}`", file(dir.join("single quote"))),
|
||||||
];
|
];
|
||||||
|
|
||||||
match_suggestions(expected_paths, suggestions)
|
match_suggestions(expected_paths, suggestions)
|
||||||
@ -500,6 +659,7 @@ fn folder_with_directorycompletions() {
|
|||||||
// Create the expected values
|
// Create the expected values
|
||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
folder(dir.join("another")),
|
folder(dir.join("another")),
|
||||||
|
folder(dir.join("directory_completion")),
|
||||||
folder(dir.join("test_a")),
|
folder(dir.join("test_a")),
|
||||||
folder(dir.join("test_b")),
|
folder(dir.join("test_b")),
|
||||||
folder(dir.join(".hidden_folder")),
|
folder(dir.join(".hidden_folder")),
|
||||||
@ -693,7 +853,7 @@ fn run_external_completion(block: &str, input: &str) -> Vec<Suggestion> {
|
|||||||
// Change config adding the external completer
|
// Change config adding the external completer
|
||||||
let mut config = engine_state.get_config().clone();
|
let mut config = engine_state.get_config().clone();
|
||||||
config.external_completer = Some(latest_block_id);
|
config.external_completer = Some(latest_block_id);
|
||||||
engine_state.set_config(&config);
|
engine_state.set_config(config);
|
||||||
|
|
||||||
// Instantiate a new completer
|
// Instantiate a new completer
|
||||||
let mut completer = NuCompleter::new(std::sync::Arc::new(engine_state), stack);
|
let mut completer = NuCompleter::new(std::sync::Arc::new(engine_state), stack);
|
||||||
@ -714,6 +874,7 @@ fn unknown_command_completion() {
|
|||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
"another\\".to_string(),
|
"another\\".to_string(),
|
||||||
"custom_completion.nu".to_string(),
|
"custom_completion.nu".to_string(),
|
||||||
|
"directory_completion\\".to_string(),
|
||||||
"nushell".to_string(),
|
"nushell".to_string(),
|
||||||
"test_a\\".to_string(),
|
"test_a\\".to_string(),
|
||||||
"test_b\\".to_string(),
|
"test_b\\".to_string(),
|
||||||
@ -724,6 +885,7 @@ fn unknown_command_completion() {
|
|||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
"another/".to_string(),
|
"another/".to_string(),
|
||||||
"custom_completion.nu".to_string(),
|
"custom_completion.nu".to_string(),
|
||||||
|
"directory_completion/".to_string(),
|
||||||
"nushell".to_string(),
|
"nushell".to_string(),
|
||||||
"test_a/".to_string(),
|
"test_a/".to_string(),
|
||||||
"test_b/".to_string(),
|
"test_b/".to_string(),
|
||||||
@ -774,6 +936,7 @@ fn filecompletions_triggers_after_cursor() {
|
|||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
"another\\".to_string(),
|
"another\\".to_string(),
|
||||||
"custom_completion.nu".to_string(),
|
"custom_completion.nu".to_string(),
|
||||||
|
"directory_completion\\".to_string(),
|
||||||
"nushell".to_string(),
|
"nushell".to_string(),
|
||||||
"test_a\\".to_string(),
|
"test_a\\".to_string(),
|
||||||
"test_b\\".to_string(),
|
"test_b\\".to_string(),
|
||||||
@ -784,6 +947,7 @@ fn filecompletions_triggers_after_cursor() {
|
|||||||
let expected_paths: Vec<String> = vec![
|
let expected_paths: Vec<String> = vec![
|
||||||
"another/".to_string(),
|
"another/".to_string(),
|
||||||
"custom_completion.nu".to_string(),
|
"custom_completion.nu".to_string(),
|
||||||
|
"directory_completion/".to_string(),
|
||||||
"nushell".to_string(),
|
"nushell".to_string(),
|
||||||
"test_a/".to_string(),
|
"test_a/".to_string(),
|
||||||
"test_b/".to_string(),
|
"test_b/".to_string(),
|
||||||
@ -836,6 +1000,34 @@ fn extern_complete_flags(mut extern_completer: NuCompleter) {
|
|||||||
match_suggestions(expected, suggestions);
|
match_suggestions(expected, suggestions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn custom_completer_triggers_cursor_before_word(mut custom_completer: NuCompleter) {
|
||||||
|
let suggestions = custom_completer.complete("cmd foo bar", 8);
|
||||||
|
let expected: Vec<String> = vec!["cmd".into(), "foo".into(), "".into()];
|
||||||
|
match_suggestions(expected, suggestions);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn custom_completer_triggers_cursor_on_word_left_boundary(mut custom_completer: NuCompleter) {
|
||||||
|
let suggestions = custom_completer.complete("cmd foo bar", 8);
|
||||||
|
let expected: Vec<String> = vec!["cmd".into(), "foo".into(), "".into()];
|
||||||
|
match_suggestions(expected, suggestions);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn custom_completer_triggers_cursor_next_to_word(mut custom_completer: NuCompleter) {
|
||||||
|
let suggestions = custom_completer.complete("cmd foo bar", 11);
|
||||||
|
let expected: Vec<String> = vec!["cmd".into(), "foo".into(), "bar".into()];
|
||||||
|
match_suggestions(expected, suggestions);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn custom_completer_triggers_cursor_after_word(mut custom_completer: NuCompleter) {
|
||||||
|
let suggestions = custom_completer.complete("cmd foo bar ", 12);
|
||||||
|
let expected: Vec<String> = vec!["cmd".into(), "foo".into(), "bar".into(), "".into()];
|
||||||
|
match_suggestions(expected, suggestions);
|
||||||
|
}
|
||||||
|
|
||||||
#[ignore = "was reverted, still needs fixing"]
|
#[ignore = "was reverted, still needs fixing"]
|
||||||
#[rstest]
|
#[rstest]
|
||||||
fn alias_offset_bug_7648() {
|
fn alias_offset_bug_7648() {
|
||||||
|
@ -4,7 +4,8 @@ use nu_engine::eval_block;
|
|||||||
use nu_parser::parse;
|
use nu_parser::parse;
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
engine::{EngineState, Stack, StateWorkingSet},
|
engine::{EngineState, Stack, StateWorkingSet},
|
||||||
PipelineData, ShellError, Span, Value,
|
eval_const::create_nu_constant,
|
||||||
|
PipelineData, ShellError, Span, Value, NU_VARIABLE_ID,
|
||||||
};
|
};
|
||||||
use nu_test_support::fs;
|
use nu_test_support::fs;
|
||||||
use reedline::Suggestion;
|
use reedline::Suggestion;
|
||||||
@ -28,39 +29,41 @@ pub fn new_engine() -> (PathBuf, String, EngineState, Stack) {
|
|||||||
// Create a new engine with default context
|
// Create a new engine with default context
|
||||||
let mut engine_state = create_default_context();
|
let mut engine_state = create_default_context();
|
||||||
|
|
||||||
|
// Add $nu
|
||||||
|
let nu_const =
|
||||||
|
create_nu_constant(&engine_state, Span::test_data()).expect("Failed creating $nu");
|
||||||
|
engine_state.set_variable_const_val(NU_VARIABLE_ID, nu_const);
|
||||||
|
|
||||||
// New stack
|
// New stack
|
||||||
let mut stack = Stack::new();
|
let mut stack = Stack::new();
|
||||||
|
|
||||||
// Add pwd as env var
|
// Add pwd as env var
|
||||||
stack.add_env_var(
|
stack.add_env_var(
|
||||||
"PWD".to_string(),
|
"PWD".to_string(),
|
||||||
Value::String {
|
Value::string(dir_str.clone(), nu_protocol::Span::new(0, dir_str.len())),
|
||||||
val: dir_str.clone(),
|
|
||||||
span: nu_protocol::Span::new(0, dir_str.len()),
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
stack.add_env_var(
|
stack.add_env_var(
|
||||||
"TEST".to_string(),
|
"TEST".to_string(),
|
||||||
Value::String {
|
Value::string(
|
||||||
val: "NUSHELL".to_string(),
|
"NUSHELL".to_string(),
|
||||||
span: nu_protocol::Span::new(0, dir_str.len()),
|
nu_protocol::Span::new(0, dir_str.len()),
|
||||||
},
|
),
|
||||||
);
|
);
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
stack.add_env_var(
|
stack.add_env_var(
|
||||||
"Path".to_string(),
|
"Path".to_string(),
|
||||||
Value::String {
|
Value::string(
|
||||||
val: "c:\\some\\path;c:\\some\\other\\path".to_string(),
|
"c:\\some\\path;c:\\some\\other\\path".to_string(),
|
||||||
span: nu_protocol::Span::new(0, dir_str.len()),
|
nu_protocol::Span::new(0, dir_str.len()),
|
||||||
},
|
),
|
||||||
);
|
);
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
stack.add_env_var(
|
stack.add_env_var(
|
||||||
"PATH".to_string(),
|
"PATH".to_string(),
|
||||||
Value::String {
|
Value::string(
|
||||||
val: "/some/path:/some/other/path".to_string(),
|
"/some/path:/some/other/path".to_string(),
|
||||||
span: nu_protocol::Span::new(0, dir_str.len()),
|
nu_protocol::Span::new(0, dir_str.len()),
|
||||||
},
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Merge environment into the permanent state
|
// Merge environment into the permanent state
|
||||||
@ -89,17 +92,50 @@ pub fn new_quote_engine() -> (PathBuf, String, EngineState, Stack) {
|
|||||||
// Add pwd as env var
|
// Add pwd as env var
|
||||||
stack.add_env_var(
|
stack.add_env_var(
|
||||||
"PWD".to_string(),
|
"PWD".to_string(),
|
||||||
Value::String {
|
Value::string(dir_str.clone(), nu_protocol::Span::new(0, dir_str.len())),
|
||||||
val: dir_str.clone(),
|
|
||||||
span: nu_protocol::Span::new(0, dir_str.len()),
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
stack.add_env_var(
|
stack.add_env_var(
|
||||||
"TEST".to_string(),
|
"TEST".to_string(),
|
||||||
Value::String {
|
Value::string(
|
||||||
val: "NUSHELL".to_string(),
|
"NUSHELL".to_string(),
|
||||||
span: nu_protocol::Span::new(0, dir_str.len()),
|
nu_protocol::Span::new(0, dir_str.len()),
|
||||||
},
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Merge environment into the permanent state
|
||||||
|
let merge_result = engine_state.merge_env(&mut stack, &dir);
|
||||||
|
assert!(merge_result.is_ok());
|
||||||
|
|
||||||
|
(dir, dir_str, engine_state, stack)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_partial_engine() -> (PathBuf, String, EngineState, Stack) {
|
||||||
|
// Target folder inside assets
|
||||||
|
let dir = fs::fixtures().join("partial_completions");
|
||||||
|
let mut dir_str = dir
|
||||||
|
.clone()
|
||||||
|
.into_os_string()
|
||||||
|
.into_string()
|
||||||
|
.unwrap_or_default();
|
||||||
|
dir_str.push(SEP);
|
||||||
|
|
||||||
|
// Create a new engine with default context
|
||||||
|
let mut engine_state = create_default_context();
|
||||||
|
|
||||||
|
// New stack
|
||||||
|
let mut stack = Stack::new();
|
||||||
|
|
||||||
|
// Add pwd as env var
|
||||||
|
stack.add_env_var(
|
||||||
|
"PWD".to_string(),
|
||||||
|
Value::string(dir_str.clone(), nu_protocol::Span::new(0, dir_str.len())),
|
||||||
|
);
|
||||||
|
stack.add_env_var(
|
||||||
|
"TEST".to_string(),
|
||||||
|
Value::string(
|
||||||
|
"NUSHELL".to_string(),
|
||||||
|
nu_protocol::Span::new(0, dir_str.len()),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Merge environment into the permanent state
|
// Merge environment into the permanent state
|
||||||
@ -162,12 +198,7 @@ pub fn merge_input(
|
|||||||
engine_state,
|
engine_state,
|
||||||
stack,
|
stack,
|
||||||
&block,
|
&block,
|
||||||
PipelineData::Value(
|
PipelineData::Value(Value::nothing(Span::unknown(),), None),
|
||||||
Value::Nothing {
|
|
||||||
span: Span::unknown(),
|
|
||||||
},
|
|
||||||
None
|
|
||||||
),
|
|
||||||
false,
|
false,
|
||||||
false
|
false
|
||||||
)
|
)
|
||||||
|
@ -5,11 +5,21 @@ edition = "2021"
|
|||||||
license = "MIT"
|
license = "MIT"
|
||||||
name = "nu-cmd-base"
|
name = "nu-cmd-base"
|
||||||
repository = "https://github.com/nushell/nushell/tree/main/crates/nu-cmd-base"
|
repository = "https://github.com/nushell/nushell/tree/main/crates/nu-cmd-base"
|
||||||
version = "0.82.0"
|
version = "0.88.2"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
nu-engine = { path = "../nu-engine", version = "0.82.0" }
|
nu-engine = { path = "../nu-engine", version = "0.88.2" }
|
||||||
nu-path = { path = "../nu-path", version = "0.82.0" }
|
nu-glob = { path = "../nu-glob", version = "0.88.2" }
|
||||||
nu-protocol = { version = "0.82.0", path = "../nu-protocol" }
|
nu-parser = { path = "../nu-parser", version = "0.88.2" }
|
||||||
|
nu-path = { path = "../nu-path", version = "0.88.2" }
|
||||||
|
nu-protocol = { path = "../nu-protocol", version = "0.88.2" }
|
||||||
|
nu-utils = { path = "../nu-utils", version = "0.88.2" }
|
||||||
|
|
||||||
|
indexmap = "2.1"
|
||||||
|
miette = "5.10.0"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
nu-test-support = { path = "../nu-test-support", version = "0.88.2" }
|
||||||
|
rstest = "0.18.2"
|
||||||
|
21
crates/nu-cmd-base/LICENSE
Normal file
21
crates/nu-cmd-base/LICENSE
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2019 - 2023 The Nushell Project Developers
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
205
crates/nu-cmd-base/src/arg_glob.rs
Normal file
205
crates/nu-cmd-base/src/arg_glob.rs
Normal file
@ -0,0 +1,205 @@
|
|||||||
|
// utilities for expanding globs in command arguments
|
||||||
|
|
||||||
|
use nu_glob::{glob_with_parent, MatchOptions, Paths};
|
||||||
|
use nu_protocol::{ShellError, Spanned};
|
||||||
|
use std::fs;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
// standard glob options to use for filesystem command arguments
|
||||||
|
|
||||||
|
const GLOB_PARAMS: MatchOptions = MatchOptions {
|
||||||
|
case_sensitive: true,
|
||||||
|
require_literal_separator: false,
|
||||||
|
require_literal_leading_dot: false,
|
||||||
|
recursive_match_hidden_dir: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
// handle an argument that could be a literal path or a glob.
|
||||||
|
// if literal path, return just that (whether user can access it or not).
|
||||||
|
// if glob, expand into matching paths, using GLOB_PARAMS options.
|
||||||
|
pub fn arg_glob(
|
||||||
|
pattern: &Spanned<String>, // alleged path or glob
|
||||||
|
cwd: &Path, // current working directory
|
||||||
|
) -> Result<Paths, ShellError> {
|
||||||
|
arg_glob_opt(pattern, cwd, GLOB_PARAMS)
|
||||||
|
}
|
||||||
|
|
||||||
|
// variant of [arg_glob] that requires literal dot prefix in pattern to match dot-prefixed path.
|
||||||
|
pub fn arg_glob_leading_dot(pattern: &Spanned<String>, cwd: &Path) -> Result<Paths, ShellError> {
|
||||||
|
arg_glob_opt(
|
||||||
|
pattern,
|
||||||
|
cwd,
|
||||||
|
MatchOptions {
|
||||||
|
require_literal_leading_dot: true,
|
||||||
|
..GLOB_PARAMS
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn arg_glob_opt(
|
||||||
|
pattern: &Spanned<String>,
|
||||||
|
cwd: &Path,
|
||||||
|
options: MatchOptions,
|
||||||
|
) -> Result<Paths, ShellError> {
|
||||||
|
// remove ansi coloring (?)
|
||||||
|
let pattern = {
|
||||||
|
Spanned {
|
||||||
|
item: nu_utils::strip_ansi_string_unlikely(pattern.item.clone()),
|
||||||
|
span: pattern.span,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// if there's a file with same path as the pattern, just return that.
|
||||||
|
let pp = cwd.join(&pattern.item);
|
||||||
|
let md = fs::metadata(pp);
|
||||||
|
#[allow(clippy::single_match)]
|
||||||
|
match md {
|
||||||
|
Ok(_metadata) => {
|
||||||
|
return Ok(Paths::single(&PathBuf::from(pattern.item), cwd));
|
||||||
|
}
|
||||||
|
// file not found, but also "invalid chars in file" (e.g * on Windows). Fall through and glob
|
||||||
|
Err(_) => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
// user wasn't referring to a specific thing in filesystem, try to glob it.
|
||||||
|
match glob_with_parent(&pattern.item, options, cwd) {
|
||||||
|
Ok(p) => Ok(p),
|
||||||
|
Err(pat_err) => Err(ShellError::InvalidGlobPattern {
|
||||||
|
msg: pat_err.msg.into(),
|
||||||
|
span: pattern.span,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
use nu_glob::GlobResult;
|
||||||
|
use nu_protocol::{Span, Spanned};
|
||||||
|
use nu_test_support::fs::Stub::EmptyFile;
|
||||||
|
use nu_test_support::playground::Playground;
|
||||||
|
use rstest::rstest;
|
||||||
|
|
||||||
|
fn spanned_string(str: &str) -> Spanned<String> {
|
||||||
|
Spanned {
|
||||||
|
item: str.to_string(),
|
||||||
|
span: Span::test_data(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn does_something() {
|
||||||
|
let act = arg_glob(&spanned_string("*"), &PathBuf::from("."));
|
||||||
|
assert!(act.is_ok());
|
||||||
|
for f in act.expect("checked ok") {
|
||||||
|
match f {
|
||||||
|
Ok(p) => {
|
||||||
|
assert!(!p.to_str().unwrap().is_empty());
|
||||||
|
}
|
||||||
|
Err(e) => panic!("unexpected error {:?}", e),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn glob_format_error() {
|
||||||
|
let act = arg_glob(&spanned_string(r#"ab]c[def"#), &PathBuf::from("."));
|
||||||
|
assert!(act.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[case("*", 4, "no dirs")]
|
||||||
|
#[case("**/*", 7, "incl dirs")]
|
||||||
|
fn glob_subdirs(#[case] pat: &str, #[case] exp_count: usize, #[case] case: &str) {
|
||||||
|
Playground::setup("glob_subdirs", |dirs, sandbox| {
|
||||||
|
sandbox.with_files(vec![
|
||||||
|
EmptyFile("yehuda.txt"),
|
||||||
|
EmptyFile("jttxt"),
|
||||||
|
EmptyFile("andres.txt"),
|
||||||
|
]);
|
||||||
|
sandbox.mkdir(".children");
|
||||||
|
sandbox.within(".children").with_files(vec![
|
||||||
|
EmptyFile("timothy.txt"),
|
||||||
|
EmptyFile("tiffany.txt"),
|
||||||
|
EmptyFile("trish.txt"),
|
||||||
|
]);
|
||||||
|
|
||||||
|
let p: Vec<GlobResult> = arg_glob(&spanned_string(pat), &dirs.test)
|
||||||
|
.expect("no error")
|
||||||
|
.collect();
|
||||||
|
assert_eq!(
|
||||||
|
exp_count,
|
||||||
|
p.iter().filter(|i| i.is_ok()).count(),
|
||||||
|
" case: {case} ",
|
||||||
|
);
|
||||||
|
|
||||||
|
// expected behavior -- that directories are included in results (if name matches pattern)
|
||||||
|
let t = p
|
||||||
|
.iter()
|
||||||
|
.any(|i| i.as_ref().unwrap().to_string_lossy().contains(".children"));
|
||||||
|
assert!(t, "check for dir, case {case}");
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[case("yehuda.txt", true, 1, "matches literal path")]
|
||||||
|
#[case("*", false, 3, "matches glob")]
|
||||||
|
#[case(r#"bad[glob.foo"#, true, 1, "matches literal, would be bad glob pat")]
|
||||||
|
fn exact_vs_glob(
|
||||||
|
#[case] pat: &str,
|
||||||
|
#[case] exp_matches_input: bool,
|
||||||
|
#[case] exp_count: usize,
|
||||||
|
#[case] case: &str,
|
||||||
|
) {
|
||||||
|
Playground::setup("exact_vs_glob", |dirs, sandbox| {
|
||||||
|
sandbox.with_files(vec![
|
||||||
|
EmptyFile("yehuda.txt"),
|
||||||
|
EmptyFile("jttxt"),
|
||||||
|
EmptyFile("bad[glob.foo"),
|
||||||
|
]);
|
||||||
|
|
||||||
|
let res = arg_glob(&spanned_string(pat), &dirs.test)
|
||||||
|
.expect("no error")
|
||||||
|
.collect::<Vec<GlobResult>>();
|
||||||
|
|
||||||
|
eprintln!("res: {:?}", res);
|
||||||
|
if exp_matches_input {
|
||||||
|
assert_eq!(
|
||||||
|
exp_count,
|
||||||
|
res.len(),
|
||||||
|
" case {case}: matches input, but count not 1? "
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
&res[0].as_ref().unwrap().to_string_lossy(),
|
||||||
|
pat, // todo: is it OK for glob to return relative paths (not to current cwd, but to arg cwd of arg_glob)?
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
assert_eq!(exp_count, res.len(), " case: {}: matched glob", case);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[case(r#"realbad[glob.foo"#, true, 1, "error, bad glob")]
|
||||||
|
fn exact_vs_bad_glob(
|
||||||
|
// if path doesn't exist but pattern is not valid glob, should get error.
|
||||||
|
#[case] pat: &str,
|
||||||
|
#[case] _exp_matches_input: bool,
|
||||||
|
#[case] _exp_count: usize,
|
||||||
|
#[case] _tag: &str,
|
||||||
|
) {
|
||||||
|
Playground::setup("exact_vs_bad_glob", |dirs, sandbox| {
|
||||||
|
sandbox.with_files(vec![
|
||||||
|
EmptyFile("yehuda.txt"),
|
||||||
|
EmptyFile("jttxt"),
|
||||||
|
EmptyFile("bad[glob.foo"),
|
||||||
|
]);
|
||||||
|
|
||||||
|
let res = arg_glob(&spanned_string(pat), &dirs.test);
|
||||||
|
assert!(res
|
||||||
|
.expect_err("expected error")
|
||||||
|
.to_string()
|
||||||
|
.contains("Invalid glob pattern"));
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
1
crates/nu-cmd-base/src/formats/mod.rs
Normal file
1
crates/nu-cmd-base/src/formats/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod to;
|
20
crates/nu-cmd-base/src/formats/to/delimited.rs
Normal file
20
crates/nu-cmd-base/src/formats/to/delimited.rs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
use indexmap::{indexset, IndexSet};
|
||||||
|
use nu_protocol::Value;
|
||||||
|
|
||||||
|
pub fn merge_descriptors(values: &[Value]) -> Vec<String> {
|
||||||
|
let mut ret: Vec<String> = vec![];
|
||||||
|
let mut seen: IndexSet<String> = indexset! {};
|
||||||
|
for value in values {
|
||||||
|
let data_descriptors = match value {
|
||||||
|
Value::Record { val, .. } => val.columns().cloned().collect(),
|
||||||
|
_ => vec!["".to_string()],
|
||||||
|
};
|
||||||
|
for desc in data_descriptors {
|
||||||
|
if !desc.is_empty() && !seen.contains(&desc) {
|
||||||
|
seen.insert(desc.to_string());
|
||||||
|
ret.push(desc.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ret
|
||||||
|
}
|
1
crates/nu-cmd-base/src/formats/to/mod.rs
Normal file
1
crates/nu-cmd-base/src/formats/to/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod delimited;
|
362
crates/nu-cmd-base/src/hook.rs
Normal file
362
crates/nu-cmd-base/src/hook.rs
Normal file
@ -0,0 +1,362 @@
|
|||||||
|
use crate::util::get_guaranteed_cwd;
|
||||||
|
use miette::Result;
|
||||||
|
use nu_engine::{eval_block, eval_block_with_early_return};
|
||||||
|
use nu_parser::parse;
|
||||||
|
use nu_protocol::cli_error::{report_error, report_error_new};
|
||||||
|
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
|
||||||
|
use nu_protocol::{BlockId, PipelineData, PositionalArg, ShellError, Span, Type, Value, VarId};
|
||||||
|
|
||||||
|
pub fn eval_env_change_hook(
|
||||||
|
env_change_hook: Option<Value>,
|
||||||
|
engine_state: &mut EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
) -> Result<(), ShellError> {
|
||||||
|
if let Some(hook) = env_change_hook {
|
||||||
|
match hook {
|
||||||
|
Value::Record { val, .. } => {
|
||||||
|
for (env_name, hook_value) in &val {
|
||||||
|
let before = engine_state
|
||||||
|
.previous_env_vars
|
||||||
|
.get(env_name)
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
let after = stack
|
||||||
|
.get_env_var(engine_state, env_name)
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
if before != after {
|
||||||
|
eval_hook(
|
||||||
|
engine_state,
|
||||||
|
stack,
|
||||||
|
None,
|
||||||
|
vec![("$before".into(), before), ("$after".into(), after.clone())],
|
||||||
|
hook_value,
|
||||||
|
"env_change",
|
||||||
|
)?;
|
||||||
|
|
||||||
|
engine_state
|
||||||
|
.previous_env_vars
|
||||||
|
.insert(env_name.to_string(), after);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
x => {
|
||||||
|
return Err(ShellError::TypeMismatch {
|
||||||
|
err_message: "record for the 'env_change' hook".to_string(),
|
||||||
|
span: x.span(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn eval_hook(
|
||||||
|
engine_state: &mut EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
input: Option<PipelineData>,
|
||||||
|
arguments: Vec<(String, Value)>,
|
||||||
|
value: &Value,
|
||||||
|
hook_name: &str,
|
||||||
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
let mut output = PipelineData::empty();
|
||||||
|
|
||||||
|
let span = value.span();
|
||||||
|
match value {
|
||||||
|
Value::String { val, .. } => {
|
||||||
|
let (block, delta, vars) = {
|
||||||
|
let mut working_set = StateWorkingSet::new(engine_state);
|
||||||
|
|
||||||
|
let mut vars: Vec<(VarId, Value)> = vec![];
|
||||||
|
|
||||||
|
for (name, val) in arguments {
|
||||||
|
let var_id = working_set.add_variable(
|
||||||
|
name.as_bytes().to_vec(),
|
||||||
|
val.span(),
|
||||||
|
Type::Any,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
vars.push((var_id, val));
|
||||||
|
}
|
||||||
|
|
||||||
|
let output = parse(
|
||||||
|
&mut working_set,
|
||||||
|
Some(&format!("{hook_name} hook")),
|
||||||
|
val.as_bytes(),
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
if let Some(err) = working_set.parse_errors.first() {
|
||||||
|
report_error(&working_set, err);
|
||||||
|
|
||||||
|
return Err(ShellError::UnsupportedConfigValue {
|
||||||
|
expected: "valid source code".into(),
|
||||||
|
value: "source code with syntax errors".into(),
|
||||||
|
span,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
(output, working_set.render(), vars)
|
||||||
|
};
|
||||||
|
|
||||||
|
engine_state.merge_delta(delta)?;
|
||||||
|
let input = if let Some(input) = input {
|
||||||
|
input
|
||||||
|
} else {
|
||||||
|
PipelineData::empty()
|
||||||
|
};
|
||||||
|
|
||||||
|
let var_ids: Vec<VarId> = vars
|
||||||
|
.into_iter()
|
||||||
|
.map(|(var_id, val)| {
|
||||||
|
stack.add_var(var_id, val);
|
||||||
|
var_id
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
match eval_block(engine_state, stack, &block, input, false, false) {
|
||||||
|
Ok(pipeline_data) => {
|
||||||
|
output = pipeline_data;
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
report_error_new(engine_state, &err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for var_id in var_ids.iter() {
|
||||||
|
stack.remove_var(*var_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Value::List { vals, .. } => {
|
||||||
|
for val in vals {
|
||||||
|
eval_hook(
|
||||||
|
engine_state,
|
||||||
|
stack,
|
||||||
|
None,
|
||||||
|
arguments.clone(),
|
||||||
|
val,
|
||||||
|
&format!("{hook_name} list, recursive"),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Value::Record { val, .. } => {
|
||||||
|
// Hooks can optionally be a record in this form:
|
||||||
|
// {
|
||||||
|
// condition: {|before, after| ... } # block that evaluates to true/false
|
||||||
|
// code: # block or a string
|
||||||
|
// }
|
||||||
|
// The condition block will be run to check whether the main hook (in `code`) should be run.
|
||||||
|
// If it returns true (the default if a condition block is not specified), the hook should be run.
|
||||||
|
let do_run_hook = if let Some(condition) = val.get("condition") {
|
||||||
|
let other_span = condition.span();
|
||||||
|
if let Ok(block_id) = condition.as_block() {
|
||||||
|
match run_hook_block(
|
||||||
|
engine_state,
|
||||||
|
stack,
|
||||||
|
block_id,
|
||||||
|
None,
|
||||||
|
arguments.clone(),
|
||||||
|
other_span,
|
||||||
|
) {
|
||||||
|
Ok(pipeline_data) => {
|
||||||
|
if let PipelineData::Value(Value::Bool { val, .. }, ..) = pipeline_data
|
||||||
|
{
|
||||||
|
val
|
||||||
|
} else {
|
||||||
|
return Err(ShellError::UnsupportedConfigValue {
|
||||||
|
expected: "boolean output".to_string(),
|
||||||
|
value: "other PipelineData variant".to_string(),
|
||||||
|
span: other_span,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
return Err(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err(ShellError::UnsupportedConfigValue {
|
||||||
|
expected: "block".to_string(),
|
||||||
|
value: format!("{}", condition.get_type()),
|
||||||
|
span: other_span,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// always run the hook
|
||||||
|
true
|
||||||
|
};
|
||||||
|
|
||||||
|
if do_run_hook {
|
||||||
|
let Some(follow) = val.get("code") else {
|
||||||
|
return Err(ShellError::CantFindColumn {
|
||||||
|
col_name: "code".into(),
|
||||||
|
span,
|
||||||
|
src_span: span,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
let source_span = follow.span();
|
||||||
|
match follow {
|
||||||
|
Value::String { val, .. } => {
|
||||||
|
let (block, delta, vars) = {
|
||||||
|
let mut working_set = StateWorkingSet::new(engine_state);
|
||||||
|
|
||||||
|
let mut vars: Vec<(VarId, Value)> = vec![];
|
||||||
|
|
||||||
|
for (name, val) in arguments {
|
||||||
|
let var_id = working_set.add_variable(
|
||||||
|
name.as_bytes().to_vec(),
|
||||||
|
val.span(),
|
||||||
|
Type::Any,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
vars.push((var_id, val));
|
||||||
|
}
|
||||||
|
|
||||||
|
let output = parse(
|
||||||
|
&mut working_set,
|
||||||
|
Some(&format!("{hook_name} hook")),
|
||||||
|
val.as_bytes(),
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
if let Some(err) = working_set.parse_errors.first() {
|
||||||
|
report_error(&working_set, err);
|
||||||
|
|
||||||
|
return Err(ShellError::UnsupportedConfigValue {
|
||||||
|
expected: "valid source code".into(),
|
||||||
|
value: "source code with syntax errors".into(),
|
||||||
|
span: source_span,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
(output, working_set.render(), vars)
|
||||||
|
};
|
||||||
|
|
||||||
|
engine_state.merge_delta(delta)?;
|
||||||
|
let input = PipelineData::empty();
|
||||||
|
|
||||||
|
let var_ids: Vec<VarId> = vars
|
||||||
|
.into_iter()
|
||||||
|
.map(|(var_id, val)| {
|
||||||
|
stack.add_var(var_id, val);
|
||||||
|
var_id
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
match eval_block(engine_state, stack, &block, input, false, false) {
|
||||||
|
Ok(pipeline_data) => {
|
||||||
|
output = pipeline_data;
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
report_error_new(engine_state, &err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for var_id in var_ids.iter() {
|
||||||
|
stack.remove_var(*var_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Value::Block { val: block_id, .. } => {
|
||||||
|
run_hook_block(
|
||||||
|
engine_state,
|
||||||
|
stack,
|
||||||
|
*block_id,
|
||||||
|
input,
|
||||||
|
arguments,
|
||||||
|
source_span,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
Value::Closure { val, .. } => {
|
||||||
|
run_hook_block(
|
||||||
|
engine_state,
|
||||||
|
stack,
|
||||||
|
val.block_id,
|
||||||
|
input,
|
||||||
|
arguments,
|
||||||
|
source_span,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
other => {
|
||||||
|
return Err(ShellError::UnsupportedConfigValue {
|
||||||
|
expected: "block or string".to_string(),
|
||||||
|
value: format!("{}", other.get_type()),
|
||||||
|
span: source_span,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Value::Block { val: block_id, .. } => {
|
||||||
|
output = run_hook_block(engine_state, stack, *block_id, input, arguments, span)?;
|
||||||
|
}
|
||||||
|
Value::Closure { val, .. } => {
|
||||||
|
output = run_hook_block(engine_state, stack, val.block_id, input, arguments, span)?;
|
||||||
|
}
|
||||||
|
other => {
|
||||||
|
return Err(ShellError::UnsupportedConfigValue {
|
||||||
|
expected: "string, block, record, or list of commands".into(),
|
||||||
|
value: format!("{}", other.get_type()),
|
||||||
|
span: other.span(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let cwd = get_guaranteed_cwd(engine_state, stack);
|
||||||
|
engine_state.merge_env(stack, cwd)?;
|
||||||
|
|
||||||
|
Ok(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_hook_block(
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
block_id: BlockId,
|
||||||
|
optional_input: Option<PipelineData>,
|
||||||
|
arguments: Vec<(String, Value)>,
|
||||||
|
span: Span,
|
||||||
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
let block = engine_state.get_block(block_id);
|
||||||
|
|
||||||
|
let input = optional_input.unwrap_or_else(PipelineData::empty);
|
||||||
|
|
||||||
|
let mut callee_stack = stack.gather_captures(engine_state, &block.captures);
|
||||||
|
|
||||||
|
for (idx, PositionalArg { var_id, .. }) in
|
||||||
|
block.signature.required_positional.iter().enumerate()
|
||||||
|
{
|
||||||
|
if let Some(var_id) = var_id {
|
||||||
|
if let Some(arg) = arguments.get(idx) {
|
||||||
|
callee_stack.add_var(*var_id, arg.1.clone())
|
||||||
|
} else {
|
||||||
|
return Err(ShellError::IncompatibleParametersSingle {
|
||||||
|
msg: "This hook block has too many parameters".into(),
|
||||||
|
span,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let pipeline_data =
|
||||||
|
eval_block_with_early_return(engine_state, &mut callee_stack, block, input, false, false)?;
|
||||||
|
|
||||||
|
if let PipelineData::Value(Value::Error { error, .. }, _) = pipeline_data {
|
||||||
|
return Err(*error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If all went fine, preserve the environment of the called block
|
||||||
|
let caller_env_vars = stack.get_env_var_names(engine_state);
|
||||||
|
|
||||||
|
// remove env vars that are present in the caller but not in the callee
|
||||||
|
// (the callee hid them)
|
||||||
|
for var in caller_env_vars.iter() {
|
||||||
|
if !callee_stack.has_env_var(engine_state, var) {
|
||||||
|
stack.remove_env_var(engine_state, var);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// add new env vars from callee to caller
|
||||||
|
for (var, value) in callee_stack.get_stack_env_vars() {
|
||||||
|
stack.add_env_var(var, value);
|
||||||
|
}
|
||||||
|
Ok(pipeline_data)
|
||||||
|
}
|
@ -76,9 +76,7 @@ where
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
if let Err(error) = r {
|
if let Err(error) = r {
|
||||||
return Value::Error {
|
return Value::error(error, span);
|
||||||
error: Box::new(error),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
v
|
v
|
||||||
|
@ -1,2 +1,7 @@
|
|||||||
|
mod arg_glob;
|
||||||
|
pub mod formats;
|
||||||
|
pub mod hook;
|
||||||
pub mod input_handler;
|
pub mod input_handler;
|
||||||
pub mod util;
|
pub mod util;
|
||||||
|
pub use arg_glob::arg_glob;
|
||||||
|
pub use arg_glob::arg_glob_leading_dot;
|
||||||
|
@ -55,3 +55,73 @@ pub fn process_range(range: &Range) -> Result<(isize, isize), MakeRangeError> {
|
|||||||
|
|
||||||
Ok((start, end))
|
Ok((start, end))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const HELP_MSG: &str = "Nushell's config file can be found with the command: $nu.config-path. \
|
||||||
|
For more help: (https://nushell.sh/book/configuration.html#configurations-with-built-in-commands)";
|
||||||
|
|
||||||
|
fn get_editor_commandline(
|
||||||
|
value: &Value,
|
||||||
|
var_name: &str,
|
||||||
|
) -> Result<(String, Vec<String>), ShellError> {
|
||||||
|
match value {
|
||||||
|
Value::String { val, .. } if !val.is_empty() => Ok((val.to_string(), Vec::new())),
|
||||||
|
Value::List { vals, .. } if !vals.is_empty() => {
|
||||||
|
let mut editor_cmd = vals.iter().map(|l| l.as_string());
|
||||||
|
match editor_cmd.next().transpose()? {
|
||||||
|
Some(editor) if !editor.is_empty() => {
|
||||||
|
let params = editor_cmd.collect::<Result<_, ShellError>>()?;
|
||||||
|
Ok((editor, params))
|
||||||
|
}
|
||||||
|
_ => Err(ShellError::GenericError {
|
||||||
|
error: "Editor executable is missing".into(),
|
||||||
|
msg: "Set the first element to an executable".into(),
|
||||||
|
span: Some(value.span()),
|
||||||
|
help: Some(HELP_MSG.into()),
|
||||||
|
inner: vec![],
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Value::String { .. } | Value::List { .. } => Err(ShellError::GenericError {
|
||||||
|
error: format!("{var_name} should be a non-empty string or list<String>"),
|
||||||
|
msg: "Specify an executable here".into(),
|
||||||
|
span: Some(value.span()),
|
||||||
|
help: Some(HELP_MSG.into()),
|
||||||
|
inner: vec![],
|
||||||
|
}),
|
||||||
|
x => Err(ShellError::CantConvert {
|
||||||
|
to_type: "string or list<string>".into(),
|
||||||
|
from_type: x.get_type().to_string(),
|
||||||
|
span: value.span(),
|
||||||
|
help: None,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_editor(
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
span: Span,
|
||||||
|
) -> Result<(String, Vec<String>), ShellError> {
|
||||||
|
let config = engine_state.get_config();
|
||||||
|
let env_vars = stack.get_env_vars(engine_state);
|
||||||
|
|
||||||
|
if let Ok(buff_editor) =
|
||||||
|
get_editor_commandline(&config.buffer_editor, "$env.config.buffer_editor")
|
||||||
|
{
|
||||||
|
Ok(buff_editor)
|
||||||
|
} else if let Some(value) = env_vars.get("EDITOR") {
|
||||||
|
get_editor_commandline(value, "$env.EDITOR")
|
||||||
|
} else if let Some(value) = env_vars.get("VISUAL") {
|
||||||
|
get_editor_commandline(value, "$env.VISUAL")
|
||||||
|
} else {
|
||||||
|
Err(ShellError::GenericError {
|
||||||
|
error: "No editor configured".into(),
|
||||||
|
msg:
|
||||||
|
"Please specify one via `$env.config.buffer_editor` or `$env.EDITOR`/`$env.VISUAL`"
|
||||||
|
.into(),
|
||||||
|
span: Some(span),
|
||||||
|
help: Some(HELP_MSG.into()),
|
||||||
|
inner: vec![],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -5,7 +5,7 @@ edition = "2021"
|
|||||||
license = "MIT"
|
license = "MIT"
|
||||||
name = "nu-cmd-dataframe"
|
name = "nu-cmd-dataframe"
|
||||||
repository = "https://github.com/nushell/nushell/tree/main/crates/nu-cmd-dataframe"
|
repository = "https://github.com/nushell/nushell/tree/main/crates/nu-cmd-dataframe"
|
||||||
version = "0.82.0"
|
version = "0.88.2"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
@ -13,20 +13,22 @@ version = "0.82.0"
|
|||||||
bench = false
|
bench = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
nu-engine = { path = "../nu-engine", version = "0.82.0" }
|
nu-engine = { path = "../nu-engine", version = "0.88.2" }
|
||||||
nu-parser = { path = "../nu-parser", version = "0.82.0" }
|
nu-parser = { path = "../nu-parser", version = "0.88.2" }
|
||||||
nu-protocol = { path = "../nu-protocol", version = "0.82.0" }
|
nu-protocol = { path = "../nu-protocol", version = "0.88.2" }
|
||||||
|
|
||||||
# Potential dependencies for extras
|
# Potential dependencies for extras
|
||||||
chrono = { version = "0.4", features = [
|
chrono = { version = "0.4", features = ["std", "unstable-locales"], default-features = false }
|
||||||
"std",
|
chrono-tz = "0.8"
|
||||||
"unstable-locales",
|
fancy-regex = "0.12"
|
||||||
], default-features = false }
|
indexmap = { version = "2.1" }
|
||||||
fancy-regex = "0.11"
|
|
||||||
indexmap = { version = "1.7", features = ["serde-1"] }
|
|
||||||
num = { version = "0.4", optional = true }
|
num = { version = "0.4", optional = true }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
sqlparser = { version = "0.33", features = ["serde"], optional = true }
|
sqlparser = { version = "0.39", optional = true }
|
||||||
|
polars-io = { version = "0.35", features = ["avro"], optional = true }
|
||||||
|
polars-arrow = { version = "0.35", optional = true }
|
||||||
|
polars-ops = { version = "0.35", optional = true }
|
||||||
|
polars-plan = { version = "0.35", optional = true }
|
||||||
|
|
||||||
[dependencies.polars]
|
[dependencies.polars]
|
||||||
features = [
|
features = [
|
||||||
@ -40,7 +42,11 @@ features = [
|
|||||||
"dtype-categorical",
|
"dtype-categorical",
|
||||||
"dtype-datetime",
|
"dtype-datetime",
|
||||||
"dtype-struct",
|
"dtype-struct",
|
||||||
"dynamic_groupby",
|
"dtype-i8",
|
||||||
|
"dtype-i16",
|
||||||
|
"dtype-u8",
|
||||||
|
"dtype-u16",
|
||||||
|
"dynamic_group_by",
|
||||||
"ipc",
|
"ipc",
|
||||||
"is_in",
|
"is_in",
|
||||||
"json",
|
"json",
|
||||||
@ -56,12 +62,12 @@ features = [
|
|||||||
"to_dummies",
|
"to_dummies",
|
||||||
]
|
]
|
||||||
optional = true
|
optional = true
|
||||||
version = "0.30.0"
|
version = "0.35"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
dataframe = ["default"]
|
dataframe = ["num", "polars", "polars-io", "polars-arrow", "polars-ops", "polars-plan", "sqlparser"]
|
||||||
default = ["num", "polars", "sqlparser"]
|
default = []
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
nu-cmd-lang = { path = "../nu-cmd-lang", version = "0.82.0" }
|
nu-cmd-lang = { path = "../nu-cmd-lang", version = "0.88.2" }
|
||||||
nu-test-support = { path = "../nu-test-support", version = "0.82.0" }
|
nu-test-support = { path = "../nu-test-support", version = "0.88.2" }
|
||||||
|
@ -23,8 +23,10 @@ impl Command for AppendDF {
|
|||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.required("other", SyntaxShape::Any, "dataframe to be appended")
|
.required("other", SyntaxShape::Any, "dataframe to be appended")
|
||||||
.switch("col", "appends in col orientation", Some('c'))
|
.switch("col", "appends in col orientation", Some('c'))
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
Type::Custom("dataframe".into()),
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
)
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,8 +19,7 @@ impl Command for ColumnsDF {
|
|||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(Type::Custom("dataframe".into()), Type::Any)
|
||||||
.output_type(Type::Any)
|
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,10 +27,10 @@ impl Command for ColumnsDF {
|
|||||||
vec![Example {
|
vec![Example {
|
||||||
description: "Dataframe columns",
|
description: "Dataframe columns",
|
||||||
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr columns",
|
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr columns",
|
||||||
result: Some(Value::List {
|
result: Some(Value::list(
|
||||||
vals: vec![Value::test_string("a"), Value::test_string("b")],
|
vec![Value::test_string("a"), Value::test_string("b")],
|
||||||
span: Span::test_data(),
|
Span::test_data(),
|
||||||
}),
|
)),
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,10 +60,7 @@ fn command(
|
|||||||
.map(|v| Value::string(*v, call.head))
|
.map(|v| Value::string(*v, call.head))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let names = Value::List {
|
let names = Value::list(names, call.head);
|
||||||
vals: names,
|
|
||||||
span: call.head,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(PipelineData::Value(names, None))
|
Ok(PipelineData::Value(names, None))
|
||||||
}
|
}
|
||||||
|
@ -23,8 +23,10 @@ impl Command for DropDF {
|
|||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.rest("rest", SyntaxShape::Any, "column names to be dropped")
|
.rest("rest", SyntaxShape::Any, "column names to be dropped")
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
Type::Custom("dataframe".into()),
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
)
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,26 +68,24 @@ fn command(
|
|||||||
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
||||||
|
|
||||||
let new_df = col_string
|
let new_df = col_string
|
||||||
.get(0)
|
.first()
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Empty names list".into(),
|
||||||
"Empty names list".into(),
|
msg: "No column names were found".into(),
|
||||||
"No column names were found".into(),
|
span: Some(col_span),
|
||||||
Some(col_span),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.and_then(|col| {
|
.and_then(|col| {
|
||||||
df.as_ref().drop(&col.item).map_err(|e| {
|
df.as_ref()
|
||||||
ShellError::GenericError(
|
.drop(&col.item)
|
||||||
"Error dropping column".into(),
|
.map_err(|e| ShellError::GenericError {
|
||||||
e.to_string(),
|
error: "Error dropping column".into(),
|
||||||
Some(col.span),
|
msg: e.to_string(),
|
||||||
None,
|
span: Some(col.span),
|
||||||
Vec::new(),
|
help: None,
|
||||||
)
|
inner: vec![],
|
||||||
})
|
})
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
// If there are more columns in the drop selection list, these
|
// If there are more columns in the drop selection list, these
|
||||||
@ -94,15 +94,15 @@ fn command(
|
|||||||
.iter()
|
.iter()
|
||||||
.skip(1)
|
.skip(1)
|
||||||
.try_fold(new_df, |new_df, col| {
|
.try_fold(new_df, |new_df, col| {
|
||||||
new_df.drop(&col.item).map_err(|e| {
|
new_df
|
||||||
ShellError::GenericError(
|
.drop(&col.item)
|
||||||
"Error dropping column".into(),
|
.map_err(|e| ShellError::GenericError {
|
||||||
e.to_string(),
|
error: "Error dropping column".into(),
|
||||||
Some(col.span),
|
msg: e.to_string(),
|
||||||
None,
|
span: Some(col.span),
|
||||||
Vec::new(),
|
help: None,
|
||||||
)
|
inner: vec![],
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
|
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ impl Command for DropDuplicates {
|
|||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.optional(
|
.optional(
|
||||||
"subset",
|
"subset",
|
||||||
SyntaxShape::Table,
|
SyntaxShape::Table(vec![]),
|
||||||
"subset of columns to drop duplicates",
|
"subset of columns to drop duplicates",
|
||||||
)
|
)
|
||||||
.switch("maintain", "maintain order", Some('m'))
|
.switch("maintain", "maintain order", Some('m'))
|
||||||
@ -34,8 +34,10 @@ impl Command for DropDuplicates {
|
|||||||
"keeps last duplicate value (by default keeps first)",
|
"keeps last duplicate value (by default keeps first)",
|
||||||
Some('l'),
|
Some('l'),
|
||||||
)
|
)
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
Type::Custom("dataframe".into()),
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
)
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,14 +100,12 @@ fn command(
|
|||||||
|
|
||||||
df.as_ref()
|
df.as_ref()
|
||||||
.unique(subset_slice, keep_strategy, None)
|
.unique(subset_slice, keep_strategy, None)
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error dropping duplicates".into(),
|
||||||
"Error dropping duplicates".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(col_span),
|
||||||
Some(col_span),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
|
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
|
||||||
}
|
}
|
||||||
|
@ -24,11 +24,13 @@ impl Command for DropNulls {
|
|||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.optional(
|
.optional(
|
||||||
"subset",
|
"subset",
|
||||||
SyntaxShape::Table,
|
SyntaxShape::Table(vec![]),
|
||||||
"subset of columns to drop nulls",
|
"subset of columns to drop nulls",
|
||||||
)
|
)
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
Type::Custom("dataframe".into()),
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
)
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,14 +115,12 @@ fn command(
|
|||||||
|
|
||||||
df.as_ref()
|
df.as_ref()
|
||||||
.drop_nulls(subset_slice)
|
.drop_nulls(subset_slice)
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error dropping nulls".into(),
|
||||||
"Error dropping nulls".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(col_span),
|
||||||
Some(col_span),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
|
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
|
||||||
}
|
}
|
||||||
|
@ -19,8 +19,10 @@ impl Command for DataTypes {
|
|||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
Type::Custom("dataframe".into()),
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
)
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,10 +79,7 @@ fn command(
|
|||||||
.dtype();
|
.dtype();
|
||||||
|
|
||||||
let dtype_str = dtype.to_string();
|
let dtype_str = dtype.to_string();
|
||||||
dtypes.push(Value::String {
|
dtypes.push(Value::string(dtype_str, call.head));
|
||||||
val: dtype_str,
|
|
||||||
span: call.head,
|
|
||||||
});
|
|
||||||
|
|
||||||
Value::string(*v, call.head)
|
Value::string(*v, call.head)
|
||||||
})
|
})
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
use super::super::values::{Column, NuDataFrame};
|
use super::super::values::NuDataFrame;
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
ast::Call,
|
ast::Call,
|
||||||
engine::{Command, EngineState, Stack},
|
engine::{Command, EngineState, Stack},
|
||||||
Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
|
Category, Example, PipelineData, ShellError, Signature, Span, Type,
|
||||||
};
|
};
|
||||||
use polars::prelude::DataFrameOps;
|
use polars::{prelude::*, series::Series};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Dummies;
|
pub struct Dummies;
|
||||||
@ -20,8 +20,11 @@ impl Command for Dummies {
|
|||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.switch("drop-first", "Drop first row", Some('d'))
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
.input_output_type(
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
)
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,24 +34,15 @@ impl Command for Dummies {
|
|||||||
description: "Create new dataframe with dummy variables from a dataframe",
|
description: "Create new dataframe with dummy variables from a dataframe",
|
||||||
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr dummies",
|
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr dummies",
|
||||||
result: Some(
|
result: Some(
|
||||||
NuDataFrame::try_from_columns(vec![
|
NuDataFrame::try_from_series(
|
||||||
Column::new(
|
vec![
|
||||||
"a_1".to_string(),
|
Series::new("a_1", &[1_u8, 0]),
|
||||||
vec![Value::test_int(1), Value::test_int(0)],
|
Series::new("a_3", &[0_u8, 1]),
|
||||||
),
|
Series::new("b_2", &[1_u8, 0]),
|
||||||
Column::new(
|
Series::new("b_4", &[0_u8, 1]),
|
||||||
"a_3".to_string(),
|
],
|
||||||
vec![Value::test_int(0), Value::test_int(1)],
|
Span::test_data(),
|
||||||
),
|
)
|
||||||
Column::new(
|
|
||||||
"b_2".to_string(),
|
|
||||||
vec![Value::test_int(1), Value::test_int(0)],
|
|
||||||
),
|
|
||||||
Column::new(
|
|
||||||
"b_4".to_string(),
|
|
||||||
vec![Value::test_int(0), Value::test_int(1)],
|
|
||||||
),
|
|
||||||
])
|
|
||||||
.expect("simple df for test should not fail")
|
.expect("simple df for test should not fail")
|
||||||
.into_value(Span::test_data()),
|
.into_value(Span::test_data()),
|
||||||
),
|
),
|
||||||
@ -57,38 +51,14 @@ impl Command for Dummies {
|
|||||||
description: "Create new dataframe with dummy variables from a series",
|
description: "Create new dataframe with dummy variables from a series",
|
||||||
example: "[1 2 2 3 3] | dfr into-df | dfr dummies",
|
example: "[1 2 2 3 3] | dfr into-df | dfr dummies",
|
||||||
result: Some(
|
result: Some(
|
||||||
NuDataFrame::try_from_columns(vec![
|
NuDataFrame::try_from_series(
|
||||||
Column::new(
|
vec![
|
||||||
"0_1".to_string(),
|
Series::new("0_1", &[1_u8, 0, 0, 0, 0]),
|
||||||
vec![
|
Series::new("0_2", &[0_u8, 1, 1, 0, 0]),
|
||||||
Value::test_int(1),
|
Series::new("0_3", &[0_u8, 0, 0, 1, 1]),
|
||||||
Value::test_int(0),
|
],
|
||||||
Value::test_int(0),
|
Span::test_data(),
|
||||||
Value::test_int(0),
|
)
|
||||||
Value::test_int(0),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
Column::new(
|
|
||||||
"0_2".to_string(),
|
|
||||||
vec![
|
|
||||||
Value::test_int(0),
|
|
||||||
Value::test_int(1),
|
|
||||||
Value::test_int(1),
|
|
||||||
Value::test_int(0),
|
|
||||||
Value::test_int(0),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
Column::new(
|
|
||||||
"0_3".to_string(),
|
|
||||||
vec![
|
|
||||||
Value::test_int(0),
|
|
||||||
Value::test_int(0),
|
|
||||||
Value::test_int(0),
|
|
||||||
Value::test_int(1),
|
|
||||||
Value::test_int(1),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
])
|
|
||||||
.expect("simple df for test should not fail")
|
.expect("simple df for test should not fail")
|
||||||
.into_value(Span::test_data()),
|
.into_value(Span::test_data()),
|
||||||
),
|
),
|
||||||
@ -113,18 +83,17 @@ fn command(
|
|||||||
call: &Call,
|
call: &Call,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
let drop_first: bool = call.has_flag("drop-first");
|
||||||
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
||||||
|
|
||||||
df.as_ref()
|
df.as_ref()
|
||||||
.to_dummies(None)
|
.to_dummies(None, drop_first)
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error calculating dummies".into(),
|
||||||
"Error calculating dummies".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(call.head),
|
||||||
Some(call.head),
|
help: Some("The only allowed column types for dummies are String or Int".into()),
|
||||||
Some("The only allowed column types for dummies are String or Int".into()),
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
|
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
|
||||||
}
|
}
|
||||||
|
@ -29,8 +29,10 @@ impl Command for FilterWith {
|
|||||||
SyntaxShape::Any,
|
SyntaxShape::Any,
|
||||||
"boolean mask used to filter data",
|
"boolean mask used to filter data",
|
||||||
)
|
)
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
Type::Custom("dataframe".into()),
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
)
|
||||||
.category(Category::Custom("dataframe or lazyframe".into()))
|
.category(Category::Custom("dataframe or lazyframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,7 +92,7 @@ fn command_eager(
|
|||||||
df: NuDataFrame,
|
df: NuDataFrame,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let mask_value: Value = call.req(engine_state, stack, 0)?;
|
let mask_value: Value = call.req(engine_state, stack, 0)?;
|
||||||
let mask_span = mask_value.span()?;
|
let mask_span = mask_value.span();
|
||||||
|
|
||||||
if NuExpression::can_downcast(&mask_value) {
|
if NuExpression::can_downcast(&mask_value) {
|
||||||
let expression = NuExpression::try_from_value(mask_value)?;
|
let expression = NuExpression::try_from_value(mask_value)?;
|
||||||
@ -103,26 +105,22 @@ fn command_eager(
|
|||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
let mask = NuDataFrame::try_from_value(mask_value)?.as_series(mask_span)?;
|
let mask = NuDataFrame::try_from_value(mask_value)?.as_series(mask_span)?;
|
||||||
let mask = mask.bool().map_err(|e| {
|
let mask = mask.bool().map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error casting to bool".into(),
|
||||||
"Error casting to bool".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(mask_span),
|
||||||
Some(mask_span),
|
help: Some("Perhaps you want to use a series with booleans as mask".into()),
|
||||||
Some("Perhaps you want to use a series with booleans as mask".into()),
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
df.as_ref()
|
df.as_ref()
|
||||||
.filter(mask)
|
.filter(mask)
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error filtering dataframe".into(),
|
||||||
"Error filtering dataframe".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(call.head),
|
||||||
Some(call.head),
|
help: Some("The only allowed column types for dummies are String or Int".into()),
|
||||||
Some("The only allowed column types for dummies are String or Int".into()),
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
|
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::super::values::{Column, NuDataFrame};
|
use super::super::values::{Column, NuDataFrame, NuExpression};
|
||||||
use nu_engine::CallExt;
|
use nu_engine::CallExt;
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
ast::Call,
|
ast::Call,
|
||||||
@ -15,7 +15,7 @@ impl Command for FirstDF {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
"Show only the first number of rows."
|
"Show only the first number of rows or create a first expression"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
@ -25,8 +25,16 @@ impl Command for FirstDF {
|
|||||||
SyntaxShape::Int,
|
SyntaxShape::Int,
|
||||||
"starting from the front, the number of rows to return",
|
"starting from the front, the number of rows to return",
|
||||||
)
|
)
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_types(vec![
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
(
|
||||||
|
Type::Custom("expression".into()),
|
||||||
|
Type::Custom("expression".into()),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
),
|
||||||
|
])
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,6 +70,11 @@ impl Command for FirstDF {
|
|||||||
.into_value(Span::test_data()),
|
.into_value(Span::test_data()),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
Example {
|
||||||
|
description: "Creates a first expression from a column",
|
||||||
|
example: "dfr col a | dfr first",
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,8 +85,19 @@ impl Command for FirstDF {
|
|||||||
call: &Call,
|
call: &Call,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
let value = input.into_value(call.head);
|
||||||
command(engine_state, stack, call, df)
|
if NuDataFrame::can_downcast(&value) {
|
||||||
|
let df = NuDataFrame::try_from_value(value)?;
|
||||||
|
command(engine_state, stack, call, df)
|
||||||
|
} else {
|
||||||
|
let expr = NuExpression::try_from_value(value)?;
|
||||||
|
let expr: NuExpression = expr.into_polars().first().into();
|
||||||
|
|
||||||
|
Ok(PipelineData::Value(
|
||||||
|
NuExpression::into_value(expr, call.head),
|
||||||
|
None,
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,11 +119,25 @@ fn command(
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::super::super::test_dataframe::test_dataframe;
|
use super::super::super::test_dataframe::{build_test_engine_state, test_dataframe_example};
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::dataframe::lazy::aggregate::LazyAggregate;
|
||||||
|
use crate::dataframe::lazy::groupby::ToLazyGroupBy;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_examples() {
|
fn test_examples_dataframe() {
|
||||||
test_dataframe(vec![Box::new(FirstDF {})])
|
let mut engine_state = build_test_engine_state(vec![Box::new(FirstDF {})]);
|
||||||
|
test_dataframe_example(&mut engine_state, &FirstDF.examples()[0]);
|
||||||
|
test_dataframe_example(&mut engine_state, &FirstDF.examples()[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_examples_expression() {
|
||||||
|
let mut engine_state = build_test_engine_state(vec![
|
||||||
|
Box::new(FirstDF {}),
|
||||||
|
Box::new(LazyAggregate {}),
|
||||||
|
Box::new(ToLazyGroupBy {}),
|
||||||
|
]);
|
||||||
|
test_dataframe_example(&mut engine_state, &FirstDF.examples()[2]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,8 +24,10 @@ impl Command for GetDF {
|
|||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.rest("rest", SyntaxShape::Any, "column names to sort dataframe")
|
.rest("rest", SyntaxShape::Any, "column names to sort dataframe")
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
Type::Custom("dataframe".into()),
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
)
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,14 +70,12 @@ fn command(
|
|||||||
|
|
||||||
df.as_ref()
|
df.as_ref()
|
||||||
.select(col_string)
|
.select(col_string)
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error selecting columns".into(),
|
||||||
"Error selecting columns".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(col_span),
|
||||||
Some(col_span),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
|
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::super::values::{utils::DEFAULT_ROWS, Column, NuDataFrame};
|
use super::super::values::{utils::DEFAULT_ROWS, Column, NuDataFrame, NuExpression};
|
||||||
use nu_engine::CallExt;
|
use nu_engine::CallExt;
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
ast::Call,
|
ast::Call,
|
||||||
@ -21,24 +21,39 @@ impl Command for LastDF {
|
|||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.optional("rows", SyntaxShape::Int, "Number of rows for tail")
|
.optional("rows", SyntaxShape::Int, "Number of rows for tail")
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_types(vec![
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
(
|
||||||
|
Type::Custom("expression".into()),
|
||||||
|
Type::Custom("expression".into()),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
),
|
||||||
|
])
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![Example {
|
vec![
|
||||||
description: "Create new dataframe with last rows",
|
Example {
|
||||||
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr last 1",
|
description: "Create new dataframe with last rows",
|
||||||
result: Some(
|
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr last 1",
|
||||||
NuDataFrame::try_from_columns(vec![
|
result: Some(
|
||||||
Column::new("a".to_string(), vec![Value::test_int(3)]),
|
NuDataFrame::try_from_columns(vec![
|
||||||
Column::new("b".to_string(), vec![Value::test_int(4)]),
|
Column::new("a".to_string(), vec![Value::test_int(3)]),
|
||||||
])
|
Column::new("b".to_string(), vec![Value::test_int(4)]),
|
||||||
.expect("simple df for test should not fail")
|
])
|
||||||
.into_value(Span::test_data()),
|
.expect("simple df for test should not fail")
|
||||||
),
|
.into_value(Span::test_data()),
|
||||||
}]
|
),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Creates a last expression from a column",
|
||||||
|
example: "dfr col a | dfr last",
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
@ -48,8 +63,19 @@ impl Command for LastDF {
|
|||||||
call: &Call,
|
call: &Call,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
let value = input.into_value(call.head);
|
||||||
command(engine_state, stack, call, df)
|
if NuDataFrame::can_downcast(&value) {
|
||||||
|
let df = NuDataFrame::try_from_value(value)?;
|
||||||
|
command(engine_state, stack, call, df)
|
||||||
|
} else {
|
||||||
|
let expr = NuExpression::try_from_value(value)?;
|
||||||
|
let expr: NuExpression = expr.into_polars().last().into();
|
||||||
|
|
||||||
|
Ok(PipelineData::Value(
|
||||||
|
NuExpression::into_value(expr, call.head),
|
||||||
|
None,
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,11 +97,24 @@ fn command(
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::super::super::test_dataframe::test_dataframe;
|
use super::super::super::test_dataframe::{build_test_engine_state, test_dataframe_example};
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::dataframe::lazy::aggregate::LazyAggregate;
|
||||||
|
use crate::dataframe::lazy::groupby::ToLazyGroupBy;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_examples() {
|
fn test_examples_dataframe() {
|
||||||
test_dataframe(vec![Box::new(LastDF {})])
|
let mut engine_state = build_test_engine_state(vec![Box::new(LastDF {})]);
|
||||||
|
test_dataframe_example(&mut engine_state, &LastDF.examples()[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_examples_expression() {
|
||||||
|
let mut engine_state = build_test_engine_state(vec![
|
||||||
|
Box::new(LastDF {}),
|
||||||
|
Box::new(LazyAggregate {}),
|
||||||
|
Box::new(ToLazyGroupBy {}),
|
||||||
|
]);
|
||||||
|
test_dataframe_example(&mut engine_state, &LastDF.examples()[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
ast::Call,
|
ast::Call,
|
||||||
engine::{Command, EngineState, Stack},
|
engine::{Command, EngineState, Stack},
|
||||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Value,
|
record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Value,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::dataframe::values::NuDataFrame;
|
use crate::dataframe::values::NuDataFrame;
|
||||||
@ -55,34 +55,18 @@ impl Command for ListDF {
|
|||||||
NuDataFrame::try_from_value(value).ok().map(|df| (name, df))
|
NuDataFrame::try_from_value(value).ok().map(|df| (name, df))
|
||||||
})
|
})
|
||||||
.map(|(name, df)| {
|
.map(|(name, df)| {
|
||||||
let name = Value::String {
|
Value::record(
|
||||||
val: name,
|
record! {
|
||||||
span: call.head,
|
"name" => Value::string(name, call.head),
|
||||||
};
|
"columns" => Value::int(df.as_ref().width() as i64, call.head),
|
||||||
|
"rows" => Value::int(df.as_ref().height() as i64, call.head),
|
||||||
let columns = Value::int(df.as_ref().width() as i64, call.head);
|
},
|
||||||
|
call.head,
|
||||||
let rows = Value::int(df.as_ref().height() as i64, call.head);
|
)
|
||||||
|
|
||||||
let cols = vec![
|
|
||||||
"name".to_string(),
|
|
||||||
"columns".to_string(),
|
|
||||||
"rows".to_string(),
|
|
||||||
];
|
|
||||||
let vals = vec![name, columns, rows];
|
|
||||||
|
|
||||||
Value::Record {
|
|
||||||
cols,
|
|
||||||
vals,
|
|
||||||
span: call.head,
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.collect::<Vec<Value>>();
|
.collect::<Vec<Value>>();
|
||||||
|
|
||||||
let list = Value::List {
|
let list = Value::list(vals, call.head);
|
||||||
vals,
|
|
||||||
span: call.head,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(list.into_pipeline_data())
|
Ok(list.into_pipeline_data())
|
||||||
}
|
}
|
||||||
|
@ -26,13 +26,13 @@ impl Command for MeltDF {
|
|||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.required_named(
|
.required_named(
|
||||||
"columns",
|
"columns",
|
||||||
SyntaxShape::Table,
|
SyntaxShape::Table(vec![]),
|
||||||
"column names for melting",
|
"column names for melting",
|
||||||
Some('c'),
|
Some('c'),
|
||||||
)
|
)
|
||||||
.required_named(
|
.required_named(
|
||||||
"values",
|
"values",
|
||||||
SyntaxShape::Table,
|
SyntaxShape::Table(vec![]),
|
||||||
"column names used as value columns",
|
"column names used as value columns",
|
||||||
Some('v'),
|
Some('v'),
|
||||||
)
|
)
|
||||||
@ -48,8 +48,10 @@ impl Command for MeltDF {
|
|||||||
"optional name for value column",
|
"optional name for value column",
|
||||||
Some('l'),
|
Some('l'),
|
||||||
)
|
)
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
Type::Custom("dataframe".into()),
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
)
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,38 +152,34 @@ fn command(
|
|||||||
let mut res = df
|
let mut res = df
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.melt(&id_col_string, &val_col_string)
|
.melt(&id_col_string, &val_col_string)
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error calculating melt".into(),
|
||||||
"Error calculating melt".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(call.head),
|
||||||
Some(call.head),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
if let Some(name) = &variable_name {
|
if let Some(name) = &variable_name {
|
||||||
res.rename("variable", &name.item).map_err(|e| {
|
res.rename("variable", &name.item)
|
||||||
ShellError::GenericError(
|
.map_err(|e| ShellError::GenericError {
|
||||||
"Error renaming column".into(),
|
error: "Error renaming column".into(),
|
||||||
e.to_string(),
|
msg: e.to_string(),
|
||||||
Some(name.span),
|
span: Some(name.span),
|
||||||
None,
|
help: None,
|
||||||
Vec::new(),
|
inner: vec![],
|
||||||
)
|
})?;
|
||||||
})?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(name) = &value_name {
|
if let Some(name) = &value_name {
|
||||||
res.rename("value", &name.item).map_err(|e| {
|
res.rename("value", &name.item)
|
||||||
ShellError::GenericError(
|
.map_err(|e| ShellError::GenericError {
|
||||||
"Error renaming column".into(),
|
error: "Error renaming column".into(),
|
||||||
e.to_string(),
|
msg: e.to_string(),
|
||||||
Some(name.span),
|
span: Some(name.span),
|
||||||
None,
|
help: None,
|
||||||
Vec::new(),
|
inner: vec![],
|
||||||
)
|
})?;
|
||||||
})?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(PipelineData::Value(
|
Ok(PipelineData::Value(
|
||||||
@ -196,50 +194,50 @@ fn check_column_datatypes<T: AsRef<str>>(
|
|||||||
col_span: Span,
|
col_span: Span,
|
||||||
) -> Result<(), ShellError> {
|
) -> Result<(), ShellError> {
|
||||||
if cols.is_empty() {
|
if cols.is_empty() {
|
||||||
return Err(ShellError::GenericError(
|
return Err(ShellError::GenericError {
|
||||||
"Merge error".into(),
|
error: "Merge error".into(),
|
||||||
"empty column list".into(),
|
msg: "empty column list".into(),
|
||||||
Some(col_span),
|
span: Some(col_span),
|
||||||
None,
|
help: None,
|
||||||
Vec::new(),
|
inner: vec![],
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checking if they are same type
|
// Checking if they are same type
|
||||||
if cols.len() > 1 {
|
if cols.len() > 1 {
|
||||||
for w in cols.windows(2) {
|
for w in cols.windows(2) {
|
||||||
let l_series = df.column(w[0].as_ref()).map_err(|e| {
|
let l_series = df
|
||||||
ShellError::GenericError(
|
.column(w[0].as_ref())
|
||||||
"Error selecting columns".into(),
|
.map_err(|e| ShellError::GenericError {
|
||||||
e.to_string(),
|
error: "Error selecting columns".into(),
|
||||||
Some(col_span),
|
msg: e.to_string(),
|
||||||
None,
|
span: Some(col_span),
|
||||||
Vec::new(),
|
help: None,
|
||||||
)
|
inner: vec![],
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let r_series = df.column(w[1].as_ref()).map_err(|e| {
|
let r_series = df
|
||||||
ShellError::GenericError(
|
.column(w[1].as_ref())
|
||||||
"Error selecting columns".into(),
|
.map_err(|e| ShellError::GenericError {
|
||||||
e.to_string(),
|
error: "Error selecting columns".into(),
|
||||||
Some(col_span),
|
msg: e.to_string(),
|
||||||
None,
|
span: Some(col_span),
|
||||||
Vec::new(),
|
help: None,
|
||||||
)
|
inner: vec![],
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
if l_series.dtype() != r_series.dtype() {
|
if l_series.dtype() != r_series.dtype() {
|
||||||
return Err(ShellError::GenericError(
|
return Err(ShellError::GenericError {
|
||||||
"Merge error".into(),
|
error: "Merge error".into(),
|
||||||
"found different column types in list".into(),
|
msg: "found different column types in list".into(),
|
||||||
Some(col_span),
|
span: Some(col_span),
|
||||||
Some(format!(
|
help: Some(format!(
|
||||||
"datatypes {} and {} are incompatible",
|
"datatypes {} and {} are incompatible",
|
||||||
l_series.dtype(),
|
l_series.dtype(),
|
||||||
r_series.dtype()
|
r_series.dtype()
|
||||||
)),
|
)),
|
||||||
Vec::new(),
|
inner: vec![],
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ mod sql_expr;
|
|||||||
mod summary;
|
mod summary;
|
||||||
mod take;
|
mod take;
|
||||||
mod to_arrow;
|
mod to_arrow;
|
||||||
|
mod to_avro;
|
||||||
mod to_csv;
|
mod to_csv;
|
||||||
mod to_df;
|
mod to_df;
|
||||||
mod to_json_lines;
|
mod to_json_lines;
|
||||||
@ -55,6 +56,7 @@ pub use sql_expr::parse_sql_expr;
|
|||||||
pub use summary::Summary;
|
pub use summary::Summary;
|
||||||
pub use take::TakeDF;
|
pub use take::TakeDF;
|
||||||
pub use to_arrow::ToArrow;
|
pub use to_arrow::ToArrow;
|
||||||
|
pub use to_avro::ToAvro;
|
||||||
pub use to_csv::ToCSV;
|
pub use to_csv::ToCSV;
|
||||||
pub use to_df::ToDataFrame;
|
pub use to_df::ToDataFrame;
|
||||||
pub use to_json_lines::ToJsonLines;
|
pub use to_json_lines::ToJsonLines;
|
||||||
@ -96,6 +98,7 @@ pub fn add_eager_decls(working_set: &mut StateWorkingSet) {
|
|||||||
SliceDF,
|
SliceDF,
|
||||||
TakeDF,
|
TakeDF,
|
||||||
ToArrow,
|
ToArrow,
|
||||||
|
ToAvro,
|
||||||
ToCSV,
|
ToCSV,
|
||||||
ToDataFrame,
|
ToDataFrame,
|
||||||
ToNu,
|
ToNu,
|
||||||
|
@ -13,6 +13,8 @@ use polars::prelude::{
|
|||||||
LazyFrame, ParallelStrategy, ParquetReader, ScanArgsIpc, ScanArgsParquet, SerReader,
|
LazyFrame, ParallelStrategy, ParquetReader, ScanArgsIpc, ScanArgsParquet, SerReader,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use polars_io::avro::AvroReader;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct OpenDataFrame;
|
pub struct OpenDataFrame;
|
||||||
|
|
||||||
@ -22,7 +24,7 @@ impl Command for OpenDataFrame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
"Opens CSV, JSON, JSON lines, arrow, or parquet file to create dataframe."
|
"Opens CSV, JSON, JSON lines, arrow, avro, or parquet file to create dataframe."
|
||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
@ -36,7 +38,7 @@ impl Command for OpenDataFrame {
|
|||||||
.named(
|
.named(
|
||||||
"type",
|
"type",
|
||||||
SyntaxShape::String,
|
SyntaxShape::String,
|
||||||
"File type: csv, tsv, json, parquet, arrow. If omitted, derive from file extension",
|
"File type: csv, tsv, json, parquet, arrow, avro. If omitted, derive from file extension",
|
||||||
Some('t'),
|
Some('t'),
|
||||||
)
|
)
|
||||||
.named(
|
.named(
|
||||||
@ -68,8 +70,7 @@ impl Command for OpenDataFrame {
|
|||||||
"Columns to be selected from csv file. CSV and Parquet file",
|
"Columns to be selected from csv file. CSV and Parquet file",
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
.input_type(Type::Any)
|
.input_output_type(Type::Any, Type::Custom("dataframe".into()))
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,19 +116,20 @@ fn command(
|
|||||||
match type_id {
|
match type_id {
|
||||||
Some((e, msg, blamed)) => match e.as_str() {
|
Some((e, msg, blamed)) => match e.as_str() {
|
||||||
"csv" | "tsv" => from_csv(engine_state, stack, call),
|
"csv" | "tsv" => from_csv(engine_state, stack, call),
|
||||||
"parquet" => from_parquet(engine_state, stack, call),
|
"parquet" | "parq" => from_parquet(engine_state, stack, call),
|
||||||
"ipc" | "arrow" => from_ipc(engine_state, stack, call),
|
"ipc" | "arrow" => from_ipc(engine_state, stack, call),
|
||||||
"json" => from_json(engine_state, stack, call),
|
"json" => from_json(engine_state, stack, call),
|
||||||
"jsonl" => from_jsonl(engine_state, stack, call),
|
"jsonl" => from_jsonl(engine_state, stack, call),
|
||||||
_ => Err(ShellError::FileNotFoundCustom(
|
"avro" => from_avro(engine_state, stack, call),
|
||||||
format!("{msg}. Supported values: csv, tsv, parquet, ipc, arrow, json"),
|
_ => Err(ShellError::FileNotFoundCustom {
|
||||||
blamed,
|
msg: format!("{msg}. Supported values: csv, tsv, parquet, ipc, arrow, json"),
|
||||||
)),
|
span: blamed,
|
||||||
|
}),
|
||||||
},
|
},
|
||||||
None => Err(ShellError::FileNotFoundCustom(
|
None => Err(ShellError::FileNotFoundCustom {
|
||||||
"File without extension".into(),
|
msg: "File without extension".into(),
|
||||||
file.span,
|
span: file.span,
|
||||||
)),
|
}),
|
||||||
}
|
}
|
||||||
.map(|value| PipelineData::Value(value, None))
|
.map(|value| PipelineData::Value(value, None))
|
||||||
}
|
}
|
||||||
@ -148,17 +150,16 @@ fn from_parquet(
|
|||||||
low_memory: false,
|
low_memory: false,
|
||||||
cloud_options: None,
|
cloud_options: None,
|
||||||
use_statistics: false,
|
use_statistics: false,
|
||||||
|
hive_partitioning: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
let df: NuLazyFrame = LazyFrame::scan_parquet(file, args)
|
let df: NuLazyFrame = LazyFrame::scan_parquet(file, args)
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Parquet reader error".into(),
|
||||||
"Parquet reader error".into(),
|
msg: format!("{e:?}"),
|
||||||
format!("{e:?}"),
|
span: Some(call.head),
|
||||||
Some(call.head),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?
|
})?
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
@ -167,14 +168,12 @@ fn from_parquet(
|
|||||||
let file: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
|
let file: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
|
||||||
let columns: Option<Vec<String>> = call.get_flag(engine_state, stack, "columns")?;
|
let columns: Option<Vec<String>> = call.get_flag(engine_state, stack, "columns")?;
|
||||||
|
|
||||||
let r = File::open(&file.item).map_err(|e| {
|
let r = File::open(&file.item).map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error opening file".into(),
|
||||||
"Error opening file".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(file.span),
|
||||||
Some(file.span),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?;
|
})?;
|
||||||
let reader = ParquetReader::new(r);
|
let reader = ParquetReader::new(r);
|
||||||
|
|
||||||
@ -185,14 +184,12 @@ fn from_parquet(
|
|||||||
|
|
||||||
let df: NuDataFrame = reader
|
let df: NuDataFrame = reader
|
||||||
.finish()
|
.finish()
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Parquet reader error".into(),
|
||||||
"Parquet reader error".into(),
|
msg: format!("{e:?}"),
|
||||||
format!("{e:?}"),
|
span: Some(call.head),
|
||||||
Some(call.head),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?
|
})?
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
@ -200,6 +197,42 @@ fn from_parquet(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn from_avro(
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
) -> Result<Value, ShellError> {
|
||||||
|
let file: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
|
||||||
|
let columns: Option<Vec<String>> = call.get_flag(engine_state, stack, "columns")?;
|
||||||
|
|
||||||
|
let r = File::open(&file.item).map_err(|e| ShellError::GenericError {
|
||||||
|
error: "Error opening file".into(),
|
||||||
|
msg: e.to_string(),
|
||||||
|
span: Some(file.span),
|
||||||
|
help: None,
|
||||||
|
inner: vec![],
|
||||||
|
})?;
|
||||||
|
let reader = AvroReader::new(r);
|
||||||
|
|
||||||
|
let reader = match columns {
|
||||||
|
None => reader,
|
||||||
|
Some(columns) => reader.with_columns(Some(columns)),
|
||||||
|
};
|
||||||
|
|
||||||
|
let df: NuDataFrame = reader
|
||||||
|
.finish()
|
||||||
|
.map_err(|e| ShellError::GenericError {
|
||||||
|
error: "Avro reader error".into(),
|
||||||
|
msg: format!("{e:?}"),
|
||||||
|
span: Some(call.head),
|
||||||
|
help: None,
|
||||||
|
inner: vec![],
|
||||||
|
})?
|
||||||
|
.into();
|
||||||
|
|
||||||
|
Ok(df.into_value(call.head))
|
||||||
|
}
|
||||||
|
|
||||||
fn from_ipc(
|
fn from_ipc(
|
||||||
engine_state: &EngineState,
|
engine_state: &EngineState,
|
||||||
stack: &mut Stack,
|
stack: &mut Stack,
|
||||||
@ -216,14 +249,12 @@ fn from_ipc(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let df: NuLazyFrame = LazyFrame::scan_ipc(file, args)
|
let df: NuLazyFrame = LazyFrame::scan_ipc(file, args)
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "IPC reader error".into(),
|
||||||
"IPC reader error".into(),
|
msg: format!("{e:?}"),
|
||||||
format!("{e:?}"),
|
span: Some(call.head),
|
||||||
Some(call.head),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?
|
})?
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
@ -232,14 +263,12 @@ fn from_ipc(
|
|||||||
let file: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
|
let file: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
|
||||||
let columns: Option<Vec<String>> = call.get_flag(engine_state, stack, "columns")?;
|
let columns: Option<Vec<String>> = call.get_flag(engine_state, stack, "columns")?;
|
||||||
|
|
||||||
let r = File::open(&file.item).map_err(|e| {
|
let r = File::open(&file.item).map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error opening file".into(),
|
||||||
"Error opening file".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(file.span),
|
||||||
Some(file.span),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?;
|
})?;
|
||||||
let reader = IpcReader::new(r);
|
let reader = IpcReader::new(r);
|
||||||
|
|
||||||
@ -250,14 +279,12 @@ fn from_ipc(
|
|||||||
|
|
||||||
let df: NuDataFrame = reader
|
let df: NuDataFrame = reader
|
||||||
.finish()
|
.finish()
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "IPC reader error".into(),
|
||||||
"IPC reader error".into(),
|
msg: format!("{e:?}"),
|
||||||
format!("{e:?}"),
|
span: Some(call.head),
|
||||||
Some(call.head),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?
|
})?
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
@ -271,14 +298,12 @@ fn from_json(
|
|||||||
call: &Call,
|
call: &Call,
|
||||||
) -> Result<Value, ShellError> {
|
) -> Result<Value, ShellError> {
|
||||||
let file: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
|
let file: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
|
||||||
let file = File::open(&file.item).map_err(|e| {
|
let file = File::open(&file.item).map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error opening file".into(),
|
||||||
"Error opening file".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(file.span),
|
||||||
Some(file.span),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let buf_reader = BufReader::new(file);
|
let buf_reader = BufReader::new(file);
|
||||||
@ -286,14 +311,12 @@ fn from_json(
|
|||||||
|
|
||||||
let df: NuDataFrame = reader
|
let df: NuDataFrame = reader
|
||||||
.finish()
|
.finish()
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Json reader error".into(),
|
||||||
"Json reader error".into(),
|
msg: format!("{e:?}"),
|
||||||
format!("{e:?}"),
|
span: Some(call.head),
|
||||||
Some(call.head),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?
|
})?
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
@ -307,14 +330,12 @@ fn from_jsonl(
|
|||||||
) -> Result<Value, ShellError> {
|
) -> Result<Value, ShellError> {
|
||||||
let infer_schema: Option<usize> = call.get_flag(engine_state, stack, "infer-schema")?;
|
let infer_schema: Option<usize> = call.get_flag(engine_state, stack, "infer-schema")?;
|
||||||
let file: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
|
let file: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
|
||||||
let file = File::open(&file.item).map_err(|e| {
|
let file = File::open(&file.item).map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error opening file".into(),
|
||||||
"Error opening file".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(file.span),
|
||||||
Some(file.span),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let buf_reader = BufReader::new(file);
|
let buf_reader = BufReader::new(file);
|
||||||
@ -324,14 +345,12 @@ fn from_jsonl(
|
|||||||
|
|
||||||
let df: NuDataFrame = reader
|
let df: NuDataFrame = reader
|
||||||
.finish()
|
.finish()
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Json lines reader error".into(),
|
||||||
"Json lines reader error".into(),
|
msg: format!("{e:?}"),
|
||||||
format!("{e:?}"),
|
span: Some(call.head),
|
||||||
Some(call.head),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?
|
})?
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
@ -357,19 +376,19 @@ fn from_csv(
|
|||||||
None => csv_reader,
|
None => csv_reader,
|
||||||
Some(d) => {
|
Some(d) => {
|
||||||
if d.item.len() != 1 {
|
if d.item.len() != 1 {
|
||||||
return Err(ShellError::GenericError(
|
return Err(ShellError::GenericError {
|
||||||
"Incorrect delimiter".into(),
|
error: "Incorrect delimiter".into(),
|
||||||
"Delimiter has to be one character".into(),
|
msg: "Delimiter has to be one character".into(),
|
||||||
Some(d.span),
|
span: Some(d.span),
|
||||||
None,
|
help: None,
|
||||||
Vec::new(),
|
inner: vec![],
|
||||||
));
|
});
|
||||||
} else {
|
} else {
|
||||||
let delimiter = match d.item.chars().next() {
|
let delimiter = match d.item.chars().next() {
|
||||||
Some(d) => d as u8,
|
Some(d) => d as u8,
|
||||||
None => unreachable!(),
|
None => unreachable!(),
|
||||||
};
|
};
|
||||||
csv_reader.with_delimiter(delimiter)
|
csv_reader.with_separator(delimiter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -388,14 +407,12 @@ fn from_csv(
|
|||||||
|
|
||||||
let df: NuLazyFrame = csv_reader
|
let df: NuLazyFrame = csv_reader
|
||||||
.finish()
|
.finish()
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Parquet reader error".into(),
|
||||||
"Parquet reader error".into(),
|
msg: format!("{e:?}"),
|
||||||
format!("{e:?}"),
|
span: Some(call.head),
|
||||||
Some(call.head),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?
|
})?
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
@ -403,14 +420,12 @@ fn from_csv(
|
|||||||
} else {
|
} else {
|
||||||
let file: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
|
let file: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
|
||||||
let csv_reader = CsvReader::from_path(&file.item)
|
let csv_reader = CsvReader::from_path(&file.item)
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error creating CSV reader".into(),
|
||||||
"Error creating CSV reader".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(file.span),
|
||||||
Some(file.span),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?
|
})?
|
||||||
.with_encoding(CsvEncoding::LossyUtf8);
|
.with_encoding(CsvEncoding::LossyUtf8);
|
||||||
|
|
||||||
@ -418,19 +433,19 @@ fn from_csv(
|
|||||||
None => csv_reader,
|
None => csv_reader,
|
||||||
Some(d) => {
|
Some(d) => {
|
||||||
if d.item.len() != 1 {
|
if d.item.len() != 1 {
|
||||||
return Err(ShellError::GenericError(
|
return Err(ShellError::GenericError {
|
||||||
"Incorrect delimiter".into(),
|
error: "Incorrect delimiter".into(),
|
||||||
"Delimiter has to be one character".into(),
|
msg: "Delimiter has to be one character".into(),
|
||||||
Some(d.span),
|
span: Some(d.span),
|
||||||
None,
|
help: None,
|
||||||
Vec::new(),
|
inner: vec![],
|
||||||
));
|
});
|
||||||
} else {
|
} else {
|
||||||
let delimiter = match d.item.chars().next() {
|
let delimiter = match d.item.chars().next() {
|
||||||
Some(d) => d as u8,
|
Some(d) => d as u8,
|
||||||
None => unreachable!(),
|
None => unreachable!(),
|
||||||
};
|
};
|
||||||
csv_reader.with_delimiter(delimiter)
|
csv_reader.with_separator(delimiter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -454,14 +469,12 @@ fn from_csv(
|
|||||||
|
|
||||||
let df: NuDataFrame = csv_reader
|
let df: NuDataFrame = csv_reader
|
||||||
.finish()
|
.finish()
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Parquet reader error".into(),
|
||||||
"Parquet reader error".into(),
|
msg: format!("{e:?}"),
|
||||||
format!("{e:?}"),
|
span: Some(call.head),
|
||||||
Some(call.head),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?
|
})?
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
|
@ -28,8 +28,10 @@ impl Command for QueryDf {
|
|||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.required("sql", SyntaxShape::String, "sql query")
|
.required("sql", SyntaxShape::String, "sql query")
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
Type::Custom("dataframe".into()),
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
)
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,22 +76,19 @@ fn command(
|
|||||||
|
|
||||||
let mut ctx = SQLContext::new();
|
let mut ctx = SQLContext::new();
|
||||||
ctx.register("df", &df.df);
|
ctx.register("df", &df.df);
|
||||||
let df_sql = ctx.execute(&sql_query).map_err(|e| {
|
let df_sql = ctx
|
||||||
ShellError::GenericError(
|
.execute(&sql_query)
|
||||||
"Dataframe Error".into(),
|
.map_err(|e| ShellError::GenericError {
|
||||||
e.to_string(),
|
error: "Dataframe Error".into(),
|
||||||
Some(call.head),
|
msg: e.to_string(),
|
||||||
None,
|
span: Some(call.head),
|
||||||
Vec::new(),
|
help: None,
|
||||||
)
|
inner: vec![],
|
||||||
})?;
|
})?;
|
||||||
let lazy = NuLazyFrame::new(false, df_sql);
|
let lazy = NuLazyFrame::new(false, df_sql);
|
||||||
|
|
||||||
let eager = lazy.collect(call.head)?;
|
let eager = lazy.collect(call.head)?;
|
||||||
let value = Value::CustomValue {
|
let value = Value::custom_value(Box::new(eager), call.head);
|
||||||
val: Box::new(eager),
|
|
||||||
span: call.head,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(PipelineData::Value(value, None))
|
Ok(PipelineData::Value(value, None))
|
||||||
}
|
}
|
||||||
|
@ -33,8 +33,10 @@ impl Command for RenameDF {
|
|||||||
SyntaxShape::Any,
|
SyntaxShape::Any,
|
||||||
"New names for the selected column(s). A string or list of strings",
|
"New names for the selected column(s). A string or list of strings",
|
||||||
)
|
)
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
Type::Custom("dataframe".into()),
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
)
|
||||||
.category(Category::Custom("dataframe or lazyframe".into()))
|
.category(Category::Custom("dataframe or lazyframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,15 +130,15 @@ fn command_eager(
|
|||||||
let new_names = extract_strings(new_names)?;
|
let new_names = extract_strings(new_names)?;
|
||||||
|
|
||||||
for (from, to) in columns.iter().zip(new_names.iter()) {
|
for (from, to) in columns.iter().zip(new_names.iter()) {
|
||||||
df.as_mut().rename(from, to).map_err(|e| {
|
df.as_mut()
|
||||||
ShellError::GenericError(
|
.rename(from, to)
|
||||||
"Error renaming".into(),
|
.map_err(|e| ShellError::GenericError {
|
||||||
e.to_string(),
|
error: "Error renaming".into(),
|
||||||
Some(call.head),
|
msg: e.to_string(),
|
||||||
None,
|
span: Some(call.head),
|
||||||
Vec::new(),
|
help: None,
|
||||||
)
|
inner: vec![],
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(PipelineData::Value(df.into_value(call.head), None))
|
Ok(PipelineData::Value(df.into_value(call.head), None))
|
||||||
@ -158,7 +160,7 @@ fn command_lazy(
|
|||||||
let value: Value = call.req(engine_state, stack, 1)?;
|
let value: Value = call.req(engine_state, stack, 1)?;
|
||||||
return Err(ShellError::IncompatibleParametersSingle {
|
return Err(ShellError::IncompatibleParametersSingle {
|
||||||
msg: "New name list has different size to column list".into(),
|
msg: "New name list has different size to column list".into(),
|
||||||
span: value.span()?,
|
span: value.span(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,8 @@ use nu_protocol::{
|
|||||||
engine::{Command, EngineState, Stack},
|
engine::{Command, EngineState, Stack},
|
||||||
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type,
|
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type,
|
||||||
};
|
};
|
||||||
|
use polars::prelude::NamedFrom;
|
||||||
|
use polars::series::Series;
|
||||||
|
|
||||||
use super::super::values::NuDataFrame;
|
use super::super::values::NuDataFrame;
|
||||||
|
|
||||||
@ -41,8 +43,10 @@ impl Command for SampleDF {
|
|||||||
)
|
)
|
||||||
.switch("replace", "sample with replace", Some('e'))
|
.switch("replace", "sample with replace", Some('e'))
|
||||||
.switch("shuffle", "shuffle sample", Some('u'))
|
.switch("shuffle", "shuffle sample", Some('u'))
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
Type::Custom("dataframe".into()),
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
)
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,12 +54,13 @@ impl Command for SampleDF {
|
|||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
description: "Sample rows from dataframe",
|
description: "Sample rows from dataframe",
|
||||||
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr sample -n 1",
|
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr sample --n-rows 1",
|
||||||
result: None, // No expected value because sampling is random
|
result: None, // No expected value because sampling is random
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Shows sample row using fraction and replace",
|
description: "Shows sample row using fraction and replace",
|
||||||
example: "[[a b]; [1 2] [3 4] [5 6]] | dfr into-df | dfr sample -f 0.5 -e",
|
example:
|
||||||
|
"[[a b]; [1 2] [3 4] [5 6]] | dfr into-df | dfr sample --fraction 0.5 --replace",
|
||||||
result: None, // No expected value because sampling is random
|
result: None, // No expected value because sampling is random
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
@ -78,7 +83,7 @@ fn command(
|
|||||||
call: &Call,
|
call: &Call,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let rows: Option<Spanned<usize>> = call.get_flag(engine_state, stack, "n-rows")?;
|
let rows: Option<Spanned<i64>> = call.get_flag(engine_state, stack, "n-rows")?;
|
||||||
let fraction: Option<Spanned<f64>> = call.get_flag(engine_state, stack, "fraction")?;
|
let fraction: Option<Spanned<f64>> = call.get_flag(engine_state, stack, "fraction")?;
|
||||||
let seed: Option<u64> = call
|
let seed: Option<u64> = call
|
||||||
.get_flag::<i64>(engine_state, stack, "seed")?
|
.get_flag::<i64>(engine_state, stack, "seed")?
|
||||||
@ -91,42 +96,38 @@ fn command(
|
|||||||
match (rows, fraction) {
|
match (rows, fraction) {
|
||||||
(Some(rows), None) => df
|
(Some(rows), None) => df
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.sample_n(rows.item, replace, shuffle, seed)
|
.sample_n(&Series::new("s", &[rows.item]), replace, shuffle, seed)
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error creating sample".into(),
|
||||||
"Error creating sample".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(rows.span),
|
||||||
Some(rows.span),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
}),
|
}),
|
||||||
(None, Some(frac)) => df
|
(None, Some(frac)) => df
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.sample_frac(frac.item, replace, shuffle, seed)
|
.sample_frac(&Series::new("frac", &[frac.item]), replace, shuffle, seed)
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error creating sample".into(),
|
||||||
"Error creating sample".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(frac.span),
|
||||||
Some(frac.span),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
}),
|
}),
|
||||||
(Some(_), Some(_)) => Err(ShellError::GenericError(
|
(Some(_), Some(_)) => Err(ShellError::GenericError {
|
||||||
"Incompatible flags".into(),
|
error: "Incompatible flags".into(),
|
||||||
"Only one selection criterion allowed".into(),
|
msg: "Only one selection criterion allowed".into(),
|
||||||
Some(call.head),
|
span: Some(call.head),
|
||||||
None,
|
help: None,
|
||||||
Vec::new(),
|
inner: vec![],
|
||||||
)),
|
}),
|
||||||
(None, None) => Err(ShellError::GenericError(
|
(None, None) => Err(ShellError::GenericError {
|
||||||
"No selection".into(),
|
error: "No selection".into(),
|
||||||
"No selection criterion was found".into(),
|
msg: "No selection criterion was found".into(),
|
||||||
Some(call.head),
|
span: Some(call.head),
|
||||||
Some("Perhaps you want to use the flag -n or -f".into()),
|
help: Some("Perhaps you want to use the flag -n or -f".into()),
|
||||||
Vec::new(),
|
inner: vec![],
|
||||||
)),
|
}),
|
||||||
}
|
}
|
||||||
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
|
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
|
||||||
}
|
}
|
||||||
|
@ -22,8 +22,10 @@ impl Command for ShapeDF {
|
|||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
Type::Custom("dataframe".into()),
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
)
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,8 +25,10 @@ impl Command for SliceDF {
|
|||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.required("offset", SyntaxShape::Int, "start of slice")
|
.required("offset", SyntaxShape::Int, "start of slice")
|
||||||
.required("size", SyntaxShape::Int, "size of slice")
|
.required("size", SyntaxShape::Int, "size of slice")
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
Type::Custom("dataframe".into()),
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
)
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,8 @@ use crate::dataframe::eager::sql_expr::parse_sql_expr;
|
|||||||
use polars::error::{ErrString, PolarsError};
|
use polars::error::{ErrString, PolarsError};
|
||||||
use polars::prelude::{col, DataFrame, DataType, IntoLazy, LazyFrame};
|
use polars::prelude::{col, DataFrame, DataType, IntoLazy, LazyFrame};
|
||||||
use sqlparser::ast::{
|
use sqlparser::ast::{
|
||||||
Expr as SqlExpr, Select, SelectItem, SetExpr, Statement, TableFactor, Value as SQLValue,
|
Expr as SqlExpr, GroupByExpr, Select, SelectItem, SetExpr, Statement, TableFactor,
|
||||||
|
Value as SQLValue,
|
||||||
};
|
};
|
||||||
use sqlparser::dialect::GenericDialect;
|
use sqlparser::dialect::GenericDialect;
|
||||||
use sqlparser::parser::Parser;
|
use sqlparser::parser::Parser;
|
||||||
@ -29,7 +30,7 @@ impl SQLContext {
|
|||||||
fn execute_select(&self, select_stmt: &Select) -> Result<LazyFrame, PolarsError> {
|
fn execute_select(&self, select_stmt: &Select) -> Result<LazyFrame, PolarsError> {
|
||||||
// Determine involved dataframe
|
// Determine involved dataframe
|
||||||
// Implicit join require some more work in query parsers, Explicit join are preferred for now.
|
// Implicit join require some more work in query parsers, Explicit join are preferred for now.
|
||||||
let tbl = select_stmt.from.get(0).ok_or_else(|| {
|
let tbl = select_stmt.from.first().ok_or_else(|| {
|
||||||
PolarsError::ComputeError(ErrString::from("No table found in select statement"))
|
PolarsError::ComputeError(ErrString::from("No table found in select statement"))
|
||||||
})?;
|
})?;
|
||||||
let mut alias_map = HashMap::new();
|
let mut alias_map = HashMap::new();
|
||||||
@ -37,7 +38,7 @@ impl SQLContext {
|
|||||||
TableFactor::Table { name, alias, .. } => {
|
TableFactor::Table { name, alias, .. } => {
|
||||||
let tbl_name = name
|
let tbl_name = name
|
||||||
.0
|
.0
|
||||||
.get(0)
|
.first()
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
PolarsError::ComputeError(ErrString::from(
|
PolarsError::ComputeError(ErrString::from(
|
||||||
"No table found in select statement",
|
"No table found in select statement",
|
||||||
@ -96,8 +97,13 @@ impl SQLContext {
|
|||||||
.collect::<Result<Vec<_>, PolarsError>>()?;
|
.collect::<Result<Vec<_>, PolarsError>>()?;
|
||||||
// Check for group by
|
// Check for group by
|
||||||
// After projection since there might be number.
|
// After projection since there might be number.
|
||||||
let group_by = select_stmt
|
let group_by = match &select_stmt.group_by {
|
||||||
.group_by
|
GroupByExpr::All =>
|
||||||
|
Err(
|
||||||
|
PolarsError::ComputeError("Group-By Error: Only positive number or expression are supported, not all".into())
|
||||||
|
)?,
|
||||||
|
GroupByExpr::Expressions(expressions) => expressions
|
||||||
|
}
|
||||||
.iter()
|
.iter()
|
||||||
.map(
|
.map(
|
||||||
|e|match e {
|
|e|match e {
|
||||||
@ -147,10 +153,10 @@ impl SQLContext {
|
|||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(agg_pj, (proj_p, expr))| (expr.clone(), (proj_p, agg_pj + group_by.len())))
|
.map(|(agg_pj, (proj_p, expr))| (expr.clone(), (proj_p, agg_pj + group_by.len())))
|
||||||
.unzip();
|
.unzip();
|
||||||
let agg_df = df.groupby(group_by).agg(agg_projection);
|
let agg_df = df.group_by(group_by).agg(agg_projection);
|
||||||
let mut final_proj_pos = groupby_pos
|
let mut final_proj_pos = groupby_pos
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.chain(agg_proj_pos.into_iter())
|
.chain(agg_proj_pos)
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
final_proj_pos.sort_by(|(proj_pa, _), (proj_pb, _)| proj_pa.cmp(proj_pb));
|
final_proj_pos.sort_by(|(proj_pa, _), (proj_pb, _)| proj_pa.cmp(proj_pb));
|
||||||
@ -182,7 +188,7 @@ impl SQLContext {
|
|||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
let ast = ast
|
let ast = ast
|
||||||
.get(0)
|
.first()
|
||||||
.ok_or_else(|| PolarsError::ComputeError(ErrString::from("No statement found")))?;
|
.ok_or_else(|| PolarsError::ComputeError(ErrString::from("No statement found")))?;
|
||||||
Ok(match ast {
|
Ok(match ast {
|
||||||
Statement::Query(query) => {
|
Statement::Query(query) => {
|
||||||
|
@ -2,8 +2,8 @@ use polars::error::PolarsError;
|
|||||||
use polars::prelude::{col, lit, DataType, Expr, LiteralValue, PolarsResult as Result, TimeUnit};
|
use polars::prelude::{col, lit, DataType, Expr, LiteralValue, PolarsResult as Result, TimeUnit};
|
||||||
|
|
||||||
use sqlparser::ast::{
|
use sqlparser::ast::{
|
||||||
BinaryOperator as SQLBinaryOperator, DataType as SQLDataType, Expr as SqlExpr,
|
ArrayElemTypeDef, BinaryOperator as SQLBinaryOperator, DataType as SQLDataType,
|
||||||
Function as SQLFunction, Value as SqlValue, WindowSpec,
|
Expr as SqlExpr, Function as SQLFunction, Value as SqlValue, WindowType,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn map_sql_polars_datatype(data_type: &SQLDataType) -> Result<DataType> {
|
fn map_sql_polars_datatype(data_type: &SQLDataType) -> Result<DataType> {
|
||||||
@ -13,7 +13,7 @@ fn map_sql_polars_datatype(data_type: &SQLDataType) -> Result<DataType> {
|
|||||||
| SQLDataType::Uuid
|
| SQLDataType::Uuid
|
||||||
| SQLDataType::Clob(_)
|
| SQLDataType::Clob(_)
|
||||||
| SQLDataType::Text
|
| SQLDataType::Text
|
||||||
| SQLDataType::String => DataType::Utf8,
|
| SQLDataType::String(_) => DataType::Utf8,
|
||||||
SQLDataType::Float(_) => DataType::Float32,
|
SQLDataType::Float(_) => DataType::Float32,
|
||||||
SQLDataType::Real => DataType::Float32,
|
SQLDataType::Real => DataType::Float32,
|
||||||
SQLDataType::Double => DataType::Float64,
|
SQLDataType::Double => DataType::Float64,
|
||||||
@ -31,9 +31,12 @@ fn map_sql_polars_datatype(data_type: &SQLDataType) -> Result<DataType> {
|
|||||||
SQLDataType::Time(_, _) => DataType::Time,
|
SQLDataType::Time(_, _) => DataType::Time,
|
||||||
SQLDataType::Timestamp(_, _) => DataType::Datetime(TimeUnit::Microseconds, None),
|
SQLDataType::Timestamp(_, _) => DataType::Datetime(TimeUnit::Microseconds, None),
|
||||||
SQLDataType::Interval => DataType::Duration(TimeUnit::Microseconds),
|
SQLDataType::Interval => DataType::Duration(TimeUnit::Microseconds),
|
||||||
SQLDataType::Array(inner_type) => match inner_type {
|
SQLDataType::Array(array_type_def) => match array_type_def {
|
||||||
Some(inner_type) => DataType::List(Box::new(map_sql_polars_datatype(inner_type)?)),
|
ArrayElemTypeDef::AngleBracket(inner_type)
|
||||||
None => {
|
| ArrayElemTypeDef::SquareBracket(inner_type) => {
|
||||||
|
DataType::List(Box::new(map_sql_polars_datatype(inner_type)?))
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
return Err(PolarsError::ComputeError(
|
return Err(PolarsError::ComputeError(
|
||||||
"SQL Datatype Array(None) was not supported in polars-sql yet!".into(),
|
"SQL Datatype Array(None) was not supported in polars-sql yet!".into(),
|
||||||
))
|
))
|
||||||
@ -114,7 +117,11 @@ pub fn parse_sql_expr(expr: &SqlExpr) -> Result<Expr> {
|
|||||||
binary_op_(left, right, op)?
|
binary_op_(left, right, op)?
|
||||||
}
|
}
|
||||||
SqlExpr::Function(sql_function) => parse_sql_function(sql_function)?,
|
SqlExpr::Function(sql_function) => parse_sql_function(sql_function)?,
|
||||||
SqlExpr::Cast { expr, data_type } => cast_(parse_sql_expr(expr)?, data_type)?,
|
SqlExpr::Cast {
|
||||||
|
expr,
|
||||||
|
data_type,
|
||||||
|
format: _,
|
||||||
|
} => cast_(parse_sql_expr(expr)?, data_type)?,
|
||||||
SqlExpr::Nested(expr) => parse_sql_expr(expr)?,
|
SqlExpr::Nested(expr) => parse_sql_expr(expr)?,
|
||||||
SqlExpr::Value(value) => literal_expr(value)?,
|
SqlExpr::Value(value) => literal_expr(value)?,
|
||||||
_ => {
|
_ => {
|
||||||
@ -125,18 +132,26 @@ pub fn parse_sql_expr(expr: &SqlExpr) -> Result<Expr> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn apply_window_spec(expr: Expr, window_spec: &Option<WindowSpec>) -> Result<Expr> {
|
fn apply_window_spec(expr: Expr, window_type: Option<&WindowType>) -> Result<Expr> {
|
||||||
Ok(match &window_spec {
|
Ok(match &window_type {
|
||||||
Some(window_spec) => {
|
Some(wtype) => match wtype {
|
||||||
// Process for simple window specification, partition by first
|
WindowType::WindowSpec(window_spec) => {
|
||||||
let partition_by = window_spec
|
// Process for simple window specification, partition by first
|
||||||
.partition_by
|
let partition_by = window_spec
|
||||||
.iter()
|
.partition_by
|
||||||
.map(parse_sql_expr)
|
.iter()
|
||||||
.collect::<Result<Vec<_>>>()?;
|
.map(parse_sql_expr)
|
||||||
expr.over(partition_by)
|
.collect::<Result<Vec<_>>>()?;
|
||||||
// Order by and Row range may not be supported at the moment
|
expr.over(partition_by)
|
||||||
}
|
// Order by and Row range may not be supported at the moment
|
||||||
|
}
|
||||||
|
// TODO: make NamedWindow work
|
||||||
|
WindowType::NamedWindow(_named) => {
|
||||||
|
return Err(PolarsError::ComputeError(
|
||||||
|
format!("Expression: {expr:?} was not supported in polars-sql yet!").into(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
},
|
||||||
None => expr,
|
None => expr,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -144,7 +159,7 @@ fn apply_window_spec(expr: Expr, window_spec: &Option<WindowSpec>) -> Result<Exp
|
|||||||
fn parse_sql_function(sql_function: &SQLFunction) -> Result<Expr> {
|
fn parse_sql_function(sql_function: &SQLFunction) -> Result<Expr> {
|
||||||
use sqlparser::ast::{FunctionArg, FunctionArgExpr};
|
use sqlparser::ast::{FunctionArg, FunctionArgExpr};
|
||||||
// Function name mostly do not have name space, so it mostly take the first args
|
// Function name mostly do not have name space, so it mostly take the first args
|
||||||
let function_name = sql_function.name.0[0].value.to_lowercase();
|
let function_name = sql_function.name.0[0].value.to_ascii_lowercase();
|
||||||
let args = sql_function
|
let args = sql_function
|
||||||
.args
|
.args
|
||||||
.iter()
|
.iter()
|
||||||
@ -160,13 +175,13 @@ fn parse_sql_function(sql_function: &SQLFunction) -> Result<Expr> {
|
|||||||
sql_function.distinct,
|
sql_function.distinct,
|
||||||
) {
|
) {
|
||||||
("sum", [FunctionArgExpr::Expr(expr)], false) => {
|
("sum", [FunctionArgExpr::Expr(expr)], false) => {
|
||||||
apply_window_spec(parse_sql_expr(expr)?, &sql_function.over)?.sum()
|
apply_window_spec(parse_sql_expr(expr)?, sql_function.over.as_ref())?.sum()
|
||||||
}
|
}
|
||||||
("count", [FunctionArgExpr::Expr(expr)], false) => {
|
("count", [FunctionArgExpr::Expr(expr)], false) => {
|
||||||
apply_window_spec(parse_sql_expr(expr)?, &sql_function.over)?.count()
|
apply_window_spec(parse_sql_expr(expr)?, sql_function.over.as_ref())?.count()
|
||||||
}
|
}
|
||||||
("count", [FunctionArgExpr::Expr(expr)], true) => {
|
("count", [FunctionArgExpr::Expr(expr)], true) => {
|
||||||
apply_window_spec(parse_sql_expr(expr)?, &sql_function.over)?.n_unique()
|
apply_window_spec(parse_sql_expr(expr)?, sql_function.over.as_ref())?.n_unique()
|
||||||
}
|
}
|
||||||
// Special case for wildcard args to count function.
|
// Special case for wildcard args to count function.
|
||||||
("count", [FunctionArgExpr::Wildcard], false) => lit(1i32).count(),
|
("count", [FunctionArgExpr::Wildcard], false) => lit(1i32).count(),
|
||||||
|
@ -29,11 +29,13 @@ impl Command for Summary {
|
|||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
Type::Custom("dataframe".into()),
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
)
|
||||||
.named(
|
.named(
|
||||||
"quantiles",
|
"quantiles",
|
||||||
SyntaxShape::Table,
|
SyntaxShape::Table(vec![]),
|
||||||
"provide optional quantiles",
|
"provide optional quantiles",
|
||||||
Some('q'),
|
Some('q'),
|
||||||
)
|
)
|
||||||
@ -118,30 +120,31 @@ fn command(
|
|||||||
let quantiles = quantiles.map(|values| {
|
let quantiles = quantiles.map(|values| {
|
||||||
values
|
values
|
||||||
.iter()
|
.iter()
|
||||||
.map(|value| match value {
|
.map(|value| {
|
||||||
Value::Float { val, span } => {
|
let span = value.span();
|
||||||
if (&0.0..=&1.0).contains(&val) {
|
match value {
|
||||||
Ok(*val)
|
Value::Float { val, .. } => {
|
||||||
} else {
|
if (&0.0..=&1.0).contains(&val) {
|
||||||
Err(ShellError::GenericError(
|
Ok(*val)
|
||||||
"Incorrect value for quantile".to_string(),
|
} else {
|
||||||
"value should be between 0 and 1".to_string(),
|
Err(ShellError::GenericError {
|
||||||
Some(*span),
|
error: "Incorrect value for quantile".into(),
|
||||||
None,
|
msg: "value should be between 0 and 1".into(),
|
||||||
Vec::new(),
|
span: Some(span),
|
||||||
))
|
help: None,
|
||||||
|
inner: vec![],
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Value::Error { error, .. } => Err(*error.clone()),
|
||||||
|
_ => Err(ShellError::GenericError {
|
||||||
|
error: "Incorrect value for quantile".into(),
|
||||||
|
msg: "value should be a float".into(),
|
||||||
|
span: Some(span),
|
||||||
|
help: None,
|
||||||
|
inner: vec![],
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
_ => match value.span() {
|
|
||||||
Ok(span) => Err(ShellError::GenericError(
|
|
||||||
"Incorrect value for quantile".to_string(),
|
|
||||||
"value should be a float".to_string(),
|
|
||||||
Some(span),
|
|
||||||
None,
|
|
||||||
Vec::new(),
|
|
||||||
)),
|
|
||||||
Err(e) => Err(e),
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<f64>, ShellError>>()
|
.collect::<Result<Vec<f64>, ShellError>>()
|
||||||
});
|
});
|
||||||
@ -247,14 +250,12 @@ fn command(
|
|||||||
let res = head.chain(tail).collect::<Vec<Series>>();
|
let res = head.chain(tail).collect::<Vec<Series>>();
|
||||||
|
|
||||||
DataFrame::new(res)
|
DataFrame::new(res)
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Dataframe Error".into(),
|
||||||
"Dataframe Error".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(call.head),
|
||||||
Some(call.head),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
|
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
|
||||||
}
|
}
|
||||||
|
@ -29,8 +29,10 @@ impl Command for TakeDF {
|
|||||||
SyntaxShape::Any,
|
SyntaxShape::Any,
|
||||||
"list of indices used to take data",
|
"list of indices used to take data",
|
||||||
)
|
)
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
Type::Custom("dataframe".into()),
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
)
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,51 +93,45 @@ fn command(
|
|||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let index_value: Value = call.req(engine_state, stack, 0)?;
|
let index_value: Value = call.req(engine_state, stack, 0)?;
|
||||||
let index_span = index_value.span()?;
|
let index_span = index_value.span();
|
||||||
let index = NuDataFrame::try_from_value(index_value)?.as_series(index_span)?;
|
let index = NuDataFrame::try_from_value(index_value)?.as_series(index_span)?;
|
||||||
|
|
||||||
let casted = match index.dtype() {
|
let casted = match index.dtype() {
|
||||||
DataType::UInt32 | DataType::UInt64 | DataType::Int32 | DataType::Int64 => {
|
DataType::UInt32 | DataType::UInt64 | DataType::Int32 | DataType::Int64 => index
|
||||||
index.cast(&DataType::UInt32).map_err(|e| {
|
.cast(&DataType::UInt32)
|
||||||
ShellError::GenericError(
|
.map_err(|e| ShellError::GenericError {
|
||||||
"Error casting index list".into(),
|
error: "Error casting index list".into(),
|
||||||
e.to_string(),
|
msg: e.to_string(),
|
||||||
Some(index_span),
|
span: Some(index_span),
|
||||||
None,
|
help: None,
|
||||||
Vec::new(),
|
inner: vec![],
|
||||||
)
|
}),
|
||||||
})
|
_ => Err(ShellError::GenericError {
|
||||||
}
|
error: "Incorrect type".into(),
|
||||||
_ => Err(ShellError::GenericError(
|
msg: "Series with incorrect type".into(),
|
||||||
"Incorrect type".into(),
|
span: Some(call.head),
|
||||||
"Series with incorrect type".into(),
|
help: Some("Consider using a Series with type int type".into()),
|
||||||
Some(call.head),
|
inner: vec![],
|
||||||
Some("Consider using a Series with type int type".into()),
|
}),
|
||||||
Vec::new(),
|
|
||||||
)),
|
|
||||||
}?;
|
}?;
|
||||||
|
|
||||||
let indices = casted.u32().map_err(|e| {
|
let indices = casted.u32().map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error casting index list".into(),
|
||||||
"Error casting index list".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(index_span),
|
||||||
Some(index_span),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
NuDataFrame::try_from_pipeline(input, call.head).and_then(|df| {
|
NuDataFrame::try_from_pipeline(input, call.head).and_then(|df| {
|
||||||
df.as_ref()
|
df.as_ref()
|
||||||
.take(indices)
|
.take(indices)
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error taking values".into(),
|
||||||
"Error taking values".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(call.head),
|
||||||
Some(call.head),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
|
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
|
||||||
})
|
})
|
||||||
|
@ -25,8 +25,7 @@ impl Command for ToArrow {
|
|||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.required("file", SyntaxShape::Filepath, "file path to save dataframe")
|
.required("file", SyntaxShape::Filepath, "file path to save dataframe")
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(Type::Custom("dataframe".into()), Type::Any)
|
||||||
.output_type(Type::Any)
|
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,36 +58,28 @@ fn command(
|
|||||||
|
|
||||||
let mut df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
let mut df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
||||||
|
|
||||||
let mut file = File::create(&file_name.item).map_err(|e| {
|
let mut file = File::create(&file_name.item).map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error with file name".into(),
|
||||||
"Error with file name".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(file_name.span),
|
||||||
Some(file_name.span),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
IpcWriter::new(&mut file).finish(df.as_mut()).map_err(|e| {
|
IpcWriter::new(&mut file)
|
||||||
ShellError::GenericError(
|
.finish(df.as_mut())
|
||||||
"Error saving file".into(),
|
.map_err(|e| ShellError::GenericError {
|
||||||
e.to_string(),
|
error: "Error saving file".into(),
|
||||||
Some(file_name.span),
|
msg: e.to_string(),
|
||||||
None,
|
span: Some(file_name.span),
|
||||||
Vec::new(),
|
help: None,
|
||||||
)
|
inner: vec![],
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let file_value = Value::String {
|
let file_value = Value::string(format!("saved {:?}", &file_name.item), file_name.span);
|
||||||
val: format!("saved {:?}", &file_name.item),
|
|
||||||
span: file_name.span,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(PipelineData::Value(
|
Ok(PipelineData::Value(
|
||||||
Value::List {
|
Value::list(vec![file_value], call.head),
|
||||||
vals: vec![file_value],
|
|
||||||
span: call.head,
|
|
||||||
},
|
|
||||||
None,
|
None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
113
crates/nu-cmd-dataframe/src/dataframe/eager/to_avro.rs
Normal file
113
crates/nu-cmd-dataframe/src/dataframe/eager/to_avro.rs
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
use std::{fs::File, path::PathBuf};
|
||||||
|
|
||||||
|
use nu_engine::CallExt;
|
||||||
|
use nu_protocol::{
|
||||||
|
ast::Call,
|
||||||
|
engine::{Command, EngineState, Stack},
|
||||||
|
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
|
||||||
|
};
|
||||||
|
use polars_io::avro::{AvroCompression, AvroWriter};
|
||||||
|
use polars_io::SerWriter;
|
||||||
|
|
||||||
|
use super::super::values::NuDataFrame;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct ToAvro;
|
||||||
|
|
||||||
|
impl Command for ToAvro {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"dfr to-avro"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Saves dataframe to avro file."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build(self.name())
|
||||||
|
.named(
|
||||||
|
"compression",
|
||||||
|
SyntaxShape::String,
|
||||||
|
"use compression, supports deflate or snappy",
|
||||||
|
Some('c'),
|
||||||
|
)
|
||||||
|
.required("file", SyntaxShape::Filepath, "file path to save dataframe")
|
||||||
|
.input_output_type(Type::Custom("dataframe".into()), Type::Any)
|
||||||
|
.category(Category::Custom("dataframe".into()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![Example {
|
||||||
|
description: "Saves dataframe to avro file",
|
||||||
|
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr to-avro test.avro",
|
||||||
|
result: None,
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
command(engine_state, stack, call, input)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_compression(call: &Call) -> Result<Option<AvroCompression>, ShellError> {
|
||||||
|
if let Some((compression, span)) = call
|
||||||
|
.get_flag_expr("compression")
|
||||||
|
.and_then(|e| e.as_string().map(|s| (s, e.span)))
|
||||||
|
{
|
||||||
|
match compression.as_ref() {
|
||||||
|
"snappy" => Ok(Some(AvroCompression::Snappy)),
|
||||||
|
"deflate" => Ok(Some(AvroCompression::Deflate)),
|
||||||
|
_ => Err(ShellError::IncorrectValue {
|
||||||
|
msg: "compression must be one of deflate or snappy".to_string(),
|
||||||
|
val_span: span,
|
||||||
|
call_span: span,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn command(
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
let file_name: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
|
||||||
|
let compression = get_compression(call)?;
|
||||||
|
|
||||||
|
let mut df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
||||||
|
|
||||||
|
let file = File::create(&file_name.item).map_err(|e| ShellError::GenericError {
|
||||||
|
error: "Error with file name".into(),
|
||||||
|
msg: e.to_string(),
|
||||||
|
span: Some(file_name.span),
|
||||||
|
help: None,
|
||||||
|
inner: vec![],
|
||||||
|
})?;
|
||||||
|
|
||||||
|
AvroWriter::new(file)
|
||||||
|
.with_compression(compression)
|
||||||
|
.finish(df.as_mut())
|
||||||
|
.map_err(|e| ShellError::GenericError {
|
||||||
|
error: "Error saving file".into(),
|
||||||
|
msg: e.to_string(),
|
||||||
|
span: Some(file_name.span),
|
||||||
|
help: None,
|
||||||
|
inner: vec![],
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let file_value = Value::string(format!("saved {:?}", &file_name.item), file_name.span);
|
||||||
|
|
||||||
|
Ok(PipelineData::Value(
|
||||||
|
Value::list(vec![file_value], call.head),
|
||||||
|
None,
|
||||||
|
))
|
||||||
|
}
|
@ -32,8 +32,7 @@ impl Command for ToCSV {
|
|||||||
Some('d'),
|
Some('d'),
|
||||||
)
|
)
|
||||||
.switch("no-header", "Indicates if file doesn't have header", None)
|
.switch("no-header", "Indicates if file doesn't have header", None)
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(Type::Custom("dataframe".into()), Type::Any)
|
||||||
.output_type(Type::Any)
|
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,7 +45,7 @@ impl Command for ToCSV {
|
|||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Saves dataframe to CSV file using other delimiter",
|
description: "Saves dataframe to CSV file using other delimiter",
|
||||||
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr to-csv test.csv -d '|'",
|
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr to-csv test.csv --delimiter '|'",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
@ -75,66 +74,58 @@ fn command(
|
|||||||
|
|
||||||
let mut df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
let mut df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
||||||
|
|
||||||
let mut file = File::create(&file_name.item).map_err(|e| {
|
let mut file = File::create(&file_name.item).map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error with file name".into(),
|
||||||
"Error with file name".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(file_name.span),
|
||||||
Some(file_name.span),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let writer = CsvWriter::new(&mut file);
|
let writer = CsvWriter::new(&mut file);
|
||||||
|
|
||||||
let writer = if no_header {
|
let writer = if no_header {
|
||||||
writer.has_header(false)
|
writer.include_header(false)
|
||||||
} else {
|
} else {
|
||||||
writer.has_header(true)
|
writer.include_header(true)
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut writer = match delimiter {
|
let mut writer = match delimiter {
|
||||||
None => writer,
|
None => writer,
|
||||||
Some(d) => {
|
Some(d) => {
|
||||||
if d.item.len() != 1 {
|
if d.item.len() != 1 {
|
||||||
return Err(ShellError::GenericError(
|
return Err(ShellError::GenericError {
|
||||||
"Incorrect delimiter".into(),
|
error: "Incorrect delimiter".into(),
|
||||||
"Delimiter has to be one char".into(),
|
msg: "Delimiter has to be one char".into(),
|
||||||
Some(d.span),
|
span: Some(d.span),
|
||||||
None,
|
help: None,
|
||||||
Vec::new(),
|
inner: vec![],
|
||||||
));
|
});
|
||||||
} else {
|
} else {
|
||||||
let delimiter = match d.item.chars().next() {
|
let delimiter = match d.item.chars().next() {
|
||||||
Some(d) => d as u8,
|
Some(d) => d as u8,
|
||||||
None => unreachable!(),
|
None => unreachable!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
writer.with_delimiter(delimiter)
|
writer.with_separator(delimiter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
writer.finish(df.as_mut()).map_err(|e| {
|
writer
|
||||||
ShellError::GenericError(
|
.finish(df.as_mut())
|
||||||
"Error writing to file".into(),
|
.map_err(|e| ShellError::GenericError {
|
||||||
e.to_string(),
|
error: "Error writing to file".into(),
|
||||||
Some(file_name.span),
|
msg: e.to_string(),
|
||||||
None,
|
span: Some(file_name.span),
|
||||||
Vec::new(),
|
help: None,
|
||||||
)
|
inner: vec![],
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let file_value = Value::String {
|
let file_value = Value::string(format!("saved {:?}", &file_name.item), file_name.span);
|
||||||
val: format!("saved {:?}", &file_name.item),
|
|
||||||
span: file_name.span,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(PipelineData::Value(
|
Ok(PipelineData::Value(
|
||||||
Value::List {
|
Value::list(vec![file_value], call.head),
|
||||||
vals: vec![file_value],
|
|
||||||
span: call.head,
|
|
||||||
},
|
|
||||||
None,
|
None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -20,8 +20,7 @@ impl Command for ToDataFrame {
|
|||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.input_type(Type::Any)
|
.input_output_type(Type::Any, Type::Custom("dataframe".into()))
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,8 +25,7 @@ impl Command for ToJsonLines {
|
|||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.required("file", SyntaxShape::Filepath, "file path to save dataframe")
|
.required("file", SyntaxShape::Filepath, "file path to save dataframe")
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(Type::Custom("dataframe".into()), Type::Any)
|
||||||
.output_type(Type::Any)
|
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,39 +58,29 @@ fn command(
|
|||||||
|
|
||||||
let mut df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
let mut df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
||||||
|
|
||||||
let file = File::create(&file_name.item).map_err(|e| {
|
let file = File::create(&file_name.item).map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error with file name".into(),
|
||||||
"Error with file name".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(file_name.span),
|
||||||
Some(file_name.span),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?;
|
})?;
|
||||||
let buf_writer = BufWriter::new(file);
|
let buf_writer = BufWriter::new(file);
|
||||||
|
|
||||||
JsonWriter::new(buf_writer)
|
JsonWriter::new(buf_writer)
|
||||||
.finish(df.as_mut())
|
.finish(df.as_mut())
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error saving file".into(),
|
||||||
"Error saving file".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(file_name.span),
|
||||||
Some(file_name.span),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let file_value = Value::String {
|
let file_value = Value::string(format!("saved {:?}", &file_name.item), file_name.span);
|
||||||
val: format!("saved {:?}", &file_name.item),
|
|
||||||
span: file_name.span,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(PipelineData::Value(
|
Ok(PipelineData::Value(
|
||||||
Value::List {
|
Value::list(vec![file_value], call.head),
|
||||||
vals: vec![file_value],
|
|
||||||
span: call.head,
|
|
||||||
},
|
|
||||||
None,
|
None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,11 @@ use nu_engine::CallExt;
|
|||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
ast::Call,
|
ast::Call,
|
||||||
engine::{Command, EngineState, Stack},
|
engine::{Command, EngineState, Stack},
|
||||||
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
record, Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::dataframe::values::NuExpression;
|
||||||
|
|
||||||
use super::super::values::NuDataFrame;
|
use super::super::values::NuDataFrame;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@ -16,7 +18,7 @@ impl Command for ToNu {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
"Converts a section of the dataframe into nushell Table."
|
"Converts a dataframe or an expression into into nushell value for access and exploration."
|
||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
@ -28,45 +30,49 @@ impl Command for ToNu {
|
|||||||
Some('n'),
|
Some('n'),
|
||||||
)
|
)
|
||||||
.switch("tail", "shows tail rows", Some('t'))
|
.switch("tail", "shows tail rows", Some('t'))
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_types(vec![
|
||||||
.output_type(Type::Any)
|
(Type::Custom("expression".into()), Type::Any),
|
||||||
|
(Type::Custom("dataframe".into()), Type::Table(vec![])),
|
||||||
|
])
|
||||||
|
//.input_output_type(Type::Any, Type::Any)
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
let cols = vec!["index".into(), "a".into(), "b".into()];
|
let rec_1 = Value::test_record(record! {
|
||||||
let rec_1 = Value::Record {
|
"index" => Value::test_int(0),
|
||||||
cols: cols.clone(),
|
"a" => Value::test_int(1),
|
||||||
vals: vec![Value::test_int(0), Value::test_int(1), Value::test_int(2)],
|
"b" => Value::test_int(2),
|
||||||
span: Span::test_data(),
|
});
|
||||||
};
|
let rec_2 = Value::test_record(record! {
|
||||||
let rec_2 = Value::Record {
|
"index" => Value::test_int(1),
|
||||||
cols: cols.clone(),
|
"a" => Value::test_int(3),
|
||||||
vals: vec![Value::test_int(1), Value::test_int(3), Value::test_int(4)],
|
"b" => Value::test_int(4),
|
||||||
span: Span::test_data(),
|
});
|
||||||
};
|
let rec_3 = Value::test_record(record! {
|
||||||
let rec_3 = Value::Record {
|
"index" => Value::test_int(2),
|
||||||
cols,
|
"a" => Value::test_int(3),
|
||||||
vals: vec![Value::test_int(2), Value::test_int(3), Value::test_int(4)],
|
"b" => Value::test_int(4),
|
||||||
span: Span::test_data(),
|
});
|
||||||
};
|
|
||||||
|
|
||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
description: "Shows head rows from dataframe",
|
description: "Shows head rows from dataframe",
|
||||||
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr into-nu",
|
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr into-nu",
|
||||||
result: Some(Value::List {
|
result: Some(Value::list(vec![rec_1, rec_2], Span::test_data())),
|
||||||
vals: vec![rec_1, rec_2],
|
|
||||||
span: Span::test_data(),
|
|
||||||
}),
|
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Shows tail rows from dataframe",
|
description: "Shows tail rows from dataframe",
|
||||||
example: "[[a b]; [1 2] [5 6] [3 4]] | dfr into-df | dfr into-nu -t -n 1",
|
example: "[[a b]; [1 2] [5 6] [3 4]] | dfr into-df | dfr into-nu --tail --rows 1",
|
||||||
result: Some(Value::List {
|
result: Some(Value::list(vec![rec_3], Span::test_data())),
|
||||||
vals: vec![rec_3],
|
},
|
||||||
span: Span::test_data(),
|
Example {
|
||||||
}),
|
description: "Convert a col expression into a nushell value",
|
||||||
|
example: "dfr col a | dfr into-nu",
|
||||||
|
result: Some(Value::test_record(record! {
|
||||||
|
"expr" => Value::test_string("column"),
|
||||||
|
"value" => Value::test_string("a"),
|
||||||
|
})),
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@ -78,20 +84,25 @@ impl Command for ToNu {
|
|||||||
call: &Call,
|
call: &Call,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
command(engine_state, stack, call, input)
|
let value = input.into_value(call.head);
|
||||||
|
if NuDataFrame::can_downcast(&value) {
|
||||||
|
dataframe_command(engine_state, stack, call, value)
|
||||||
|
} else {
|
||||||
|
expression_command(call, value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn command(
|
fn dataframe_command(
|
||||||
engine_state: &EngineState,
|
engine_state: &EngineState,
|
||||||
stack: &mut Stack,
|
stack: &mut Stack,
|
||||||
call: &Call,
|
call: &Call,
|
||||||
input: PipelineData,
|
input: Value,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let rows: Option<usize> = call.get_flag(engine_state, stack, "rows")?;
|
let rows: Option<usize> = call.get_flag(engine_state, stack, "rows")?;
|
||||||
let tail: bool = call.has_flag("tail");
|
let tail: bool = call.has_flag("tail");
|
||||||
|
|
||||||
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
let df = NuDataFrame::try_from_value(input)?;
|
||||||
|
|
||||||
let values = if tail {
|
let values = if tail {
|
||||||
df.tail(rows, call.head)?
|
df.tail(rows, call.head)?
|
||||||
@ -104,21 +115,30 @@ fn command(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let value = Value::List {
|
let value = Value::list(values, call.head);
|
||||||
vals: values,
|
|
||||||
span: call.head,
|
Ok(PipelineData::Value(value, None))
|
||||||
};
|
}
|
||||||
|
fn expression_command(call: &Call, input: Value) -> Result<PipelineData, ShellError> {
|
||||||
|
let expr = NuExpression::try_from_value(input)?;
|
||||||
|
let value = expr.to_value(call.head)?;
|
||||||
|
|
||||||
Ok(PipelineData::Value(value, None))
|
Ok(PipelineData::Value(value, None))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
use super::super::super::expressions::ExprCol;
|
||||||
use super::super::super::test_dataframe::test_dataframe;
|
use super::super::super::test_dataframe::test_dataframe;
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_examples() {
|
fn test_examples_dataframe_input() {
|
||||||
test_dataframe(vec![Box::new(ToNu {})])
|
test_dataframe(vec![Box::new(ToNu {})])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_examples_expression_input() {
|
||||||
|
test_dataframe(vec![Box::new(ToNu {}), Box::new(ExprCol {})])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,8 +25,7 @@ impl Command for ToParquet {
|
|||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.required("file", SyntaxShape::Filepath, "file path to save dataframe")
|
.required("file", SyntaxShape::Filepath, "file path to save dataframe")
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(Type::Custom("dataframe".into()), Type::Any)
|
||||||
.output_type(Type::Any)
|
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,36 +58,28 @@ fn command(
|
|||||||
|
|
||||||
let mut df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
let mut df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
||||||
|
|
||||||
let file = File::create(&file_name.item).map_err(|e| {
|
let file = File::create(&file_name.item).map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error with file name".into(),
|
||||||
"Error with file name".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(file_name.span),
|
||||||
Some(file_name.span),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
ParquetWriter::new(file).finish(df.as_mut()).map_err(|e| {
|
ParquetWriter::new(file)
|
||||||
ShellError::GenericError(
|
.finish(df.as_mut())
|
||||||
"Error saving file".into(),
|
.map_err(|e| ShellError::GenericError {
|
||||||
e.to_string(),
|
error: "Error saving file".into(),
|
||||||
Some(file_name.span),
|
msg: e.to_string(),
|
||||||
None,
|
span: Some(file_name.span),
|
||||||
Vec::new(),
|
help: None,
|
||||||
)
|
inner: vec![],
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let file_value = Value::String {
|
let file_value = Value::string(format!("saved {:?}", &file_name.item), file_name.span);
|
||||||
val: format!("saved {:?}", &file_name.item),
|
|
||||||
span: file_name.span,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(PipelineData::Value(
|
Ok(PipelineData::Value(
|
||||||
Value::List {
|
Value::list(vec![file_value], call.head),
|
||||||
vals: vec![file_value],
|
|
||||||
span: call.head,
|
|
||||||
},
|
|
||||||
None,
|
None,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -27,8 +27,10 @@ impl Command for WithColumn {
|
|||||||
SyntaxShape::Any,
|
SyntaxShape::Any,
|
||||||
"series to be added or expressions used to define the new columns",
|
"series to be added or expressions used to define the new columns",
|
||||||
)
|
)
|
||||||
.input_type(Type::Custom("dataframe".into()))
|
.input_output_type(
|
||||||
.output_type(Type::Custom("dataframe".into()))
|
Type::Custom("dataframe".into()),
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
)
|
||||||
.category(Category::Custom("dataframe or lazyframe".into()))
|
.category(Category::Custom("dataframe or lazyframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,7 +114,7 @@ impl Command for WithColumn {
|
|||||||
Err(ShellError::CantConvert {
|
Err(ShellError::CantConvert {
|
||||||
to_type: "lazy or eager dataframe".into(),
|
to_type: "lazy or eager dataframe".into(),
|
||||||
from_type: value.get_type().to_string(),
|
from_type: value.get_type().to_string(),
|
||||||
span: value.span()?,
|
span: value.span(),
|
||||||
help: None,
|
help: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -126,14 +128,11 @@ fn command_eager(
|
|||||||
mut df: NuDataFrame,
|
mut df: NuDataFrame,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let new_column: Value = call.req(engine_state, stack, 0)?;
|
let new_column: Value = call.req(engine_state, stack, 0)?;
|
||||||
let column_span = new_column.span()?;
|
let column_span = new_column.span();
|
||||||
|
|
||||||
if NuExpression::can_downcast(&new_column) {
|
if NuExpression::can_downcast(&new_column) {
|
||||||
let vals: Vec<Value> = call.rest(engine_state, stack, 0)?;
|
let vals: Vec<Value> = call.rest(engine_state, stack, 0)?;
|
||||||
let value = Value::List {
|
let value = Value::list(vals, call.head);
|
||||||
vals,
|
|
||||||
span: call.head,
|
|
||||||
};
|
|
||||||
let expressions = NuExpression::extract_exprs(value)?;
|
let expressions = NuExpression::extract_exprs(value)?;
|
||||||
let lazy = NuLazyFrame::new(true, df.lazy().with_columns(&expressions));
|
let lazy = NuLazyFrame::new(true, df.lazy().with_columns(&expressions));
|
||||||
|
|
||||||
@ -152,14 +151,12 @@ fn command_eager(
|
|||||||
|
|
||||||
df.as_mut()
|
df.as_mut()
|
||||||
.with_column(series)
|
.with_column(series)
|
||||||
.map_err(|e| {
|
.map_err(|e| ShellError::GenericError {
|
||||||
ShellError::GenericError(
|
error: "Error adding column to dataframe".into(),
|
||||||
"Error adding column to dataframe".into(),
|
msg: e.to_string(),
|
||||||
e.to_string(),
|
span: Some(column_span),
|
||||||
Some(column_span),
|
help: None,
|
||||||
None,
|
inner: vec![],
|
||||||
Vec::new(),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.map(|df| {
|
.map(|df| {
|
||||||
PipelineData::Value(
|
PipelineData::Value(
|
||||||
@ -177,10 +174,7 @@ fn command_lazy(
|
|||||||
lazy: NuLazyFrame,
|
lazy: NuLazyFrame,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let vals: Vec<Value> = call.rest(engine_state, stack, 0)?;
|
let vals: Vec<Value> = call.rest(engine_state, stack, 0)?;
|
||||||
let value = Value::List {
|
let value = Value::list(vals, call.head);
|
||||||
vals,
|
|
||||||
span: call.head,
|
|
||||||
};
|
|
||||||
let expressions = NuExpression::extract_exprs(value)?;
|
let expressions = NuExpression::extract_exprs(value)?;
|
||||||
|
|
||||||
let lazy: NuLazyFrame = lazy.into_polars().with_columns(&expressions).into();
|
let lazy: NuLazyFrame = lazy.into_polars().with_columns(&expressions).into();
|
||||||
|
@ -4,7 +4,7 @@ use nu_engine::CallExt;
|
|||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
ast::Call,
|
ast::Call,
|
||||||
engine::{Command, EngineState, Stack},
|
engine::{Command, EngineState, Stack},
|
||||||
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
record, Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@ -26,8 +26,10 @@ impl Command for ExprAlias {
|
|||||||
SyntaxShape::String,
|
SyntaxShape::String,
|
||||||
"Alias name for the expression",
|
"Alias name for the expression",
|
||||||
)
|
)
|
||||||
.input_type(Type::Custom("expression".into()))
|
.input_output_type(
|
||||||
.output_type(Type::Custom("expression".into()))
|
Type::Custom("expression".into()),
|
||||||
|
Type::Custom("expression".into()),
|
||||||
|
)
|
||||||
.category(Category::Custom("expression".into()))
|
.category(Category::Custom("expression".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,23 +38,13 @@ impl Command for ExprAlias {
|
|||||||
description: "Creates and alias expression",
|
description: "Creates and alias expression",
|
||||||
example: "dfr col a | dfr as new_a | dfr into-nu",
|
example: "dfr col a | dfr as new_a | dfr into-nu",
|
||||||
result: {
|
result: {
|
||||||
let cols = vec!["expr".into(), "value".into()];
|
let record = Value::test_record(record! {
|
||||||
let expr = Value::test_string("column");
|
"expr" => Value::test_record(record! {
|
||||||
let value = Value::test_string("a");
|
"expr" => Value::test_string("column"),
|
||||||
let expr = Value::Record {
|
"value" => Value::test_string("a"),
|
||||||
cols,
|
}),
|
||||||
vals: vec![expr, value],
|
"alias" => Value::test_string("new_a"),
|
||||||
span: Span::test_data(),
|
});
|
||||||
};
|
|
||||||
|
|
||||||
let cols = vec!["expr".into(), "alias".into()];
|
|
||||||
let value = Value::test_string("new_a");
|
|
||||||
|
|
||||||
let record = Value::Record {
|
|
||||||
cols,
|
|
||||||
vals: vec![expr, value],
|
|
||||||
span: Span::test_data(),
|
|
||||||
};
|
|
||||||
|
|
||||||
Some(record)
|
Some(record)
|
||||||
},
|
},
|
||||||
@ -86,7 +78,7 @@ impl Command for ExprAlias {
|
|||||||
mod test {
|
mod test {
|
||||||
use super::super::super::test_dataframe::test_dataframe;
|
use super::super::super::test_dataframe::test_dataframe;
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::dataframe::expressions::ExprAsNu;
|
use crate::dataframe::eager::ToNu;
|
||||||
use crate::dataframe::expressions::ExprCol;
|
use crate::dataframe::expressions::ExprCol;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -94,7 +86,7 @@ mod test {
|
|||||||
test_dataframe(vec![
|
test_dataframe(vec![
|
||||||
Box::new(ExprAlias {}),
|
Box::new(ExprAlias {}),
|
||||||
Box::new(ExprCol {}),
|
Box::new(ExprCol {}),
|
||||||
Box::new(ExprAsNu {}),
|
Box::new(ToNu {}),
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,8 +22,7 @@ impl Command for ExprArgWhere {
|
|||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.required("column name", SyntaxShape::Any, "Expression to evaluate")
|
.required("column name", SyntaxShape::Any, "Expression to evaluate")
|
||||||
.input_type(Type::Any)
|
.input_output_type(Type::Any, Type::Custom("expression".into()))
|
||||||
.output_type(Type::Custom("expression".into()))
|
|
||||||
.category(Category::Custom("expression".into()))
|
.category(Category::Custom("expression".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,68 +0,0 @@
|
|||||||
use super::super::values::NuExpression;
|
|
||||||
|
|
||||||
use nu_protocol::{
|
|
||||||
ast::Call,
|
|
||||||
engine::{Command, EngineState, Stack},
|
|
||||||
Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct ExprAsNu;
|
|
||||||
|
|
||||||
impl Command for ExprAsNu {
|
|
||||||
fn name(&self) -> &str {
|
|
||||||
"dfr into-nu"
|
|
||||||
}
|
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
|
||||||
"Convert expression into a nu value for access and exploration."
|
|
||||||
}
|
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
|
||||||
Signature::build(self.name())
|
|
||||||
.input_type(Type::Custom("expression".into()))
|
|
||||||
.output_type(Type::Any)
|
|
||||||
.category(Category::Custom("expression".into()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
|
||||||
vec![Example {
|
|
||||||
description: "Convert a col expression into a nushell value",
|
|
||||||
example: "dfr col a | dfr into-nu",
|
|
||||||
result: Some(Value::Record {
|
|
||||||
cols: vec!["expr".into(), "value".into()],
|
|
||||||
vals: vec![Value::test_string("column"), Value::test_string("a")],
|
|
||||||
span: Span::test_data(),
|
|
||||||
}),
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
|
||||||
vec!["convert", "conversion"]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run(
|
|
||||||
&self,
|
|
||||||
_engine_state: &EngineState,
|
|
||||||
_stack: &mut Stack,
|
|
||||||
call: &Call,
|
|
||||||
input: PipelineData,
|
|
||||||
) -> Result<PipelineData, ShellError> {
|
|
||||||
let expr = NuExpression::try_from_pipeline(input, call.head)?;
|
|
||||||
let value = expr.to_value(call.head);
|
|
||||||
|
|
||||||
Ok(PipelineData::Value(value, None))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use super::super::super::test_dataframe::test_dataframe;
|
|
||||||
use super::super::ExprCol;
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_examples() {
|
|
||||||
test_dataframe(vec![Box::new(ExprAsNu {}), Box::new(ExprCol {})])
|
|
||||||
}
|
|
||||||
}
|
|
@ -3,7 +3,7 @@ use nu_engine::CallExt;
|
|||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
ast::Call,
|
ast::Call,
|
||||||
engine::{Command, EngineState, Stack},
|
engine::{Command, EngineState, Stack},
|
||||||
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
record, Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
|
||||||
};
|
};
|
||||||
use polars::prelude::col;
|
use polars::prelude::col;
|
||||||
|
|
||||||
@ -26,8 +26,7 @@ impl Command for ExprCol {
|
|||||||
SyntaxShape::String,
|
SyntaxShape::String,
|
||||||
"Name of column to be used",
|
"Name of column to be used",
|
||||||
)
|
)
|
||||||
.input_type(Type::Any)
|
.input_output_type(Type::Any, Type::Custom("expression".into()))
|
||||||
.output_type(Type::Custom("expression".into()))
|
|
||||||
.category(Category::Custom("expression".into()))
|
.category(Category::Custom("expression".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,11 +34,10 @@ impl Command for ExprCol {
|
|||||||
vec![Example {
|
vec![Example {
|
||||||
description: "Creates a named column expression and converts it to a nu object",
|
description: "Creates a named column expression and converts it to a nu object",
|
||||||
example: "dfr col a | dfr into-nu",
|
example: "dfr col a | dfr into-nu",
|
||||||
result: Some(Value::Record {
|
result: Some(Value::test_record(record! {
|
||||||
cols: vec!["expr".into(), "value".into()],
|
"expr" => Value::test_string("column"),
|
||||||
vals: vec![Value::test_string("column"), Value::test_string("a")],
|
"value" => Value::test_string("a"),
|
||||||
span: Span::test_data(),
|
})),
|
||||||
}),
|
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,10 +63,10 @@ impl Command for ExprCol {
|
|||||||
mod test {
|
mod test {
|
||||||
use super::super::super::test_dataframe::test_dataframe;
|
use super::super::super::test_dataframe::test_dataframe;
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::dataframe::expressions::as_nu::ExprAsNu;
|
use crate::dataframe::eager::ToNu;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_examples() {
|
fn test_examples() {
|
||||||
test_dataframe(vec![Box::new(ExprCol {}), Box::new(ExprAsNu {})])
|
test_dataframe(vec![Box::new(ExprCol {}), Box::new(ToNu {})])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,8 +31,7 @@ impl Command for ExprConcatStr {
|
|||||||
SyntaxShape::List(Box::new(SyntaxShape::Any)),
|
SyntaxShape::List(Box::new(SyntaxShape::Any)),
|
||||||
"Expression(s) that define the string concatenation",
|
"Expression(s) that define the string concatenation",
|
||||||
)
|
)
|
||||||
.input_type(Type::Any)
|
.input_output_type(Type::Any, Type::Custom("expression".into()))
|
||||||
.output_type(Type::Custom("expression".into()))
|
|
||||||
.category(Category::Custom("expression".into()))
|
.category(Category::Custom("expression".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user