Refactor the folder structure which only had 2 'real' modules, now it's more real, no need for splitting into folders beside the external one
This commit is contained in:
196
src/plugin_directory_navigation.cpp
Normal file
196
src/plugin_directory_navigation.cpp
Normal file
@@ -0,0 +1,196 @@
|
||||
// @todo: On save rename files, delete files it should apply the changes
|
||||
// Instead of toying with Reopen maybe it should actually detect changes in directory etc. on update
|
||||
|
||||
void CMD_OpenUpFolder() {
|
||||
// @todo: Test this for weird cases
|
||||
BSet main = GetBSet(PrimaryWindowID);
|
||||
String name = ChopLastSlash(main.buffer->name);
|
||||
Open(name);
|
||||
} RegisterCommand(CMD_OpenUpFolder, "ctrl-o", "Open current's file directory or up directory in other cases");
|
||||
|
||||
void InsertDirectoryNavigation(Buffer *buffer) {
|
||||
Assert(buffer->is_dir);
|
||||
Scratch scratch;
|
||||
ResetBuffer(buffer);
|
||||
RawAppendf(buffer, "\n");
|
||||
RawAppendf(buffer, "..\n");
|
||||
for (FileIter it = IterateFiles(scratch, buffer->name); IsValid(it); Advance(&it)) {
|
||||
RawAppendf(buffer, "%S\n", it.filename);
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateDirectoryNavigation() {
|
||||
#if 0
|
||||
ProfileFunction();
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
Buffer *buffer = active.buffer;
|
||||
|
||||
Scratch scratch;
|
||||
Array<String> existing_lines = Split(scratch, AllocCharString(scratch, buffer), "\n");
|
||||
bool modified = false;
|
||||
for (FileIter it = IterateFiles(scratch, buffer->name); IsValid(it); Advance(&it)) {
|
||||
bool found = false;
|
||||
ForItem (existing, existing_lines) {
|
||||
if (existing == it.filename) {
|
||||
found = true;
|
||||
UnorderedRemove(&existing_lines, existing);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (found == false) {
|
||||
modified = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (existing_lines.len > 1) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
ReopenBuffer(buffer);
|
||||
}
|
||||
|
||||
UpdateFuzzySearchView();
|
||||
#endif
|
||||
}
|
||||
|
||||
void OpenDirectoryNavigation(View *view) {
|
||||
SetFuzzy(view);
|
||||
// view->update_hook = UpdateDirectoryNavigation;
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
InsertDirectoryNavigation(buffer);
|
||||
SelectRange(view, Range{});
|
||||
}
|
||||
|
||||
#if 0
|
||||
|
||||
typedef struct {
|
||||
HANDLE dir;
|
||||
OVERLAPPED ov;
|
||||
HANDLE event;
|
||||
BYTE buffer[4096];
|
||||
BOOL pending;
|
||||
} DirectoryWatcher;
|
||||
|
||||
BOOL InitDirectoryWatcher(DirectoryWatcher *w, const wchar_t *path)
|
||||
{
|
||||
ZeroMemory(w, sizeof(*w));
|
||||
|
||||
w->dir = CreateFileW(
|
||||
path,
|
||||
FILE_LIST_DIRECTORY,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
||||
NULL,
|
||||
OPEN_EXISTING,
|
||||
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
|
||||
NULL
|
||||
);
|
||||
|
||||
if (w->dir == INVALID_HANDLE_VALUE)
|
||||
return FALSE;
|
||||
|
||||
w->event = CreateEventW(NULL, TRUE, FALSE, NULL);
|
||||
if (!w->event)
|
||||
return FALSE;
|
||||
|
||||
w->ov.hEvent = w->event;
|
||||
w->pending = FALSE;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BOOL WasDirectoryModified(DirectoryWatcher *w)
|
||||
{
|
||||
DWORD bytes;
|
||||
|
||||
if (!w->pending) {
|
||||
ResetEvent(w->event);
|
||||
w->pending = ReadDirectoryChangesW(
|
||||
w->dir,
|
||||
w->buffer,
|
||||
sizeof(w->buffer),
|
||||
TRUE,
|
||||
FILE_NOTIFY_CHANGE_FILE_NAME |
|
||||
FILE_NOTIFY_CHANGE_DIR_NAME |
|
||||
FILE_NOTIFY_CHANGE_SIZE |
|
||||
FILE_NOTIFY_CHANGE_LAST_WRITE,
|
||||
NULL,
|
||||
&w->ov,
|
||||
NULL
|
||||
);
|
||||
if (!w->pending)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (WaitForSingleObject(w->event, 0) == WAIT_OBJECT_0) {
|
||||
GetOverlappedResult(w->dir, &w->ov, &bytes, FALSE);
|
||||
w->pending = FALSE;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void DestroyDirectoryWatcher(DirectoryWatcher *w)
|
||||
{
|
||||
if (w->dir != INVALID_HANDLE_VALUE)
|
||||
CloseHandle(w->dir);
|
||||
if (w->event)
|
||||
CloseHandle(w->event);
|
||||
}
|
||||
|
||||
//
|
||||
DirectoryWatcher w;
|
||||
InitDirectoryWatcher(&w, L"C:\\mydir");
|
||||
|
||||
if (WasDirectoryModified(&w)) {
|
||||
// something changed
|
||||
}
|
||||
|
||||
#if OS_LINUX
|
||||
#include <sys/inotify.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
typedef struct {
|
||||
int fd;
|
||||
int wd;
|
||||
} DirectoryWatcher;
|
||||
|
||||
int InitDirectoryWatcher(DirectoryWatcher *w, const char *path)
|
||||
{
|
||||
w->fd = inotify_init1(IN_NONBLOCK);
|
||||
if (w->fd < 0)
|
||||
return 0;
|
||||
|
||||
w->wd = inotify_add_watch(
|
||||
w->fd,
|
||||
path,
|
||||
IN_CREATE | IN_DELETE | IN_MOVE | IN_MODIFY
|
||||
);
|
||||
|
||||
return w->wd >= 0;
|
||||
}
|
||||
|
||||
int WasDirectoryModified(DirectoryWatcher *w)
|
||||
{
|
||||
char buffer[4096];
|
||||
ssize_t len = read(w->fd, buffer, sizeof(buffer));
|
||||
|
||||
if (len > 0)
|
||||
return 1;
|
||||
|
||||
return 0; // no data = no changes
|
||||
}
|
||||
|
||||
void DestroyDirectoryWatcher(DirectoryWatcher *w)
|
||||
{
|
||||
if (w->wd >= 0)
|
||||
inotify_rm_watch(w->fd, w->wd);
|
||||
if (w->fd >= 0)
|
||||
close(w->fd);
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user