25#include <wx/clipbrd.h>
26#include <wx/dcclient.h>
29#include <wx/textctrl.h>
38#include <wx/msw/winundef.h>
47static bool IsFilterMatch(
const struct Logline& ll,
const std::string& s) {
48 if (ll.navmsg->source->iface.find(s) != std::string::npos)
return true;
49 return ll.message.find(s) != std::string::npos;
52static void UpdateColor(wxColour& colour,
unsigned rgb) {
53 if (rgb == kUndefinedColor) {
54 colour = wxNullColour;
60static std::string Timestamp(
const NavmsgTimePoint& when) {
62 using namespace std::chrono;
64 auto now = when.time_since_epoch();
65 auto _hours = duration_cast<hours>(now);
67 auto _minutes = duration_cast<minutes>(now);
69 auto _seconds = duration_cast<seconds>(now);
71 auto ms = duration_cast<milliseconds>(now);
72#ifdef CSTDIO_TTYSCROLL_TIMESTAMP
75 snprintf(buf,
sizeof(buf),
"%02ld:%02ld:%02ld.%03ld", _hours.count() % 24,
76 _minutes.count(), _seconds.count(), ms.count());
80 ss << setw(2) << setfill(
'0') << _hours.count() % 24 <<
":" << setw(2)
81 << _minutes.count() <<
":" << setw(2) << _seconds.count() <<
"." << setw(3)
89 static const wxColor kDarkGreen(30, 72, 56);
90 if (ns.status != NavmsgStatus::State::kOk)
91 color = wxColour(
"RED");
92 else if (ns.accepted == NavmsgStatus::Accepted::kFilteredNoOutput)
93 color = wxColour(
"CORAL");
94 else if (ns.accepted == NavmsgStatus::Accepted::kFilteredDropped)
95 color = wxColour(
"MAROON");
96 else if (ns.direction == NavmsgStatus::Direction::kOutput)
97 color = wxColour(
"BLUE");
98 else if (ns.direction == NavmsgStatus::Direction::kHandled)
101 color = wxColour(
"ORANGE");
107 if (ns.status != NavmsgStatus::State::kOk)
108 UpdateColor(color, g_dm_not_ok);
109 else if (ns.accepted == NavmsgStatus::Accepted::kFilteredNoOutput)
110 UpdateColor(color, g_dm_filtered);
111 else if (ns.accepted == NavmsgStatus::Accepted::kFilteredDropped)
112 UpdateColor(color, g_dm_dropped);
113 else if (ns.direction == NavmsgStatus::Direction::kOutput)
114 UpdateColor(color, g_dm_output);
115 else if (ns.direction == NavmsgStatus::Direction::kInput)
116 UpdateColor(color, g_dm_input);
118 UpdateColor(color, g_dm_ok);
119 return color.IsOk() ? color : defaults(ns);
125 if (!ll.message.empty()) ws << Timestamp(ll.navmsg->created_at) <<
" ";
126 if (ll.state.direction == NavmsgStatus::Direction::kOutput)
127 ws <<
" " << kUtfRightArrow <<
" ";
128 else if (ll.state.direction == NavmsgStatus::Direction::kInput)
129 ws <<
" " << kUtfLeftwardsArrowToBar <<
" ";
130 else if (ll.state.direction == NavmsgStatus::Direction::kInternal)
131 ws <<
" " << kUtfLeftRightArrow <<
" ";
133 ws <<
" " << kUtfLeftArrow <<
" ";
135 if (ll.state.status != NavmsgStatus::State::kOk)
136 ws << kUtfMultiplicationX;
137 else if (ll.state.accepted == NavmsgStatus::Accepted::kFilteredNoOutput)
138 ws << kUtfFallingDiagonal;
139 else if (ll.state.accepted == NavmsgStatus::Accepted::kFilteredDropped)
140 ws << kUtfCircledDivisionSlash;
144 std::stringstream error_msg;
145 if (ll.state.status != NavmsgStatus::State::kOk) {
147 << (ll.error_msg.empty() ?
"Unknown errror" : ll.error_msg);
149 std::string iface(ll.navmsg ? ll.navmsg->source->iface :
"");
150 if (iface.size() > 30)
151 iface = std::string(
"...") + iface.substr(iface.size() - 27);
154 dc.DrawText(ws, 0, y);
156 ws << ll.message << error_msg.str();
157 dc.DrawText(ws, data_pos, y);
159 std::max(m_text_width, GetTextExtent(ws).GetWidth() + data_pos);
163 : wxScrolledWindow(parent), m_n_lines(n_lines), m_is_paused(false) {
164 SetName(
"TtyScroll");
166 dc.GetTextExtent(
"Line Height", NULL, &m_line_height);
167 SetScrollRate(m_line_height, m_line_height);
168 for (
unsigned i = 0; i < m_n_lines; i++) m_lines.push_back(
Logline());
169 SetColors(std::make_unique<UserColorsByState>());
170 Bind(wxEVT_SIZE, [&](wxSizeEvent& ev) { OnSize(ev); });
173void TtyScroll::OnSize(wxSizeEvent& ev) {
174 m_n_lines = ev.GetSize().y / GetCharHeight();
175 while (m_lines.size() < m_n_lines) m_lines.push_back(
Logline());
180 if (m_is_paused || !m_filter.
Pass(ll.state, ll.navmsg))
return;
181 if (!m_quick_filter.empty() && !IsFilterMatch(ll, m_quick_filter))
return;
182 while (m_lines.size() > m_n_lines - 1) m_lines.pop_front();
183 m_lines.push_back(ll);
188 m_color_by_state = std::move(color_by_state);
191void TtyScroll::OnDraw(wxDC& dc) {
193 wxRect rect_update = GetUpdateRegion().GetBox();
194 CalcUnscrolledPosition(rect_update.x, rect_update.y, &rect_update.x,
196 size_t line_from = rect_update.y / m_line_height;
197 size_t line_to = rect_update.GetBottom() / m_line_height;
198 if (line_to > m_n_lines - 1) line_to = m_n_lines - 1;
200 wxCoord y = line_from * m_line_height;
202 for (
size_t line = line_from; line <= line_to; line++) {
204 dc.SetTextForeground((*m_color_by_state)(m_lines[line].state));
205 DrawLine(dc, m_lines[line], 40 * GetCharWidth(), y);
208 SetVirtualSize(m_text_width, (m_n_lines + 1) * m_line_height);
212 std::stringstream ss;
213 for (
auto& line : m_lines) ss << line.message <<
"\n";
214 if (wxTheClipboard->Open()) {
215 wxTheClipboard->SetData(
new wxTextDataObject(ss.str()));
216 wxTheClipboard->Close();
bool Pass(NavmsgStatus status, const std::shared_ptr< const NavMsg > &msg)
Return true if message is not matched by filter.
Representation of message status as determined by the multiplexer.
Global variables stored in configuration file.
General observable implementation with several specializations.