Chat-O-Matic/application/views/RenderView.cpp
Jaidyn Ann 5966df1a34 Print a 'date divider' when the day changes
I.E., if one message is sent on 2021-05-02 and the next on 2021-05-03, a
divider displaying the new date is printed before the second message.
2021-07-25 11:28:07 -05:00

72 lines
1.5 KiB
C++

/*
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "RenderView.h"
#include <InterfaceDefs.h>
RenderView::RenderView(const char* name)
:
RunView(name),
fLastDay(364),
fLastYear(64)
{
}
void
RenderView::AppendMessage(const char* nick, const char* message,
rgb_color nameColor, time_t time)
{
if (BString(message).IsEmpty() == true) return;
AppendTimestamp(time);
Append("<", nameColor);
Append(nick);
Append("> ", nameColor);
Append(message);
if (BString(message).EndsWith("\n") == false) Append("\n");
}
void
RenderView::AppendGeneric(const char* message)
{
if (BString(message).IsEmpty() == true) return;
AppendTimestamp(time(NULL));
Append(message, ui_color(B_PANEL_TEXT_COLOR), B_BOLD_FACE);
if (BString(message).EndsWith("\n") == false) Append("\n");
}
void
RenderView::AppendTimestamp(time_t time)
{
tm* tm = localtime(&time);
// If day changed, print date divider
if (fLastDay < tm->tm_yday || fLastYear < tm->tm_year) {
char datestamp[11] = { '\0' };
strftime(datestamp, 10, "%Y-%m-%d", tm);
BString stamp("――― %date% ―――\n");
stamp.ReplaceAll("%date%", datestamp);
Append(stamp.String(), ui_color(B_PANEL_TEXT_COLOR), B_ITALIC_FACE);
fLastDay = tm->tm_yday;
fLastYear = tm->tm_year;
}
if (time == 0) {
Append("[xx:xx] ", ui_color(B_LINK_HOVER_COLOR));
return;
}
char timestamp[9] = { '\0' };
strftime(timestamp, 8, "[%H:%M] ", tm);
Append(timestamp, ui_color(B_LINK_HOVER_COLOR));
}