Show colored text

This commit is contained in:
RocketRobz 2018-10-15 22:24:42 -06:00
parent 8e453d275a
commit ac8140fbac
2 changed files with 68 additions and 51 deletions

View File

@ -32,7 +32,7 @@
#define SCREEN_COLS 32 #define SCREEN_COLS 32
#define ENTRIES_PER_SCREEN 22 #define ENTRIES_PER_SCREEN 22
#define ENTRIES_START_ROW 2 #define ENTRIES_START_ROW 1
#define ENTRY_PAGE_LENGTH 10 #define ENTRY_PAGE_LENGTH 10
using namespace std; using namespace std;
@ -155,6 +155,7 @@ void driveMenu (void) {
if (!dmTextPrinted) { if (!dmTextPrinted) {
consoleInit(NULL, 1, BgType_Text4bpp, BgSize_T_256x256, 15, 0, false, true); consoleInit(NULL, 1, BgType_Text4bpp, BgSize_T_256x256, 15, 0, false, true);
printf ("\x1B[40m"); // Print foreground black color
if (assignedOp[dmCursorPosition] == 0) { if (assignedOp[dmCursorPosition] == 0) {
printf ("[sd:] SDCARD"); printf ("[sd:] SDCARD");
if (sdLabel[0] != '\0') { if (sdLabel[0] != '\0') {
@ -174,6 +175,7 @@ void driveMenu (void) {
printf ("[nitro:] NDS GAME IMAGE\n"); printf ("[nitro:] NDS GAME IMAGE\n");
printf ("(Game Virtual)"); printf ("(Game Virtual)");
} }
printf ("\x1B[47m"); // Print foreground white color
if (isDSiMode() && sdMountedDone) { if (isDSiMode() && sdMountedDone) {
if (sdMounted) { if (sdMounted) {
printf ("\x1b[21;0H"); printf ("\x1b[21;0H");
@ -195,22 +197,24 @@ void driveMenu (void) {
consoleInit(NULL, 0, BgType_Text4bpp, BgSize_T_256x256, 15, 0, true, true); consoleInit(NULL, 0, BgType_Text4bpp, BgSize_T_256x256, 15, 0, true, true);
printf ("\x1B[42m"); // Print green color
printf ("[root]"); printf ("[root]");
printf ("\x1B[47m"); // Print foreground white color
// Move to 2nd row // Move to 2nd row
printf ("\x1b[1;0H"); printf ("\x1b[1;0H");
// Print line of dashes
printf ("--------------------------------");
// Show cursor
printf ("\x1b[%d;0H*", dmCursorPosition + ENTRIES_START_ROW);
if (maxCursors == -1) { if (maxCursors == -1) {
printf ("\x1b[2;1H"); printf ("\x1b[2;0H");
printf ("No drives found!"); printf ("No drives found!");
} else } else
for (int i = 0; i <= maxCursors; i++) { for (int i = 0; i <= maxCursors; i++) {
iprintf ("\x1b[%d;1H", i + ENTRIES_START_ROW); iprintf ("\x1b[%d;0H", i + ENTRIES_START_ROW);
if (dmCursorPosition == i) {
printf ("\x1B[47m"); // Print foreground white color
} else {
printf ("\x1B[40m"); // Print foreground black color
}
if (assignedOp[i] == 0) { if (assignedOp[i] == 0) {
printf ("[sd:] SDCARD"); printf ("[sd:] SDCARD");
if (sdLabel[0] != '\0') { if (sdLabel[0] != '\0') {
@ -243,6 +247,8 @@ void driveMenu (void) {
stored_SCFG_MC = REG_SCFG_MC; stored_SCFG_MC = REG_SCFG_MC;
printf ("\x1B[42m"); // Print green color for time text
// Power saving loop. Only poll the keys once per frame and sleep the CPU if there is nothing else to do // Power saving loop. Only poll the keys once per frame and sleep the CPU if there is nothing else to do
do { do {
// Move to right side of screen // Move to right side of screen
@ -268,6 +274,8 @@ void driveMenu (void) {
} }
} while (!(pressed & KEY_UP) && !(pressed & KEY_DOWN) && !(pressed & KEY_A) && !(held & KEY_R)); } while (!(pressed & KEY_UP) && !(pressed & KEY_DOWN) && !(pressed & KEY_A) && !(held & KEY_R));
printf ("\x1B[47m"); // Print foreground white color
if ((pressed & KEY_UP) && maxCursors != -1) { if ((pressed & KEY_UP) && maxCursors != -1) {
dmCursorPosition -= 1; dmCursorPosition -= 1;
dmTextPrinted = false; dmTextPrinted = false;

View File

@ -39,11 +39,14 @@
#define SCREEN_COLS 32 #define SCREEN_COLS 32
#define ENTRIES_PER_SCREEN 22 #define ENTRIES_PER_SCREEN 22
#define ENTRIES_START_ROW 2 #define ENTRIES_START_ROW 1
#define OPTIONS_ENTRIES_START_ROW 2
#define ENTRY_PAGE_LENGTH 10 #define ENTRY_PAGE_LENGTH 10
using namespace std; using namespace std;
static int fileOffset = 0;
static char path[PATH_MAX]; static char path[PATH_MAX];
struct DirEntry { struct DirEntry {
@ -115,15 +118,13 @@ void getDirectoryContents (vector<DirEntry>& dirContents) {
} }
void showDirectoryContents (const vector<DirEntry>& dirContents, int startRow) { void showDirectoryContents (const vector<DirEntry>& dirContents, int startRow) {
char path[PATH_MAX];
getcwd(path, PATH_MAX); getcwd(path, PATH_MAX);
// Clear the screen // Clear the screen
iprintf ("\x1b[2J"); iprintf ("\x1b[2J");
// Print the path // Print the path
printf ("\x1B[42m"); // Print green color
if (strlen(path) < SCREEN_COLS) { if (strlen(path) < SCREEN_COLS) {
iprintf ("%s", path); iprintf ("%s", path);
} else { } else {
@ -132,27 +133,33 @@ void showDirectoryContents (const vector<DirEntry>& dirContents, int startRow) {
// Move to 2nd row // Move to 2nd row
iprintf ("\x1b[1;0H"); iprintf ("\x1b[1;0H");
// Print line of dashes
iprintf ("--------------------------------");
// Print directory listing // Print directory listing
for (int i = 0; i < ((int)dirContents.size() - startRow) && i < ENTRIES_PER_SCREEN; i++) { for (int i = 0; i < ((int)dirContents.size() - startRow) && i < ENTRIES_PER_SCREEN; i++) {
const DirEntry* entry = &dirContents.at(i + startRow); const DirEntry* entry = &dirContents.at(i + startRow);
char entryName[SCREEN_COLS + 1]; char entryName[SCREEN_COLS + 1];
// Set row // Set row
iprintf ("\x1b[%d;0H", i + ENTRIES_START_ROW); iprintf ("\x1b[%d;0H", i + ENTRIES_START_ROW);
if (fileOffset == i) {
if (entry->isDirectory) { printf ("\x1B[47m"); // Print foreground white color
strncpy (entryName, entry->name.c_str(), SCREEN_COLS);
entryName[SCREEN_COLS - 3] = '\0';
iprintf (" [%s]", entryName);
} else { } else {
strncpy (entryName, entry->name.c_str(), SCREEN_COLS); printf ("\x1B[40m"); // Print foreground black color
entryName[SCREEN_COLS - 1] = '\0'; }
iprintf (" %s", entryName);
strncpy (entryName, entry->name.c_str(), SCREEN_COLS);
entryName[SCREEN_COLS] = '\0';
printf (entryName);
if (strcmp(entry->name.c_str(), "..") == 0) {
printf ("\x1b[%d;28H", i + ENTRIES_START_ROW);
printf ("(..)");
} else if (entry->isDirectory) {
printf ("\x1b[%d;27H", i + ENTRIES_START_ROW);
printf ("(dir)");
} }
} }
printf ("\x1B[47m"); // Print foreground white color
} }
int fileBrowse_A(DirEntry* entry, char path[PATH_MAX]) { int fileBrowse_A(DirEntry* entry, char path[PATH_MAX]) {
@ -177,7 +184,7 @@ int fileBrowse_A(DirEntry* entry, char path[PATH_MAX]) {
break; break;
} }
} }
iprintf ("\x1b[%d;0H", cursorScreenPos + ENTRIES_START_ROW); iprintf ("\x1b[%d;0H", cursorScreenPos + OPTIONS_ENTRIES_START_ROW);
if (entry->isApp) { if (entry->isApp) {
maxCursors++; maxCursors++;
assignedOp[maxCursors] = 0; assignedOp[maxCursors] = 0;
@ -204,11 +211,11 @@ int fileBrowse_A(DirEntry* entry, char path[PATH_MAX]) {
printf("(<A> select, <B> cancel)"); printf("(<A> select, <B> cancel)");
while (true) { while (true) {
// Clear old cursors // Clear old cursors
for (int i = ENTRIES_START_ROW+cursorScreenPos; i < (maxCursors+1) + ENTRIES_START_ROW+cursorScreenPos; i++) { for (int i = OPTIONS_ENTRIES_START_ROW+cursorScreenPos; i < (maxCursors+1) + OPTIONS_ENTRIES_START_ROW+cursorScreenPos; i++) {
iprintf ("\x1b[%d;0H ", i); iprintf ("\x1b[%d;0H ", i);
} }
// Show cursor // Show cursor
iprintf ("\x1b[%d;0H->", optionOffset + ENTRIES_START_ROW+cursorScreenPos); iprintf ("\x1b[%d;0H->", optionOffset + OPTIONS_ENTRIES_START_ROW+cursorScreenPos);
// Power saving loop. Only poll the keys once per frame and sleep the CPU if there is nothing else to do // Power saving loop. Only poll the keys once per frame and sleep the CPU if there is nothing else to do
do { do {
@ -227,39 +234,39 @@ int fileBrowse_A(DirEntry* entry, char path[PATH_MAX]) {
if (pressed & KEY_A) { if (pressed & KEY_A) {
if (assignedOp[optionOffset] == 0) { if (assignedOp[optionOffset] == 0) {
applaunch = true; applaunch = true;
iprintf ("\x1b[%d;3H", optionOffset + ENTRIES_START_ROW+cursorScreenPos); iprintf ("\x1b[%d;3H", optionOffset + OPTIONS_ENTRIES_START_ROW+cursorScreenPos);
printf("Now loading..."); printf("Now loading...");
} else if (assignedOp[optionOffset] == 1) { } else if (assignedOp[optionOffset] == 1) {
if (access("sd:/gm9i", F_OK) != 0) { if (access("sd:/gm9i", F_OK) != 0) {
iprintf ("\x1b[%d;3H", optionOffset + ENTRIES_START_ROW+cursorScreenPos); iprintf ("\x1b[%d;3H", optionOffset + OPTIONS_ENTRIES_START_ROW+cursorScreenPos);
printf("Creating directory..."); printf("Creating directory...");
mkdir("sd:/gm9i", 0777); mkdir("sd:/gm9i", 0777);
} }
if (access("sd:/gm9i/out", F_OK) != 0) { if (access("sd:/gm9i/out", F_OK) != 0) {
iprintf ("\x1b[%d;3H", optionOffset + ENTRIES_START_ROW+cursorScreenPos); iprintf ("\x1b[%d;3H", optionOffset + OPTIONS_ENTRIES_START_ROW+cursorScreenPos);
printf("Creating directory..."); printf("Creating directory...");
mkdir("sd:/gm9i/out", 0777); mkdir("sd:/gm9i/out", 0777);
} }
char destPath[256]; char destPath[256];
snprintf(destPath, sizeof(destPath), "sd:/gm9i/out/%s", entry->name.c_str()); snprintf(destPath, sizeof(destPath), "sd:/gm9i/out/%s", entry->name.c_str());
iprintf ("\x1b[%d;3H", optionOffset + ENTRIES_START_ROW+cursorScreenPos); iprintf ("\x1b[%d;3H", optionOffset + OPTIONS_ENTRIES_START_ROW+cursorScreenPos);
printf("Copying... "); printf("Copying... ");
remove(destPath); remove(destPath);
fcopy(entry->name.c_str(), destPath); fcopy(entry->name.c_str(), destPath);
} else if (assignedOp[optionOffset] == 2) { } else if (assignedOp[optionOffset] == 2) {
if (access("fat:/gm9i", F_OK) != 0) { if (access("fat:/gm9i", F_OK) != 0) {
iprintf ("\x1b[%d;3H", optionOffset + ENTRIES_START_ROW+cursorScreenPos); iprintf ("\x1b[%d;3H", optionOffset + OPTIONS_ENTRIES_START_ROW+cursorScreenPos);
printf("Creating directory..."); printf("Creating directory...");
mkdir("fat:/gm9i", 0777); mkdir("fat:/gm9i", 0777);
} }
if (access("fat:/gm9i/out", F_OK) != 0) { if (access("fat:/gm9i/out", F_OK) != 0) {
iprintf ("\x1b[%d;3H", optionOffset + ENTRIES_START_ROW+cursorScreenPos); iprintf ("\x1b[%d;3H", optionOffset + OPTIONS_ENTRIES_START_ROW+cursorScreenPos);
printf("Creating directory..."); printf("Creating directory...");
mkdir("fat:/gm9i/out", 0777); mkdir("fat:/gm9i/out", 0777);
} }
char destPath[256]; char destPath[256];
snprintf(destPath, sizeof(destPath), "fat:/gm9i/out/%s", entry->name.c_str()); snprintf(destPath, sizeof(destPath), "fat:/gm9i/out/%s", entry->name.c_str());
iprintf ("\x1b[%d;3H", optionOffset + ENTRIES_START_ROW+cursorScreenPos); iprintf ("\x1b[%d;3H", optionOffset + OPTIONS_ENTRIES_START_ROW+cursorScreenPos);
printf("Copying... "); printf("Copying... ");
remove(destPath); remove(destPath);
fcopy(entry->name.c_str(), destPath); fcopy(entry->name.c_str(), destPath);
@ -287,7 +294,7 @@ bool fileBrowse_paste(char path[PATH_MAX]) {
printf (" "); // Clear time printf (" "); // Clear time
consoleInit(NULL, 1, BgType_Text4bpp, BgSize_T_256x256, 15, 0, false, true); consoleInit(NULL, 1, BgType_Text4bpp, BgSize_T_256x256, 15, 0, false, true);
printf("Paste file here?\n\n"); printf("Paste file here?\n\n");
iprintf ("\x1b[%d;0H", ENTRIES_START_ROW); iprintf ("\x1b[%d;0H", OPTIONS_ENTRIES_START_ROW);
maxCursors++; maxCursors++;
printf(" Copy path\n"); printf(" Copy path\n");
if (!clipboardInNitro) { if (!clipboardInNitro) {
@ -298,11 +305,11 @@ bool fileBrowse_paste(char path[PATH_MAX]) {
printf("(<A> select, <B> cancel)"); printf("(<A> select, <B> cancel)");
while (true) { while (true) {
// Clear old cursors // Clear old cursors
for (int i = ENTRIES_START_ROW; i < (maxCursors+1) + ENTRIES_START_ROW; i++) { for (int i = OPTIONS_ENTRIES_START_ROW; i < (maxCursors+1) + OPTIONS_ENTRIES_START_ROW; i++) {
iprintf ("\x1b[%d;0H ", i); iprintf ("\x1b[%d;0H ", i);
} }
// Show cursor // Show cursor
iprintf ("\x1b[%d;0H->", optionOffset + ENTRIES_START_ROW); iprintf ("\x1b[%d;0H->", optionOffset + OPTIONS_ENTRIES_START_ROW);
// Power saving loop. Only poll the keys once per frame and sleep the CPU if there is nothing else to do // Power saving loop. Only poll the keys once per frame and sleep the CPU if there is nothing else to do
do { do {
@ -321,7 +328,7 @@ bool fileBrowse_paste(char path[PATH_MAX]) {
if (pressed & KEY_A) { if (pressed & KEY_A) {
char destPath[256]; char destPath[256];
snprintf(destPath, sizeof(destPath), "%s%s", path, clipboardFilename); snprintf(destPath, sizeof(destPath), "%s%s", path, clipboardFilename);
iprintf ("\x1b[%d;3H", optionOffset + ENTRIES_START_ROW); iprintf ("\x1b[%d;3H", optionOffset + OPTIONS_ENTRIES_START_ROW);
if (optionOffset == 0) { if (optionOffset == 0) {
printf("Copying..."); printf("Copying...");
remove(destPath); remove(destPath);
@ -348,7 +355,7 @@ bool fileBrowse_paste(char path[PATH_MAX]) {
string browseForFile (void) { string browseForFile (void) {
int pressed = 0; int pressed = 0;
int screenOffset = 0; int screenOffset = 0;
int fileOffset = 0; fileOffset = 0;
off_t fileSize = 0; off_t fileSize = 0;
vector<DirEntry> dirContents; vector<DirEntry> dirContents;
@ -356,20 +363,26 @@ string browseForFile (void) {
while (true) { while (true) {
consoleInit(NULL, 1, BgType_Text4bpp, BgSize_T_256x256, 15, 0, false, true); consoleInit(NULL, 1, BgType_Text4bpp, BgSize_T_256x256, 15, 0, false, true);
printf ("\x1B[40m"); // Print foreground black color
DirEntry* entry = &dirContents.at(fileOffset); DirEntry* entry = &dirContents.at(fileOffset);
printf (entry->name.c_str()); printf (entry->name.c_str());
printf ("\n"); printf ("\n");
if (entry->isDirectory) { if (strcmp(entry->name.c_str(), "..") != 0) {
printf ("(dir)"); if (entry->isDirectory) {
} else { printf ("(dir)");
fileSize = getFileSize(entry->name.c_str()); } else {
printf ("%i Bytes", (int)fileSize); fileSize = getFileSize(entry->name.c_str());
printf ("%i Bytes", (int)fileSize);
}
} }
if (clipboardOn) { if (clipboardOn) {
printf ("\x1b[10;0H"); printf ("\x1b[10;0H");
printf ("\x1B[47m"); // Print foreground white color
printf ("[CLIPBOARD]\n"); printf ("[CLIPBOARD]\n");
printf ("\x1B[40m"); // Print foreground black color
printf (clipboardFilename); printf (clipboardFilename);
} }
printf ("\x1B[47m"); // Print foreground white color
printf ("\x1b[19;0H"); printf ("\x1b[19;0H");
printf (titleName); printf (titleName);
printf ("\x1b[20;0H"); printf ("\x1b[20;0H");
@ -384,15 +397,10 @@ string browseForFile (void) {
consoleInit(NULL, 0, BgType_Text4bpp, BgSize_T_256x256, 15, 0, true, true); consoleInit(NULL, 0, BgType_Text4bpp, BgSize_T_256x256, 15, 0, true, true);
showDirectoryContents (dirContents, screenOffset); showDirectoryContents (dirContents, screenOffset);
// Clear old cursors
/*for (int i = ENTRIES_START_ROW; i < ENTRIES_PER_SCREEN + ENTRIES_START_ROW; i++) {
iprintf ("\x1b[%d;0H ", i);
}*/
// Show cursor
iprintf ("\x1b[%d;0H*", fileOffset - screenOffset + ENTRIES_START_ROW);
stored_SCFG_MC = REG_SCFG_MC; stored_SCFG_MC = REG_SCFG_MC;
printf ("\x1B[42m"); // Print green color for time text
// Power saving loop. Only poll the keys once per frame and sleep the CPU if there is nothing else to do // Power saving loop. Only poll the keys once per frame and sleep the CPU if there is nothing else to do
do { do {
// Move to right side of screen // Move to right side of screen
@ -411,7 +419,8 @@ string browseForFile (void) {
&& !(pressed & KEY_A) && !(pressed & KEY_B) && !(pressed & KEY_X) && !(pressed & KEY_Y) && !(pressed & KEY_A) && !(pressed & KEY_B) && !(pressed & KEY_X) && !(pressed & KEY_Y)
&& !(pressed & KEY_SELECT)); && !(pressed & KEY_SELECT));
iprintf ("\x1b[%d;0H*", fileOffset - screenOffset + ENTRIES_START_ROW); printf ("\x1B[47m"); // Print foreground white color
iprintf ("\x1b[%d;0H", fileOffset - screenOffset + ENTRIES_START_ROW);
if (isDSiMode() && !pressed && secondaryDrive && REG_SCFG_MC == 0x11 && flashcardMounted) { if (isDSiMode() && !pressed && secondaryDrive && REG_SCFG_MC == 0x11 && flashcardMounted) {
flashcardUnmount(); flashcardUnmount();