Skip to content

Commit

Permalink
[harmony] - Refactors the sorting logic and reuse it for across both …
Browse files Browse the repository at this point in the history
…dialogs rmkit-dev#169
  • Loading branch information
hirako2000 committed Mar 21, 2022
1 parent 92b5525 commit b16c79a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/harmony/app/canvas.cpy
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ namespace app_ui:
self.project_name = file_tokens[0]

void load_project_from_dir(string dir):
vector<string> filenames = util::lsdir(dir, ".raw")
sort(filenames.begin(), filenames.end())
filenames := util::lsdir(dir, ".raw")
sort(filenames.begin(), filenames.end()) // do we really need to sort?

for auto f : filenames:
tokens := str_utils::split(f, '.')
Expand Down
27 changes: 2 additions & 25 deletions src/harmony/app/dialogs.cpy
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#include <algorithm>
#include <dirent.h>
#include <sys/stat.h>

string ABOUT_TEXT = "\
rmHarmony is a sketching app based on libremarkable and mr. doob's harmony. \
brought to you by the letters N and O. icons are from fontawesome \n\n\
Expand Down Expand Up @@ -70,25 +66,7 @@ namespace app_ui:
self.page_size = self.h / self.opt_h - 1

void populate():

vector<tuple<int, string>> entries;

filenames := util::lsdir(SAVE_DIR, ".png")
char full_path[PATH_MAX]
struct stat buf
for (auto filename : filenames)
sprintf(full_path, "%s/%s", SAVE_DIR, filename.c_str())
if(stat(full_path, &buf))
debug "Failed stat() on ", full_path
continue
entries.push_back({buf.st_mtime, filename})
filenames.clear()
sort(entries.begin(), entries.end())

for (auto e : entries)
filenames.push_back(std::get<1>(e))
reverse(filenames.begin(),filenames.end())
filenames := util::lsdir(SAVE_DIR, ".png", util::MODIFIED_DATE_DESC)
self.options = filenames

void on_row_selected(string name):
Expand Down Expand Up @@ -118,7 +96,6 @@ namespace app_ui:
ui::MainLoop::hide_overlay()

void populate():
vector<string> filenames = util::lsdir(SAVE_DIR, ".hrm")
sort(filenames.begin(),filenames.end())
filenames := util::lsdir(SAVE_DIR, ".hrm", util::MODIFIED_DATE_DESC)
self.options = filenames

28 changes: 27 additions & 1 deletion src/rmkit/util/lsdir.cpy
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
#include <algorithm>
#include <dirent.h>
#include <sys/stat.h>
#include "../shared/string.h"

namespace util:
vector<string> lsdir(string dirname, string ext=""):

enum SORTING { DEFAULT, MODIFIED_DATE_DESC }

void sort_by_modified_date(vector<string> &filenames, string dirname):
struct stat buf
vector<tuple<int, string>> entries
for (auto filename : filenames)
string full_path = ""
full_path.append(dirname).append("/").append(filename)
if(stat(full_path.c_str(), &buf))
debug "Failed stat() on ", full_path
continue
entries.push_back({buf.st_mtime, filename})

sort(entries.begin(), entries.end())
filenames.clear()
for (auto e : entries)
filenames.push_back(std::get<1>(e))

reverse(filenames.begin(), filenames.end())

vector<string> lsdir(string dirname, string ext="", SORTING sort=DEFAULT):
DIR *dir
struct dirent *ent

Expand All @@ -16,4 +39,7 @@ namespace util:
else:
perror ("")

if (sort == MODIFIED_DATE_DESC)
sort_by_modified_date(filenames, dirname)

return filenames

0 comments on commit b16c79a

Please sign in to comment.