Skip to content

Commit

Permalink
Regression bugfix: harstatus string fails to display date & time
Browse files Browse the repository at this point in the history
bug #66003

Signed-off-by: Alexander Naumov <[email protected]>
  • Loading branch information
alexander-naumov committed Jul 29, 2024
1 parent adfc581 commit d8b70b6
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 3 deletions.
22 changes: 22 additions & 0 deletions src/doc/screen.1
Original file line number Diff line number Diff line change
Expand Up @@ -4050,6 +4050,20 @@ is used instead.
Here is the full list of supported escapes:
.IP %
the escape character itself
.IP A
AM or PM. 'AM' stands for the Latin 'ante meridiem', translating to
"before midday". This is the time before noon. 'PM' stands for post
meridiem or 'after midday' – the time after noon.
.IP a
the same as 'A', but written in small letters: 'am' or 'pm'.
.IP C
12 hours time format like 1:48
.IP c
the same like 'C', but uses 24 hours time format (for example, 13:48)
.IP D
day of the week (for example, Mon, Wed, Fri)
.IP d
day of the month (number).
.IP E
sets %? to true if the escape character has been pressed.
.IP e
Expand All @@ -4064,6 +4078,10 @@ hardstatus of the window
hostname of the system
.IP O
The count of screen windows. Prefix with '-' to limit to current window group.
.IP M
name of the month (for example, Aug, Dec)
.IP m
month of the year (number)
.IP n
window number
.IP P
Expand All @@ -4086,6 +4104,10 @@ all window numbers and names except the current one
the executed command including arguments running in this windows
.IP X
the executed command without arguments running in this windows
.IP Y
year (four numbers like '2024')
.IP y
last two digits of the year (for example, '24')
.IP ?
the part to the next '%?' is displayed only if a '%' escape
inside the part expands to a non-empty string
Expand Down
123 changes: 121 additions & 2 deletions src/winmsg.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* Copyright (c) 2013
/* Copyrigth (c) 2024
* Alexander Naumov ([email protected])
* Copyright (c) 2013
* Mike Gerwitz ([email protected])
* Copyright (c) 2010
* Juergen Weigert ([email protected])
Expand Down Expand Up @@ -53,6 +55,11 @@ WinMsgBuf *g_winmsg;
/* maximum limit on MakeWinMsgEv recursion */
#define WINMSG_RECLIMIT 10

#ifndef USE_LOCALE
static const char days[] = "SunMonTueWedThuFriSat";
static const char months[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
#endif

/* escape char for backtick output */
#define WINMSG_BT_ESC '\005'

Expand Down Expand Up @@ -406,6 +413,75 @@ winmsg_esc_ex(WinArgv, Window *win)
}
}

static void
__WinMsgEscEsc_YEAR(WinMsgBufContext *wmbc, struct tm *tm)
{
wmbc_printf(wmbc, "%04d", tm->tm_year + 1900);
}

static void
__WinMsgEscEsc_year(WinMsgBufContext *wmbc, struct tm *tm)
{
wmbc_printf(wmbc, "%02d", tm->tm_year % 100);
}

static void
__WinMsgEscEsc_MONTH(WinMsgBufContext *wmbc, struct tm *tm)
{
#ifdef USE_LOCALE
strftime(wmbc, l, (longflg ? "%B" : "%b"), tm);
#else
wmbc_printf(wmbc, "%3.3s", months + 3 * tm->tm_mon);
#endif
}

static void
__WinMsgEscEsc_month(WinMsgBufContext *wmbc, struct tm *tm)
{
wmbc_printf(wmbc, "%02d", tm->tm_mon + 1);
}

static void
__WinMsgEscEsc_DAY(WinMsgBufContext *wmbc, struct tm *tm)
{
#ifdef USE_LOCALE
strftime(wmbc, l, (longflg ? "%A" : "%a"), tm);
#else
wmbc_printf(wmbc, "%3.3s", days + 3 * tm->tm_wday);
#endif
}

static void
__WinMsgEscEsc_day(WinMsgBufContext *wmbc, struct tm *tm)
{
wmbc_printf(wmbc, "%02d", tm->tm_mday % 100);
}

static void
__WinMsgEscEsc_HOUR(WinMsgBufContext *wmbc, struct tm *tm)
{
wmbc_printf(wmbc, tm->tm_hour >= 12 ? "PM" : "AM");
}

static void
__WinMsgEscEsc_hour(WinMsgBufContext *wmbc, struct tm *tm)
{
wmbc_printf(wmbc, tm->tm_hour >= 12 ? "pm" : "am");
}


static void
__WinMsgEscEsc_TIME(WinMsgBufContext *wmbc, struct tm *tm, WinMsgEsc esc)
{
wmbc_printf(wmbc, esc.flags.zero ? "%02d:%02d" : "%2d:%02d", (tm->tm_hour + 11) % 12 + 1, tm->tm_min);
}

static void
__WinMsgEscEsc_time(WinMsgBufContext *wmbc, struct tm *tm, WinMsgEsc esc)
{
wmbc_printf(wmbc, esc.flags.zero ? "%02d:%02d" : "%2d:%02d", tm->tm_hour, tm->tm_min);
}

winmsg_esc_ex(WinNum, Window *win)
{
if (esc->num == 0)
Expand Down Expand Up @@ -548,14 +624,20 @@ char *MakeWinMsgEv(WinMsgBuf *winmsg, char *str, Window *win,
int chesc, int padlen, Event *ev, int rec)
{
static int tick;
struct timeval now;
int qmnumrend = 0;
int numpad = 0;
int lastpad = 0;
WinMsgBufContext *wmbc;
WinMsgEsc esc;
WinMsgCond *cond;

struct tm *tm;
struct timeval now;
tm = 0;
gettimeofday(&now, NULL);
time_t nowsec = now.tv_sec;
tm = localtime(&nowsec);

/* TODO: temporary to work into existing code */
if (winmsg == NULL) {
if (g_winmsg == NULL) {
Expand Down Expand Up @@ -615,7 +697,44 @@ char *MakeWinMsgEv(WinMsgBuf *winmsg, char *str, Window *win,
if ((esc.flags.lng = (*s == 'L')) != 0)
s++;

if (!tick || tick > 3600)
tick = 3600;

switch (*s) {
case WINESC_TIME:
__WinMsgEscEsc_TIME(wmbc, tm, esc);
if (!tick || tick > 60)
tick = 60;
break;
case WINESC_time:
__WinMsgEscEsc_time(wmbc, tm, esc);
if (!tick || tick > 60)
tick = 60;
break;
case WINESC_HOUR:
__WinMsgEscEsc_HOUR(wmbc, tm);
break;
case WINESC_hour:
__WinMsgEscEsc_hour(wmbc, tm);
break;
case WINESC_DAY:
__WinMsgEscEsc_DAY(wmbc, tm);
break;
case WINESC_day:
__WinMsgEscEsc_day(wmbc, tm);
break;
case WINESC_MONTH:
__WinMsgEscEsc_MONTH(wmbc, tm);
break;
case WINESC_month:
__WinMsgEscEsc_month(wmbc, tm);
break;
case WINESC_YEAR:
__WinMsgEscEsc_YEAR(wmbc, tm);
break;
case WINESC_year:
__WinMsgEscEsc_year(wmbc, tm);
break;
case WINESC_COND:
WinMsgDoEscEx(Cond, &qmnumrend);
break;
Expand Down
14 changes: 13 additions & 1 deletion src/winmsg.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* Copyright (c) 2013
/* Copyrigth (c) 2024
* Alexander Naumov ([email protected])
* Copyright (c) 2013
* Mike Gerwitz ([email protected])
* Copyright (c) 2010
* Juergen Weigert ([email protected])
Expand Down Expand Up @@ -46,12 +48,20 @@

/* escape characters (alphabetical order) */
typedef enum {
WINESC_TIME = 'C',
WINESC_time = 'c',
WINESC_HOUR = 'A',
WINESC_hour = 'a',
WINESC_DAY = 'D',
WINESC_day = 'd',
WINESC_ESC_SEEN = 'E',
WINESC_FOCUS = 'F',
WINESC_WFLAGS = 'f',
WINESC_WIN_GROUP = 'g',
WINESC_HOST = 'H',
WINESC_HSTATUS = 'h',
WINESC_MONTH = 'M',
WINESC_month = 'm',
WINESC_WIN_LOGNAME = 'N',
WINESC_WIN_NUM = 'n',
WINESC_WIN_COUNT = 'O',
Expand All @@ -65,6 +75,8 @@ typedef enum {
WINESC_WIN_NAMES = 'w',
WINESC_CMD = 'X',
WINESC_CMD_ARGS = 'x',
WINESC_YEAR = 'Y',
WINESC_year = 'y',
WINESC_REND_START = '{',
WINESC_REND_END = '}',
WINESC_REND_POP = '-',
Expand Down

0 comments on commit d8b70b6

Please sign in to comment.