Skip to content

Commit

Permalink
List apps
Browse files Browse the repository at this point in the history
  • Loading branch information
NezbednikSK committed Aug 28, 2023
1 parent f081b76 commit 330a8ae
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 37 deletions.
3 changes: 1 addition & 2 deletions data/default_config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"repositories": [
"hbb1.oscwii.org",
"hbb4.oscwii.org"
"hbb1.oscwii.org"
]
}
4 changes: 2 additions & 2 deletions source/config.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef CONSOLE_H
#define CONSOLE_H
#ifndef CONFIG_H
#define CONFIG_H

#define VERSION "0.0dev"
#define USERAGENT "LibreShop/" VERSION
Expand Down
6 changes: 6 additions & 0 deletions source/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
// Uncomment if this is a development build
#define DEBUG

// Useful for quick debugging
//#define SKIP_LIBRARY_BOOT

// Useful for repo stuff
//#define ALWAYS_DEFAULT_CONFIG

#endif

#if ! defined(DEBUG_H) && defined(DEBUG)
Expand Down
8 changes: 6 additions & 2 deletions source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ int main(int argc, char **argv) {
printf("\n");

FILE* _config = fopen(APPS_DIR "/config.json", "r");
#ifdef DEBUG
#ifdef ALWAYS_DEFAULT_CONFIG
fclose(_config);
_config = NULL;
#endif
Expand All @@ -150,6 +150,9 @@ int main(int argc, char **argv) {

printf("\n");

json_object_set(config, "temp", json_object());

#ifndef SKIP_LIBRARY_BOOT
DIR* library_check = opendir(REPO_DIR);
if (!library_check) {
closedir(library_check);
Expand Down Expand Up @@ -367,9 +370,10 @@ int main(int argc, char **argv) {

printf("\n");
logprint(1, "Syncing complete!\n");
#endif

start_tui(config);

clear_screen();
printf("Exiting...\n");

Expand Down
171 changes: 140 additions & 31 deletions source/tui.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,115 @@ void print_bottombar(int limit, int offset, int file, char* bottom) {
free(wholeline);
}

void surf_category(json_t* config, const char* hostname, const char* name, json_t* category) {
json_error_t error;

const char* categoryname = json_string_value(json_object_get(category, "display_name"));
const char* catslug = json_string_value(json_object_get(category, "name"));

char* directory = malloc(strlen(REPO_DIR) + 1 + 2 + strlen(hostname) + 1 + strlen(catslug) + 1);
sprintf(directory, REPO_DIR "/D_%s/%s", hostname, catslug);

char* jsonname = malloc(strlen(hostname) + 1 + strlen(catslug) + 1 + 2);
sprintf(jsonname, "%s+%s", hostname, catslug);
for (int i = 0; i < strlen(jsonname); i++) {
if (jsonname[i] == '.') jsonname[i] = '_';
}

char* jsonname_arr = malloc(strlen(jsonname) + 3);
sprintf(jsonname_arr, "D_%s", jsonname);

json_t* widetemp = json_object_get(config, "temp");
json_t* temp = json_object_get(widetemp, jsonname);

int appsamount = 0;
if (!temp) {
json_object_set(widetemp, jsonname, json_object());
temp = json_object_get(widetemp, jsonname);
json_object_set(widetemp, jsonname_arr, json_array());
json_t* temptemp_arr = json_object_get(widetemp, jsonname_arr);

DIR* dir = opendir(directory);
if (dir == NULL) {
printf("Could not open %s.\n", directory);
free(directory);
free(jsonname);
free(jsonname_arr);
home_exit(true);
}
struct dirent* item;

while ((item = readdir(dir)) != NULL) {
if (strcmp(item->d_name, ".") == 0 || strcmp(item->d_name, "..") == 0) continue;
int itemlen = strlen(directory) + 1 + strlen(item->d_name);
char* itempath = malloc(itemlen + 1);
sprintf(itempath, "%s/%s", directory, item->d_name);

json_t* app = json_load_file(itempath, 0, &error);
if (!app) {
free(itempath);
free(directory);
free(jsonname);
free(jsonname_arr);
home_exit(true);
}

const char* appname = json_string_value(json_object_get(app, "name"));
json_object_set(temp, appname, app);
json_array_append(temptemp_arr, json_string(appname));

appsamount++;
free(itempath);
}

closedir(dir);
}
else {
appsamount = json_array_size(json_object_get(widetemp, jsonname_arr));
}
json_t* temp_arr = json_object_get(widetemp, jsonname_arr);

char* title = malloc(8 + strlen(name) + 3 + strlen(categoryname) + 1);
sprintf(title, "Surfing %s > %s", name, categoryname);

int index = 0;
int offset = 0;
while(true) {
clear_screen();
print_topbar(title);

int printable = MIN(25, appsamount);
for (int i = 0; i < (offset + printable); i++) {
json_t* item = json_array_get(temp_arr, i);
if (i < offset) continue;
i -= offset;
print_cursor(i, index);
printf("%s\n", json_string_value(item));
i += offset;
}

print_bottombar(appsamount, offset, printable - 1, "A to engage, B for back, HOME to exit");
int ret = process_inputs(appsamount, &offset, &index);
if (ret == 2) break;
else if (ret == -1) {
free(title);
free(directory);
free(jsonname);
home_exit(true);
}
else if (ret == 1) {
clear_screen();
printf("app here index %d\n", index);
debug_npause();
}
}

free(title);
free(directory);
free(jsonname);
free(jsonname_arr);
}

void surf_repository(json_t* config, const char* hostname) {
json_error_t error;

Expand Down Expand Up @@ -160,9 +269,7 @@ void surf_repository(json_t* config, const char* hostname) {
home_exit(true);
}
else if (ret == 1) {
clear_screen();
printf("apps here\n");
debug_npause();
surf_category(config, hostname, name, json_array_get(categories, index));
}
}

Expand All @@ -185,41 +292,43 @@ void start_tui(json_t* config) {
int reposamount = json_array_size(repositories);
int index = 0;
int offset = 0;
while(true) {
clear_screen();
print_topbar("Pick a repository");
for (int i = 0; true; i++) {
if (i != 0 || reposamount != 1) {
clear_screen();
print_topbar("Pick a repository");

rewinddir(repos);
rewinddir(repos);

int file = -1;
int skipped = offset;
while ((item = readdir(repos)) != NULL) {
if (strcmp(item->d_name, ".") == 0 || strcmp(item->d_name, "..") == 0) continue;
if (item->d_name[0] == 'D') continue;
if (skipped > 0) {
skipped--;
continue;
}
file++;
if (file >= 25) break;

char* filepath = malloc(strlen(REPO_DIR) + 1 + strlen(item->d_name) + 1); // plus slash plus nul
sprintf(filepath, "%s/%s", REPO_DIR, item->d_name);
int file = -1;
int skipped = offset;
while ((item = readdir(repos)) != NULL) {
if (strcmp(item->d_name, ".") == 0 || strcmp(item->d_name, "..") == 0) continue;
if (item->d_name[0] == 'D') continue;
if (skipped > 0) {
skipped--;
continue;
}
file++;
if (file >= 25) break;

char* filepath = malloc(strlen(REPO_DIR) + 1 + strlen(item->d_name) + 1); // plus slash plus nul
sprintf(filepath, "%s/%s", REPO_DIR, item->d_name);

json_t* repo = json_load_file(filepath, 0, &error);
const char* provider = json_string_value(json_object_get(repo, "provider"));
const char* name = json_string_value(json_object_get(repo, "name"));
json_t* repo = json_load_file(filepath, 0, &error);
const char* provider = json_string_value(json_object_get(repo, "provider"));
const char* name = json_string_value(json_object_get(repo, "name"));

print_cursor(file, index);
printf("%s: %s\n", provider, name);
print_cursor(file, index);
printf("%s: %s\n", provider, name);

json_decref(repo);
free(filepath);
}
json_decref(repo);
free(filepath);
}


print_bottombar(reposamount, offset, file, "A to engage, 1 for settings, HOME to exit");
int ret = process_inputs(reposamount, &offset, &index);
print_bottombar(reposamount, offset, file, "A to engage, 1 for settings, HOME to exit");
}
int ret = (i == 0 && reposamount == 1) ? 1 : process_inputs(reposamount, &offset, &index);

if (ret == -1) break;
else if (ret == 1) {
Expand Down

0 comments on commit 330a8ae

Please sign in to comment.