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