My Project
shell_print.cpp
1 /*
2  * uuid-console - Microcontroller console shell
3  * Copyright 2019 Simon Arlott
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <uuid/console.h>
20 
21 #include <Arduino.h>
22 #include <stdarg.h>
23 
24 #include <string>
25 
26 namespace uuid {
27 
28 namespace console {
29 
30 size_t Shell::print(const std::string &data) {
31  if (data.empty()) {
32  return 0;
33  } else {
34  return write(reinterpret_cast<const uint8_t*>(data.c_str()), data.length());
35  }
36 }
37 
38 size_t Shell::println(const std::string &data) {
39  size_t len = print(data);
40  len += println();
41  return len;
42 }
43 
44 size_t Shell::printf(const char *format, ...) {
45  va_list ap;
46 
47  va_start(ap, format);
48  size_t len = vprintf(format, ap);
49  va_end(ap);
50 
51  return len;
52 }
53 
54 size_t Shell::printf(const __FlashStringHelper *format, ...) {
55  va_list ap;
56 
57  va_start(ap, format);
58  size_t len = vprintf(format, ap);
59  va_end(ap);
60 
61  return len;
62 }
63 
64 size_t Shell::printfln(const char *format, ...) {
65  va_list ap;
66 
67  va_start(ap, format);
68  size_t len = vprintf(format, ap);
69  va_end(ap);
70 
71  len += println();
72  return len;
73 }
74 
75 size_t Shell::printfln(const __FlashStringHelper *format, ...) {
76  va_list ap;
77 
78  va_start(ap, format);
79  size_t len = vprintf(format, ap);
80  va_end(ap);
81 
82  len += println();
83  return len;
84 }
85 
86 size_t Shell::vprintf(const char *format, va_list ap) {
87  size_t print_len = 0;
88  va_list copy_ap;
89 
90  va_copy(copy_ap, ap);
91 
92  int format_len = ::vsnprintf(nullptr, 0, format, ap);
93  if (format_len > 0) {
94  std::string text(static_cast<std::string::size_type>(format_len), '\0');
95 
96  ::vsnprintf(&text[0], text.capacity() + 1, format, copy_ap);
97  print_len = print(text);
98  }
99 
100  va_end(copy_ap);
101  return print_len;
102 }
103 
104 size_t Shell::vprintf(const __FlashStringHelper *format, va_list ap) {
105  size_t print_len = 0;
106  va_list copy_ap;
107 
108  va_copy(copy_ap, ap);
109 
110  int format_len = ::vsnprintf_P(nullptr, 0, reinterpret_cast<PGM_P>(format), ap);
111  if (format_len > 0) {
112  std::string text(static_cast<std::string::size_type>(format_len), '\0');
113 
114  ::vsnprintf_P(&text[0], text.capacity() + 1, reinterpret_cast<PGM_P>(format), copy_ap);
115  print_len = print(text);
116  }
117 
118  va_end(copy_ap);
119  return print_len;
120 }
121 
123  for (auto &available_command : available_commands()) {
124  CommandLine command_line{available_command.name(), available_command.arguments()};
125 
126  command_line.escape_initial_parameters(available_command.name().size());
127  println(command_line.to_string(maximum_command_line_length()));
128  }
129 }
130 
132  print(F("\033[G\033[K"));
133 }
134 
135 void Shell::erase_characters(size_t count) {
136  print(std::string(count, '\x08'));
137  print(F("\033[K"));
138 }
139 
140 } // namespace console
141 
142 } // namespace uuid
uuid::console::Shell::maximum_command_line_length
size_t maximum_command_line_length() const
Get the maximum length of a command line.
Definition: shell.cpp:398
uuid::console::CommandLine
Representation of a command line, with parameters separated by spaces and an optional trailing space.
Definition: console.h:1642
uuid::console::Shell::available_commands
Commands::AvailableCommands available_commands() const
Get the available commands in the current context and with the current flags.
Definition: shell.cpp:98
uuid::console::Shell::print
size_t print(const std::string &data)
Output a string.
Definition: shell_print.cpp:30
uuid::console::CommandLine::escape_initial_parameters
void escape_initial_parameters()
Only escape the number of parameters that currently exist when formatting the command line for output...
Definition: console.h:1728
uuid::console::Shell::printfln
size_t printfln(const char *format,...)
Output a message followed by CRLF end of line characters.
Definition: shell_print.cpp:64
uuid::console::Shell::println
size_t println(const std::string &data)
Output a string followed by CRLF end of line characters.
Definition: shell_print.cpp:38
uuid::console::Shell::vprintf
size_t vprintf(const char *format, va_list ap)
Output a message.
Definition: shell_print.cpp:86
uuid::console::Shell::printf
size_t printf(const char *format,...)
Output a message.
Definition: shell_print.cpp:44
uuid::console::Shell::print_all_available_commands
void print_all_available_commands()
Output a list of all available commands with their arguments.
Definition: shell_print.cpp:122
uuid
Common utilities.
Definition: get_uptime_ms.cpp:28
uuid::console::Shell::write
size_t write(uint8_t data) final override
Write one byte to the output stream.
Definition: shell_stream.cpp:116
uuid::console::Shell::erase_current_line
virtual void erase_current_line()
Output ANSI escape sequence to erase the current line.
Definition: shell_print.cpp:131
uuid::console::Shell::erase_characters
virtual void erase_characters(size_t count)
Output ANSI escape sequence to erase characters.
Definition: shell_print.cpp:135