PHP交互式CLI:历史记录的向上和向下箭头

时间:2013-08-15 18:13:41

标签: php command-line readline

我有一个涉及我的php交互式CLI程序的错误。

我使用readline_add_history向历史记录添加命令,并使用readline读取它们。

错误是,如果使用向上箭头键,我将历史记录滚动到5个字符或更长的命令,命令的第一个字符“粘住”,并且在我继续使用箭头键后将保留在那里再次滚动。只有在我按下回车后它才会消失。

例如:

% hello
<enter>
%
<up arrow key>
% hello
<down arrow key>
% h
<up arrow key>
% hhello
<down arrow key>
% h
<enter>
%
<up arrow key>
% hello
** notice here that the extra h isn't read in as a command. **

在哪里可以查看涉及箭头键/命令历史记录的代码?我已经在网上寻找资源,了解readline如何处理箭头键和历史记录,但我找不到任何东西。

我的代码是这样的:

while(true)
{

$command = readline( "\n%" );

.. do stuff ..

readline_add_history( $command );

}

我也有一个readline_completion_function但是当我使用箭头键时它没有被调用。

1 个答案:

答案 0 :(得分:0)

如果提示字段具有“不可打印”字样,则readline()和提示字段(使用PHP 5.5.9 Ubuntu进行测试)似乎存在错误。它中的字符,如\ n,你已经使用过。

我遇到了类似的问题,当尝试使用ESC代码为提示着色时,它只会导致历史记录功能变得混乱:(

示例:$ _command = readline(&#34; \ 033 [32m PHP&gt; \ 033 [35m&#34;);

如果您希望历史记录功能正常工作,则提示只能包含正常的字符。