33 ,
description_<"Generate a simple command line interface for inspecting and sending data to the bound components.">
35 ,
copyright_<"Copyright 2023 Sygaldry contributors">
36 ,
license_<"SPDX-License-Identifier: MIT">
38 [[no_unique_address]] Logger log;
39 [[no_unique_address]] Reader reader;
40 [[no_unique_address]] Commands commands;
44 log.println(
"CLI enabled. Write `/help` for a list of available commands.");
49 static constexpr size_t MAX_ARGS = 5;
50 static constexpr size_t BUFFER_SIZE = 128;
52 char * argv[MAX_ARGS];
53 unsigned char write_pos = 0;
54 char buffer[BUFFER_SIZE];
56 void _try_to_match_and_execute(Components& components)
58 boost::pfr::for_each_field(commands, [&](
auto& command)
62 if constexpr (std::is_same_v<
decltype(command),
Help&>)
64 retcode = command.main(log, commands);
66 else retcode = command.main(argc, argv, log, components);
67 if (retcode != 0) _complain_about_command_failure(retcode);
70 bool _is_whitespace(
char c)
72 if (c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\r')
return true;
78 return write_pos == 0 || buffer[write_pos-1] == 0;
81 bool _overflow()
const
83 return argc == MAX_ARGS || write_pos == BUFFER_SIZE-1;
89 for (
int i = 0; i < argc - 1; ++i) log.print(argv[i],
" ");
90 if (argc > 0) log.print(argv[argc - 1]);
99 void _complain_about_command_failure(
int retcode)
101 log.println(
"command failed!");
104 void process(
const char c, Components& components)
106 if (_is_whitespace(c))
107 buffer[write_pos++] = 0;
110 buffer[write_pos] = c;
111 buffer[write_pos+1] = 0;
113 argv[argc++] = &buffer[write_pos];
117 if (c ==
'\n' || c ==
'\r')
120 _try_to_match_and_execute(components);
126 log.println(
"CLI line buffer overflow!");
133 void external_sources(Components& components)
135 while(reader.ready()) process(reader.getchar(), components);
bool osc_match_pattern(const char *pattern, const char *address)
Match an OSC address pattern against the given address.
Definition sygbp-osc_match_pattern.cpp:12
Document a textual description of an entity, e.g. an endpoint, component or binding.
Definition sygah-metadata.hpp:35
Document the name of an entity, e.g. an endpoint, component, or binding.
Definition sygah-metadata.hpp:33