Mercurial > hg > cmmr2012-drupal-site
comparison vendor/psy/psysh/src/Command/HistoryCommand.php @ 4:a9cd425dd02b
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:11:55 +0000 |
parents | c75dbcec494b |
children |
comparison
equal
deleted
inserted
replaced
3:307d7a7fd348 | 4:a9cd425dd02b |
---|---|
108 $matches = []; | 108 $matches = []; |
109 $highlighted = []; | 109 $highlighted = []; |
110 foreach ($history as $i => $line) { | 110 foreach ($history as $i => $line) { |
111 if ($this->filter->match($line, $matches)) { | 111 if ($this->filter->match($line, $matches)) { |
112 if (isset($matches[0])) { | 112 if (isset($matches[0])) { |
113 $chunks = explode($matches[0], $history[$i]); | 113 $chunks = \explode($matches[0], $history[$i]); |
114 $chunks = array_map([__CLASS__, 'escape'], $chunks); | 114 $chunks = \array_map([__CLASS__, 'escape'], $chunks); |
115 $glue = sprintf('<urgent>%s</urgent>', self::escape($matches[0])); | 115 $glue = \sprintf('<urgent>%s</urgent>', self::escape($matches[0])); |
116 | 116 |
117 $highlighted[$i] = implode($glue, $chunks); | 117 $highlighted[$i] = \implode($glue, $chunks); |
118 } | 118 } |
119 } else { | 119 } else { |
120 unset($history[$i]); | 120 unset($history[$i]); |
121 } | 121 } |
122 } | 122 } |
123 } | 123 } |
124 | 124 |
125 if ($save = $input->getOption('save')) { | 125 if ($save = $input->getOption('save')) { |
126 $output->writeln(sprintf('Saving history in %s...', $save)); | 126 $output->writeln(\sprintf('Saving history in %s...', $save)); |
127 file_put_contents($save, implode(PHP_EOL, $history) . PHP_EOL); | 127 \file_put_contents($save, \implode(PHP_EOL, $history) . PHP_EOL); |
128 $output->writeln('<info>History saved.</info>'); | 128 $output->writeln('<info>History saved.</info>'); |
129 } elseif ($input->getOption('replay')) { | 129 } elseif ($input->getOption('replay')) { |
130 if (!($input->getOption('show') || $input->getOption('head') || $input->getOption('tail'))) { | 130 if (!($input->getOption('show') || $input->getOption('head') || $input->getOption('tail'))) { |
131 throw new \InvalidArgumentException('You must limit history via --head, --tail or --show before replaying'); | 131 throw new \InvalidArgumentException('You must limit history via --head, --tail or --show before replaying'); |
132 } | 132 } |
133 | 133 |
134 $count = count($history); | 134 $count = \count($history); |
135 $output->writeln(sprintf('Replaying %d line%s of history', $count, ($count !== 1) ? 's' : '')); | 135 $output->writeln(\sprintf('Replaying %d line%s of history', $count, ($count !== 1) ? 's' : '')); |
136 $this->getApplication()->addInput($history); | 136 $this->getApplication()->addInput($history); |
137 } elseif ($input->getOption('clear')) { | 137 } elseif ($input->getOption('clear')) { |
138 $this->clearHistory(); | 138 $this->clearHistory(); |
139 $output->writeln('<info>History cleared.</info>'); | 139 $output->writeln('<info>History cleared.</info>'); |
140 } else { | 140 } else { |
154 * | 154 * |
155 * @return array [ start, end ] | 155 * @return array [ start, end ] |
156 */ | 156 */ |
157 private function extractRange($range) | 157 private function extractRange($range) |
158 { | 158 { |
159 if (preg_match('/^\d+$/', $range)) { | 159 if (\preg_match('/^\d+$/', $range)) { |
160 return [$range, $range + 1]; | 160 return [$range, $range + 1]; |
161 } | 161 } |
162 | 162 |
163 $matches = []; | 163 $matches = []; |
164 if ($range !== '..' && preg_match('/^(\d*)\.\.(\d*)$/', $range, $matches)) { | 164 if ($range !== '..' && \preg_match('/^(\d*)\.\.(\d*)$/', $range, $matches)) { |
165 $start = $matches[1] ? intval($matches[1]) : 0; | 165 $start = $matches[1] ? \intval($matches[1]) : 0; |
166 $end = $matches[2] ? intval($matches[2]) + 1 : PHP_INT_MAX; | 166 $end = $matches[2] ? \intval($matches[2]) + 1 : PHP_INT_MAX; |
167 | 167 |
168 return [$start, $end]; | 168 return [$start, $end]; |
169 } | 169 } |
170 | 170 |
171 throw new \InvalidArgumentException('Unexpected range: ' . $range); | 171 throw new \InvalidArgumentException('Unexpected range: ' . $range); |
183 private function getHistorySlice($show, $head, $tail) | 183 private function getHistorySlice($show, $head, $tail) |
184 { | 184 { |
185 $history = $this->readline->listHistory(); | 185 $history = $this->readline->listHistory(); |
186 | 186 |
187 // don't show the current `history` invocation | 187 // don't show the current `history` invocation |
188 array_pop($history); | 188 \array_pop($history); |
189 | 189 |
190 if ($show) { | 190 if ($show) { |
191 list($start, $end) = $this->extractRange($show); | 191 list($start, $end) = $this->extractRange($show); |
192 $length = $end - $start; | 192 $length = $end - $start; |
193 } elseif ($head) { | 193 } elseif ($head) { |
194 if (!preg_match('/^\d+$/', $head)) { | 194 if (!\preg_match('/^\d+$/', $head)) { |
195 throw new \InvalidArgumentException('Please specify an integer argument for --head'); | 195 throw new \InvalidArgumentException('Please specify an integer argument for --head'); |
196 } | 196 } |
197 | 197 |
198 $start = 0; | 198 $start = 0; |
199 $length = intval($head); | 199 $length = \intval($head); |
200 } elseif ($tail) { | 200 } elseif ($tail) { |
201 if (!preg_match('/^\d+$/', $tail)) { | 201 if (!\preg_match('/^\d+$/', $tail)) { |
202 throw new \InvalidArgumentException('Please specify an integer argument for --tail'); | 202 throw new \InvalidArgumentException('Please specify an integer argument for --tail'); |
203 } | 203 } |
204 | 204 |
205 $start = count($history) - $tail; | 205 $start = \count($history) - $tail; |
206 $length = intval($tail) + 1; | 206 $length = \intval($tail) + 1; |
207 } else { | 207 } else { |
208 return $history; | 208 return $history; |
209 } | 209 } |
210 | 210 |
211 return array_slice($history, $start, $length, true); | 211 return \array_slice($history, $start, $length, true); |
212 } | 212 } |
213 | 213 |
214 /** | 214 /** |
215 * Validate that only one of the given $options is set. | 215 * Validate that only one of the given $options is set. |
216 * | 216 * |
225 $count++; | 225 $count++; |
226 } | 226 } |
227 } | 227 } |
228 | 228 |
229 if ($count > 1) { | 229 if ($count > 1) { |
230 throw new \InvalidArgumentException('Please specify only one of --' . implode(', --', $options)); | 230 throw new \InvalidArgumentException('Please specify only one of --' . \implode(', --', $options)); |
231 } | 231 } |
232 } | 232 } |
233 | 233 |
234 /** | 234 /** |
235 * Clear the readline history. | 235 * Clear the readline history. |