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    let ret = match stderr.lock().write_all(output.as_ref()) {
81        Ok(_) => Ok(stderr.lock().flush()?),
82        Err(err) => Err(err),
83    };
84
85    ret
86}
87
88// See default_files/README.md for a description of these files
89pub fn get_default_env() -> &'static str {
90    include_str!("default_files/default_env.nu")
91}
92
93pub fn get_scaffold_env() -> &'static str {
94    include_str!("default_files/scaffold_env.nu")
95}
96
97pub fn get_doc_env() -> &'static str {
98    include_str!("default_files/doc_env.nu")
99}
100
101pub fn get_default_config() -> &'static str {
102    include_str!("default_files/default_config.nu")
103}
104
105pub fn get_scaffold_config() -> &'static str {
106    include_str!("default_files/scaffold_config.nu")
107}
108
109pub fn get_doc_config() -> &'static str {
110    include_str!("default_files/doc_config.nu")
111}
112
113pub fn get_ls_colors(lscolors_env_string: Option<String>) -> LsColors {
114    if let Some(s) = lscolors_env_string {
115        LsColors::from_string(&s)
116    } else {
117        LsColors::from_string(
118            &[
119                "st=0",
120                "di=0;38;5;81",
121                "so=0;38;5;16;48;5;203",
122                "ln=0;38;5;203",
123                "cd=0;38;5;203;48;5;236",
124                "ex=1;38;5;203",
125                "or=0;38;5;16;48;5;203",
126                "fi=0",
127                "bd=0;38;5;81;48;5;236",
128                "ow=0",
129                "mi=0;38;5;16;48;5;203",
130                "*~=0;38;5;243",
131                "no=0",
132                "tw=0",
133                "pi=0;38;5;16;48;5;81",
134                "*.z=4;38;5;203",
135                "*.t=0;38;5;48",
136                "*.o=0;38;5;243",
137                "*.d=0;38;5;48",
138                "*.a=1;38;5;203",
139                "*.c=0;38;5;48",
140                "*.m=0;38;5;48",
141                "*.p=0;38;5;48",
142                "*.r=0;38;5;48",
143                "*.h=0;38;5;48",
144                "*.ml=0;38;5;48",
145                "*.ll=0;38;5;48",
146                "*.gv=0;38;5;48",
147                "*.cp=0;38;5;48",
148                "*.xz=4;38;5;203",
149                "*.hs=0;38;5;48",
150                "*css=0;38;5;48",
151                "*.ui=0;38;5;149",
152                "*.pl=0;38;5;48",
153                "*.ts=0;38;5;48",
154                "*.gz=4;38;5;203",
155                "*.so=1;38;5;203",
156                "*.cr=0;38;5;48",
157                "*.fs=0;38;5;48",
158                "*.bz=4;38;5;203",
159                "*.ko=1;38;5;203",
160                "*.as=0;38;5;48",
161                "*.sh=0;38;5;48",
162                "*.pp=0;38;5;48",
163                "*.el=0;38;5;48",
164                "*.py=0;38;5;48",
165                "*.lo=0;38;5;243",
166                "*.bc=0;38;5;243",
167                "*.cc=0;38;5;48",
168                "*.pm=0;38;5;48",
169                "*.rs=0;38;5;48",
170                "*.di=0;38;5;48",
171                "*.jl=0;38;5;48",
172                "*.rb=0;38;5;48",
173                "*.md=0;38;5;185",
174                "*.js=0;38;5;48",
175                "*.cjs=0;38;5;48",
176                "*.mjs=0;38;5;48",
177                "*.go=0;38;5;48",
178                "*.vb=0;38;5;48",
179                "*.hi=0;38;5;243",
180                "*.kt=0;38;5;48",
181                "*.hh=0;38;5;48",
182                "*.cs=0;38;5;48",
183                "*.mn=0;38;5;48",
184                "*.nb=0;38;5;48",
185                "*.7z=4;38;5;203",
186                "*.ex=0;38;5;48",
187                "*.rm=0;38;5;208",
188                "*.ps=0;38;5;186",
189                "*.td=0;38;5;48",
190                "*.la=0;38;5;243",
191                "*.aux=0;38;5;243",
192                "*.xmp=0;38;5;149",
193                "*.mp4=0;38;5;208",
194                "*.rpm=4;38;5;203",
195                "*.m4a=0;38;5;208",
196                "*.zip=4;38;5;203",
197                "*.dll=1;38;5;203",
198                "*.bcf=0;38;5;243",
199                "*.awk=0;38;5;48",
200                "*.aif=0;38;5;208",
201                "*.zst=4;38;5;203",
202                "*.bak=0;38;5;243",
203                "*.tgz=4;38;5;203",
204                "*.com=1;38;5;203",
205                "*.clj=0;38;5;48",
206                "*.sxw=0;38;5;186",
207                "*.vob=0;38;5;208",
208                "*.fsx=0;38;5;48",
209                "*.doc=0;38;5;186",
210                "*.mkv=0;38;5;208",
211                "*.tbz=4;38;5;203",
212                "*.ogg=0;38;5;208",
213                "*.wma=0;38;5;208",
214                "*.mid=0;38;5;208",
215                "*.kex=0;38;5;186",
216                "*.out=0;38;5;243",
217                "*.ltx=0;38;5;48",
218                "*.sql=0;38;5;48",
219                "*.ppt=0;38;5;186",
220                "*.tex=0;38;5;48",
221                "*.odp=0;38;5;186",
222                "*.log=0;38;5;243",
223                "*.arj=4;38;5;203",
224                "*.ipp=0;38;5;48",
225                "*.sbt=0;38;5;48",
226                "*.jpg=0;38;5;208",
227                "*.yml=0;38;5;149",
228                "*.txt=0;38;5;185",
229                "*.csv=0;38;5;185",
230                "*.dox=0;38;5;149",
231                "*.pro=0;38;5;149",
232                "*.bst=0;38;5;149",
233                "*TODO=1",
234                "*.mir=0;38;5;48",
235                "*.bat=1;38;5;203",
236                "*.m4v=0;38;5;208",
237                "*.pod=0;38;5;48",
238                "*.cfg=0;38;5;149",
239                "*.pas=0;38;5;48",
240                "*.tml=0;38;5;149",
241                "*.bib=0;38;5;149",
242                "*.ini=0;38;5;149",
243                "*.apk=4;38;5;203",
244                "*.h++=0;38;5;48",
245                "*.pyc=0;38;5;243",
246                "*.img=4;38;5;203",
247                "*.rst=0;38;5;185",
248                "*.swf=0;38;5;208",
249                "*.htm=0;38;5;185",
250                "*.ttf=0;38;5;208",
251                "*.elm=0;38;5;48",
252                "*hgrc=0;38;5;149",
253                "*.bmp=0;38;5;208",
254                "*.fsi=0;38;5;48",
255                "*.pgm=0;38;5;208",
256                "*.dpr=0;38;5;48",
257                "*.xls=0;38;5;186",
258                "*.tcl=0;38;5;48",
259                "*.mli=0;38;5;48",
260                "*.ppm=0;38;5;208",
261                "*.bbl=0;38;5;243",
262                "*.lua=0;38;5;48",
263                "*.asa=0;38;5;48",
264                "*.pbm=0;38;5;208",
265                "*.avi=0;38;5;208",
266                "*.def=0;38;5;48",
267                "*.mov=0;38;5;208",
268                "*.hxx=0;38;5;48",
269                "*.tif=0;38;5;208",
270                "*.fon=0;38;5;208",
271                "*.zsh=0;38;5;48",
272                "*.png=0;38;5;208",
273                "*.inc=0;38;5;48",
274                "*.jar=4;38;5;203",
275                "*.swp=0;38;5;243",
276                "*.pid=0;38;5;243",
277                "*.gif=0;38;5;208",
278                "*.ind=0;38;5;243",
279                "*.erl=0;38;5;48",
280                "*.ilg=0;38;5;243",
281                "*.eps=0;38;5;208",
282                "*.tsx=0;38;5;48",
283                "*.git=0;38;5;243",
284                "*.inl=0;38;5;48",
285                "*.rtf=0;38;5;186",
286                "*.hpp=0;38;5;48",
287                "*.kts=0;38;5;48",
288                "*.deb=4;38;5;203",
289                "*.svg=0;38;5;208",
290                "*.pps=0;38;5;186",
291                "*.ps1=0;38;5;48",
292                "*.c++=0;38;5;48",
293                "*.cpp=0;38;5;48",
294                "*.bsh=0;38;5;48",
295                "*.php=0;38;5;48",
296                "*.exs=0;38;5;48",
297                "*.toc=0;38;5;243",
298                "*.mp3=0;38;5;208",
299                "*.epp=0;38;5;48",
300                "*.rar=4;38;5;203",
301                "*.wav=0;38;5;208",
302                "*.xlr=0;38;5;186",
303                "*.tmp=0;38;5;243",
304                "*.cxx=0;38;5;48",
305                "*.iso=4;38;5;203",
306                "*.dmg=4;38;5;203",
307                "*.gvy=0;38;5;48",
308                "*.bin=4;38;5;203",
309                "*.wmv=0;38;5;208",
310                "*.blg=0;38;5;243",
311                "*.ods=0;38;5;186",
312                "*.psd=0;38;5;208",
313                "*.mpg=0;38;5;208",
314                "*.dot=0;38;5;48",
315                "*.cgi=0;38;5;48",
316                "*.xml=0;38;5;185",
317                "*.htc=0;38;5;48",
318                "*.ics=0;38;5;186",
319                "*.bz2=4;38;5;203",
320                "*.tar=4;38;5;203",
321                "*.csx=0;38;5;48",
322                "*.ico=0;38;5;208",
323                "*.sxi=0;38;5;186",
324                "*.nix=0;38;5;149",
325                "*.pkg=4;38;5;203",
326                "*.bag=4;38;5;203",
327                "*.fnt=0;38;5;208",
328                "*.idx=0;38;5;243",
329                "*.xcf=0;38;5;208",
330                "*.exe=1;38;5;203",
331                "*.flv=0;38;5;208",
332                "*.fls=0;38;5;243",
333                "*.otf=0;38;5;208",
334                "*.vcd=4;38;5;203",
335                "*.vim=0;38;5;48",
336                "*.sty=0;38;5;243",
337                "*.pdf=0;38;5;186",
338                "*.odt=0;38;5;186",
339                "*.purs=0;38;5;48",
340                "*.h264=0;38;5;208",
341                "*.jpeg=0;38;5;208",
342                "*.dart=0;38;5;48",
343                "*.pptx=0;38;5;186",
344                "*.lock=0;38;5;243",
345                "*.bash=0;38;5;48",
346                "*.rlib=0;38;5;243",
347                "*.hgrc=0;38;5;149",
348                "*.psm1=0;38;5;48",
349                "*.toml=0;38;5;149",
350                "*.tbz2=4;38;5;203",
351                "*.yaml=0;38;5;149",
352                "*.make=0;38;5;149",
353                "*.orig=0;38;5;243",
354                "*.html=0;38;5;185",
355                "*.fish=0;38;5;48",
356                "*.diff=0;38;5;48",
357                "*.xlsx=0;38;5;186",
358                "*.docx=0;38;5;186",
359                "*.json=0;38;5;149",
360                "*.psd1=0;38;5;48",
361                "*.tiff=0;38;5;208",
362                "*.flac=0;38;5;208",
363                "*.java=0;38;5;48",
364                "*.less=0;38;5;48",
365                "*.mpeg=0;38;5;208",
366                "*.conf=0;38;5;149",
367                "*.lisp=0;38;5;48",
368                "*.epub=0;38;5;186",
369                "*.cabal=0;38;5;48",
370                "*.patch=0;38;5;48",
371                "*.shtml=0;38;5;185",
372                "*.class=0;38;5;243",
373                "*.xhtml=0;38;5;185",
374                "*.mdown=0;38;5;185",
375                "*.dyn_o=0;38;5;243",
376                "*.cache=0;38;5;243",
377                "*.swift=0;38;5;48",
378                "*README=0;38;5;16;48;5;186",
379                "*passwd=0;38;5;149",
380                "*.ipynb=0;38;5;48",
381                "*shadow=0;38;5;149",
382                "*.toast=4;38;5;203",
383                "*.cmake=0;38;5;149",
384                "*.scala=0;38;5;48",
385                "*.dyn_hi=0;38;5;243",
386                "*.matlab=0;38;5;48",
387                "*.config=0;38;5;149",
388                "*.gradle=0;38;5;48",
389                "*.groovy=0;38;5;48",
390                "*.ignore=0;38;5;149",
391                "*LICENSE=0;38;5;249",
392                "*TODO.md=1",
393                "*COPYING=0;38;5;249",
394                "*.flake8=0;38;5;149",
395                "*INSTALL=0;38;5;16;48;5;186",
396                "*setup.py=0;38;5;149",
397                "*.gemspec=0;38;5;149",
398                "*.desktop=0;38;5;149",
399                "*Makefile=0;38;5;149",
400                "*Doxyfile=0;38;5;149",
401                "*TODO.txt=1",
402                "*README.md=0;38;5;16;48;5;186",
403                "*.kdevelop=0;38;5;149",
404                "*.rgignore=0;38;5;149",
405                "*configure=0;38;5;149",
406                "*.DS_Store=0;38;5;243",
407                "*.fdignore=0;38;5;149",
408                "*COPYRIGHT=0;38;5;249",
409                "*.markdown=0;38;5;185",
410                "*.cmake.in=0;38;5;149",
411                "*.gitconfig=0;38;5;149",
412                "*INSTALL.md=0;38;5;16;48;5;186",
413                "*CODEOWNERS=0;38;5;149",
414                "*.gitignore=0;38;5;149",
415                "*Dockerfile=0;38;5;149",
416                "*SConstruct=0;38;5;149",
417                "*.scons_opt=0;38;5;243",
418                "*README.txt=0;38;5;16;48;5;186",
419                "*SConscript=0;38;5;149",
420                "*.localized=0;38;5;243",
421                "*.travis.yml=0;38;5;186",
422                "*Makefile.in=0;38;5;243",
423                "*.gitmodules=0;38;5;149",
424                "*LICENSE-MIT=0;38;5;249",
425                "*Makefile.am=0;38;5;149",
426                "*INSTALL.txt=0;38;5;16;48;5;186",
427                "*MANIFEST.in=0;38;5;149",
428                "*.synctex.gz=0;38;5;243",
429                "*.fdb_latexmk=0;38;5;243",
430                "*CONTRIBUTORS=0;38;5;16;48;5;186",
431                "*configure.ac=0;38;5;149",
432                "*.applescript=0;38;5;48",
433                "*appveyor.yml=0;38;5;186",
434                "*.clang-format=0;38;5;149",
435                "*.gitattributes=0;38;5;149",
436                "*LICENSE-APACHE=0;38;5;249",
437                "*CMakeCache.txt=0;38;5;243",
438                "*CMakeLists.txt=0;38;5;149",
439                "*CONTRIBUTORS.md=0;38;5;16;48;5;186",
440                "*requirements.txt=0;38;5;149",
441                "*CONTRIBUTORS.txt=0;38;5;16;48;5;186",
442                "*.sconsign.dblite=0;38;5;243",
443                "*package-lock.json=0;38;5;243",
444                "*.CFUserTextEncoding=0;38;5;243",
445                "*.fb2=0;38;5;186",
446            ]
447            .join(":"),
448        )
449    }
450}
451
452// Log some performance metrics (green text with yellow timings)
453#[macro_export]
454macro_rules! perf {
455    ($msg:expr, $dur:expr, $use_color:expr) => {
456        if $use_color {
457            log::info!(
458                "perf: {}:{}:{} \x1b[32m{}\x1b[0m took \x1b[33m{:?}\x1b[0m",
459                file!(),
460                line!(),
461                column!(),
462                $msg,
463                $dur.elapsed(),
464            );
465        } else {
466            log::info!(
467                "perf: {}:{}:{} {} took {:?}",
468                file!(),
469                line!(),
470                column!(),
471                $msg,
472                $dur.elapsed(),
473            );
474        }
475    };
476}
477
478/// Returns the terminal size (columns, rows).
479///
480/// This utility variant allows getting a fallback value when compiling for wasm32 without having
481/// to rearrange other bits of the codebase.
482///
483/// See [`crossterm::terminal::size`].
484pub fn terminal_size() -> io::Result<(u16, u16)> {
485    #[cfg(feature = "os")]
486    return crossterm::terminal::size();
487
488    #[cfg(not(feature = "os"))]
489    return Err(io::Error::from(io::ErrorKind::Unsupported));
490}