Skip to main content

nu_utils/
utils.rs

1#[cfg(windows)]
2use crossterm_winapi::{ConsoleMode, Handle};
3use lscolors::LsColors;
4use std::io::{self, Result, Write};
5
6pub fn enable_vt_processing() -> Result<()> {
7    #[cfg(windows)]
8    {
9        let console_out_mode = ConsoleMode::from(Handle::current_out_handle()?);
10        let old_out_mode = console_out_mode.mode()?;
11        let console_in_mode = ConsoleMode::from(Handle::current_in_handle()?);
12        let old_in_mode = console_in_mode.mode()?;
13
14        enable_vt_processing_input(console_in_mode, old_in_mode)?;
15        enable_vt_processing_output(console_out_mode, old_out_mode)?;
16    }
17    Ok(())
18}
19
20#[cfg(windows)]
21fn enable_vt_processing_input(console_in_mode: ConsoleMode, mode: u32) -> Result<()> {
22    //
23    // Input Mode flags:
24    //
25    // #define ENABLE_PROCESSED_INPUT              0x0001
26    // #define ENABLE_LINE_INPUT                   0x0002
27    // #define ENABLE_ECHO_INPUT                   0x0004
28    // #define ENABLE_WINDOW_INPUT                 0x0008
29    // #define ENABLE_MOUSE_INPUT                  0x0010
30    // #define ENABLE_INSERT_MODE                  0x0020
31    // #define ENABLE_QUICK_EDIT_MODE              0x0040
32    // #define ENABLE_EXTENDED_FLAGS               0x0080
33    // #define ENABLE_AUTO_POSITION                0x0100
34    // #define ENABLE_VIRTUAL_TERMINAL_INPUT       0x0200
35
36    const ENABLE_PROCESSED_INPUT: u32 = 0x0001;
37    const ENABLE_LINE_INPUT: u32 = 0x0002;
38    const ENABLE_ECHO_INPUT: u32 = 0x0004;
39    const ENABLE_VIRTUAL_TERMINAL_INPUT: u32 = 0x0200;
40
41    console_in_mode.set_mode(
42        mode | ENABLE_VIRTUAL_TERMINAL_INPUT
43            & ENABLE_ECHO_INPUT
44            & ENABLE_LINE_INPUT
45            & ENABLE_PROCESSED_INPUT,
46    )
47}
48
49#[cfg(windows)]
50fn enable_vt_processing_output(console_out_mode: ConsoleMode, mode: u32) -> Result<()> {
51    //
52    // Output Mode flags:
53    //
54    // #define ENABLE_PROCESSED_OUTPUT             0x0001
55    // #define ENABLE_WRAP_AT_EOL_OUTPUT           0x0002
56    // #define ENABLE_VIRTUAL_TERMINAL_PROCESSING  0x0004
57    // #define DISABLE_NEWLINE_AUTO_RETURN         0x0008
58    // #define ENABLE_LVB_GRID_WORLDWIDE           0x0010
59
60    pub const ENABLE_PROCESSED_OUTPUT: u32 = 0x0001;
61    pub const ENABLE_VIRTUAL_TERMINAL_PROCESSING: u32 = 0x0004;
62
63    console_out_mode.set_mode(mode | ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
64}
65
66pub fn stdout_write_all_and_flush<T>(output: T) -> Result<()>
67where
68    T: AsRef<[u8]>,
69{
70    let stdout = std::io::stdout();
71    stdout.lock().write_all(output.as_ref())?;
72    stdout.lock().flush()
73}
74
75pub fn stderr_write_all_and_flush<T>(output: T) -> Result<()>
76where
77    T: AsRef<[u8]>,
78{
79    let stderr = std::io::stderr();
80
81    match stderr.lock().write_all(output.as_ref()) {
82        Ok(_) => Ok(stderr.lock().flush()?),
83        Err(err) => Err(err),
84    }
85}
86
87pub fn get_ls_colors(lscolors_env_string: Option<String>) -> LsColors {
88    if let Some(s) = lscolors_env_string {
89        LsColors::from_string(&s)
90    } else {
91        LsColors::from_string(
92            &[
93                "*~=0;38;5;243",
94                "bd=0;38;5;81;48;5;236",
95                "ca=0",
96                "cd=0;38;5;203;48;5;236",
97                "di=0;38;5;81",
98                "do=0;38;5;16;48;5;203",
99                "ex=1;38;5;203",
100                "fi=0",
101                "ln=0;38;5;203",
102                "mh=0",
103                "mi=0;38;5;16;48;5;203",
104                "no=0",
105                "or=0;38;5;16;48;5;203",
106                "ow=0",
107                "pi=0;38;5;16;48;5;81",
108                "rs=0",
109                "sg=0",
110                "so=0;38;5;16;48;5;203",
111                "st=0",
112                "su=0",
113                "tw=0",
114                "*.1=0;38;5;185",
115                "*.a=1;38;5;203",
116                "*.c=0;38;5;48",
117                "*.d=0;38;5;48",
118                "*.f=0;38;5;48",
119                "*.h=0;38;5;48",
120                "*.m=0;38;5;48",
121                "*.o=0;38;5;243",
122                "*.p=0;38;5;48",
123                "*.r=0;38;5;48",
124                "*.t=0;38;5;48",
125                "*.v=0;38;5;48",
126                "*.z=4;38;5;203",
127                "*.7z=4;38;5;203",
128                "*.ai=0;38;5;208",
129                "*.as=0;38;5;48",
130                "*.au=0;38;5;208",
131                "*.bc=0;38;5;243",
132                "*.br=4;38;5;203",
133                "*.bz=4;38;5;203",
134                "*.cc=0;38;5;48",
135                "*.cp=0;38;5;48",
136                "*.cr=0;38;5;48",
137                "*.cs=0;38;5;48",
138                "*.db=4;38;5;203",
139                "*.di=0;38;5;48",
140                "*.el=0;38;5;48",
141                "*.ex=0;38;5;48",
142                "*.fs=0;38;5;48",
143                "*.go=0;38;5;48",
144                "*.gv=0;38;5;48",
145                "*.gz=4;38;5;203",
146                "*.ha=0;38;5;48",
147                "*.hh=0;38;5;48",
148                "*.hi=0;38;5;243",
149                "*.hs=0;38;5;48",
150                "*.jl=0;38;5;48",
151                "*.js=0;38;5;48",
152                "*.ko=1;38;5;203",
153                "*.kt=0;38;5;48",
154                "*.la=0;38;5;243",
155                "*.ll=0;38;5;48",
156                "*.lo=0;38;5;243",
157                "*.lz=4;38;5;203",
158                "*.ma=0;38;5;208",
159                "*.mb=0;38;5;208",
160                "*.md=0;38;5;185",
161                "*.mk=0;38;5;149",
162                "*.ml=0;38;5;48",
163                "*.mn=0;38;5;48",
164                "*.nb=0;38;5;48",
165                "*.nu=0;38;5;48",
166                "*.pl=0;38;5;48",
167                "*.pm=0;38;5;48",
168                "*.pp=0;38;5;48",
169                "*.ps=0;38;5;186",
170                "*.py=0;38;5;48",
171                "*.rb=0;38;5;48",
172                "*.rc=0;38;5;149",
173                "*.rm=0;38;5;208",
174                "*.rs=0;38;5;48",
175                "*.sh=0;38;5;48",
176                "*.so=1;38;5;203",
177                "*.sz=4;38;5;203",
178                "*.td=0;38;5;48",
179                "*.tf=0;38;5;48",
180                "*.ts=0;38;5;48",
181                "*.ui=0;38;5;149",
182                "*.vb=0;38;5;48",
183                "*.wv=0;38;5;208",
184                "*.xz=4;38;5;203",
185                "*.zz=4;38;5;203",
186                "*FAQ=0;38;5;16;48;5;186",
187                "*.3ds=0;38;5;208",
188                "*.3fr=0;38;5;208",
189                "*.3g2=0;38;5;208",
190                "*.3ga=0;38;5;208",
191                "*.3gp=0;38;5;208",
192                "*.3mf=0;38;5;208",
193                "*.aac=0;38;5;208",
194                "*.adb=0;38;5;48",
195                "*.ads=0;38;5;48",
196                "*.afm=0;38;5;208",
197                "*.aif=0;38;5;208",
198                "*.amf=0;38;5;208",
199                "*.amr=0;38;5;208",
200                "*.ape=0;38;5;208",
201                "*.apk=4;38;5;203",
202                "*.ari=0;38;5;208",
203                "*.arj=4;38;5;203",
204                "*.arw=0;38;5;208",
205                "*.asa=0;38;5;48",
206                "*.ase=0;38;5;208",
207                "*.asf=0;38;5;208",
208                "*.asm=0;38;5;48",
209                "*.aux=0;38;5;243",
210                "*.avi=0;38;5;208",
211                "*.awk=0;38;5;48",
212                "*.azw=0;38;5;186",
213                "*.bag=4;38;5;203",
214                "*.bak=0;38;5;243",
215                "*.bat=1;38;5;203",
216                "*.bay=0;38;5;208",
217                "*.bbl=0;38;5;243",
218                "*.bcf=0;38;5;243",
219                "*.bib=0;38;5;149",
220                "*.bin=4;38;5;203",
221                "*.blg=0;38;5;243",
222                "*.bmp=0;38;5;208",
223                "*.bsh=0;38;5;48",
224                "*.bsp=4;38;5;203",
225                "*.bst=0;38;5;149",
226                "*.bz2=4;38;5;203",
227                "*.bzl=0;38;5;149",
228                "*.c++=0;38;5;48",
229                "*.cab=4;38;5;203",
230                "*.caf=0;38;5;208",
231                "*.cap=0;38;5;208",
232                "*.cbr=0;38;5;186",
233                "*.cbz=0;38;5;186",
234                "*.cda=0;38;5;208",
235                "*.cfg=0;38;5;149",
236                "*.cgi=0;38;5;48",
237                "*.cjs=0;38;5;48",
238                "*.clj=0;38;5;48",
239                "*.com=1;38;5;203",
240                "*.cpp=0;38;5;48",
241                "*.cr2=0;38;5;208",
242                "*.cr3=0;38;5;208",
243                "*.crw=0;38;5;208",
244                "*.crx=4;38;5;203",
245                "*.css=0;38;5;48",
246                "*.csv=0;38;5;186",
247                "*.csx=0;38;5;48",
248                "*.cxx=0;38;5;48",
249                "*.dae=0;38;5;208",
250                "*.dat=0;38;5;208",
251                "*.dcr=0;38;5;208",
252                "*.dcs=0;38;5;208",
253                "*.deb=4;38;5;203",
254                "*.def=0;38;5;48",
255                "*.dll=1;38;5;203",
256                "*.dmg=4;38;5;203",
257                "*.dmp=0;38;5;243",
258                "*.dng=0;38;5;208",
259                "*.doc=0;38;5;186",
260                "*.dot=0;38;5;48",
261                "*.dox=0;38;5;149",
262                "*.dpr=0;38;5;48",
263                "*.drf=0;38;5;208",
264                "*.dts=0;38;5;208",
265                "*.dwg=0;38;5;208",
266                "*.dxf=0;38;5;208",
267                "*.ear=4;38;5;203",
268                "*.eip=0;38;5;208",
269                "*.elc=0;38;5;48",
270                "*.elm=0;38;5;48",
271                "*.eln=0;38;5;48",
272                "*.eml=0;38;5;186",
273                "*.epp=0;38;5;48",
274                "*.eps=0;38;5;208",
275                "*.erf=0;38;5;208",
276                "*.erl=0;38;5;48",
277                "*.etx=0;38;5;185",
278                "*.exe=1;38;5;203",
279                "*.exr=0;38;5;208",
280                "*.exs=0;38;5;48",
281                "*.f4v=0;38;5;208",
282                "*.fb2=0;38;5;186",
283                "*.fbx=0;38;5;208",
284                "*.fcm=0;38;5;208",
285                "*.fff=0;38;5;208",
286                "*.fls=0;38;5;243",
287                "*.flv=0;38;5;208",
288                "*.fnt=0;38;5;208",
289                "*.fon=0;38;5;208",
290                "*.for=0;38;5;48",
291                "*.fsi=0;38;5;48",
292                "*.fsx=0;38;5;48",
293                "*.fvd=4;38;5;203",
294                "*.gif=0;38;5;208",
295                "*.git=0;38;5;243",
296                "*.gp3=0;38;5;208",
297                "*.gp4=0;38;5;208",
298                "*.gpr=0;38;5;208",
299                "*.gvy=0;38;5;48",
300                "*.h++=0;38;5;48",
301                "*.hda=0;38;5;208",
302                "*.hip=0;38;5;208",
303                "*.hpp=0;38;5;48",
304                "*.htc=0;38;5;48",
305                "*.htm=0;38;5;185",
306                "*.hxx=0;38;5;48",
307                "*.ico=0;38;5;208",
308                "*.ics=0;38;5;186",
309                "*.idx=0;38;5;243",
310                "*.igs=0;38;5;208",
311                "*.iiq=0;38;5;208",
312                "*.ilg=0;38;5;243",
313                "*.img=4;38;5;203",
314                "*.inc=0;38;5;48",
315                "*.ind=0;38;5;243",
316                "*.ini=0;38;5;149",
317                "*.inl=0;38;5;48",
318                "*.ino=0;38;5;48",
319                "*.ipa=4;38;5;203",
320                "*.ipp=0;38;5;48",
321                "*.iso=4;38;5;203",
322                "*.jad=4;38;5;203",
323                "*.jar=4;38;5;203",
324                "*.jpg=0;38;5;208",
325                "*.jsx=0;38;5;48",
326                "*.jxl=0;38;5;208",
327                "*.k25=0;38;5;208",
328                "*.kdc=0;38;5;208",
329                "*.key=0;38;5;186",
330                "*.kra=0;38;5;208",
331                "*.kts=0;38;5;48",
332                "*.lhs=0;38;5;48",
333                "*.log=0;38;5;243",
334                "*.lrz=4;38;5;203",
335                "*.ltx=0;38;5;48",
336                "*.lua=0;38;5;48",
337                "*.lzo=4;38;5;203",
338                "*.m2v=0;38;5;208",
339                "*.m3u=0;38;5;208",
340                "*.m4a=0;38;5;208",
341                "*.m4v=0;38;5;208",
342                "*.mdc=0;38;5;208",
343                "*.mdx=0;38;5;185",
344                "*.mef=0;38;5;208",
345                "*.mht=4;38;5;203",
346                "*.mid=0;38;5;208",
347                "*.mir=0;38;5;48",
348                "*.mjs=0;38;5;48",
349                "*.mkd=0;38;5;185",
350                "*.mkv=0;38;5;208",
351                "*.mli=0;38;5;48",
352                "*.mly=0;38;5;48",
353                "*.mod=0;38;5;208",
354                "*.mos=0;38;5;208",
355                "*.mov=0;38;5;208",
356                "*.mp3=0;38;5;208",
357                "*.mp4=0;38;5;208",
358                "*.mpg=0;38;5;208",
359                "*.mrw=0;38;5;208",
360                "*.msg=0;38;5;186",
361                "*.msi=4;38;5;203",
362                "*.mtl=0;38;5;208",
363                "*.nef=0;38;5;208",
364                "*.nfo=0;38;5;185",
365                "*.nim=0;38;5;48",
366                "*.nix=0;38;5;149",
367                "*.nrg=4;38;5;203",
368                "*.nrw=0;38;5;208",
369                "*.obj=0;38;5;208",
370                "*.obm=0;38;5;208",
371                "*.odp=0;38;5;186",
372                "*.ods=0;38;5;186",
373                "*.odt=0;38;5;186",
374                "*.oga=0;38;5;208",
375                "*.ogg=0;38;5;208",
376                "*.ogm=0;38;5;208",
377                "*.ogv=0;38;5;208",
378                "*.orf=0;38;5;208",
379                "*.org=0;38;5;185",
380                "*.otf=0;38;5;208",
381                "*.otl=0;38;5;208",
382                "*.out=0;38;5;243",
383                "*.pak=4;38;5;203",
384                "*.pas=0;38;5;48",
385                "*.pbm=0;38;5;208",
386                "*.pcm=0;38;5;208",
387                "*.pcx=0;38;5;208",
388                "*.pdf=0;38;5;186",
389                "*.pef=0;38;5;208",
390                "*.pfa=0;38;5;208",
391                "*.pfb=0;38;5;208",
392                "*.pfm=0;38;5;208",
393                "*.pgm=0;38;5;208",
394                "*.php=0;38;5;48",
395                "*.pid=0;38;5;243",
396                "*.pk3=4;38;5;203",
397                "*.pkg=4;38;5;203",
398                "*.ply=0;38;5;208",
399                "*.png=0;38;5;208",
400                "*.pod=0;38;5;48",
401                "*.ppm=0;38;5;208",
402                "*.pps=0;38;5;186",
403                "*.ppt=0;38;5;186",
404                "*.pro=0;38;5;149",
405                "*.ps1=0;38;5;48",
406                "*.psd=0;38;5;208",
407                "*.ptx=0;38;5;208",
408                "*.pxd=0;38;5;208",
409                "*.pxm=0;38;5;208",
410                "*.pxn=0;38;5;208",
411                "*.pyc=0;38;5;243",
412                "*.pyd=0;38;5;243",
413                "*.pyo=0;38;5;243",
414                "*.qoi=0;38;5;208",
415                "*.r3d=0;38;5;208",
416                "*.raf=0;38;5;208",
417                "*.rar=4;38;5;203",
418                "*.raw=0;38;5;208",
419                "*.rpm=4;38;5;203",
420                "*.rss=0;38;5;185",
421                "*.rst=0;38;5;185",
422                "*.rtf=0;38;5;186",
423                "*.rw2=0;38;5;208",
424                "*.rwl=0;38;5;208",
425                "*.rwz=0;38;5;208",
426                "*.s3m=0;38;5;208",
427                "*.s7z=4;38;5;203",
428                "*.sbt=0;38;5;48",
429                "*.sed=0;38;5;48",
430                "*.sid=0;38;5;208",
431                "*.sql=0;38;5;48",
432                "*.sr2=0;38;5;208",
433                "*.srf=0;38;5;208",
434                "*.srw=0;38;5;208",
435                "*.stl=0;38;5;208",
436                "*.stp=0;38;5;208",
437                "*.sty=0;38;5;243",
438                "*.svg=0;38;5;208",
439                "*.swf=0;38;5;208",
440                "*.swn=0;38;5;243",
441                "*.swo=0;38;5;243",
442                "*.swp=0;38;5;243",
443                "*.sxi=0;38;5;186",
444                "*.sxw=0;38;5;186",
445                "*.tar=4;38;5;203",
446                "*.tbz=4;38;5;203",
447                "*.tcl=0;38;5;48",
448                "*.tex=0;38;5;48",
449                "*.tga=0;38;5;208",
450                "*.tgz=4;38;5;203",
451                "*.tif=0;38;5;208",
452                "*.tml=0;38;5;149",
453                "*.tmp=0;38;5;243",
454                "*.toc=0;38;5;243",
455                "*.tsv=0;38;5;186",
456                "*.tsx=0;38;5;48",
457                "*.ttf=0;38;5;208",
458                "*.txt=0;38;5;185",
459                "*.typ=0;38;5;185",
460                "*.usd=0;38;5;208",
461                "*.vba=0;38;5;48",
462                "*.vbs=0;38;5;48",
463                "*.vcd=4;38;5;203",
464                "*.vcf=0;38;5;186",
465                "*.vdf=4;38;5;203",
466                "*.vdi=4;38;5;203",
467                "*.vfd=4;38;5;203",
468                "*.vhd=4;38;5;203",
469                "*.vim=0;38;5;48",
470                "*.vob=0;38;5;208",
471                "*.vpk=4;38;5;203",
472                "*.vsh=0;38;5;48",
473                "*.war=4;38;5;203",
474                "*.wav=0;38;5;208",
475                "*.wma=0;38;5;208",
476                "*.wmv=0;38;5;208",
477                "*.wrl=0;38;5;208",
478                "*.wvc=0;38;5;208",
479                "*.x3d=0;38;5;208",
480                "*.x3f=0;38;5;208",
481                "*.xcf=0;38;5;208",
482                "*.xla=0;38;5;186",
483                "*.xlr=0;38;5;186",
484                "*.xls=0;38;5;186",
485                "*.xlt=0;38;5;186",
486                "*.xml=0;38;5;185",
487                "*.xmp=0;38;5;149",
488                "*.xpi=4;38;5;203",
489                "*.xpm=0;38;5;208",
490                "*.xsd=0;38;5;185",
491                "*.xsh=0;38;5;48",
492                "*.yml=0;38;5;149",
493                "*.zig=0;38;5;48",
494                "*.zip=4;38;5;203",
495                "*.zoo=4;38;5;203",
496                "*.zsh=0;38;5;48",
497                "*.zst=4;38;5;203",
498                "*TODO=1",
499                "*cljc=0;38;5;48",
500                "*hgrc=0;38;5;149",
501                "*mtab=0;38;5;149",
502                "*.adoc=0;38;5;185",
503                "*.agda=0;38;5;48",
504                "*.aiff=0;38;5;208",
505                "*.alac=0;38;5;208",
506                "*.avif=0;38;5;208",
507                "*.azw3=0;38;5;186",
508                "*.bash=0;38;5;48",
509                "*.braw=0;38;5;208",
510                "*.cljs=0;38;5;48",
511                "*.cljw=0;38;5;48",
512                "*.conf=0;38;5;149",
513                "*.cpio=4;38;5;203",
514                "*.dart=0;38;5;48",
515                "*.data=0;38;5;208",
516                "*.diff=0;38;5;48",
517                "*.divx=0;38;5;208",
518                "*.djvu=0;38;5;186",
519                "*.docm=0;38;5;186",
520                "*.docx=0;38;5;186",
521                "*.dump=0;38;5;243",
522                "*.epsf=0;38;5;208",
523                "*.epub=0;38;5;186",
524                "*.fish=0;38;5;48",
525                "*.flac=0;38;5;208",
526                "*.fxml=0;38;5;185",
527                "*.h264=0;38;5;208",
528                "*.hack=0;38;5;48",
529                "*.heif=0;38;5;208",
530                "*.hgrc=0;38;5;149",
531                "*.html=0;38;5;185",
532                "*.icns=0;38;5;208",
533                "*.iges=0;38;5;208",
534                "*.info=0;38;5;185",
535                "*.java=0;38;5;48",
536                "*.jhtm=0;38;5;185",
537                "*.jpeg=0;38;5;208",
538                "*.json=0;38;5;149",
539                "*.less=0;38;5;48",
540                "*.lisp=0;38;5;48",
541                "*.lock=0;38;5;243",
542                "*.lzma=4;38;5;203",
543                "*.make=0;38;5;149",
544                "*.midi=0;38;5;208",
545                "*.mobi=0;38;5;186",
546                "*.mojo=0;38;5;48",
547                "*.mp4a=0;38;5;208",
548                "*.mpeg=0;38;5;208",
549                "*.nims=0;38;5;48",
550                "*.norg=0;38;5;185",
551                "*.odin=0;38;5;48",
552                "*.opus=0;38;5;208",
553                "*.orig=0;38;5;243",
554                "*.part=0;38;5;243",
555                "*.path=0;38;5;149",
556                "*.pcap=0;38;5;243",
557                "*.ppts=0;38;5;186",
558                "*.pptx=0;38;5;186",
559                "*.prql=0;38;5;48",
560                "*.psd1=0;38;5;48",
561                "*.psm1=0;38;5;48",
562                "*.purs=0;38;5;48",
563                "*.qcow=4;38;5;203",
564                "*.rake=0;38;5;149",
565                "*.raku=0;38;5;48",
566                "*.rlib=0;38;5;243",
567                "*.rmvb=0;38;5;208",
568                "*.sass=0;38;5;48",
569                "*.scad=0;38;5;48",
570                "*.scss=0;38;5;48",
571                "*.step=0;38;5;208",
572                "*.swap=0;38;5;149",
573                "*.tbz2=4;38;5;203",
574                "*.tiff=0;38;5;208",
575                "*.toml=0;38;5;149",
576                "*.usda=0;38;5;208",
577                "*.usdc=0;38;5;208",
578                "*.usdz=0;38;5;208",
579                "*.vala=0;38;5;48",
580                "*.vapi=0;38;5;48",
581                "*.vhdx=4;38;5;203",
582                "*.vmdk=4;38;5;203",
583                "*.warc=4;38;5;203",
584                "*.webm=0;38;5;208",
585                "*.webp=0;38;5;208",
586                "*.woff=0;38;5;208",
587                "*.xbps=4;38;5;203",
588                "*.xlsm=0;38;5;186",
589                "*.xlsx=0;38;5;186",
590                "*.xltm=0;38;5;186",
591                "*.yaml=0;38;5;149",
592                "*.zipx=4;38;5;203",
593                "*.zpaq=4;38;5;203",
594                "*.zstd=4;38;5;203",
595                "*fstab=0;38;5;149",
596                "*group=0;38;5;149",
597                "*hosts=0;38;5;149",
598                "*stdin=0;38;5;243",
599                "*v.mod=0;38;5;149",
600                "*.aria2=0;38;5;243",
601                "*.bazel=0;38;5;149",
602                "*.blend=0;38;5;208",
603                "*.cabal=0;38;5;48",
604                "*.cache=0;38;5;243",
605                "*.class=0;38;5;243",
606                "*.cmake=0;38;5;149",
607                "*.ctags=0;38;5;243",
608                "*.dylib=1;38;5;203",
609                "*.dyn_o=0;38;5;243",
610                "*.gcode=0;38;5;48",
611                "*.ipynb=0;38;5;48",
612                "*.jhtml=0;38;5;185",
613                "*.lagda=0;38;5;48",
614                "*.mdown=0;38;5;185",
615                "*.mhtml=4;38;5;203",
616                "*.mount=0;38;5;149",
617                "*.pages=0;38;5;186",
618                "*.paq8n=4;38;5;203",
619                "*.paq8o=4;38;5;203",
620                "*.patch=0;38;5;48",
621                "*.plist=0;38;5;243",
622                "*.pptsm=0;38;5;186",
623                "*.pptxm=0;38;5;186",
624                "*.rmeta=0;38;5;243",
625                "*.scala=0;38;5;48",
626                "*.shtml=0;38;5;185",
627                "*.state=0;38;5;243",
628                "*.swift=0;38;5;48",
629                "*.timer=0;38;5;149",
630                "*.toast=4;38;5;203",
631                "*.vcard=0;38;5;186",
632                "*.woff2=0;38;5;208",
633                "*.xhtml=0;38;5;185",
634                "*Icon\r=0;38;5;243",
635                "*LEGACY=0;38;5;16;48;5;186",
636                "*NOTICE=0;38;5;16;48;5;186",
637                "*README=0;38;5;16;48;5;186",
638                "*go.mod=0;38;5;149",
639                "*go.sum=0;38;5;243",
640                "*mounts=0;38;5;149",
641                "*passwd=0;38;5;149",
642                "*shadow=0;38;5;149",
643                "*stderr=0;38;5;243",
644                "*stdout=0;38;5;243",
645                "*.bashrc=0;38;5;48",
646                "*.config=0;38;5;149",
647                "*.device=0;38;5;149",
648                "*.dyn_hi=0;38;5;243",
649                "*.flake8=0;38;5;149",
650                "*.github=0;38;5;243",
651                "*.gradle=0;38;5;48",
652                "*.groovy=0;38;5;48",
653                "*.ignore=0;38;5;149",
654                "*.matlab=0;38;5;48",
655                "*.nimble=0;38;5;48",
656                "*.sample=0;38;5;208",
657                "*.socket=0;38;5;149",
658                "*.target=0;38;5;149",
659                "*.tfvars=0;38;5;48",
660                "*AUTHORS=0;38;5;16;48;5;186",
661                "*CHANGES=0;38;5;16;48;5;186",
662                "*COPYING=0;38;5;249",
663                "*HISTORY=0;38;5;16;48;5;186",
664                "*INSTALL=0;38;5;16;48;5;186",
665                "*LICENCE=0;38;5;249",
666                "*LICENSE=0;38;5;249",
667                "*PATENTS=0;38;5;249",
668                "*TODO.md=1",
669                "*VERSION=0;38;5;16;48;5;186",
670                "*crontab=0;38;5;149",
671                "*uv.lock=0;38;5;243",
672                "*.alembic=0;38;5;208",
673                "*.desktop=0;38;5;149",
674                "*.gemspec=0;38;5;149",
675                "*.mailmap=0;38;5;149",
676                "*.numbers=0;38;5;186",
677                "*.profile=0;38;5;48",
678                "*.service=0;38;5;149",
679                "*.textile=0;38;5;185",
680                "*.tfstate=0;38;5;243",
681                "*Doxyfile=0;38;5;149",
682                "*Makefile=0;38;5;149",
683                "*TODO.txt=1",
684                "*crypttab=0;38;5;149",
685                "*grub.cfg=0;38;5;149",
686                "*hostname=0;38;5;149",
687                "*networks=0;38;5;149",
688                "*setup.py=0;38;5;149",
689                "*.DS_Store=0;38;5;243",
690                "*.asciidoc=0;38;5;185",
691                "*.aseprite=0;38;5;208",
692                "*.cmake.in=0;38;5;149",
693                "*.fdignore=0;38;5;149",
694                "*.kdevelop=0;38;5;149",
695                "*.lockfile=0;38;5;243",
696                "*.markdown=0;38;5;185",
697                "*.rgignore=0;38;5;149",
698                "*.snapshot=0;38;5;149",
699                "*.tfignore=0;38;5;149",
700                "*CHANGELOG=0;38;5;16;48;5;186",
701                "*COPYRIGHT=0;38;5;249",
702                "*README.md=0;38;5;16;48;5;186",
703                "*bun.lockb=0;38;5;243",
704                "*configure=0;38;5;149",
705                "*securetty=0;38;5;149",
706                "*.automount=0;38;5;149",
707                "*.gitconfig=0;38;5;149",
708                "*.gitignore=0;38;5;149",
709                "*.localized=0;38;5;243",
710                "*.scons_opt=0;38;5;243",
711                "*.terraform=0;38;5;48",
712                "*.timestamp=0;38;5;243",
713                "*CHANGES.md=0;38;5;16;48;5;186",
714                "*CODEOWNERS=0;38;5;149",
715                "*Dockerfile=0;38;5;149",
716                "*INSTALL.md=0;38;5;16;48;5;186",
717                "*README.txt=0;38;5;16;48;5;186",
718                "*SConscript=0;38;5;149",
719                "*SConstruct=0;38;5;149",
720                "*.cirrus.yml=0;38;5;186",
721                "*.gitmodules=0;38;5;149",
722                "*.synctex.gz=0;38;5;243",
723                "*.travis.yml=0;38;5;186",
724                "*CHANGES.txt=0;38;5;16;48;5;186",
725                "*INSTALL.txt=0;38;5;16;48;5;186",
726                "*LICENSE-MIT=0;38;5;249",
727                "*MANIFEST.in=0;38;5;149",
728                "*Makefile.am=0;38;5;149",
729                "*Makefile.in=0;38;5;243",
730                "*known_hosts=0;38;5;149",
731                "*poetry.lock=0;38;5;243",
732                "*.applescript=0;38;5;48",
733                "*.fdb_latexmk=0;38;5;243",
734                "*.sparseimage=4;38;5;203",
735                "*.webmanifest=0;38;5;149",
736                "*CHANGELOG.md=0;38;5;16;48;5;186",
737                "*CONTRIBUTING=0;38;5;16;48;5;186",
738                "*CONTRIBUTORS=0;38;5;16;48;5;186",
739                "*appveyor.yml=0;38;5;186",
740                "*configure.ac=0;38;5;149",
741                "*.bash_profile=0;38;5;48",
742                "*.clang-format=0;38;5;149",
743                "*.dockerignore=0;38;5;149",
744                "*.editorconfig=0;38;5;149",
745                "*CHANGELOG.txt=0;38;5;16;48;5;186",
746                "*Containerfile=0;38;5;149",
747                "*.aseprite-keys=0;38;5;185",
748                "*.gitattributes=0;38;5;149",
749                "*.gitlab-ci.yml=0;38;5;186",
750                "*CMakeCache.txt=0;38;5;243",
751                "*CMakeLists.txt=0;38;5;149",
752                "*LICENSE-APACHE=0;38;5;249",
753                "*pyproject.toml=0;38;5;149",
754                "*CODE_OF_CONDUCT=0;38;5;16;48;5;186",
755                "*CONTRIBUTING.md=0;38;5;16;48;5;186",
756                "*CONTRIBUTORS.md=0;38;5;16;48;5;186",
757                "*authorized_keys=0;38;5;149",
758                "*.containerignore=0;38;5;149",
759                "*.sconsign.dblite=0;38;5;243",
760                "*CONTRIBUTING.txt=0;38;5;16;48;5;186",
761                "*CONTRIBUTORS.txt=0;38;5;16;48;5;186",
762                "*requirements.txt=0;38;5;149",
763                "*.aseprite-brushes=0;38;5;185",
764                "*package-lock.json=0;38;5;243",
765                "*CODE_OF_CONDUCT.md=0;38;5;16;48;5;186",
766                "*.CFUserTextEncoding=0;38;5;243",
767                "*.aseprite-extension=4;38;5;203",
768                "*CODE_OF_CONDUCT.txt=0;38;5;16;48;5;186",
769                "*azure-pipelines.yml=0;38;5;186",
770            ]
771            .join(":"),
772        )
773    }
774}
775
776// Log some performance metrics (green text with yellow timings).
777// Uses a custom target "nu::perf::<module_path>" so that --log-level perf
778// can filter these logs via the simplelog target filter.
779#[macro_export]
780macro_rules! perf {
781    // `perf!(msg, start_instant, use_color)`: logs the time elapsed since `start_instant`.
782    ($msg:expr, $dur:expr, $use_color:expr) => {{
783        $crate::perf!($msg, elapsed: $dur.elapsed(), $use_color)
784    }};
785    // `perf!(msg, elapsed: duration, use_color)`: logs an already measured duration, for phases
786    // that ran before the logger existed.
787    ($msg:expr, elapsed: $elapsed:expr, $use_color:expr) => {{
788        let target = concat!("nu::perf::", module_path!());
789        if $use_color {
790            log::info!(
791                target: target,
792                "perf: {}:{}:{} \x1b[32m{}\x1b[0m took \x1b[33m{:?}\x1b[0m",
793                file!(),
794                line!(),
795                column!(),
796                $msg,
797                $elapsed,
798            );
799        } else {
800            log::info!(
801                target: target,
802                "perf: {}:{}:{} {} took {:?}",
803                file!(),
804                line!(),
805                column!(),
806                $msg,
807                $elapsed,
808            );
809        }
810    }};
811}
812
813/// Returns the terminal size (columns, rows).
814///
815/// This utility variant allows getting a fallback value when compiling for wasm32 without having
816/// to rearrange other bits of the codebase.
817///
818/// Queries the terminal attached to the process. Without one, the `COLUMNS` and `LINES`
819/// environment variables are used, and failing that the 80x24 that terminfo assumes.
820/// `crossterm::terminal::size` would spawn `tput cols` and `tput lines` at that point (which
821/// report those same values), two process spawns per call, which made every `table` and
822/// `term size` in a non-interactive script cost several milliseconds.
823pub fn terminal_size() -> io::Result<(u16, u16)> {
824    #[cfg(all(feature = "os", unix))]
825    return Ok(crossterm::terminal::window_size()
826        .map(|size| (size.columns, size.rows))
827        .ok()
828        .or_else(terminal_size_from_env)
829        .unwrap_or((80, 24)));
830
831    // The Windows query does not spawn anything.
832    #[cfg(all(feature = "os", not(unix)))]
833    return crossterm::terminal::size();
834
835    #[cfg(not(feature = "os"))]
836    return Err(io::Error::from(io::ErrorKind::Unsupported));
837}
838
839/// Terminal size from the `COLUMNS` and `LINES` environment variables, when both are set.
840#[cfg(all(feature = "os", unix))]
841fn terminal_size_from_env() -> Option<(u16, u16)> {
842    let dimension = |name: &str| {
843        std::env::var(name)
844            .ok()?
845            .trim()
846            .parse::<u16>()
847            .ok()
848            .filter(|value| *value > 0)
849    };
850    Some((dimension("COLUMNS")?, dimension("LINES")?))
851}