Skip to content

Commit

Permalink
Add console clearing capability
Browse files Browse the repository at this point in the history
This program is expected to be used interactively only. Therefore, we add behavior to the program such that the console is cleared every time the user interacts with the menu. That way, any messages printed because of user input appear at the top and the menu appears again for the user to enter their next interaction, which keeps the UI clean.

NOTE: I originally implemented this behavior in the CSCI 112 project and copied it over to here.
  • Loading branch information
kryzet committed Dec 3, 2024
1 parent 1059395 commit c9b1be0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
61 changes: 61 additions & 0 deletions project_3x3_rubik_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,64 @@ void rightySequence();
void reverseRightySequence();
void sledgehammerSequence();

static void clear_console() {
/* The definition of `clear_screen()` below is courtesy of
https://stackoverflow.com/a/42500322. Please note that the answer has been
licensed under CC BY-SA 3.0, and the code has been originally adapted from
https://cplusplus.com/articles/4z18T05o/#OSSpecificWays and modified such
that it can be cross-compiled and have consistent behavior on all
mainstream operating systems. The original licensing from
https://cplusplus.com is unknown. */
#ifdef _WIN32
HANDLE h_std_out;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cell_count;
COORD home_coords = { 0, 0 };

h_std_out = GetStdHandle(STD_OUTPUT_HANDLE);
if (h_std_out == INVALID_HANDLE_VALUE) return;

/* Get the number of cells in the current buffer */
if (!GetConsoleScreenBufferInfo(h_std_out, &csbi)) return;
cell_count = csbi.dwSize.X * csbi.dwSize.Y;

/* Fill the entire buffer with spaces */
if (!FillConsoleOutputCharacter(
h_std_out,
(TCHAR)' ',
cell_count,
home_coords,
&count
)) return;

/* Fill the entire buffer with the current colors and attributes */
if (!FillConsoleOutputAttribute(
h_std_out,
csbi.wAttributes,
cell_count,
home_coords,
&count
)) return;

/* Move the cursor home */
SetConsoleCursorPosition(h_std_out, home_coords);
#else
/* Hassan: Not _WIN32. This assumes that it indicates a POSIX-compatible
build environment */
if (!cur_term)
{
int result;
setupterm(NULL, STDOUT_FILENO, &result);
if (result <= 0) return;
}

char action[] = "clear"; /* This was added in order to comply with ISO
* C++11
*/
putp(tigetstr(action));
#endif
}

int main()
{
Expand Down Expand Up @@ -178,6 +236,9 @@ int main()
continue; // Will reshow the menu
}

/* This program only expects to be used interactively, so buffer
cleanliness is important */
clear_console();
// Handle user input
switch (choice) {
case 1:
Expand Down
11 changes: 10 additions & 1 deletion project_3x3_rubik_solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <queue>
#ifdef _WIN32
#define NOMINMAX
#include <Windows.h>
#else
#include <curses.h> // Wasn't included in original code
// https://linux.die.net/man/3/setupterm
#include <term.h>
#include <unistd.h> // Included before `term.h` in original code
#endif

0 comments on commit c9b1be0

Please sign in to comment.