Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | 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
 *
200 lars 6
 * (c) 2012-2023 Justin Hileman
148 lars 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
    public static function isSupported(): bool
39
    {
40
        static::bootstrapHoa();
41
 
42
        return HoaUstring::checkMbString() && HoaConsoleTput::isSupported();
43
    }
44
 
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public static function supportsBracketedPaste(): bool
49
    {
50
        return false;
51
    }
52
 
53
    /**
54
     * Doesn't (currently) support history file, size or erase dupes configs.
55
     */
56
    public function __construct($historyFile = null, $historySize = 0, $eraseDups = false)
57
    {
58
        static::bootstrapHoa(true);
59
 
60
        $this->hoaReadline = new HoaReadline();
61
        $this->hoaReadline->addMapping('\C-l', function () {
62
            $this->redisplay();
63
 
64
            return HoaReadline::STATE_NO_ECHO;
65
        });
66
 
67
        $this->tput = new HoaConsoleTput();
68
        HoaConsole::setTput($this->tput);
69
 
70
        $this->input = new HoaConsoleInput();
71
        HoaConsole::setInput($this->input);
72
 
73
        $this->output = new HoaConsoleOutput();
74
        HoaConsole::setOutput($this->output);
75
    }
76
 
77
    /**
78
     * Bootstrap some things that Hoa used to do itself.
79
     */
80
    public static function bootstrapHoa(bool $withTerminalResize = false)
81
    {
82
        // A side effect registers hoa:// stream wrapper
83
        \class_exists('Psy\Readline\Hoa\ProtocolWrapper');
84
 
85
        // A side effect registers hoa://Library/Stream
86
        \class_exists('Psy\Readline\Hoa\Stream');
87
 
88
        // A side effect binds terminal resize
89
        $withTerminalResize && \class_exists('Psy\Readline\Hoa\ConsoleWindow');
90
    }
91
 
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function addHistory(string $line): bool
96
    {
97
        $this->hoaReadline->addHistory($line);
98
 
99
        return true;
100
    }
101
 
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function clearHistory(): bool
106
    {
107
        $this->hoaReadline->clearHistory();
108
 
109
        return true;
110
    }
111
 
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function listHistory(): array
116
    {
117
        $i = 0;
118
        $list = [];
119
        while (($item = $this->hoaReadline->getHistory($i++)) !== null) {
120
            $list[] = $item;
121
        }
122
 
123
        return $list;
124
    }
125
 
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function readHistory(): bool
130
    {
131
        return true;
132
    }
133
 
134
    /**
135
     * {@inheritdoc}
136
     *
137
     * @throws BreakException if user hits Ctrl+D
138
     *
139
     * @return string
140
     */
141
    public function readline(string $prompt = null)
142
    {
143
        $this->lastPrompt = $prompt;
144
 
145
        return $this->hoaReadline->readLine($prompt);
146
    }
147
 
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function redisplay()
152
    {
153
        $currentLine = $this->hoaReadline->getLine();
154
        HoaConsoleCursor::clear('all');
155
        echo $this->lastPrompt, $currentLine;
156
    }
157
 
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function writeHistory(): bool
162
    {
163
        return true;
164
    }
165
}