Skip to content

Commit

Permalink
Responsive interface bar in XTOP
Browse files Browse the repository at this point in the history
  • Loading branch information
kala13x committed Nov 19, 2024
1 parent f3f49e4 commit e3f1671
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 53 deletions.
74 changes: 37 additions & 37 deletions src/sys/xcli.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,42 +72,42 @@ XSTATUS XCLI_GetWindowSize(xcli_size_t *pCli)
csbi.srWindow.Right = csbi.srWindow.Left = 0;
csbi.srWindow.Top = csbi.srWindow.Bottom = 0;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
pCli->nWinColumns = (size_t)csbi.srWindow.Right - (size_t)csbi.srWindow.Left + 1;
pCli->nWinRows = (size_t)csbi.srWindow.Bottom - (size_t)csbi.srWindow.Top + 1;
pCli->nColumns = (size_t)csbi.srWindow.Right - (size_t)csbi.srWindow.Left + 1;
pCli->nRows = (size_t)csbi.srWindow.Bottom - (size_t)csbi.srWindow.Top + 1;
#else
struct winsize size;
size.ws_col = size.ws_row = 0;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
pCli->nWinColumns = size.ws_col;
pCli->nWinRows = size.ws_row;
pCli->nColumns = size.ws_col;
pCli->nRows = size.ws_row;
#endif

return (pCli->nWinColumns && pCli->nWinRows) ? XSTDOK : XSTDERR;
return (pCli->nColumns && pCli->nRows) ? XSTDOK : XSTDERR;
}

void XWindow_Init(xcli_win_t *pWin, xbool_t bAscii)
{
XArray_Init(&pWin->lineArray, NULL, 0, 0);
XArray_Init(&pWin->lines, NULL, 0, 0);
pWin->eType = XCLI_RENDER_FRAME;
pWin->frameSize.nWinColumns = 0;
pWin->frameSize.nWinRows = 0;
pWin->frame.nColumns = 0;
pWin->frame.nRows = 0;
pWin->bAscii = bAscii;
}

XSTATUS XWindow_UpdateSize(xcli_win_t *pWin)
{
XSTATUS nStatus = XCLI_GetWindowSize(&pWin->frameSize);
if (nStatus != XSTDERR) pWin->frameSize.nWinRows--;
XSTATUS nStatus = XCLI_GetWindowSize(&pWin->frame);
if (nStatus != XSTDERR) pWin->frame.nRows--;
return nStatus;
}

XSTATUS XWindow_AddLine(xcli_win_t *pWin, char *pLine, size_t nLength)
{
xcli_size_t *pSize = &pWin->frameSize;
xarray_t *pLines = &pWin->lineArray;
xcli_size_t *pFrame = &pWin->frame;
xarray_t *pLines = &pWin->lines;

if (XWindow_UpdateSize(pWin) == XSTDERR) return XSTDERR;
if (pLines->nUsed >= pSize->nWinRows) return XSTDNON;
if (pLines->nUsed >= pFrame->nRows) return XSTDNON;

if (XArray_AddData(pLines, pLine, nLength) < 0)
{
Expand All @@ -120,11 +120,11 @@ XSTATUS XWindow_AddLine(xcli_win_t *pWin, char *pLine, size_t nLength)

XSTATUS XWindow_AddLineFmt(xcli_win_t *pWin, const char *pFmt, ...)
{
xcli_size_t *pSize = &pWin->frameSize;
xarray_t *pLines = &pWin->lineArray;
xcli_size_t *pFrame = &pWin->frame;
xarray_t *pLines = &pWin->lines;

if (XWindow_UpdateSize(pWin) == XSTDERR) return XSTDERR;
if (pLines->nUsed >= pSize->nWinRows) return XSTDNON;
if (pLines->nUsed >= pFrame->nRows) return XSTDNON;

size_t nLength = 0;
va_list args;
Expand All @@ -149,7 +149,7 @@ XSTATUS XWindow_AddLineFmt(xcli_win_t *pWin, const char *pFmt, ...)
XSTATUS XWindow_AddEmptyLine(xcli_win_t *pWin)
{
if (XWindow_UpdateSize(pWin) == XSTDERR) return XSTDERR;
size_t nColumns = pWin->frameSize.nWinColumns;
size_t nColumns = pWin->frame.nColumns;
char emptyLine[XLINE_MAX];

size_t i, nSpaces = XSTD_MIN(nColumns, sizeof(emptyLine) - 1);
Expand All @@ -162,7 +162,7 @@ XSTATUS XWindow_AddEmptyLine(xcli_win_t *pWin)
XSTATUS XWindow_AddAligned(xcli_win_t *pWin, const char *pInput, const char *pFmt, uint8_t nAlign)
{
if (XWindow_UpdateSize(pWin) == XSTDERR) return XSTDERR;
size_t nColumns = pWin->frameSize.nWinColumns;
size_t nColumns = pWin->frame.nColumns;

size_t nInputLen = strlen(pInput);
if (!nInputLen) return XSTDERR;
Expand Down Expand Up @@ -206,7 +206,7 @@ int XWindow_ClearScreen(xbool_t bAscii)
XSTATUS XWindow_RenderLine(xcli_win_t *pWin, xbyte_buffer_t *pLine, xarray_data_t *pArrData)
{
if (XWindow_UpdateSize(pWin) == XSTDERR) return XSTDERR;
size_t nChars = 0, nMaxSize = pWin->frameSize.nWinColumns;
size_t nChars = 0, nMaxSize = pWin->frame.nColumns;

XByteBuffer_SetData(pLine, (uint8_t*)pArrData->pData, pArrData->nSize);
pLine->nSize = pArrData->nSize;
Expand Down Expand Up @@ -247,17 +247,17 @@ XSTATUS XWindow_RenderLine(xcli_win_t *pWin, xbyte_buffer_t *pLine, xarray_data_
return XSTDOK;
}

XSTATUS XWindow_GetFrame(xcli_win_t *pWin, xbyte_buffer_t *pFrame)
XSTATUS XWindow_GetFrame(xcli_win_t *pWin, xbyte_buffer_t *pFrameBuff)
{
if (pWin == NULL || pFrame == NULL) return XSTDERR;
XByteBuffer_Init(pFrame, XSTDNON, XSTDNON);
if (pWin == NULL || pFrameBuff == NULL) return XSTDERR;
XByteBuffer_Init(pFrameBuff, XSTDNON, XSTDNON);

while (pWin->lineArray.nUsed < pWin->frameSize.nWinRows)
while (pWin->lines.nUsed < pWin->frame.nRows)
if (XWindow_AddEmptyLine(pWin) < 0) return XSTDERR;

xcli_size_t *pSize = &pWin->frameSize;
xarray_t *pLines = &pWin->lineArray;
size_t i, nRows = XSTD_MIN(pSize->nWinRows, pLines->nUsed);
xcli_size_t *pFrame = &pWin->frame;
xarray_t *pLines = &pWin->lines;
size_t i, nRows = XSTD_MIN(pFrame->nRows, pLines->nUsed);

for (i = 0; i < nRows; i++)
{
Expand All @@ -269,14 +269,14 @@ XSTATUS XWindow_GetFrame(xcli_win_t *pWin, xbyte_buffer_t *pFrame)

if (XWindow_RenderLine(pWin, &lineBuff, pData) < 0)
{
XByteBuffer_Clear(pFrame);
XByteBuffer_Clear(pFrameBuff);
XArray_Clear(pLines);
return XSTDERR;
}

if (XByteBuffer_AddBuff(pFrame, &lineBuff) < 0)
if (XByteBuffer_AddBuff(pFrameBuff, &lineBuff) < 0)
{
XByteBuffer_Clear(pFrame);
XByteBuffer_Clear(pFrameBuff);
XArray_Clear(pLines);
return XSTDERR;
}
Expand All @@ -292,8 +292,8 @@ XSTATUS XWindow_Display(xcli_win_t *pWin)
if (pWin->eType == XCLI_LINE_BY_LINE)
{
XWindow_ClearScreen(pWin->bAscii);
xarray_t *pLines = &pWin->lineArray;
size_t i, nWinRows = pWin->frameSize.nWinRows;
xarray_t *pLines = &pWin->lines;
size_t i, nWinRows = pWin->frame.nRows;
size_t nRows = XSTD_MIN(nWinRows, pLines->nUsed);

for (i = 0; i < nRows; i++)
Expand Down Expand Up @@ -336,13 +336,13 @@ XSTATUS XWindow_Display(xcli_win_t *pWin)
XSTATUS XWindow_Flush(xcli_win_t *pWin)
{
XSTATUS nStatus = XWindow_Display(pWin);
XArray_Clear(&pWin->lineArray);
XArray_Clear(&pWin->lines);
return nStatus;
}

void XWindow_Destroy(xcli_win_t *pWin)
{
xarray_t *pArr = &pWin->lineArray;
xarray_t *pArr = &pWin->lines;
XArray_Destroy(pArr);
}

Expand All @@ -352,8 +352,8 @@ void XWindow_Destroy(xcli_win_t *pWin)

XSTATUS XProgBar_UpdateWindowSize(xcli_bar_t *pCtx)
{
xcli_size_t *pSize = &pCtx->frameSize;
return XCLI_GetWindowSize(pSize);
xcli_size_t *pFrame = &pCtx->frame;
return XCLI_GetWindowSize(pFrame);
}

void XProgBar_GetDefaults(xcli_bar_t *pCtx)
Expand Down Expand Up @@ -405,7 +405,7 @@ void XProgBar_Finish(xcli_bar_t *pCtx)
void XProgBar_MakeMove(xcli_bar_t *pCtx)
{
XProgBar_UpdateWindowSize(pCtx);
size_t nColumns = pCtx->frameSize.nWinColumns;
size_t nColumns = pCtx->frame.nColumns;

char sProgress[XLINE_MAX];
char sSpaces[XLINE_MAX];
Expand Down Expand Up @@ -482,7 +482,7 @@ xbool_t XProgBar_CalculateBounds(xcli_bar_t *pCtx)
xstrncpyfl(pCtx->sPercent, sizeof(pCtx->sPercent),
XCLI_PERCENT_MAX, XSTR_SPACE_CHAR, "%.1f%%", pCtx->fPercent);

size_t nColumns = pCtx->frameSize.nWinColumns;
size_t nColumns = pCtx->frame.nColumns;
size_t nPreLen = strlen(pCtx->sPrefix);
size_t nSufLen = strlen(pCtx->sSuffix);
size_t nPctLen = strlen(pCtx->sPercent);
Expand Down
12 changes: 6 additions & 6 deletions src/sys/xcli.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ extern "C" {
#define XCLI_LEFT 2

typedef struct xcli_size_ {
size_t nWinColumns;
size_t nWinRows;
size_t nColumns;
size_t nRows;
} xcli_size_t;

int XCLI_GetPass(const char *pText, char *pPass, size_t nSize);
Expand All @@ -44,8 +44,8 @@ typedef enum {

typedef struct xcli_wind_ {
xcli_disp_type_t eType;
xcli_size_t frameSize;
xarray_t lineArray;
xcli_size_t frame;
xarray_t lines;
xbool_t bAscii;
} xcli_win_t;

Expand All @@ -60,12 +60,12 @@ XSTATUS XWindow_AddLineFmt(xcli_win_t *pWin, const char *pFmt, ...);
XSTATUS XWindow_AddLine(xcli_win_t *pWin, char *pLine, size_t nLine);
XSTATUS XWindow_AddEmptyLine(xcli_win_t *pWin);

XSTATUS XWindow_GetFrame(xcli_win_t *pWin, xbyte_buffer_t *pFrame);
XSTATUS XWindow_GetFrame(xcli_win_t *pWin, xbyte_buffer_t *pFrameBuff);
XSTATUS XWindow_Display(xcli_win_t *pWin);
XSTATUS XWindow_Flush(xcli_win_t *pWin);

typedef struct xcli_bar_ {
xcli_size_t frameSize;
xcli_size_t frame;
size_t nBarLength;
size_t nBarUsed;

Expand Down
2 changes: 1 addition & 1 deletion src/xver.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#define XUTILS_VERSION_MAX 2
#define XUTILS_VERSION_MIN 6
#define XUTILS_BUILD_NUMBER 1
#define XUTILS_BUILD_NUMBER 2

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion tools/xpass.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ static xbool_t XPass_ParseConfig(xpass_ctx_t *pCtx)

xcli_size_t cliSize;
XCLI_GetWindowSize(&cliSize);
pCtx->nFrameLength = cliSize.nWinColumns - 1;
pCtx->nFrameLength = cliSize.nColumns - 1;

XJSON_Destroy(&json);
return XTRUE;
Expand Down
42 changes: 34 additions & 8 deletions tools/xtop.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,19 @@
"HI IO "\
"ST GT GN"

/*
#define XTOP_IFACE_HEADER \
"IFACE "\
"RX "\
"TX "\
"SUM "\
"MAC IP"
"SUM "\
"MAC IP"
*/

#define XTOP_IFACE_HEADER \
"IFACE "\
"RX "\
"TX SUM"

static int g_nInterrupted = 0;
extern char *optarg;
Expand Down Expand Up @@ -396,7 +403,7 @@ XSTATUS XTOPApp_AddCPULoadBar(xcli_win_t *pWin, xcli_bar_t *pBar, xcpu_stats_t *
xstrnul(sFirst);

XProgBar_UpdateWindowSize(pBar);
pBar->frameSize.nWinColumns /= 2;
pBar->frame.nColumns /= 2;

for (i = 0; i < pCPU->nCoreCount; i++)
{
Expand Down Expand Up @@ -426,7 +433,7 @@ XSTATUS XTOPApp_AddCPULoadBar(xcli_win_t *pWin, xcli_bar_t *pBar, xcpu_stats_t *

if (i == nNext || nNext >= pCPU->nCoreCount)
{
xstrnfill(sSecond, sizeof(sSecond), pBar->frameSize.nWinColumns, XSTR_SPACE_CHAR);
xstrnfill(sSecond, sizeof(sSecond), pBar->frame.nColumns, XSTR_SPACE_CHAR);
return XWindow_AddLineFmt(pWin, "%s%s", sFirst, sSecond);
}

Expand Down Expand Up @@ -591,8 +598,8 @@ XSTATUS XTOPApp_AddOverallBar(xcli_win_t *pWin, xcli_bar_t *pBar, xmem_info_t *p
XSTR_CLR_LIGHT_BLUE, XU32ToFloat(pCPU->nLoadAvg[2]), XSTR_FMT_RESET);

/* Create half-empry line for pretty print */
XProgBar_UpdateWindowSize(pBar); pBar->frameSize.nWinColumns /= 2;
xstrnfill(sLine, sizeof(sLine), pBar->frameSize.nWinColumns, XSTR_SPACE_CHAR);
XProgBar_UpdateWindowSize(pBar); pBar->frame.nColumns /= 2;
xstrnfill(sLine, sizeof(sLine), pBar->frame.nColumns, XSTR_SPACE_CHAR);

/* Create and append process track info next to swap bar */
XKBToUnit(sUsed, sizeof(sUsed), pMemInfo->nResidentMemory, XTRUE);
Expand Down Expand Up @@ -705,11 +712,17 @@ XSTATUS XTOPApp_AddInterface(xcli_win_t *pWin, xtop_args_t *pArgs, xnet_iface_t
xstrnlcpyf(sRound, sizeof(sRound), 15, XSTR_SPACE_CHAR, "%s", sData);
xstrncat(sLine, sizeof(sLine), "%s/s", sRound);

xstrnlcpyf(sRound, sizeof(sRound), strlen(pIface->sHWAddr) + 4, XSTR_SPACE_CHAR, "%s", pIface->sHWAddr);
uint8_t nAddrPadding = (pWin->frame.nColumns < 106) ? 7 : 8;
nAddrPadding = (pWin->frame.nColumns < 104) ? 6 : nAddrPadding;
nAddrPadding = (pWin->frame.nColumns < 102) ? 5 : nAddrPadding;
nAddrPadding = (pWin->frame.nColumns < 100) ? 4 : nAddrPadding;
nAddrPadding = (pWin->frame.nColumns < 98) ? 3 : nAddrPadding;

xstrnlcpyf(sRound, sizeof(sRound), strlen(pIface->sHWAddr) + nAddrPadding, XSTR_SPACE_CHAR, "%s", pIface->sHWAddr);
if (strncmp(pIface->sHWAddr, XNET_HWADDR_DEFAULT, 17)) xstrncat(sLine, sizeof(sLine), "%s", sRound);
else xstrncat(sLine, sizeof(sLine), "%s%s%s", XSTR_FMT_DIM, sRound, XSTR_FMT_RESET);

xstrnlcpyf(sRound, sizeof(sRound), strlen(pIface->sIPAddr) + 4, XSTR_SPACE_CHAR, "%s", pIface->sIPAddr);
xstrnlcpyf(sRound, sizeof(sRound), strlen(pIface->sIPAddr) + nAddrPadding, XSTR_SPACE_CHAR, "%s", pIface->sIPAddr);
if (strncmp(pIface->sIPAddr, XNET_IPADDR_DEFAULT, 7)) xstrncat(sLine, sizeof(sLine), "%s", sRound);
else xstrncat(sLine, sizeof(sLine), "%s%s%s", XSTR_FMT_DIM, sRound, XSTR_FMT_RESET);

Expand Down Expand Up @@ -748,6 +761,19 @@ XSTATUS XTOPApp_AddNetworkInfo(xcli_win_t *pWin, xtop_args_t *pArgs, xarray_t *p
size_t nPreHdr = nLength > 4 ? nLength - 4 : nLength;
xstrnfill(sLine, sizeof(sLine), nPreHdr, XSTR_SPACE_CHAR);
xstrncat(sLine, sizeof(sLine), "%s", XTOP_IFACE_HEADER);

uint8_t nSpacePadding = (pWin->frame.nColumns < 106) ? 17 : 18;
nSpacePadding = (pWin->frame.nColumns < 104) ? 16 : nSpacePadding;
nSpacePadding = (pWin->frame.nColumns < 102) ? 15 : nSpacePadding;
nSpacePadding = (pWin->frame.nColumns < 100) ? 14 : nSpacePadding;
nSpacePadding = (pWin->frame.nColumns < 98) ? 13 : nSpacePadding;

xstrnlcpyf(sRound, sizeof(sRound), nSpacePadding, XSTR_SPACE_CHAR, "%s", "MAC");
xstrncat(sLine, sizeof(sLine), "%s", sRound);

xstrnlcpyf(sRound, sizeof(sRound), nSpacePadding - 1, XSTR_SPACE_CHAR, "%s", "IP");
xstrncat(sLine, sizeof(sLine), "%s", sRound);

XWindow_AddAligned(pWin, sLine, XSTR_BACK_BLUE, XCLI_LEFT);

if (nTrackID >= 0)
Expand Down

0 comments on commit e3f1671

Please sign in to comment.