Fix directory listing bugs

Signed-off-by: Mahyar Koshkouei <deltabeard@users.noreply.github.com>
This commit is contained in:
Mahyar Koshkouei
2017-01-05 22:07:39 +00:00
parent 28d2e8d594
commit 0e6a86c551

View File

@@ -25,7 +25,7 @@
/* Default folder */ /* Default folder */
#define DEFAULT_DIR "sdmc:/" #define DEFAULT_DIR "sdmc:/"
/* Maximum number of lines that can be displayed */ /* Maximum number of lines that can be displayed */
#define MAX_LIST 28 #define MAX_LIST 27
enum file_types { enum file_types {
FILE_TYPE_ERROR = -1, FILE_TYPE_ERROR = -1,
@@ -93,7 +93,7 @@ int main(int argc, char **argv)
consoleSelect(&bottomScreen); consoleSelect(&bottomScreen);
#endif #endif
if((kHeld & KEY_UP) && fileNum > 0) if((kHeld & KEY_UP) && fileNum > 0 && fileNum > 0)
{ {
/* Limit the speed of the selector */ /* Limit the speed of the selector */
if(osGetTime() - mill < 100) if(osGetTime() - mill < 100)
@@ -105,23 +105,25 @@ int main(int argc, char **argv)
from--; from--;
mill = osGetTime(); mill = osGetTime();
consoleClear();
if(listDir(from, MAX_LIST, fileNum) < 0)
err_print("Unable to list directory.");
} }
if((kHeld & KEY_DOWN) && fileNum < fileMax) if((kHeld & KEY_DOWN) && fileNum < fileMax && fileNum < fileMax)
{ {
if(osGetTime() - mill < 100) if(osGetTime() - mill < 100)
continue; continue;
fileNum++; fileNum++;
if(fileNum >= MAX_LIST && fileMax - fileNum >= 0 && from < fileMax - MAX_LIST) if(fileNum >= MAX_LIST && fileMax - fileNum >= 0 &&
from < fileMax - MAX_LIST)
from++; from++;
mill = osGetTime(); mill = osGetTime();
}
if(kHeld & (KEY_DOWN | KEY_UP))
{
consoleClear(); consoleClear();
if(listDir(from, MAX_LIST, fileNum) < 0) if(listDir(from, MAX_LIST, fileNum) < 0)
err_print("Unable to list directory."); err_print("Unable to list directory.");
@@ -286,7 +288,10 @@ int listDir(int from, int max, int select)
goto err; goto err;
if(from == 0) if(from == 0)
{
printf("%c../\n", select == 0 ? '>' : ' '); printf("%c../\n", select == 0 ? '>' : ' ');
listed++;
}
while((ep = readdir(dp)) != NULL) while((ep = readdir(dp)) != NULL)
{ {
@@ -295,15 +300,16 @@ int listDir(int from, int max, int select)
if(fileNum <= from) if(fileNum <= from)
continue; continue;
if(fileNum == max + from)
break;
listed++; listed++;
printf("%c%.38s%s\n", printf("%c%s%.37s%s\n",
select == fileNum ? '>' : ' ', select == fileNum ? '>' : ' ',
ep->d_type == DT_DIR ? "\x1b[34;1m" : "",
ep->d_name, ep->d_name,
ep->d_type == DT_DIR ? "/" : ""); ep->d_type == DT_DIR ? "/\x1b[0m" : "");
if(fileNum == max + from)
break;
} }
if(closedir(dp) != 0) if(closedir(dp) != 0)