]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/console.pm
Merge pull request #1005 from SaberUK/master+minor-tweaks
[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.8.0;
24 }
25
26 use strict;
27 use warnings FATAL => qw(all);
28
29 use File::Path            qw(mkpath);
30 use File::Spec::Functions qw(rel2abs);
31 use Exporter              qw(import);
32
33 our @EXPORT = qw(print_format
34                  print_error
35                  print_warning
36                  prompt_bool
37                  prompt_dir
38                  prompt_string);
39
40 my %FORMAT_CODES = (
41         DEFAULT => "\e[0m",
42         BOLD    => "\e[1m",
43
44         RED    => "\e[1;31m",
45         GREEN  => "\e[1;32m",
46         YELLOW => "\e[1;33m",
47         BLUE   => "\e[1;34m"
48 );
49
50 sub __console_format($$) {
51         my ($name, $data) = @_;
52         return $data unless -t STDOUT;
53         return $FORMAT_CODES{uc $name} . $data . $FORMAT_CODES{DEFAULT};
54 }
55
56 sub print_format($;$) {
57         my $message = shift;
58         my $stream = shift || *STDOUT;
59         while ($message =~ /(<\|(\S+)\s(.+?)\|>)/) {
60                 my $formatted = __console_format $2, $3;
61                 $message =~ s/\Q$1\E/$formatted/;
62         }
63         print { $stream } $message;
64 }
65
66 sub print_error($) {
67         my $message = shift;
68         print_format "<|RED Error:|> $message\n", *STDERR;
69         exit 1;
70 }
71
72 sub print_warning($) {
73         my $message = shift;
74         print_format "<|YELLOW Warning:|> $message\n", *STDERR;
75 }
76
77 sub prompt_bool($$$) {
78         my ($interactive, $question, $default) = @_;
79         my $answer = prompt_string($interactive, $question, $default ? 'y' : 'n');
80         return $answer =~ /y/i;
81 }
82
83 sub prompt_dir($$$;$) {
84         my ($interactive, $question, $default, $create_now) = @_;
85         my ($answer, $create);
86         do {
87                 $answer = rel2abs(prompt_string($interactive, $question, $default));
88                 $create = prompt_bool($interactive && !-d $answer, "$answer does not exist. Create it?", 'y');
89                 if ($create && $create_now) {
90                         my $mkpath = eval {
91                                 mkpath($answer, 0, 0750);
92                                 return 1;
93                         };
94                         unless (defined $mkpath) {
95                                 print_warning "unable to create $answer!\n";
96                                 $create = 0;
97                         }
98                 }
99         } while (!$create);
100         return $answer;
101 }
102
103 sub prompt_string($$$) {
104         my ($interactive, $question, $default) = @_;
105         return $default unless $interactive;
106         print_format "$question\n";
107         print_format "[<|GREEN $default|>] => ";
108         chomp(my $answer = <STDIN>);
109         print "\n";
110         return $answer ? $answer : $default;
111 }
112
113 1;