From e3f1671e38109684af734ca0d10725587f8edb17 Mon Sep 17 00:00:00 2001 From: Sandro Kalatozishvili Date: Tue, 19 Nov 2024 14:17:18 +0400 Subject: [PATCH] Responsive interface bar in XTOP --- src/sys/xcli.c | 74 +++++++++++++++++++++++++------------------------- src/sys/xcli.h | 12 ++++---- src/xver.h | 2 +- tools/xpass.c | 2 +- tools/xtop.c | 42 ++++++++++++++++++++++------ 5 files changed, 79 insertions(+), 53 deletions(-) diff --git a/src/sys/xcli.c b/src/sys/xcli.c index 6833bee..17e2f00 100644 --- a/src/sys/xcli.c +++ b/src/sys/xcli.c @@ -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) { @@ -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; @@ -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); @@ -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; @@ -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; @@ -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++) { @@ -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; } @@ -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++) @@ -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); } @@ -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) @@ -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]; @@ -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); diff --git a/src/sys/xcli.h b/src/sys/xcli.h index 9a7c6ed..93074b0 100644 --- a/src/sys/xcli.h +++ b/src/sys/xcli.h @@ -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); @@ -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; @@ -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; diff --git a/src/xver.h b/src/xver.h index d3ef075..d14304d 100644 --- a/src/xver.h +++ b/src/xver.h @@ -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" { diff --git a/tools/xpass.c b/tools/xpass.c index 449b760..8891b90 100644 --- a/tools/xpass.c +++ b/tools/xpass.c @@ -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; diff --git a/tools/xtop.c b/tools/xtop.c index 0e55274..0ef646f 100644 --- a/tools/xtop.c +++ b/tools/xtop.c @@ -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; @@ -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++) { @@ -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); } @@ -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); @@ -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); @@ -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)