Skip to content

Commit

Permalink
Store chip-tool interactive history in the general chip-tool storage …
Browse files Browse the repository at this point in the history
…directory. (#30803)

Addresses part of #30790

Co-authored-by: Andrei Litvin <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Feb 17, 2024
1 parent 1ef9f8c commit e3c757d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
32 changes: 27 additions & 5 deletions examples/chip-tool/commands/interactive/InteractiveCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <editline.h>

constexpr char kInteractiveModePrompt[] = ">>> ";
constexpr char kInteractiveModeHistoryFilePath[] = "/tmp/chip_tool_history";
constexpr char kInteractiveModeHistoryFileName[] = "chip_tool_history";
constexpr char kInteractiveModeStopCommand[] = "quit()";
constexpr char kCategoryError[] = "Error";
constexpr char kCategoryProgress[] = "Info";
Expand Down Expand Up @@ -257,7 +257,9 @@ void ENFORCE_FORMAT(3, 0) InteractiveServerLoggingCallback(const char * module,
gInteractiveServerResult.MaybeAddLog(module, category, base64Message);
}

char * GetCommand(char * command)
} // namespace

char * InteractiveStartCommand::GetCommand(char * command)
{
if (command != nullptr)
{
Expand All @@ -271,12 +273,32 @@ char * GetCommand(char * command)
if (command != nullptr && *command)
{
add_history(command);
write_history(kInteractiveModeHistoryFilePath);
write_history(GetHistoryFilePath().c_str());
}

return command;
}
} // namespace

std::string InteractiveStartCommand::GetHistoryFilePath() const
{
std::string storageDir;
if (GetStorageDirectory().HasValue())
{
storageDir = GetStorageDirectory().Value();
}
else
{
// Match what GetFilename in ExamplePersistentStorage.cpp does.
const char * dir = getenv("TMPDIR");
if (dir == nullptr)
{
dir = "/tmp";
}
storageDir = dir;
}

return storageDir + "/" + kInteractiveModeHistoryFileName;
}

CHIP_ERROR InteractiveServerCommand::RunCommand()
{
Expand Down Expand Up @@ -329,7 +351,7 @@ CHIP_ERROR InteractiveServerCommand::LogJSON(const char * json)

CHIP_ERROR InteractiveStartCommand::RunCommand()
{
read_history(kInteractiveModeHistoryFilePath);
read_history(GetHistoryFilePath().c_str());

// Logs needs to be redirected in order to refresh the screen appropriately when something
// is dumped to stdout while the user is typing a command.
Expand Down
4 changes: 4 additions & 0 deletions examples/chip-tool/commands/interactive/InteractiveCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class InteractiveStartCommand : public InteractiveCommand

/////////// CHIPCommand Interface /////////
CHIP_ERROR RunCommand() override;

private:
char * GetCommand(char * command);
std::string GetHistoryFilePath() const;
};

class InteractiveServerCommand : public InteractiveCommand, public WebSocketServerDelegate, public RemoteDataModelLoggerDelegate
Expand Down

0 comments on commit e3c757d

Please sign in to comment.