Skip to content

Commit

Permalink
feat(cli): add support for tab completion (#167)
Browse files Browse the repository at this point in the history
* feat(cli): add support for tab completion
* Updated Cli to support pressing tab to tab complete commands in the current menu and submenus

* rebuild docs

* fix static analysis
  • Loading branch information
finger563 authored Mar 5, 2024
1 parent 7ea3174 commit d5ae840
Show file tree
Hide file tree
Showing 100 changed files with 246 additions and 198 deletions.
3 changes: 2 additions & 1 deletion components/cli/include/cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ class Cli : private cli::CliSession {
Prompt();
if (!in.good())
Exit();
auto line = line_input_.get_user_input(in, [this]() { Prompt(); });
auto line = line_input_.get_user_input(
in, [this]() { Prompt(); }, [this](auto line) { return GetCompletions(line); });
if (in.eof()) {
Exit();
} else {
Expand Down
40 changes: 39 additions & 1 deletion components/cli/include/line_input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class LineInput {
/// function for printing the prompt if there is one
typedef std::function<void(void)> prompt_fn;

/// function for getting completions for a given input
typedef std::function<std::vector<std::string>(const std::string &)> get_completions_fn;

/// Storage for the input history as a double-ended queue of strings
typedef std::deque<std::string> History;

Expand Down Expand Up @@ -115,9 +118,13 @@ class LineInput {
* @brief Get user input with arrow key and backspace support
* @param is Reference to a std::istream from which to read input
* @param prompt Function to show prompt at the beginning of the line
* (optional)
* @param get_completions Function to get completions for the current input
* (optional)
* @return User input as a std::string
*/
std::string get_user_input(std::istream &is, prompt_fn prompt = nullptr) {
std::string get_user_input(std::istream &is, prompt_fn prompt = nullptr,
get_completions_fn get_completions = nullptr) {
int start_pos_x, start_pos_y;
get_cursor_position(start_pos_x, start_pos_y);

Expand Down Expand Up @@ -212,10 +219,41 @@ class LineInput {
redraw(start_pos_x, input, prompt);
pos_x--;
}
} else if (ch == '\t') {
// handle tab key for completions, printing them all in one line if there are multiple
// or just inserting the completion if there is only one
auto current_line = input;
auto completions =
get_completions ? get_completions(current_line) : std::vector<std::string>();
if (completions.size() == 1) {
// need to clear the line and redraw the prompt and input, and move
// the cursor to the end
clear_line();
input = completions[0];
redraw(start_pos_x, input, prompt);
pos_x = start_pos_x + input.size();
} else if (completions.size() > 1) {
// print all the completions in one line below the current input,
// then move the cursor back to the input line
std::cout << std::endl;
// make sure to clear to the end of the line
clear_to_end_of_line();
for (const auto &completion : completions) {
std::cout << completion << " ";
}
// need to handle the case where we were at the bottom of the screen,
// so we need to move the cursor up one line before printing the
// completions and then move it back down after
if (pos_y == terminal_height_) {
pos_y--;
}
}
} else if (ch == '\n') { // Enter
// print a new line to move to the next line, since this was the end
// of input
fmt::print("\n");
// and clear the line from the cursor to the end
clear_to_end_of_line();
break;
} else { // Regular character
input.insert(input.begin() + pos_x - start_pos_x, ch);
Expand Down
2 changes: 1 addition & 1 deletion docs/adc/adc_types.html
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>ADC Types</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/0cb92927/docs/en/adc/adc_types.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/82a5c3ce/docs/en/adc/adc_types.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/ads1x15.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>ADS1x15 I2C ADC</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/0cb92927/docs/en/adc/ads1x15.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/82a5c3ce/docs/en/adc/ads1x15.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -168,7 +168,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/0cb92927/components/ads1x15/include/ads1x15.hpp">components/ads1x15/include/ads1x15.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/82a5c3ce/components/ads1x15/include/ads1x15.hpp">components/ads1x15/include/ads1x15.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/ads7138.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>ADS7138 I2C ADC</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/0cb92927/docs/en/adc/ads7138.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/82a5c3ce/docs/en/adc/ads7138.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -173,7 +173,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/0cb92927/components/ads7138/include/ads7138.hpp">components/ads7138/include/ads7138.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/82a5c3ce/components/ads7138/include/ads7138.hpp">components/ads7138/include/ads7138.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/continuous_adc.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>Continuous ADC</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/0cb92927/docs/en/adc/continuous_adc.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/82a5c3ce/docs/en/adc/continuous_adc.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -173,7 +173,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/0cb92927/components/adc/include/continuous_adc.hpp">components/adc/include/continuous_adc.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/82a5c3ce/components/adc/include/continuous_adc.hpp">components/adc/include/continuous_adc.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
2 changes: 1 addition & 1 deletion docs/adc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
<li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
<li>ADC APIs</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/0cb92927/docs/en/adc/index.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/82a5c3ce/docs/en/adc/index.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/oneshot_adc.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>Oneshot ADC</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/0cb92927/docs/en/adc/oneshot_adc.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/82a5c3ce/docs/en/adc/oneshot_adc.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -172,7 +172,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/0cb92927/components/adc/include/oneshot_adc.hpp">components/adc/include/oneshot_adc.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/82a5c3ce/components/adc/include/oneshot_adc.hpp">components/adc/include/oneshot_adc.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/tla2528.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>TLA2528 I2C ADC</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/0cb92927/docs/en/adc/tla2528.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/82a5c3ce/docs/en/adc/tla2528.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -173,7 +173,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/0cb92927/components/tla2528/include/tla2528.hpp">components/tla2528/include/tla2528.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/82a5c3ce/components/tla2528/include/tla2528.hpp">components/tla2528/include/tla2528.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
4 changes: 2 additions & 2 deletions docs/base_component.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
<li><a href="index.html" class="icon icon-home"></a> &raquo;</li>
<li>Base Compoenent</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/0cb92927/docs/en/base_component.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/82a5c3ce/docs/en/base_component.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -162,7 +162,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/0cb92927/components/base_component/include/base_component.hpp">components/base_component/include/base_component.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/82a5c3ce/components/base_component/include/base_component.hpp">components/base_component/include/base_component.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
4 changes: 2 additions & 2 deletions docs/base_peripheral.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
<li><a href="index.html" class="icon icon-home"></a> &raquo;</li>
<li>Base Peripheral</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/0cb92927/docs/en/base_peripheral.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/82a5c3ce/docs/en/base_peripheral.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -166,7 +166,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/0cb92927/components/base_peripheral/include/base_peripheral.hpp">components/base_peripheral/include/base_peripheral.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/82a5c3ce/components/base_peripheral/include/base_peripheral.hpp">components/base_peripheral/include/base_peripheral.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
2 changes: 1 addition & 1 deletion docs/battery/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
<li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
<li>Battery APIs</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/0cb92927/docs/en/battery/index.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/82a5c3ce/docs/en/battery/index.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand Down
4 changes: 2 additions & 2 deletions docs/battery/max1704x.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
<li><a href="index.html">Battery APIs</a> &raquo;</li>
<li>MAX1704X</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/0cb92927/docs/en/battery/max1704x.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/82a5c3ce/docs/en/battery/max1704x.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand Down Expand Up @@ -180,7 +180,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/0cb92927/components/max1704x/include/max1704x.hpp">components/max1704x/include/max1704x.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/82a5c3ce/components/max1704x/include/max1704x.hpp">components/max1704x/include/max1704x.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
4 changes: 2 additions & 2 deletions docs/bldc/bldc_driver.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
<li><a href="index.html">BLDC APIs</a> &raquo;</li>
<li>BLDC Driver</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/0cb92927/docs/en/bldc/bldc_driver.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/82a5c3ce/docs/en/bldc/bldc_driver.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -164,7 +164,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/0cb92927/components/bldc_driver/include/bldc_driver.hpp">components/bldc_driver/include/bldc_driver.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/82a5c3ce/components/bldc_driver/include/bldc_driver.hpp">components/bldc_driver/include/bldc_driver.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
8 changes: 4 additions & 4 deletions docs/bldc/bldc_motor.html
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
<li><a href="index.html">BLDC APIs</a> &raquo;</li>
<li>BLDC Motor</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/0cb92927/docs/en/bldc/bldc_motor.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/82a5c3ce/docs/en/bldc/bldc_motor.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand Down Expand Up @@ -180,7 +180,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/0cb92927/components/bldc_motor/include/bldc_motor.hpp">components/bldc_motor/include/bldc_motor.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/82a5c3ce/components/bldc_motor/include/bldc_motor.hpp">components/bldc_motor/include/bldc_motor.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down Expand Up @@ -652,13 +652,13 @@ <h4>Example Usage<a class="headerlink" href="#classespp_1_1BldcMotor_1bldc_motor
<section id="id1">
<h3>Header File<a class="headerlink" href="#id1" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/0cb92927/components/bldc_motor/include/bldc_types.hpp">components/bldc_motor/include/bldc_types.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/82a5c3ce/components/bldc_motor/include/bldc_types.hpp">components/bldc_motor/include/bldc_types.hpp</a></p></li>
</ul>
</section>
<section id="id2">
<h3>Header File<a class="headerlink" href="#id2" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/0cb92927/components/bldc_motor/include/sensor_direction.hpp">components/bldc_motor/include/sensor_direction.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/82a5c3ce/components/bldc_motor/include/sensor_direction.hpp">components/bldc_motor/include/sensor_direction.hpp</a></p></li>
</ul>
</section>
</section>
Expand Down
2 changes: 1 addition & 1 deletion docs/bldc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
<li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
<li>BLDC APIs</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/0cb92927/docs/en/bldc/index.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/82a5c3ce/docs/en/bldc/index.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand Down
4 changes: 2 additions & 2 deletions docs/ble/battery_service.html
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
<li><a href="index.html">BLE APIs</a> &raquo;</li>
<li>Battery Service</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/0cb92927/docs/en/ble/battery_service.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/82a5c3ce/docs/en/ble/battery_service.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -167,7 +167,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/0cb92927/components/ble_gatt_server/include/battery_service.hpp">components/ble_gatt_server/include/battery_service.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/82a5c3ce/components/ble_gatt_server/include/battery_service.hpp">components/ble_gatt_server/include/battery_service.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
Loading

0 comments on commit d5ae840

Please sign in to comment.