Skip to content

Commit 237ee0a

Browse files
committed
feat: add os::cls
1 parent ad98d1a commit 237ee0a

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

argon/vm/mod/os.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,63 @@ ARGON_FUNCTION(os_chdir, chdir,
8989
return (ArObject *) IncRef(Nil);
9090
}
9191

92+
ARGON_FUNCTION(os_cls, cls,
93+
"Clear the console screen.\n"
94+
"\n"
95+
"This function clears the console screen.",
96+
nullptr, false, false) {
97+
#ifdef _ARGON_PLATFORM_WINDOWS
98+
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
99+
CONSOLE_SCREEN_BUFFER_INFO csbi;
100+
DWORD count;
101+
DWORD cellCount;
102+
COORD homeCoords = {0, 0};
103+
104+
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) {
105+
ErrorFromWinErr();
106+
107+
return nullptr;
108+
}
109+
110+
cellCount = csbi.dwSize.X * csbi.dwSize.Y;
111+
112+
if (!FillConsoleOutputCharacter(hConsole, (TCHAR)' ', cellCount, homeCoords, &count)) {
113+
ErrorFromWinErr();
114+
115+
return nullptr;
116+
}
117+
118+
if (!FillConsoleOutputAttribute(hConsole, csbi.wAttributes, cellCount, homeCoords, &count)) {
119+
ErrorFromWinErr();
120+
121+
return nullptr;
122+
}
123+
124+
SetConsoleCursorPosition(hConsole, homeCoords);
125+
#else
126+
const char *clearScreen = "\033[2J"; // Clear entire screen
127+
const char *moveCursor = "\033[H"; // Move cursor to home position
128+
129+
if (isatty(STDOUT_FILENO)) {
130+
if (write(STDOUT_FILENO, clearScreen, 4) == -1 ||
131+
write(STDOUT_FILENO, moveCursor, 3) == -1) {
132+
ErrorFromErrno(errno);
133+
134+
return nullptr;
135+
}
136+
} else {
137+
// If not a terminal, just print a form feed character
138+
if (write(STDOUT_FILENO, "\f", 1) == -1) {
139+
ErrorFromErrno(errno);
140+
141+
return nullptr;
142+
}
143+
}
144+
#endif
145+
146+
return ARGON_NIL_VALUE;
147+
}
148+
92149
ARGON_FUNCTION(os_dup, dup,
93150
"Duplicate or reassigns a file descriptor.\n"
94151
"\n"
@@ -1140,6 +1197,7 @@ const ModuleEntry os_entries[] = {
11401197
#endif
11411198

11421199
MODULE_EXPORT_FUNCTION(os_chdir),
1200+
MODULE_EXPORT_FUNCTION(os_cls),
11431201
#ifdef _ARGON_PLATFORM_WINDOWS
11441202
MODULE_EXPORT_FUNCTION(os_createprocess),
11451203
#endif

0 commit comments

Comments
 (0)