hand 0.1.2

Easy to use, pretty cmd log for lazy devs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
pub mod marks {
    pub const INFO: &str = "ℹ️";
    pub const WARN: &str = "⚠️";
    pub const ERROR: &str = "";
    pub const SUCCESS: &str = "";
    pub const WAIT: &str = "";
    pub const INPUT: &str = "⌨️";
}

/// Prints log message to stderr with a custom head without new line.
///
/// # Examples
///
/// ```
/// use hand::*;
/// use colored::*;
///
/// custom!("⚠️".yellow().bold(), "error message"); // ⚠️ error message
/// custom!("🔍".bold(), "searching for something {} ... ", 11); // 🔍 searching for something 11
///
/// ```
#[cfg(not(test))]
#[macro_export]
macro_rules! custom {
    ($head:expr, $($arg:tt)*) => { eprint!("{} {}", $head, format_args!($($arg)*)) }
}

#[cfg(test)]
macro_rules! custom {
    ($head:expr, $($arg:tt)*) => { format!("{} {}", $head, format_args!($($arg)*)) }
}

/// Prints log message to stderr with a custom head with new line.
///
/// # Examples
///
/// ```
/// use hand::*;
/// use colored::*;
///
/// customln!("#".bright_yellow().bold(), "Task completed"); // # Task completed
/// customln!("@", "The dog is seeking for you ... {}", "too long"); // @ The dog is seeking for you ... too long
/// ```
#[macro_export]
macro_rules! customln {
    ($head:expr, $($arg:tt)*) => { custom!($head, "{}\n", format_args!($($arg)*)) }
}

/// Prints log message to stderr with a custom prefix and head without new line.
///
/// # Examples
/// ```
/// use hand::*;
/// use colored::*;
///
/// scopecustom!("Fetching", "🌐", "fetching data from {} ... ", "www.example.com"); // [Fetching] 🌐 fetching data from www.example.com ...
/// scopecustom!("Scanning", "🚨".bright_red().bold(), "{} viruses detected ... ", 3); // [Scanning] 🚨 3 viruses detected ...
/// ```
#[cfg(not(test))]
#[macro_export]
macro_rules! scopecustom {
    ($prefix:expr, $head:expr, $($arg:tt)*) => {
        eprint!("\u{1b}[2m[{}]\u{1b}[0m {} {}", $prefix, $head, format_args!($($arg)*))
    }
}

#[cfg(test)]
macro_rules! scopecustom {
    ($prefix:expr, $head:expr, $($arg:tt)*) => {
        format!("\u{1b}[2m[{}]\u{1b}[0m {} {}", $prefix, $head, format_args!($($arg)*))
    }
}

/// Prints log message to stderr with a custom prefix and head with new line.
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// scopecustomln!("Phone", "🔋", "charging is done"); // [Phone] 🔋 charging is done
/// scopecustomln!("www.download.com", "💾", "file saved to {}", "./downloads"); // [www.download.com] 💾 file saved to ./downloads
/// ```
#[macro_export]
macro_rules! scopecustomln {
    ($prefix:expr, $head:expr, $($arg:tt)*) => {
        scopecustom!($prefix, $head, "{}\n", format_args!($($arg)*))
    }
}


/// Prints info log message to stderr without new line.
///
/// # Examples
/// ```
/// use hand::*;
///
/// info!("testing info ..."); // ℹ testing info ...
/// info!("testing info for {} users", 4); // ℹ testing info for 4 users
/// ```
#[macro_export]
macro_rules! info {
    ($($arg:tt)*) => { custom!($crate::io::marks::INFO, $($arg)*) }
}

/// Prints info log message to stderr with a new line.
///
/// # Examples
/// ```
/// use hand::*;
///
/// infoln!("Waiting all jobs to complete"); // ℹ Waiting all jobs to complete
/// infoln!("Next checking will be at {}", "10:00 AM"); // ℹ Next checking will be at 10:00 AM
/// ```
#[macro_export]
macro_rules! infoln {
    ($($arg:tt)*) => { customln!($crate::io::marks::INFO, $($arg)*) }
}

/// Prints log message to stderr without a new line, with a specified prefix.
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// scopeinfo!("working hard", "long time no see ..."); // [working hard] ℹ long time no see ...
/// scopeinfo!("eta: 1 hour", "waiting file `{}` to download ... ", "tlauncher.exe"); // [eta: 1 hour] ℹ waiting file `tlauncher.exe` to download ...
/// ```
#[macro_export]
macro_rules! scopeinfo {
    ($prefix:expr, $($arg:tt)*) => { scopecustom!($prefix, $crate::io::marks::INFO, $($arg)*) }
}

/// Prints log message to stderr with a new line, with a specified prefix.
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// scopeinfoln!("Building", "Not completed"); // [Building] ℹ Not completed
/// scopeinfoln!("Dogs", "Dogs are {}", if true { "good" } else { "bad" }); // [Dogs] ℹ Dogs are good
/// ```
#[macro_export]
macro_rules! scopeinfoln {
    ($prefix:expr, $($arg:tt)*) => { scopecustomln!($prefix, $crate::io::marks::INFO, $($arg)*) }
}

/// Prints warn log message to stderr without a new line.
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// warn!("test warn"); // ⚠ test warn
/// warn!("no more warnings {}", "i said"); // ⚠ no more warnings i said
/// ```
#[macro_export]
macro_rules! warn {
    ($($arg:tt)*) => { custom!($crate::io::marks::WARN, $($arg)*) }
}

/// Prints warn log message to stderr with a new line.
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// warnln!("You have not weared a mask"); // ⚠ You have not weared a mask
/// warnln!("You have not sent {} dollars to you mom", 1000); // ⚠ You have not sent 1000 dollars to you mom
/// ```
#[macro_export]
macro_rules! warnln {
    ($($arg:tt)*) => { customln!($crate::io::marks::WARN, $($arg)*) }
}

/// Prints log message to stderr without a new line, with a specified prefix.
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// scopewarn!("driving", "computing the distance ... "); // [driving] ⚠ computing the distance ...
/// scopewarn!("fixing", "fixing the problem ... "); // [fixing] ⚠ fixing the problem ...
/// ```
#[macro_export]
macro_rules! scopewarn {
    ($prefix:expr, $($arg:tt)*) => { scopecustom!($prefix, $crate::io::marks::WARN, $($arg)*) }
}

/// Prints log message to stderr with a new line, with a specified prefix.
///
/// # Examples
/// ```
/// use hand::*;
///
/// scopewarnln!("car", "the problem fixed"); // [car] ⚠ the problem fixed
/// scopewarnln!("boilerplate", "your code has too many boilerplate"); // [boilerplate] ⚠ your code has too many boilerplate
/// ```
#[macro_export]
macro_rules! scopewarnln {
    ($prefix:expr, $($arg:tt)*) => { scopecustomln!($prefix, $crate::io::marks::WARN, $($arg)*) }
}

/// Prints a log message to stderr without a new line, with a specified prefix.
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// success!("Operation successful"); // ✔ Operation successful
/// success!("Data processed"); // ✔ Data processed
/// ```
#[macro_export]
macro_rules! success {
    ($($arg:tt)*) => { custom!($crate::io::marks::SUCCESS, $($arg)*) }
}

/// Prints a log message to stderr with a new line, with a specified prefix.
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// successln!("Task completed"); // ✔ Task completed
/// successln!("Process finished"); // ✔ Process finished
/// ```
#[macro_export]
macro_rules! successln {
    ($($arg:tt)*) => { customln!($crate::io::marks::SUCCESS, $($arg)*) }
}

/// Prints a log message to stderr without a new line, with a specified prefix.
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// scopesuccess!("Installing", "Completed in {} secs.", 9);
/// waitln!("Reboot in 3 seconds");
/// // [Installing] ✔ Completed in 9 secs. ⌛ Reboot in 3 seconds
/// ```
#[macro_export]
macro_rules! scopesuccess {
    ($prefix:expr, $($arg:tt)*) => { scopecustom!($prefix, $crate::io::marks::SUCCESS, $($arg)*) }
}

/// Prints a log message to stderr with a new line, with a specified prefix.
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// scopesuccessln!("Deploy", "Finished in {} secs.", 9); // [Deploy] ✔ Finished in 9 secs.
/// scopesuccessln!("Cleaning up", "Finished"); // [Cleaning up] ✔ Finished
/// ```
#[macro_export]
macro_rules! scopesuccessln {
    ($prefix:expr, $($arg:tt)*) => { scopecustomln!($prefix, $crate::io::marks::SUCCESS, $($arg)*) }
}

/// Prints a log message to stderr with a new line, with a specified prefix.
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// error!("An error occurred"); // ❌ An error occurred
/// error!("Invalid input"); // ❌ Invalid input
/// ```
#[macro_export]
macro_rules! error {
    ($($arg:tt)*) => { custom!($crate::io::marks::ERROR, $($arg)*) }
}

/// Prints a log message to stderr with a new line, with a specified prefix.
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// errorln!("Critical error: {} {} seconds", "your pc will die in", 3); // ❌ Critical error your pc will die in 3 seconds
/// errorln!("Fatal error occurred"); // ❌ Fatal error occurred
/// ```
#[macro_export]
macro_rules! errorln {
    ($($arg:tt)*) => { customln!($crate::io::marks::ERROR, $($arg)*) }
}

/// Prints a log message to stderr without a new line, with a specified prefix.
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// scopeerror!("github.com", "Unable to fetch. Retrying ... ");
/// successln!("Retrying successful")
/// // [github.com] ❌ Unable to fetch. Retrying ... ✅ Retrying successful
/// ```
#[macro_export]
macro_rules! scopeerror {
    ($prefix:expr, $($arg:tt)*) => { scopecustom!($prefix, $crate::io::marks::ERROR, $($arg)*) }
}

/// Prints a log message to stderr with a new line, with a specified prefix.
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// scopeerrorln!("FATAL", "Your GPU died"); // [FATAL] ❌ Your GPU died
/// scopeerrorln!("FATAL", "Your motherboard blow up"); // [FATAL] ❌ Your motherboard blow up
/// ```
#[macro_export]
macro_rules! scopeerrorln {
    ($prefix:expr, $($arg:tt)*) => { scopecustomln!($prefix, $crate::io::marks::ERROR, $($arg)*) }
}

/// Prints a log message to stderr without a new line, with a specified prefix.
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// wait!("Waiting for input"); // ⌛ Waiting for input
/// wait!("Processing data"); // ⌛ Processing data
/// ```
#[macro_export]
macro_rules! wait {
    ($($arg:tt)*) => { custom!($crate::io::marks::WAIT, $($arg)*) }
}

/// Prints a log message to stderr with a new line, with a specified prefix.
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// waitln!("This operation can take a while"); // ⌛ This operation can take a while
/// waitln!("Fetching results"); // ⌛ Fetching results
/// ```
#[macro_export]
macro_rules! waitln {
    ($($arg:tt)*) => { customln!($crate::io::marks::WAIT, $($arg)*) }
}

/// Prints a log message to stderr without a new line, with a specified prefix.
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// scopewait!("reading config", "reading config ... ");
/// successln!("done in {} secs", 13.578);
/// // [reading config] ⌛ reading config ... ✅ done in 13.578 secs
/// ```
#[macro_export]
macro_rules! scopewait {
    ($prefix:expr, $($arg:tt)*) => { scopecustom!($prefix, $crate::io::marks::WAIT, $($arg)*) }
}

/// Prints a log message to stderr with a new line, with a specified prefix.
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// scopewaitln!("Documenting", "Wait until Give me an Oscar will be done"); // [Documenting] ⌛ Wait until Give me an Oscar will be done
/// scopewaitln!("Testing", "Wait for the test to be done"); // [Testing] ⌛ Wait for the test to be done
/// ```
#[macro_export]
macro_rules! scopewaitln {
    ($prefix:expr, $($arg:tt)*) => { scopecustomln!($prefix, $crate::io::marks::WAIT, $($arg)*) }
}


/// Outputs an input log message with the '⌨️' prefix to stderr without a linebreak
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// input!("Enter your name: "); // ⌨️ Enter your name:
/// input!("enter the folder path\n> "); // ⌨️ enter the folder path
///                                      // >
/// ```
#[macro_export]
macro_rules! input {
    ($($arg:tt)*) => { custom!($crate::io::marks::INPUT, $($arg)*) }
}

/// Outputs an input log message with the '⌨️' prefix to stderr with a linebreak
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// inputln!("Enter your age"); // ⌨️ Enter your age
/// inputln!("How many cores to use? > "); // ⌨️ How many cores to use? >
/// ```
#[macro_export]
macro_rules! inputln {
    ($($arg:tt)*) => { customln!($crate::io::marks::INPUT, $($arg)*) }
}

/// Outputs an input log message with the '⌨️' prefix and the specified scope to stderr without a linebreak
///
/// # Examples
///
/// ```
/// use hand::*;
///
/// scopeinput!("Authentication", "Password: "); // [Authentication] ⌨️ Password:
/// scopesuccessln!("Authentication", "Successfully logged in"); // [Authentication] ✅ Successfully logged in
/// ```
#[macro_export]
macro_rules! scopeinput {
    ($prefix:expr, $($arg:tt)*) => { scopecustom!($prefix, $crate::io::marks::INPUT, $($arg)*) }
}

/// Outputs an input log message with the '⌨️' prefix and the specified scope to stderr with a linebreak
///
/// # Examples
///
/// **¯\\_(ツ)_/¯**
#[macro_export]
macro_rules! scopeinputln {
    ($prefix:expr, $($arg:tt)*) => { scopecustomln!($prefix, $crate::io::marks::INPUT, $($arg)*) }
}

#[cfg(test)]
mod tests {
    use crate::*;
    use super::marks;

    #[test]
    fn output() {
        println!("\nOUTPUTS\n");

        print!("{} => same line\n", info!("info"));
        print!("{}", infoln!("infoln"));
        print!("{} => same line\n", scopeinfo!("scope", "scopeinfo"));
        print!("{}", scopeinfoln!("scope", "scopeinfoln"));

        print!("{} => same line\n", warn!("warn"));
        print!("{}", warnln!("warnln"));
        print!("{} => same line\n", scopewarn!("scope", "scopewarn"));
        print!("{}", scopewarnln!("scope", "scopewarnln"));

        print!("{} => same line\n", success!("success"));
        print!("{}", successln!("successln"));
        print!("{} => same line\n", scopesuccess!("scope", "scopesuccess"));
        print!("{}", scopesuccessln!("scope", "scopesuccessln"));

        print!("{} => same line\n", wait!("wait"));
        print!("{}", waitln!("waitln"));
        print!("{} => same line\n", scopewait!("scope", "scopewait"));
        print!("{}", scopewaitln!("scope", "scopewaitln"));

        print!("{} => same line\n", error!("error"));
        print!("{}", errorln!("errorln"));
        print!("{} => same line\n", scopeerror!("scope", "scopeerror"));
        print!("{}", scopeerrorln!("scope", "scopeerrorln"));

        print!("{} => same line\n", input!("input"));
        print!("{}", inputln!("inputln"));
        print!("{} => same line\n", scopeinput!("scope", "scopeinput"));
        print!("{}", scopeinputln!("scope", "scopeinputln"));

        println!("\n");

        // // remove dashes to show the output
        // assert!(false);
    }

    #[test]
    fn info() {
        assert_eq!(
            info!("test info"),
            format!("{} test info", marks::INFO)
        );
        assert_eq!(
            infoln!("test infoln"),
            format!("{} test infoln\n", marks::INFO)
        );
        assert_eq!(
            scopeinfo!("preinfo", "some formatting {}", 12.333),
            format!("\u{1b}[2m[preinfo]\u{1b}[0m {} some formatting 12.333", marks::INFO)
        );
        assert_eq!(
            scopeinfoln!("preinfoln", "some formatting {}", 123),
            format!("\u{1b}[2m[preinfoln]\u{1b}[0m {} some formatting 123\n", marks::INFO)
        );
    }

    #[test]
    fn warn() {
        assert_eq!(
            warn!("test warn"),
            format!("{} test warn", marks::WARN)
        );
        assert_eq!(
            warnln!("test warnln"),
            format!("{} test warnln\n", marks::WARN)
        );
        assert_eq!(
            scopewarn!("prewarn", "some formatting {}", 12.333 ),
            format!("\u{1b}[2m[prewarn]\u{1b}[0m {} some formatting 12.333", marks::WARN)
        );
        assert_eq!(
            scopewarnln!("prewarnln", "some formatting {}", 123),
            format!("\u{1b}[2m[prewarnln]\u{1b}[0m {} some formatting 123\n", marks::WARN)
        );
    }

    #[test]
    fn success() {
        assert_eq!(
            format!("{} => {}", success!("test success"), "same line"),
            format!("{} test success => same line", marks::SUCCESS)
        );
        assert_eq!(
            successln!("test successln"),
            format!("{} test successln\n", marks::SUCCESS)
        );
        assert_eq!(
            format!("{} => {}", scopesuccess!("presuccess", "some formatting {}", 12.333), "same line"),
            format!("\u{1b}[2m[presuccess]\u{1b}[0m {} some formatting 12.333 => same line", marks::SUCCESS)
        );
        assert_eq!(
            scopesuccessln!("presuccessln", "some formatting {}", 123),
            format!("\u{1b}[2m[presuccessln]\u{1b}[0m {} some formatting 123\n", marks::SUCCESS)
        );
    }

    #[test]
    fn error() {
        assert_eq!(
            error!("test error"),
            format!("{} test error", marks::ERROR)
        );
        assert_eq!(
            errorln!("test errorln"),
            format!("{} test errorln\n", marks::ERROR)
        );
        assert_eq!(
            scopeerror!("preerror", "some formatting {}", 12.333 ),
            format!("\u{1b}[2m[preerror]\u{1b}[0m {} some formatting 12.333", marks::ERROR)
        );
        assert_eq!(
            scopeerrorln!("preerrorln", "some formatting {}", 123),
            format!("\u{1b}[2m[preerrorln]\u{1b}[0m {} some formatting 123\n", marks::ERROR)
        );
    }

    #[test]
    fn wait() {
        assert_eq!(
            wait!("test wait"),
            format!("{} test wait", marks::WAIT)
        );
        assert_eq!(
            waitln!("test waitln"),
            format!("{} test waitln\n", marks::WAIT)
        );
        assert_eq!(
            scopewait!("prewait", "some formatting {}", 12.333 ),
            format!("\u{1b}[2m[prewait]\u{1b}[0m {} some formatting 12.333", marks::WAIT)
        );
        assert_eq!(
            scopewaitln!("prewaitln", "some formatting {}", 123),
            format!("\u{1b}[2m[prewaitln]\u{1b}[0m {} some formatting 123\n", marks::WAIT)
        );
    }
    
    #[test]
    fn input() {
        assert_eq!(
            input!("test input: "),
            format!("{} test input: ", marks::INPUT)
        );
        assert_eq!(
            inputln!("test inputln\n> "),
            format!("{} test inputln\n> ", marks::INPUT)
        );
        assert_eq!(
            scopeinput!("preinput", "some formatting {}", 12.333 ),
            format!("\u{1b}[2m[preinput]\u{1b}[0m {} some formatting 12.333", marks::INPUT)
        );
        assert_eq!(
            scopeinputln!("preinputln", "some formatting {}", 123),
            format!("\u{1b}[2m[preinputln]\u{1b}[0m {} some formatting 123\n", marks::INPUT)
        );
    }
}