Skip to content

Commit

Permalink
Fixed XTOP blink issue and adjusted network bar positions
Browse files Browse the repository at this point in the history
  • Loading branch information
kala13x committed Nov 19, 2024
1 parent db85815 commit 8e2732f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/data/xstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#define XSTR_FMT_HIDE "\x1B[8m"
#define XSTR_FMT_CROSS "\x1B[9m"
#define XSTR_FMT_RESET XSTR_CLR_NONE
#define XSTR_SCREEN_CLEAR "\033[H\033[J"

#define XSTR_DOUBLE_ARG_MAX 309
#define XSTR_INT_ARG_MAX 32
Expand Down
17 changes: 13 additions & 4 deletions src/sys/xcli.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,13 @@ XSTATUS XCLI_GetWindowSize(xcli_size_t *pCli)
return (pCli->nWinColumns && pCli->nWinRows) ? XSTDOK : XSTDERR;
}

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

XSTATUS XWindow_UpdateSize(xcli_win_t *pWin)
Expand Down Expand Up @@ -183,9 +184,17 @@ XSTATUS XWindow_AddAligned(xcli_win_t *pWin, const char *pInput, const char *pFm
return XWindow_AddLineFmt(pWin, "%s%s%s%s%s", pFmt, sPreBuf, pInput, sAfterBuf, XSTR_FMT_RESET);
}

int XWindow_ClearScreen()
int XWindow_ClearScreen(xbool_t bAscii)
{
int nRet = XSTDNON;

if (bAscii)
{
printf(XSTR_SCREEN_CLEAR);
fflush(stdout);
return nRet;
}

#if !defined(_WIN32) && !defined(_WIN64)
nRet = system("clear");
#else
Expand Down Expand Up @@ -282,7 +291,7 @@ XSTATUS XWindow_Display(xcli_win_t *pWin)

if (pWin->eType == XCLI_LINE_BY_LINE)
{
XWindow_ClearScreen();
XWindow_ClearScreen(pWin->bAscii);
xarray_t *pLines = &pWin->lineArray;
size_t i, nWinRows = pWin->frameSize.nWinRows;
size_t nRows = XSTD_MIN(nWinRows, pLines->nUsed);
Expand Down Expand Up @@ -313,7 +322,7 @@ XSTATUS XWindow_Display(xcli_win_t *pWin)
nStatus = XWindow_GetFrame(pWin, &frameBuf);
if (nStatus == XSTDERR) return nStatus;

XWindow_ClearScreen();
XWindow_ClearScreen(pWin->bAscii);
printf("%s\r", (char*)frameBuf.pData);
fflush(stdout);

Expand Down
6 changes: 4 additions & 2 deletions src/sys/xcli.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
extern "C" {
#endif

#include "xstd.h"
#include "xbuf.h"
#include "list.h"

Expand Down Expand Up @@ -45,13 +46,14 @@ typedef struct xcli_wind_ {
xcli_disp_type_t eType;
xcli_size_t frameSize;
xarray_t lineArray;
xbool_t bAscii;
} xcli_win_t;

void XWindow_Init(xcli_win_t *pWin);
void XWindow_Init(xcli_win_t *pWin, xbool_t bAscii);
void XWindow_Destroy(xcli_win_t *pWin);

XSTATUS XWindow_UpdateSize(xcli_win_t *pWin);
int XWindow_ClearScreen();
int XWindow_ClearScreen(xbool_t bAscii);

XSTATUS XWindow_AddAligned(xcli_win_t *pWin, const char *pInput, const char *pFmt, uint8_t nAlign);
XSTATUS XWindow_AddLineFmt(xcli_win_t *pWin, const char *pFmt, ...);
Expand Down
10 changes: 10 additions & 0 deletions src/sys/xtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ size_t XBytesToUnit(char *pDst, size_t nSize, size_t nBytes, xbool_t bShort)
{
const char *pUnit;
double fVal = 0.;
xbool_t bIsGB = XFALSE;

if (nBytes > 1073741824)
{
fVal = (double)nBytes / (double)1073741824;
pUnit = bShort ? "G" : "GB";
bIsGB = XTRUE;

}
else if (nBytes > 1048576)
{
Expand All @@ -73,6 +76,13 @@ size_t XBytesToUnit(char *pDst, size_t nSize, size_t nBytes, xbool_t bShort)
pUnit = bShort ? "B" : " B";
}

if (bIsGB)
{
return bShort ?
xstrncpyf(pDst, nSize, "%.0f%s", fVal, pUnit) :
xstrncpyf(pDst, nSize, "%.0f %s", fVal, pUnit);
}

return bShort ?
xstrncpyf(pDst, nSize, "%.1f%s", fVal, pUnit) :
xstrncpyf(pDst, nSize, "%.2f %s", fVal, pUnit);
Expand Down
58 changes: 38 additions & 20 deletions tools/xtop.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "api.h"

#define XTOP_VERSION_MAJ 1
#define XTOP_VERSION_MIN 2
#define XTOP_VERSION_MIN 3

#define XTOP_SORT_DISABLE 0
#define XTOP_SORT_BUSY 1
Expand All @@ -41,11 +41,11 @@
"ST GT GN"

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

static int g_nInterrupted = 0;
extern char *optarg;
Expand All @@ -67,6 +67,7 @@ typedef struct xtop_args_ {
xbool_t bDaemon;
xbool_t bServer;
xbool_t bClient;
xbool_t bAscii;

char sLink[XLINK_MAX];
char sAddr[XLINK_MAX];
Expand Down Expand Up @@ -125,14 +126,15 @@ void XTOPApp_DisplayUsage(const char *pName)

printf("Usage: %s [-e <count>] [-i <iface>] [-m <seconds>] [-t <type>]\n", pName);
printf(" %s [-a <addr>] [-p <port>] [-l <path>] [-u <pid>] [-d] [-s]\n", XTOPApp_WhiteSpace(nLength));
printf(" %s [-U <user>] [-P <pass>] [-K <key>] [-c] [-v] [-h]\n\n", XTOPApp_WhiteSpace(nLength));
printf(" %s [-U <user>] [-P <pass>] [-K <key>] [-c] [-v] [-x] [-h]\n\n", XTOPApp_WhiteSpace(nLength));

printf("Options are:\n");
printf(" %s-e%s <count> # Limit extra CPU info to given number\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);
printf(" %s-i%s <iface> # Interface name to display on top\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);
printf(" %s-m%s <seconds> # Monitoring interval seconds\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);
printf(" %s-t%s <type> # Sort result by selected type%s*%s\n", XSTR_CLR_CYAN, XSTR_FMT_RESET, XSTR_CLR_RED, XSTR_FMT_RESET);
printf(" %s-u%s <pid> # Track process CPU and memory usage\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);
printf(" %s-x%s # Use ASCII code to clear screen\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);
printf(" %s-h%s # Print version and usage\n\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);

printf("%sXTOP has a REST API server and client mode to send%s\n", XSTR_FMT_DIM, XSTR_FMT_RESET);
Expand All @@ -156,10 +158,14 @@ void XTOPApp_DisplayUsage(const char *pName)
printf(" %sf%s: Free on top\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);
printf(" %sn%s: Sort by name\n\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);

printf("%sIf XTOP window is blinking during refresh, try to use ASCII code%s\n", XSTR_FMT_DIM, XSTR_FMT_RESET);
printf("%sfor screen clearing. Use CLI argument -x to enable ASCII mode.%s\n\n", XSTR_FMT_DIM, XSTR_FMT_RESET);

printf("Examples:\n");
printf("1) %s -m 2 -t b -u 2274\n", pName);
printf("2) %s -t f -u 2274 -i enp4s0\n", pName);
printf("3) %s -sa 127.0.0.1 -p 8080\n\n", pName);
printf("1) %s -x -e 8\n", pName);
printf("2) %s -m 2 -t b -u 2274\n", pName);
printf("3) %s -t f -u 2274 -i enp4s0\n", pName);
printf("4) %s -sa 127.0.0.1 -p 8080\n\n", pName);
}

uint8_t XTOPApp_GetSortType(const char *pArg)
Expand All @@ -176,6 +182,7 @@ int XTOPApp_ParseArgs(xtop_args_t *pArgs, int argc, char *argv[])
pArgs->bDaemon = XFALSE;
pArgs->bServer = XFALSE;
pArgs->bClient = XFALSE;
pArgs->bAscii = XFALSE;
pArgs->pStats = NULL;
pArgs->nSort = XTOP_SORT_LEN;

Expand All @@ -197,7 +204,7 @@ int XTOPApp_ParseArgs(xtop_args_t *pArgs, int argc, char *argv[])
xbool_t bVerbose = XFALSE;
int nChar = 0;

while ((nChar = getopt(argc, argv, "a:e:i:K:U:P:l:m:p:t:u:c1:d1:s1:d1:v1:h1")) != -1)
while ((nChar = getopt(argc, argv, "a:e:i:K:U:P:l:m:p:t:u:c1:d1:s1:d1:x1:v1:h1")) != -1)
{
switch (nChar)
{
Expand Down Expand Up @@ -243,6 +250,9 @@ int XTOPApp_ParseArgs(xtop_args_t *pArgs, int argc, char *argv[])
case 's':
pArgs->bServer = XTRUE;
break;
case 'x':
pArgs->bAscii = XTRUE;
break;
case 'v':
bVerbose = XTRUE;
break;
Expand Down Expand Up @@ -683,23 +693,23 @@ XSTATUS XTOPApp_AddInterface(xcli_win_t *pWin, xtop_args_t *pArgs, xnet_iface_t
xstrncpy(sLine, sizeof(sLine), sName);

XBytesToUnit(sData, sizeof(sData), pIface->nBytesReceivedPerSec, XFALSE);
xstrnlcpyf(sRound, sizeof(sRound), 18, XSTR_SPACE_CHAR, "%s", sData);
xstrnlcpyf(sRound, sizeof(sRound), 15, XSTR_SPACE_CHAR, "%s", sData);
xstrncat(sLine, sizeof(sLine), "%s/s", sRound);

XBytesToUnit(sData, sizeof(sData), pIface->nBytesSentPerSec, XFALSE);
xstrnlcpyf(sRound, sizeof(sRound), 18, XSTR_SPACE_CHAR, "%s", sData);
xstrnlcpyf(sRound, sizeof(sRound), 15, XSTR_SPACE_CHAR, "%s", sData);
xstrncat(sLine, sizeof(sLine), "%s/s", sRound);

uint64_t nSum = pIface->nBytesReceivedPerSec + pIface->nBytesSentPerSec;
XBytesToUnit(sData, sizeof(sData), nSum, XFALSE);
xstrnlcpyf(sRound, sizeof(sRound), 18, XSTR_SPACE_CHAR, "%s", sData);
xstrnlcpyf(sRound, sizeof(sRound), 15, XSTR_SPACE_CHAR, "%s", sData);
xstrncat(sLine, sizeof(sLine), "%s/s", sRound);

xstrnlcpyf(sRound, sizeof(sRound), strlen(pIface->sHWAddr) + 8, XSTR_SPACE_CHAR, "%s", pIface->sHWAddr);
xstrnlcpyf(sRound, sizeof(sRound), strlen(pIface->sHWAddr) + 4, 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) + 8, XSTR_SPACE_CHAR, "%s", pIface->sIPAddr);
xstrnlcpyf(sRound, sizeof(sRound), strlen(pIface->sIPAddr) + 4, 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 @@ -755,15 +765,15 @@ XSTATUS XTOPApp_AddNetworkInfo(xcli_win_t *pWin, xtop_args_t *pArgs, xarray_t *p

xstrnlcpyf(sLine, sizeof(sLine), nLength + 1, XSTR_SPACE_CHAR, "%s", "total");
XBytesToUnit(sData, sizeof(sData), nSumRX, XFALSE);
xstrnlcpyf(sRound, sizeof(sRound), 18, XSTR_SPACE_CHAR, "%s", sData);
xstrnlcpyf(sRound, sizeof(sRound), 15, XSTR_SPACE_CHAR, "%s", sData);
xstrncat(sLine, sizeof(sLine), "%s/s", sRound);

XBytesToUnit(sData, sizeof(sData), nSumTX, XFALSE);
xstrnlcpyf(sRound, sizeof(sRound), 18, XSTR_SPACE_CHAR, "%s", sData);
xstrnlcpyf(sRound, sizeof(sRound), 15, XSTR_SPACE_CHAR, "%s", sData);
xstrncat(sLine, sizeof(sLine), "%s/s", sRound);

XBytesToUnit(sData, sizeof(sData), nSumRX + nSumTX, XFALSE);
xstrnlcpyf(sRound, sizeof(sRound), 18, XSTR_SPACE_CHAR, "%s", sData);
xstrnlcpyf(sRound, sizeof(sRound), 15, XSTR_SPACE_CHAR, "%s", sData);
xstrncat(sLine, sizeof(sLine), "%s/s", sRound);
return XWindow_AddAligned(pWin, sLine, XSTR_CLR_LIGHT_CYAN, XCLI_LEFT);
}
Expand Down Expand Up @@ -1545,14 +1555,16 @@ int main(int argc, char *argv[])
}

xcli_win_t win;
XWindow_Init(&win);
XWindow_Init(&win, args.bAscii);

xcli_bar_t bar;
XProgBar_GetDefaults(&bar);
bar.bInPercent = XTRUE;
bar.bInSuffix = XTRUE;
bar.cLoader = '|';

xbool_t bFirst = XTRUE;

while (!g_nInterrupted)
{
if (args.bClient && XTOPApp_GetRemoteStats(&args, &stats) < 0) break;
Expand Down Expand Up @@ -1586,6 +1598,12 @@ int main(int argc, char *argv[])
XArray_Destroy(&netIfaces);
}

if (bFirst)
{
XWindow_ClearScreen(XFALSE);
bFirst = XFALSE;
}

XWindow_Flush(&win);
xusleep(args.nIntervalU);
}
Expand Down

0 comments on commit 8e2732f

Please sign in to comment.