25#include <wx/clipbrd.h>
26#include <wx/dcclient.h>
28#include <wx/textctrl.h>
36#include <wx/msw/winundef.h>
45static bool IsFilterMatch(
const struct Logline& ll,
const std::string& s) {
46 if (ll.navmsg->source->iface.find(s) != std::string::npos)
return true;
47 return ll.message.find(s) != std::string::npos;
50static std::string Timestamp(
const NavmsgTimePoint& when) {
52 using namespace std::chrono;
54 auto now = when.time_since_epoch();
55 auto _hours = duration_cast<hours>(now);
57 auto _minutes = duration_cast<minutes>(now);
59 auto _seconds = duration_cast<seconds>(now);
61 auto ms = duration_cast<milliseconds>(now);
62#ifdef CSTDIO_TTYSCROLL_TIMESTAMP
65 snprintf(buf,
sizeof(buf),
"%02ld:%02ld:%02ld.%03ld", _hours.count() % 24,
66 _minutes.count(), _seconds.count(), ms.count());
70 ss << setw(2) << setfill(
'0') << _hours.count() % 24 <<
":" << setw(2)
71 << _minutes.count() <<
":" << setw(2) << _seconds.count() <<
"." << setw(3)
79 static const wxColor kDarkGreen(30, 72, 56);
80 if (ns.status != NavmsgStatus::State::kOk)
81 color = wxColour(
"RED");
82 else if (ns.accepted == NavmsgStatus::Accepted::kFilteredNoOutput)
83 color = wxColour(
"CORAL");
84 else if (ns.accepted == NavmsgStatus::Accepted::kFilteredDropped)
85 color = wxColour(
"MAROON");
86 else if (ns.direction == NavmsgStatus::Direction::kOutput)
87 color = wxColour(
"BLUE");
88 else if (ns.direction == NavmsgStatus::Direction::kInput)
89 color = wxColour(
"ORANGE");
98 if (!ll.message.empty()) ws << Timestamp(ll.navmsg->created_at) <<
" ";
99 if (ll.state.direction == NavmsgStatus::Direction::kOutput)
100 ws <<
" " << kUtfRightArrow <<
" ";
101 else if (ll.state.direction == NavmsgStatus::Direction::kInput)
102 ws <<
" " << kUtfLeftwardsArrowToBar <<
" ";
103 else if (ll.state.direction == NavmsgStatus::Direction::kInternal)
104 ws <<
" " << kUtfLeftRightArrow <<
" ";
106 ws <<
" " << kUtfLeftArrow <<
" ";
108 if (ll.state.status != NavmsgStatus::State::kOk)
109 ws << kUtfMultiplicationX;
110 else if (ll.state.accepted == NavmsgStatus::Accepted::kFilteredNoOutput)
111 ws << kUtfFallingDiagonal;
112 else if (ll.state.accepted == NavmsgStatus::Accepted::kFilteredDropped)
113 ws << kUtfCircledDivisionSlash;
117 std::stringstream error_msg;
118 if (ll.state.status != NavmsgStatus::State::kOk) {
120 << (ll.error_msg.empty() ?
"Unknown errror" : ll.error_msg);
122 std::string iface(ll.navmsg ? ll.navmsg->source->iface :
"");
123 if (iface.size() > 30)
124 iface = std::string(
"...") + iface.substr(iface.size() - 27);
127 dc.DrawText(ws, 0, y);
129 ws << ll.message << error_msg.str();
130 dc.DrawText(ws, data_pos, y);
132 std::max(m_text_width, GetTextExtent(ws).GetWidth() + data_pos);
136 : wxScrolledWindow(parent), m_n_lines(n_lines), m_is_paused(false) {
137 SetName(
"TtyScroll");
139 dc.GetTextExtent(
"Line Height", NULL, &m_line_height);
140 SetScrollRate(m_line_height, m_line_height);
141 for (
unsigned i = 0; i < m_n_lines; i++) m_lines.push_back(
Logline());
142 SetColors(std::make_unique<StdColorsByState>());
143 Bind(wxEVT_SIZE, [&](wxSizeEvent& ev) { OnSize(ev); });
146void TtyScroll::OnSize(wxSizeEvent& ev) {
147 m_n_lines = ev.GetSize().y / GetCharHeight();
148 while (m_lines.size() < m_n_lines) m_lines.push_back(
Logline());
153 if (m_is_paused || !m_filter.
Pass(ll.state, ll.navmsg))
return;
154 if (!m_quick_filter.empty() && !IsFilterMatch(ll, m_quick_filter))
return;
155 while (m_lines.size() > m_n_lines - 1) m_lines.pop_front();
156 m_lines.push_back(ll);
161 m_color_by_state = std::move(color_by_state);
164void TtyScroll::OnDraw(wxDC& dc) {
166 wxRect rect_update = GetUpdateRegion().GetBox();
167 CalcUnscrolledPosition(rect_update.x, rect_update.y, &rect_update.x,
169 size_t line_from = rect_update.y / m_line_height;
170 size_t line_to = rect_update.GetBottom() / m_line_height;
171 if (line_to > m_n_lines - 1) line_to = m_n_lines - 1;
173 wxCoord y = line_from * m_line_height;
175 for (
size_t line = line_from; line <= line_to; line++) {
177 dc.SetTextForeground((*m_color_by_state)(m_lines[line].state));
178 DrawLine(dc, m_lines[line], 40 * GetCharWidth(), y);
181 SetVirtualSize(m_text_width, (m_n_lines + 1) * m_line_height);
185 std::stringstream ss;
186 for (
auto& line : m_lines) ss << line.message <<
"\n";
187 if (wxTheClipboard->Open()) {
188 wxTheClipboard->SetData(
new wxTextDataObject(ss.str()));
189 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.
General observable implementation with several specializations.