Ugly hack for Read history on startup #zp4

Hi,

I was stuck on the shell challange at the history file from env variable. Something wierd happens and the first item of readline_list_history() is an emptry string.
To work around it, I wrote this (admittedly ugly) piece of code, but I’m still not sure why the empty string shows up in the first place.
All code is here.
Any insights would be greatly appreciated!

Thanks,
Razvan

        global $history;
        $localHistory = readline_list_history();
        if (isset($localHistory[0]) && isset($history[0])) {
            $localHistory[0] = $history[0];
        }

        $offset = (int) ($this->args[0] ?? 0);
        $prevCommands = array_slice($localHistory, -$offset, null, true);
        foreach ($prevCommands as $index => $prevCommand) {
            $line = sprintf('%5d  %s', $index + 1, trim($prevCommand));
            fwrite($this->out, $line . PHP_EOL);
        }

Hey @razvbir, I added a couple of logs before readline_callback_read_char and just inside afterLine:

Looks like localHistory[0] is cleared between readline_callback_read_char and afterLine:

Not sure why that’s happening. Might be a PHP/readline issue.

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.