openMSX
InfoCommand.cc
Go to the documentation of this file.
1 #include "InfoCommand.hh"
2 #include "TclObject.hh"
3 #include "CommandException.hh"
4 #include "unreachable.hh"
5 #include "view.hh"
6 #include <iostream>
7 #include <cassert>
8 
9 namespace openmsx {
10 
11 InfoCommand::InfoCommand(CommandController& commandController_, const std::string& name_)
12  : Command(commandController_, name_)
13 {
14 }
15 
17 {
18  assert(infoTopics.empty());
19 }
20 
22 {
23 #ifndef NDEBUG
24  if (infoTopics.contains(topic.getName())) {
25  std::cerr << "INTERNAL ERROR: already have an info topic with "
26  "name " << topic.getName() << '\n';
28  }
29 #endif
30  infoTopics.insert_noDuplicateCheck(&topic);
31 }
32 
34 {
35  if (!infoTopics.contains(topic.getName())) {
36  std::cerr << "INTERNAL ERROR: can't unregister topic with name "
37  << topic.getName() << ", not found!\n";
39  }
40  infoTopics.erase(topic.getName());
41 }
42 
43 // Command
44 
45 void InfoCommand::execute(span<const TclObject> tokens,
46  TclObject& result)
47 {
48  switch (tokens.size()) {
49  case 1:
50  // list topics
52  infoTopics, [](auto* t) { return t->getName(); }));
53  break;
54  default:
55  // show info about topic
56  assert(tokens.size() >= 2);
57  const auto& topic = tokens[1].getString();
58  auto it = infoTopics.find(topic);
59  if (it == end(infoTopics)) {
60  throw CommandException("No info on: ", topic);
61  }
62  (*it)->execute(tokens, result);
63  break;
64  }
65 }
66 
67 std::string InfoCommand::help(span<const TclObject> tokens) const
68 {
69  std::string result;
70  switch (tokens.size()) {
71  case 1:
72  // show help on info cmd
73  result = "Show info on a certain topic\n"
74  " info [topic] [...]\n";
75  break;
76  default:
77  // show help on a certain topic
78  assert(tokens.size() >= 2);
79  auto topic = tokens[1].getString();
80  auto it = infoTopics.find(topic);
81  if (it == end(infoTopics)) {
82  throw CommandException("No info on: ", topic);
83  }
84  result = (*it)->help(tokens);
85  break;
86  }
87  return result;
88 }
89 
90 void InfoCommand::tabCompletion(std::vector<std::string>& tokens) const
91 {
92  switch (tokens.size()) {
93  case 2: {
94  // complete topic
95  completeString(tokens, view::transform(infoTopics,
96  [](auto* t) -> std::string_view { return t->getName(); }));
97  break;
98  }
99  default:
100  // show help on a certain topic
101  assert(tokens.size() >= 3);
102  auto it = infoTopics.find(tokens[1]);
103  if (it != end(infoTopics)) {
104  (*it)->tabCompletion(tokens);
105  }
106  break;
107  }
108 }
109 
110 } // namespace openmsx
TclObject t
const std::string & getName() const
Definition: Completer.hh:23
static void completeString(std::vector< std::string > &tokens, ITER begin, ITER end, bool caseSensitive=true)
Definition: Completer.hh:133
void unregisterTopic(InfoTopic &topic)
Definition: InfoCommand.cc:33
void registerTopic(InfoTopic &topic)
Definition: InfoCommand.cc:21
InfoCommand(CommandController &commandController, const std::string &name)
Definition: InfoCommand.cc:11
void addListElements(ITER first, ITER last)
Definition: TclObject.hh:130
Definition: span.hh:126
constexpr index_type size() const noexcept
Definition: span.hh:296
This file implemented 3 utility functions:
Definition: Autofire.cc:9
constexpr auto transform(Range &&range, UnaryOp op)
Definition: view.hh:392
#define UNREACHABLE
Definition: unreachable.hh:38
constexpr auto end(const zstring_view &x)
Definition: zstring_view.hh:84