Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
6
 * (c) 2012-2022 Justin Hileman
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Psy\Readline;
13
 
14
use Psy\Exception\BreakException;
15
use Psy\Readline\Hoa\Console as HoaConsole;
16
use Psy\Readline\Hoa\ConsoleCursor as HoaConsoleCursor;
17
use Psy\Readline\Hoa\ConsoleInput as HoaConsoleInput;
18
use Psy\Readline\Hoa\ConsoleOutput as HoaConsoleOutput;
19
use Psy\Readline\Hoa\ConsoleTput as HoaConsoleTput;
20
use Psy\Readline\Hoa\Readline as HoaReadline;
21
use Psy\Readline\Hoa\Ustring as HoaUstring;
22
 
23
/**
24
 * Userland Readline implementation.
25
 */
26
class Userland implements Readline
27
{
28
    /** @var HoaReadline */
29
    private $hoaReadline;
30
 
31
    /** @var string|null */
32
    private $lastPrompt;
33
 
34
    private $tput;
35
    private $input;
36
    private $output;
37
 
38
    /**
39
     * @return bool
40
     */
41
    public static function isSupported(): bool
42
    {
43
        static::bootstrapHoa();
44
 
45
        return HoaUstring::checkMbString() && HoaConsoleTput::isSupported();
46
    }
47
 
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public static function supportsBracketedPaste(): bool
52
    {
53
        return false;
54
    }
55
 
56
    /**
57
     * Doesn't (currently) support history file, size or erase dupes configs.
58
     */
59
    public function __construct($historyFile = null, $historySize = 0, $eraseDups = false)
60
    {
61
        static::bootstrapHoa(true);
62
 
63
        $this->hoaReadline = new HoaReadline();
64
        $this->hoaReadline->addMapping('\C-l', function () {
65
            $this->redisplay();
66
 
67
            return HoaReadline::STATE_NO_ECHO;
68
        });
69
 
70
        $this->tput = new HoaConsoleTput();
71
        HoaConsole::setTput($this->tput);
72
 
73
        $this->input = new HoaConsoleInput();
74
        HoaConsole::setInput($this->input);
75
 
76
        $this->output = new HoaConsoleOutput();
77
        HoaConsole::setOutput($this->output);
78
    }
79
 
80
    /**
81
     * Bootstrap some things that Hoa used to do itself.
82
     */
83
    public static function bootstrapHoa(bool $withTerminalResize = false)
84
    {
85
        // A side effect registers hoa:// stream wrapper
86
        \class_exists('Psy\Readline\Hoa\ProtocolWrapper');
87
 
88
        // A side effect registers hoa://Library/Stream
89
        \class_exists('Psy\Readline\Hoa\Stream');
90
 
91
        // A side effect binds terminal resize
92
        $withTerminalResize && \class_exists('Psy\Readline\Hoa\ConsoleWindow');
93
    }
94
 
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function addHistory(string $line): bool
99
    {
100
        $this->hoaReadline->addHistory($line);
101
 
102
        return true;
103
    }
104
 
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function clearHistory(): bool
109
    {
110
        $this->hoaReadline->clearHistory();
111
 
112
        return true;
113
    }
114
 
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function listHistory(): array
119
    {
120
        $i = 0;
121
        $list = [];
122
        while (($item = $this->hoaReadline->getHistory($i++)) !== null) {
123
            $list[] = $item;
124
        }
125
 
126
        return $list;
127
    }
128
 
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function readHistory(): bool
133
    {
134
        return true;
135
    }
136
 
137
    /**
138
     * {@inheritdoc}
139
     *
140
     * @throws BreakException if user hits Ctrl+D
141
     *
142
     * @return string
143
     */
144
    public function readline(string $prompt = null)
145
    {
146
        $this->lastPrompt = $prompt;
147
 
148
        return $this->hoaReadline->readLine($prompt);
149
    }
150
 
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function redisplay()
155
    {
156
        $currentLine = $this->hoaReadline->getLine();
157
        HoaConsoleCursor::clear('all');
158
        echo $this->lastPrompt, $currentLine;
159
    }
160
 
161
    /**
162
     * {@inheritdoc}
163
     */
164
    public function writeHistory(): bool
165
    {
166
        return true;
167
    }
168
}