mirror of
https://github.com/tomasriveral/NoteWrapper.git
synced 2026-08-12 02:18:38 +02:00
Reworked configuration.
This commit is contained in:
@@ -19,13 +19,16 @@ make
|
|||||||
4. configure `./config.json`
|
4. configure `./config.json`
|
||||||
5. run the binary `notewrapper`
|
5. run the binary `notewrapper`
|
||||||
|
|
||||||
|
### Configuration
|
||||||
|
Change `$/.config/notewrapper/config.json`. If it does not exist. On building, it should copy a default config.
|
||||||
|
|
||||||
### What needs to be done (a lot)
|
### What needs to be done (a lot)
|
||||||
- [x] Add a way to create vaults
|
- [x] Add a way to create vaults
|
||||||
- [x] Add a way to delete vaults
|
- [x] Add a way to delete vaults
|
||||||
- [ ] Add a way to delete notes
|
- [ ] Add a way to delete notes
|
||||||
- [x] Stylisize a bit the TUI
|
- [x] Stylisize a bit the TUI
|
||||||
- [ ] Add more options to the config file
|
- [ ] Add more options to the config file
|
||||||
- [ ] Search for config.json in other directories such as ~/.config/notewrapper/ and not only this directory
|
- [x] Search for config.json in other directories such as ~/.config/notewrapper/ and not only this directory
|
||||||
- [x] Port vivify.vim to nixpkgs
|
- [x] Port vivify.vim to nixpkgs
|
||||||
- [ ] Add a way to have vaults in different directories
|
- [ ] Add a way to have vaults in different directories
|
||||||
- [ ] Actually open vivify when opening nvim
|
- [ ] Actually open vivify when opening nvim
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"directory": "/home/tomasr/Documents/Notes/"
|
"directory": "$/Documents/Notes/"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
CONFIG_DIR := $(HOME)/.config/notewrapper
|
||||||
|
CONFIG_FILE := $(CONFIG_DIR)/config.json
|
||||||
CC = gcc
|
CC = gcc
|
||||||
SRC = startup.c
|
SRC = startup.c
|
||||||
TARGET = notewrapper
|
TARGET = notewrapper
|
||||||
@@ -7,11 +9,26 @@ DEBUG ?= 0
|
|||||||
ifeq ($(DEBUG),1)
|
ifeq ($(DEBUG),1)
|
||||||
CFLAGS += -g
|
CFLAGS += -g
|
||||||
endif
|
endif
|
||||||
.PHONY: all clean run
|
.PHONY: all clean run install_config
|
||||||
all: $(TARGET)
|
|
||||||
|
# existing targets
|
||||||
|
all: install_config $(TARGET)
|
||||||
|
|
||||||
$(TARGET): $(SRC)
|
$(TARGET): $(SRC)
|
||||||
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
|
@$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
|
||||||
|
|
||||||
run: $(TARGET)
|
run: $(TARGET)
|
||||||
./$(TARGET)
|
@./$(TARGET)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(TARGET)
|
@rm -f $(TARGET)
|
||||||
|
|
||||||
|
# new target for configuration
|
||||||
|
install_config:
|
||||||
|
@mkdir -p $(CONFIG_DIR)
|
||||||
|
@if [ ! -f $(CONFIG_FILE) ]; then \
|
||||||
|
cp ./config.json $(CONFIG_FILE); \
|
||||||
|
echo "config.json installed to $(CONFIG_FILE)"; \
|
||||||
|
else \
|
||||||
|
echo "$(CONFIG_FILE) already exists, skipping installing default config file"; \
|
||||||
|
fi
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <ftw.h> // used for deleting dirs
|
#include <ftw.h> // used for deleting dirs
|
||||||
|
#include <pwd.h> // for getting home directory
|
||||||
|
|
||||||
// small helper functions here. Big ones are after
|
// small helper functions here. Big ones are after
|
||||||
int compareString(const void *a, const void *b) { // this will be the function used by qsort
|
int compareString(const void *a, const void *b) { // this will be the function used by qsort
|
||||||
@@ -48,6 +49,7 @@ int rmrf(char *path) {
|
|||||||
|
|
||||||
// big critical functions here. Small ones are before
|
// big critical functions here. Small ones are before
|
||||||
char *createNewNote(char dirToVault[PATH_MAX], char *vaultFromDir, int debug) {
|
char *createNewNote(char dirToVault[PATH_MAX], char *vaultFromDir, int debug) {
|
||||||
|
// (TODO LATER) Add check. If the user creates a note with a name that already exists. it erases the old one
|
||||||
// input from user for the name
|
// input from user for the name
|
||||||
char fileName[256];
|
char fileName[256];
|
||||||
initscr();
|
initscr();
|
||||||
@@ -147,8 +149,7 @@ char** getVaultsFromDirectory(char *dirString, size_t *count, int debug) {
|
|||||||
// originally from https://www.geeksforgeeks.org/c/c-program-list-files-sub-directories-directory/
|
// originally from https://www.geeksforgeeks.org/c/c-program-list-files-sub-directories-directory/
|
||||||
struct dirent *vaultsDirectoryEntry;
|
struct dirent *vaultsDirectoryEntry;
|
||||||
DIR *vaultsDirectory = opendir(dirString);
|
DIR *vaultsDirectory = opendir(dirString);
|
||||||
if (vaultsDirectory == NULL) // opendir returns NULL if couldn't open directory
|
if (vaultsDirectory == NULL) { // opendir returns NULL if couldn't open directory
|
||||||
{
|
|
||||||
printf("\e[0;31mERROR: Could not open current directory\e[0m\n" );
|
printf("\e[0;31mERROR: Could not open current directory\e[0m\n" );
|
||||||
exit(1); //something is fucked up
|
exit(1); //something is fucked up
|
||||||
}
|
}
|
||||||
@@ -339,46 +340,79 @@ int main(int argc, char *argv[]) {
|
|||||||
debug = 1;
|
debug = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Read config file
|
|
||||||
FILE *f = fopen("./config.json", "r");
|
//---------------------------------------------------------------------------------------------
|
||||||
if (!f) {
|
//---------------------------------------------------------------------------------------------
|
||||||
fprintf(stderr, "\e[0;31mERROR: Could not open config.json\e[0m\n");
|
// this part handles the config.json file
|
||||||
return 1;
|
|
||||||
|
// gets the home directory
|
||||||
|
struct passwd *pw = getpwuid(getuid());
|
||||||
|
const char *homedir = pw->pw_dir;
|
||||||
|
|
||||||
|
// check if the config file exists
|
||||||
|
char configPath[PATH_MAX];
|
||||||
|
snprintf(configPath, sizeof(configPath), "%s/.config/notewrapper/config.json", homedir);
|
||||||
|
if (debug) {printf("\e[0;32m[DEBUG]\e[0m the path to the config file is %s\n", configPath);}
|
||||||
|
if (stat(configPath, &(struct stat){0}) == -1) { // if the config directory do not exist
|
||||||
|
printf("\e[0;31mERROR: the config file (\e[0;32m$/.config/notewrapper/config.json\e[0;31m) does not exist.\nCompiling the program with \e[0;32mmake\e[0;31m should solve this error.\e[0m\n");
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// opens config.json
|
||||||
|
FILE *f = fopen(configPath, "r");
|
||||||
|
if (!f) {
|
||||||
|
printf("\e[0;31mERROR: The config file does exist, but can not be open. Something went wrong.\e[0;32m\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// loads and read the config file
|
||||||
|
//gets the size
|
||||||
fseek(f, 0, SEEK_END);
|
fseek(f, 0, SEEK_END);
|
||||||
long size = ftell(f);
|
long size = ftell(f);
|
||||||
rewind(f);
|
rewind(f);
|
||||||
|
//gets the data
|
||||||
char *data = malloc(size + 1);
|
char *data = malloc(size+1);
|
||||||
size_t read_bytes = fread(data, 1, size, f);
|
if (!data) {
|
||||||
if (read_bytes != size) {
|
fclose(f);
|
||||||
fprintf(stderr, "\e[0;31mERROR: Failed to read file\e[0m\n");
|
printf("\e[0;31mERROR: malloc failed allocating memory for the variable data. Something went wrong.\e[0m\n");
|
||||||
free(data);
|
exit(1);
|
||||||
fclose(f);
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
data[size] = 0;
|
size_t readBytes = fread(data, 1, size, f); // 1 --> size of each item
|
||||||
|
if (readBytes != size) {
|
||||||
|
printf("\e[0;31mERROR: Failed to read config file (%zu bytes read, expected %ld)\e[0m\n", readBytes, size);
|
||||||
|
free(data);
|
||||||
|
fclose(f);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
data[size] = '\0';
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
// Parse JSON
|
// parse the JSON
|
||||||
|
|
||||||
cJSON *json = cJSON_Parse(data);
|
cJSON *json = cJSON_Parse(data);
|
||||||
if (!json) {
|
if (!json) {
|
||||||
fprintf(stderr, "\e[0;31mERROR: JSON parse error\[0m\n");
|
printf("\e[0;31mERROR: JSON parse error\e[0m\n"); // (TODO LATER) replace all printf("\e[0;31m with fpintf(stderr( "\e[0;31m and maybe debug messages too
|
||||||
free(data);
|
free(data);
|
||||||
return 1;
|
exit(1);
|
||||||
}
|
}
|
||||||
|
cJSON *dirJson = cJSON_GetObjectItem(json, "directory");
|
||||||
// Get "directory"
|
char *notesDirectoryString = malloc(PATH_MAX);
|
||||||
cJSON *dirJsonFormat = cJSON_GetObjectItem(json, "directory");
|
if (dirJson && cJSON_IsString(dirJson) && dirJson->valuestring != NULL) {
|
||||||
|
const char *rawPath = dirJson->valuestring;
|
||||||
char *notesDirectoryString = NULL;
|
if (rawPath[0] == '$') { // expands $ in the path
|
||||||
if (dirJsonFormat && cJSON_IsString(dirJsonFormat) && dirJsonFormat->valuestring != NULL) {
|
snprintf(notesDirectoryString, PATH_MAX, "%s/%s", homedir, rawPath+1);
|
||||||
notesDirectoryString = strdup(dirJsonFormat->valuestring); // independent string
|
} else {
|
||||||
|
notesDirectoryString = strdup(rawPath);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
notesDirectoryString = "~/Documents/Notes/"; // default
|
// default vault path if it is not set in the config
|
||||||
|
snprintf(notesDirectoryString, PATH_MAX, "%s/Documents/Notes/", homedir);
|
||||||
}
|
}
|
||||||
|
//cleans up
|
||||||
|
cJSON_Delete(json);
|
||||||
|
free(data);
|
||||||
|
//---------------------------------------------------------------------------------------------
|
||||||
|
//---------------------------------------------------------------------------------------------
|
||||||
int shouldExit = 0;
|
int shouldExit = 0;
|
||||||
while(!shouldExit) {
|
while(!shouldExit) {
|
||||||
// this loop is the vault selector
|
// this loop is the vault selector
|
||||||
@@ -431,7 +465,7 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
if (strcmp(noteSelected, "Create new note") != 0 && strcmp(noteSelected,"Back to vault selection") != 0 && strcmp(noteSelected, "Delete vault") != 0 && strcmp(noteSelected,"Quit (Ctrl+C)") != 0) {
|
if (strcmp(noteSelected, "Create new note") != 0 && strcmp(noteSelected,"Back to vault selection") != 0 && strcmp(noteSelected, "Delete vault") != 0 && strcmp(noteSelected,"Quit (Ctrl+C)") != 0) {
|
||||||
char fullPath[PATH_MAX];
|
char fullPath[PATH_MAX];
|
||||||
sprintf(fullPath, "%s/%s/%s", notesDirectoryString, vaultSelected, noteSelected);
|
sprintf(fullPath, "%s/%s/%s", notesDirectoryString, vaultSelected, noteSelected); // (TODO LATER) change all sprintf to snprintf which checks for buffer size
|
||||||
openNvim(fullPath, debug);
|
openNvim(fullPath, debug);
|
||||||
} else if (strcmp(noteSelected,"Create new note") == 0) {
|
} else if (strcmp(noteSelected,"Create new note") == 0) {
|
||||||
char *pathToOpen = createNewNote(notesDirectoryString, vaultSelected, debug);
|
char *pathToOpen = createNewNote(notesDirectoryString, vaultSelected, debug);
|
||||||
@@ -467,10 +501,5 @@ int main(int argc, char *argv[]) {
|
|||||||
shouldExit = 1;
|
shouldExit = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cleanup
|
|
||||||
if (dirJsonFormat && notesDirectoryString != dirJsonFormat->valuestring) free(notesDirectoryString); // only free strdup
|
|
||||||
cJSON_Delete(json);
|
|
||||||
free(data);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user