Skip to content

Commit f8a6250

Browse files
committed
Refactored CLI layer, implemented async input mode from terminal and implemented interactive interface for XTOP
1 parent a1e2831 commit f8a6250

File tree

5 files changed

+332
-122
lines changed

5 files changed

+332
-122
lines changed

src/sys/cli.c

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,89 @@
1212
#include "str.h"
1313
#include "xtime.h"
1414

15+
#define XBAR_FRAME_BYTES 3
16+
#define XCLI_PERCENT_MAX 4
17+
18+
XSTATUS XCLI_SetInputMode(void *pAttributes)
19+
{
1520
#ifdef __linux__
16-
#include <termios.h>
21+
struct termios tattr;
22+
if (!isatty(STDIN_FILENO)) return XSTDERR;
23+
24+
struct termios *pSavedAttrs = (struct termios *)pAttributes;
25+
tcgetattr(STDIN_FILENO, pSavedAttrs);
26+
tcgetattr(STDIN_FILENO, &tattr);
27+
28+
tattr.c_lflag &= ~(ICANON | ECHO);
29+
tattr.c_cc[VMIN] = 1;
30+
tattr.c_cc[VTIME] = 0;
31+
tcsetattr(STDIN_FILENO, TCSANOW, &tattr);
32+
return XSTDOK;
33+
#else
34+
(void)pAttributes;
35+
return XSTDNON;
1736
#endif
37+
}
1838

19-
#define XBAR_FRAME_BYTES 3
20-
#define XCLI_PERCENT_MAX 4
39+
XSTATUS XCLI_RestoreAttributes(void *pAttributes)
40+
{
41+
#ifdef __linux__
42+
if (pAttributes == NULL) return XSTDERR;
43+
struct termios *pSavedAttrs = (struct termios *)pAttributes;
44+
tcsetattr(STDIN_FILENO, TCSANOW, pSavedAttrs);
45+
return XSTDOK;
46+
#else
47+
(void)pAttributes;
48+
return XSTDNON;
49+
#endif
50+
}
2151

22-
int XCLI_GetPass(const char *pText, char *pPass, size_t nSize)
52+
XSTATUS XCLI_ReadStdin(char *pBuffer, size_t nSize, xbool_t bAsync)
2353
{
54+
if (pBuffer == NULL || !nSize) return XSTDINV;
55+
int nLength = XSTDNON;
56+
pBuffer[0] = XSTR_NUL;
57+
58+
#ifdef __linux__
59+
if (bAsync)
60+
{
61+
int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
62+
if (flags < 0) return XSTDERR;
63+
64+
if (!(flags & O_NONBLOCK))
65+
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
66+
}
67+
68+
nLength = read(STDIN_FILENO, pBuffer, nSize);
69+
if (nLength < 0)
70+
{
71+
if (errno == EWOULDBLOCK ||
72+
errno == EAGAIN) return XSTDNON;
73+
74+
return XSTDERR;
75+
}
76+
#endif
77+
78+
if (nSize > 1) pBuffer[nLength] = 0;
79+
return nLength;
80+
}
81+
82+
XSTATUS XCLI_GetChar(char *pChar, xbool_t bAsync)
83+
{
84+
#ifdef __linux__
85+
if (pChar == NULL) return XSTDERR;
86+
return XCLI_ReadStdin(pChar, 1, bAsync);
87+
#else
88+
(void)pChar;
89+
return XSTDNON;
90+
#endif
91+
}
92+
93+
XSTATUS XCLI_GetPass(const char *pText, char *pPass, size_t nSize)
94+
{
95+
if (pPass == NULL || !nSize) return XSTDERR;
2496
size_t nLength = XSTDNON;
97+
2598
#ifdef __linux__
2699
struct termios oflags, nflags;
27100
tcgetattr(fileno(stdin), &oflags);
@@ -43,10 +116,10 @@ int XCLI_GetPass(const char *pText, char *pPass, size_t nSize)
43116
#endif
44117

45118
pPass[nLength] = 0;
46-
return (int)nLength;
119+
return (XSTATUS)nLength;
47120
}
48121

49-
int XCLI_GetInput(const char *pText, char *pInput, size_t nSize, xbool_t bCutNewLine)
122+
XSTATUS XCLI_GetInput(const char *pText, char *pInput, size_t nSize, xbool_t bCutNewLine)
50123
{
51124
XASSERT(pInput, XSTDINV);
52125
pInput[0] = XSTR_NUL;

src/sys/cli.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ extern "C" {
1818
#include "buf.h"
1919
#include "list.h"
2020

21+
#ifdef __linux__
22+
#include <termios.h>
23+
#endif
24+
2125
#define XCLI_BAR_INTERVAL 100000
2226
#define XCLI_BUF_SIZE 256
2327

@@ -30,8 +34,13 @@ typedef struct xcli_size_ {
3034
size_t nRows;
3135
} xcli_size_t;
3236

33-
int XCLI_GetPass(const char *pText, char *pPass, size_t nSize);
34-
int XCLI_GetInput(const char *pText, char *pInput, size_t nSize, xbool_t bCutNewLine);
37+
XSTATUS XCLI_SetInputMode(void *pAttributes);
38+
XSTATUS XCLI_RestoreAttributes(void *pAttributes);
39+
XSTATUS XCLI_ReadStdin(char *pBuffer, size_t nSize, xbool_t bAsync);
40+
41+
XSTATUS XCLI_GetChar(char *pChar, xbool_t bAsync);
42+
XSTATUS XCLI_GetPass(const char *pText, char *pPass, size_t nSize);
43+
XSTATUS XCLI_GetInput(const char *pText, char *pInput, size_t nSize, xbool_t bCutNewLine);
3544

3645
XSTATUS XCLI_GetWindowSize(xcli_size_t *pSize);
3746
size_t XCLI_CountFormat(xcli_size_t *pSize, const char *pLine, size_t nLength, size_t *pPosit);

src/sys/thread.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void *XTask_WorkerThread(void *pContext)
127127
bIsPaused = XTRUE;
128128
}
129129

130-
xusleep((uint32_t)nInterval);
130+
if (nInterval) xusleep((uint32_t)nInterval);
131131
continue;
132132
}
133133

src/xver.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
#define XUTILS_VERSION_MAX 2
1414
#define XUTILS_VERSION_MIN 7
15-
#define XUTILS_BUILD_NUMBER 1
16-
#define XUTILS_BUILD_DATE "20Jun2025"
15+
#define XUTILS_BUILD_NUMBER 2
16+
#define XUTILS_BUILD_DATE "21Jun2025"
1717

1818
#ifdef __cplusplus
1919
extern "C" {

0 commit comments

Comments
 (0)