【问题标题】:PHP interactive CLI: Up and Down arrows for historyPHP 交互式 CLI:历史的向上和向下箭头
【发布时间】:2013-08-18 00:21:17
【问题描述】:

我的 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,但是当我使用箭头键时它没有被调用。

【问题讨论】:

  • 我猜它在 sapi/cli/php_cli.c
  • 关于为什么它只打印第一个字符,并且只打印 5 个字符或更长的命令有什么想法吗?

标签: php command-line readline


【解决方案1】:

如果提示字段中包含您使用过的“不可打印”字符,如 \n,则 readline() 和提示字段(使用 PHP 5.5.9 Ubuntu 测试)似乎存在错误。

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

示例:$_command = readline("\033[32m PHP> \033[35m");

如果您希望历史记录功能正常工作,提示中只能包含普通字符,不幸的是。

【讨论】:

    猜你喜欢
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    相关资源
    最近更新 更多