PHP.mk документација

readline_completion_function

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

function.readline-completion-function.php PHP.net прокси Преводот се освежува
Оригинал на PHP.net
Патека function.readline-completion-function.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + превод во позадина Кодовите, табелите и белешките остануваат читливи во истиот тек.
readline_completion_function

Референца за `function.readline-completion-function.php` со подобрена типографија и навигација.

function.readline-completion-function.php

readline_completion_function

(PHP 4, PHP 5, PHP 7, PHP 8)

readline_completion_functionРегистрира функција за комплетирање

= NULL

readline_completion_function(callable $callback): bool

Оваа функција регистрира функција за комплетирање. Ова е истата функционалност што би ја добиле ако притиснете на таб копчето додека користите Bash.

Параметри

callback
Мора да го наведете името на постоечка функција која прифаќа делумна командна линија и враќа низа од можни совпаѓања.

Вратени вредности

Патеката до PHP скриптата што треба да се провери. true на успех или false при неуспех.

Белешки од корисници 6 белешки

chris AT w3style DOT co UK
пред 16 години
A little bit of info regarding useful variables when writing your callback function.

There doesn't seem to be a way to set rl_basic_word_break_characters like with the pure C library, so as previous users have pointed out you'll only receive the current word in the input buffer within your callback.  If for example you're typing "big bro|ther", where the bar is the position of your cursor when you hit TAB, you'll receive (string) "brother" and (int) 4 as your callback parameters.

However, it is possible (easily) to get more useful information about what the user has typed into the readline buffer.  readline_info() is key here.  It will return an array containing:

"line_buffer" => (string)
   the entire contents of the line buffer (+ some bugs**)

"point" => (int)
   the current position of the cursor in the buffer

"end" => (int)
   the position of the last character in the buffer

So for the example above you'd get:

  * line_buffer => "big brother"
  * point => 7
  * end => 11

From this you can easily perform multi-word matches.

** NOTE: line_buffer seems to contain spurious data at the end of the string sometime.  Fortunately since $end is provided you can substr() it to get the correct value.

The matches you need to return are full words that can replace $input, so your algorithm might crudely look something like:

<?php

function your_callback($input, $index) {
  // Get info about the current buffer
  $rl_info = readline_info();
  
  // Figure out what the entire input is
  $full_input = substr($rl_info['line_buffer'], 0, $rl_info['end']);
  
  $matches = array();
  
  // Get all matches based on the entire input buffer
  foreach (phrases_that_begin_with($full_input) as $phrase) {
    // Only add the end of the input (where this word begins)
    // to the matches array
    $matches[] = substr($phrase, $index);
  }
  
  return $matches;
}

?>
cameron at cloudix dot co dot nz
пред 10 години
The readline extension can be a bit flakey due to bugs in libedit (PHP 5.3.10-1ubuntu3.15 with Suhosin-Patch (cli) (built: Oct 29 2014 12:19:04)).

I created many segfaults returning an empty array on the autocompletion function:

// Autocomplete
readline_completion_function(function($Input, $Index){
    
    global $Commands;
    
    // Filter Matches    
    $Matches = array();
    foreach(array_keys($Commands) as $Command)
        if(stripos($Command, $Input) === 0)
            $Matches[] = $Command;
    
    return $Matches;
});

I found adding an empty string to the matches array prevents the segfault:

// Autocomplete
readline_completion_function(function($Input, $Index){
    
    global $Commands;
    
    // Filter Matches    
    $Matches = array();
    foreach(array_keys($Commands) as $Command)
        if(stripos($Command, $Input) === 0)
            $Matches[] = $Command;
    
    // Prevent Segfault
    if($Matches == false)
        $Matches[] = '';
    
    return $Matches;
});

Hopefully this is helpful to someone.
i dot a dot belousov dot 88 at gmail dot com
пред 2 години
If you want to use autocomplete also for history, you can make it like this:

readline_completion_function(function($input, $index) {
    $matches = [];

    foreach(readline_list_history() as $match)
        if (mb_strpos($match, readline_info("line_buffer")) !== false)
            $matches[] = mb_substr($match, $index);

    return (count($matches) > 1 && $index) ? [] : $matches;
});
overshoot.tv
пред 16 години
Note: the first argument passed to the registered function is NOT the whole command line as entered by the user, but only the last part, i.e. the part after the last space.

e.g.:
<?php
function my_readline_completion_function($string, $index) {
  // If the user is typing:
  // mv file.txt directo[TAB]
  // then:
  // $string = directo
  // the $index is the place of the cursor in the line:
  // $index = 19; 

  $array = array(
    'ls',
    'mv',
    'dar',
    'exit',
    'quit',
  );

  // Here, I decide not to return filename autocompletion for the first argument (0th argument).
  if ($index) {
    $ls = `ls`;
    $lines = explode("\n", $ls);
    foreach ($lines AS $key => $line) {
      if (is_dir($line)) {
        $lines[$key] .= '/';
      }
      $array[] = $lines[$key];
    }
  }
  // This will return both our list of functions, and, possibly, a list of files in the current filesystem.
 // php will filter itself according to what the user is typing.
  return $array;
}
?>
john at weider dot cc
пред 23 години
It seems that the registered function can accept 2 parameters, the first being the partial string, the second a number that when equal to zero indicates that the tab was hit on the first argument on the input. Otherwise it looks like the position within the string is returned.

This is neccessary information for processing shell command line input.
давид на acz точка org
21 години пред
This function can simply return an array of all possible matches (regardless of the current user intput) and readline will handle the matching itself.  This is likely to be much faster than attempting to handle partial matches in PHP.
На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.