Skip to content

Commit

Permalink
textreader: Initial implementation of a text reader for reading logs/…
Browse files Browse the repository at this point in the history
…configs (eventually leading to an editor)
  • Loading branch information
Joel16 committed Sep 6, 2020
1 parent 8da8375 commit 73c8348
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 3 deletions.
9 changes: 8 additions & 1 deletion include/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ enum MENU_STATES {
MENU_STATE_PROPERTIES,
MENU_STATE_SETTINGS,
MENU_STATE_IMAGEVIEWER,
MENU_STATE_ARCHIVEEXTRACT
MENU_STATE_ARCHIVEEXTRACT,
MENU_STATE_TEXTREADER
};

typedef struct {
Expand All @@ -35,8 +36,14 @@ typedef struct {
Tex texture;
} MenuItem;

typedef struct {
char *buf;
size_t buf_size;
} TextReader;

extern SDL_Window *window;
extern MenuItem item;
extern TextReader text_reader;

namespace GUI {
inline void ResetCheckbox(void) {
Expand Down
1 change: 1 addition & 0 deletions include/windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Windows {
void FileBrowserWindow(bool *focus, bool *first_item);
void ImageWindow(Tex *texture);
void SettingsWindow(void);
void TextReaderWindow(void);
}

#endif
2 changes: 1 addition & 1 deletion source/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ namespace Config {
std::strcpy(config.cwd, "/");

// Delete config file if config file is updated. This will rarely happen.
if (config_version_holder < CONFIG_VERSION) {
if (config_version_holder < CONFIG_VERSION) {
fsFsDeleteFile(fs, "/switch/NX-Shell/config.json");
Config::SetDefault(&config);
return Config::Save(config);
Expand Down
9 changes: 9 additions & 0 deletions source/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ namespace GUI {
item.state = MENU_STATE_HOME;
}
}
else if (item.state == MENU_STATE_TEXTREADER) {
text_reader.buf_size = 0;
delete[] text_reader.buf;
item.state = MENU_STATE_HOME;
}
else
item.state = MENU_STATE_HOME;
}
Expand Down Expand Up @@ -149,6 +154,10 @@ namespace GUI {
case MENU_STATE_SETTINGS:
Windows::SettingsWindow();
break;

case MENU_STATE_TEXTREADER:
Windows::TextReaderWindow();
break;

default:
break;
Expand Down
34 changes: 33 additions & 1 deletion source/windows/filebrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "gui.h"
#include "imgui.h"
#include "imgui_internal.h"
#include "log.h"
#include "windows.h"

namespace Windows {
Expand Down Expand Up @@ -44,18 +45,49 @@ namespace Windows {

ImGui::SameLine();
if (ImGui::Selectable(filename.c_str())) {
char path[FS_MAX_PATH + 1];

switch(file_type) {
case FileTypeArchive:
item.state = MENU_STATE_ARCHIVEEXTRACT;
break;

case FileTypeImage:
char path[FS_MAX_PATH + 1];
if ((std::snprintf(path, FS_MAX_PATH, "%s/%s", config.cwd, item.selected_filename.c_str())) > 0) {
Textures::LoadImageFile(path, &item.texture);
item.state = MENU_STATE_IMAGEVIEWER;
}
break;

case FileTypeText:
if ((std::snprintf(path, FS_MAX_PATH, "%s/%s", config.cwd, item.selected_filename.c_str())) > 0) {
Log::Exit();

FsFile file;
Result ret = 0;
if (R_FAILED(ret = fsFsOpenFile(fs, path, FsOpenMode_Read, &file)))
Log::Error("fsFsOpenFile(%s) failed: 0x%x\n", path, ret);

s64 size = 0;
if (R_FAILED(ret = fsFileGetSize(&file, &size))) {
Log::Error("fsFileGetSize(%s) failed: 0x%x\n", path, ret);
fsFileClose(&file);
}

text_reader.buf = new char[size];

u64 bytes_read = 0;
if (R_FAILED(ret = fsFileRead(&file, 0, text_reader.buf, static_cast<u64>(size), FsReadOption_None, &bytes_read))) {
Log::Error("fsFileRead(%s) failed: 0x%x\n", path, ret);
fsFileClose(&file);
}

fsFileClose(&file);
Log::Init();
text_reader.buf_size = bytes_read;
item.state = MENU_STATE_TEXTREADER;
}
break;

default:
break;
Expand Down
19 changes: 19 additions & 0 deletions source/windows/textreader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "config.h"
#include "gui.h"
#include "imgui.h"
#include "windows.h"

TextReader text_reader;

namespace Windows {
void TextReaderWindow(void) {
Windows::SetupWindow();

if (ImGui::Begin(item.selected_filename.c_str(), nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse)) {
ImGui::SetNextWindowFocus();
ImGui::InputTextMultiline(item.selected_filename.c_str(), text_reader.buf, text_reader.buf_size, ImVec2(1270.0f, 660.0f));
}

Windows::ExitWindow();
}
}

0 comments on commit 73c8348

Please sign in to comment.