]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/console.pm
Allow print_error and print_warning to take multiple lines.
[user/henk/code/inspircd.git] / make / console.pm
1 #
2 # InspIRCd -- Internet Relay Chat Daemon
3 #
4 #   Copyright (C) 2014 Peter Powell <petpow@saberuk.com>
5 #
6 # This file is part of InspIRCd.  InspIRCd is free software: you can
7 # redistribute it and/or modify it under the terms of the GNU General Public
8 # License as published by the Free Software Foundation, version 2.
9 #
10 # This program is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18
19
20 package make::console;
21
22 BEGIN {
23         require 5.10.0;
24 }
25
26 use feature ':5.10';
27 use strict;
28 use warnings FATAL => qw(all);
29
30 use File::Path            qw(mkpath);
31 use File::Spec::Functions qw(rel2abs);
32 use Exporter              qw(import);
33
34 our @EXPORT = qw(print_format
35                  print_error
36                  print_warning
37                  prompt_bool
38                  prompt_dir
39                  prompt_string);
40
41 my %FORMAT_CODES = (
42         DEFAULT => "\e[0m",
43         BOLD    => "\e[1m",
44
45         RED    => "\e[1;31m",
46         GREEN  => "\e[1;32m",
47         YELLOW => "\e[1;33m",
48         BLUE   => "\e[1;34m"
49 );
50
51 sub __console_format($$) {
52         my ($name, $data) = @_;
53         return $data unless -t STDOUT;
54         return $FORMAT_CODES{uc $name} . $data . $FORMAT_CODES{DEFAULT};
55 }
56
57 sub print_format($;$) {
58         my $message = shift;
59         my $stream = shift // *STDOUT;
60         while ($message =~ /(<\|(\S+)\s(.+?)\|>)/) {
61                 my $formatted = __console_format $2, $3;
62                 $message =~ s/\Q$1\E/$formatted/;
63         }
64         print { $stream } $message;
65 }
66
67 sub print_error {
68         print_format "<|RED Error:|> ", *STDERR;
69         for my $line (@_) {
70                 print_format "$line\n", *STDERR;
71         }
72         exit 1;
73 }
74
75 sub print_warning {
76         print_format "<|YELLOW Warning:|> ", *STDERR;
77         for my $line (@_) {
78                 print_format "$line\n", *STDERR;
79         }
80 }
81
82 sub prompt_bool($$$) {
83         my ($interactive, $question, $default) = @_;
84         my $answer = prompt_string($interactive, $question, $default ? 'y' : 'n');
85         return $answer =~ /y/i;
86 }
87
88 sub prompt_dir($$$;$) {
89         my ($interactive, $question, $default, $create_now) = @_;
90         my ($answer, $create);
91         do {
92                 $answer = rel2abs(prompt_string($interactive, $question, $default));
93                 $create = prompt_bool($interactive && !-d $answer, "$answer does not exist. Create it?", 'y');
94                 if ($create && $create_now) {
95                         unless (create_directory $answer, 0750) {
96                                 print_warning "unable to create $answer: $!\n";
97                                 $create = 0;
98                         }
99                 }
100         } while (!$create);
101         return $answer;
102 }
103
104 sub prompt_string($$$) {
105         my ($interactive, $question, $default) = @_;
106         return $default unless $interactive;
107         print_format "$question\n";
108         print_format "[<|GREEN $default|>] => ";
109         chomp(my $answer = <STDIN>);
110         say '';
111         return $answer ? $answer : $default;
112 }
113
114 1;