Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core\output;
18
 
19
use core_text;
20
use core\check\result as check_result;
21
 
22
defined('MOODLE_INTERNAL') || die();
23
 
24
global $CFG;
25
require_once("{$CFG->libdir}/clilib.php");
26
 
27
/**
28
 * A renderer that generates output for command-line scripts.
29
 *
30
 * The implementation of this renderer is probably incomplete.
31
 *
32
 * @copyright 2009 Tim Hunt
33
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 * @since Moodle 2.0
35
 * @package core
36
 * @category output
37
 */
38
class core_renderer_cli extends core_renderer {
39
    /**
40
     * @var array $progressmaximums stores the largest percentage for a progress bar.
41
     */
42
    private $progressmaximums = [];
43
 
44
    /**
45
     * Returns the page header.
46
     *
47
     * @return string HTML fragment
48
     */
49
    public function header() {
50
        return $this->page->heading . "\n";
51
    }
52
 
53
    /**
54
     * Renders a Check API result
55
     *
56
     * To aid in CLI consistency this status is NOT translated and the visual
57
     * width is always exactly 10 chars.
58
     *
59
     * @param check_result $result
60
     * @return string HTML fragment
61
     */
62
    protected function render_check_result(check_result $result) {
63
        $status = $result->get_status();
64
 
65
        $labels = [
66
            check_result::NA        => '      ' . cli_ansi_format('<colour:darkGray>') . ' NA ',
67
            check_result::OK        => '      ' . cli_ansi_format('<colour:green>') . ' OK ',
68
            check_result::INFO      => '    '   . cli_ansi_format('<colour:blue>') . ' INFO ',
69
            check_result::UNKNOWN   => ' '      . cli_ansi_format('<colour:darkGray>') . ' UNKNOWN ',
70
            check_result::WARNING   => ' '      . cli_ansi_format('<colour:black><bgcolour:yellow>') . ' WARNING ',
71
            check_result::ERROR     => '   '    . cli_ansi_format('<bgcolour:red>') . ' ERROR ',
72
            check_result::CRITICAL  => ''       . cli_ansi_format('<bgcolour:red>') . ' CRITICAL ',
73
        ];
74
        $string = $labels[$status] . cli_ansi_format('<colour:normal>');
75
        return $string;
76
    }
77
 
78
    /**
79
     * Renders a Check API result
80
     *
81
     * @param check_result $result
82
     * @return string fragment
83
     */
84
    public function check_result(check_result $result) {
85
        return $this->render_check_result($result);
86
    }
87
 
88
    /**
89
     * Renders a progress bar.
90
     *
91
     * Do not use $OUTPUT->render($bar), instead use progress_bar::create().
92
     *
93
     * @param  progress_bar $bar The bar.
94
     * @return string ascii fragment
95
     */
96
    public function render_progress_bar(progress_bar $bar) {
97
        $size = 55; // The width of the progress bar in chars.
98
        $ascii = "\n";
99
 
100
        if (stream_isatty(STDOUT)) {
101
            $ascii .= "[" . str_repeat(' ', $size) . "] 0% \n";
102
            return cli_ansi_format($ascii);
103
        }
104
 
105
        $this->progressmaximums[$bar->get_id()] = 0;
106
        $ascii .= '[';
107
        return $ascii;
108
    }
109
 
110
    /**
111
     * Renders an update to a progress bar.
112
     *
113
     * Note: This does not cleanly map to a renderable class and should
114
     * never be used directly.
115
     *
116
     * @param  string $id
117
     * @param  float $percent
118
     * @param  string $msg Message
119
     * @param  string $estimate time remaining message
120
     * @param  bool $error (Unused in cli)
121
     * @return string ascii fragment
122
     */
123
    public function render_progress_bar_update(string $id, float $percent, string $msg, string $estimate,
124
        bool $error = false): string {
125
        $size = 55; // The width of the progress bar in chars.
126
        $ascii = '';
127
 
128
        // If we are rendering to a terminal then we can safely use ansii codes
129
        // to move the cursor and redraw the complete progress bar each time
130
        // it is updated.
131
        if (stream_isatty(STDOUT)) {
132
            $colour = $percent == 100 ? 'green' : 'blue';
133
 
134
            $done = $percent * $size * 0.01;
135
            $whole = floor($done);
136
            $bar = "<colour:$colour>";
137
            $bar .= str_repeat('█', $whole);
138
 
139
            if ($whole < $size) {
140
                // By using unicode chars for partial blocks we can have higher
141
                // precision progress bar.
142
                $fraction = floor(($done - $whole) * 8);
143
                $bar .= core_text::substr(' ▏▎▍▌▋▊▉', $fraction, 1);
144
 
145
                // Fill the rest of the empty bar.
146
                $bar .= str_repeat(' ', $size - $whole - 1);
147
            }
148
 
149
            $bar .= '<colour:normal>';
150
 
151
            if ($estimate) {
152
                $estimate = "- $estimate";
153
            }
154
 
155
            $ascii .= '<cursor:up>';
156
            $ascii .= '<cursor:up>';
157
            $ascii .= sprintf("[$bar] %3.1f%% %-22s\n", $percent, $estimate);
158
            $ascii .= sprintf("%-80s\n", $msg);
159
            return cli_ansi_format($ascii);
160
        }
161
 
162
        // If we are not rendering to a tty, ie when piped to another command
163
        // or on windows we need to progressively render the progress bar
164
        // which can only ever go forwards.
165
        $done = round($percent * $size * 0.01);
166
        $delta = max(0, $done - $this->progressmaximums[$id]);
167
 
168
        $ascii .= str_repeat('#', $delta);
169
        if ($percent >= 100 && $delta > 0) {
170
            $ascii .= sprintf("] %3.1f%%", $percent) . "\n$msg\n";
171
        }
172
        $this->progressmaximums[$id] += $delta;
173
        return $ascii;
174
    }
175
 
176
    /**
177
     * Returns a template fragment representing a Heading.
178
     *
179
     * @param string $text The text of the heading
180
     * @param int $level The level of importance of the heading
181
     * @param string $classes A space-separated list of CSS classes
182
     * @param string $id An optional ID
183
     * @return string A template fragment for a heading
184
     */
185
    public function heading($text, $level = 2, $classes = 'main', $id = null) {
186
        $text .= "\n";
187
        switch ($level) {
188
            case 1:
189
                return '=>' . $text;
190
            case 2:
191
                return '-->' . $text;
192
            default:
193
                return $text;
194
        }
195
    }
196
 
197
    /**
198
     * Returns a template fragment representing a fatal error.
199
     *
200
     * @param string $message The message to output
201
     * @param string $moreinfourl URL where more info can be found about the error
202
     * @param string $link Link for the Continue button
203
     * @param array $backtrace The execution backtrace
204
     * @param null|string $debuginfo Debugging information
205
     * @param string $errorcode
206
     * @return string A template fragment for a fatal error
207
     */
208
    public function fatal_error($message, $moreinfourl, $link, $backtrace, $debuginfo = null, $errorcode = "") {
209
        global $CFG;
210
 
211
        $output = "!!! $message !!!\n";
212
 
213
        if ($CFG->debugdeveloper) {
214
            if (!empty($debuginfo)) {
215
                $output .= $this->notification($debuginfo, 'notifytiny');
216
            }
217
            if (!empty($backtrace)) {
218
                $output .= $this->notification('Stack trace: ' . format_backtrace($backtrace, true), 'notifytiny');
219
            }
220
        }
221
 
222
        return $output;
223
    }
224
 
225
    /**
226
     * Returns a template fragment representing a notification.
227
     *
228
     * @param string $message The message to print out.
229
     * @param string $type    The type of notification. See constants on \core\output\notification.
230
     * @param bool $closebutton Whether to show a close icon to remove the notification (default true).
231
     * @param string|null $title The title of the notification.
232
     * @param ?string $titleicon if the title should have an icon you can give the icon name with the component
233
     *  (e.g. 'i/circleinfo, core' or 'i/circleinfo' if the icon is from core)
234
     * @return string A template fragment for a notification
235
     */
236
    public function notification($message, $type = null, $closebutton = true, ?string $title = null, ?string $titleicon = null) {
237
        $message = clean_text($message);
238
        if ($type === 'notifysuccess' || $type === 'success') {
239
            return "++ $message ++\n";
240
        }
241
        return "!! $message !!\n";
242
    }
243
 
244
    /**
245
     * There is no footer for a cli request, however we must override the
246
     * footer method to prevent the default footer.
247
     */
248
    public function footer() {
249
    }
250
 
251
    /**
252
     * Render a notification (that is, a status message about something that has
253
     * just happened).
254
     *
255
     * @param notification $notification the notification to print out
256
     * @return string plain text output
257
     */
258
    public function render_notification(notification $notification) {
259
        return $this->notification($notification->get_message(), $notification->get_message_type());
260
    }
261
}
262
 
263
// Alias this class to the old name.
264
// This file will be autoloaded by the legacyclasses autoload system.
265
// In future all uses of this class will be corrected and the legacy references will be removed.
266
class_alias(core_renderer_cli::class, \core_renderer_cli::class);