1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
#include <assert.h>
#include <dirent.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "../include/read_cfg.h"
#include "../include/entry.h"
char sep = '/';
char *find_config(){
char *home = getenv("HOME");
char *path = malloc(sizeof(char) * BUF_LEN);
char choices[2][BUF_LEN];
int check_count = 2;
int i;
sprintf(choices[0], "%s%c.config%cterminal-media-launcher%cconfig.lua", home, sep, sep, sep);
sprintf(choices[1], "%s%c.terminal-media-launcher%cconfig.lua", home, sep, sep);
for(i = 0; i < check_count; i++){
strcpy(path, choices[i]);
printf("Checking for config at %s: ", choices[i]);
if(access(path, R_OK) == 0){
printf("Using config \"%s\"\n\n", path);
return path;
}
else printf("File does not exist\n");
}
mkconfig_wizard(choices[0]);
strcpy(path, choices[0]);
return path;
}
void mkconfig_wizard(char *path){
char input;
FILE *fp;
char *home = getenv("HOME");
printf("\nNo configuration file found. Auto-generate one now at \"%s\"? [Y/n] ", path);
fflush(stdout);
scanf(" %c", &input);
if(input == 'n'){
printf("Configuration will not be auto-generated\n");
refer_to_doc();
exit(0);
}
printf("Generating configuration file at \"%s\"...\n", path);
//ensure directories have been created
if(home == NULL){
printf("Failed: HOME is NULL\n");
exit(1);
}
sprintf(path, "%s%c.config%c", home, sep, sep);
mkdir(path, 0755);
sprintf(path, "%s%c.config%cterminal-media-launcher%c", home, sep, sep, sep);
mkdir(path, 0755);
sprintf(path, "%s%c.config%cterminal-media-launcher%cconfig.lua", home, sep, sep, sep);
//open file for writing, make sure non-NULL
fp = fopen(path, "w");
if(fp == NULL){
printf("Failed: \"%s\" could not be open for writing\n", path);
exit(1);
}
fprintf(fp,
"-- This file was auto-generated by terminal-media-launcher. See docs/terminal-media-launcher-config.md or terminal-media-launcher-config(5) for documentation\n"
"-- The default launcher is set to \"xdg-open\" which will open files based on the relevant default application set through xdg\n\n"
"local lfs = require \"lfs\"\n"
"\n"
"local function addGroup(name, launcher, flags)\n"
" assert(type(name) == \"string\")\n"
"\n"
" -- create Groups table if needed\n"
" if Groups == nil then\n"
" Groups = {}\n"
" end\n"
"\n"
" local new_group = {}\n"
" new_group.name = name\n"
" new_group.Entries = {}\n"
" if launcher ~= nil then\n"
" new_group.Launcher = launcher\n"
" end\n"
" if flags ~= nil then\n"
" new_group.Flags = flags\n"
" end\n"
"\n"
" table.insert(Groups, new_group)\n"
" return new_group\n"
"end\n"
"\n"
"local function addEntries(parentGroup, startDir, filePattern, recursive)\n"
" -- recursive arg is a boolean for whether or not to descend into subdirectories (false by default)\n"
" assert(type(parentGroup) == \"table\")\n"
" assert(type(parentGroup.Entries) == \"table\")\n"
" assert(type(startDir) == \"string\")\n"
" assert(type(filePattern) == \"string\")\n"
"\n"
" for file in lfs.dir(startDir) do\n"
" local fullFilePath = startDir .. \"/\" .. file\n"
" if file ~= \".\" and file ~= \"..\" then\n"
" -- descend into subdirectory if recursive is set to true\n"
" if lfs.attributes(fullFilePath).mode == \"directory\" and recursive == true then\n"
" addEntries(parentGroup, fullFilePath, filePattern, recursive)\n"
" elseif lfs.attributes(fullFilePath).mode == \"file\" then\n"
" if string.match(file, filePattern) then\n"
" table.insert(parentGroup.Entries, {\n"
" name = file,\n"
" path = fullFilePath\n"
" })\n"
" end\n"
" end\n"
" end\n"
" end\n"
"end\n"
"\n"
"local music = addGroup(\"Music\", \"xdg-open\")\n"
"addEntries(music, os.getenv(\"HOME\") .. \"/Music\", \".*\", true)\n"
"\n"
"local pictures = addGroup(\"Pictures\", \"xdg-open\")\n"
"addEntries(pictures, os.getenv(\"HOME\") .. \"/Pictures\", \".*\", true)\n"
"\n"
"local videos = addGroup(\"Videos\", \"xdg-open\")\n"
"addEntries(videos, os.getenv(\"HOME\") .. \"/Videos\", \".*\", true)\n");
fclose(fp);
printf("done\nIt is highly recommended to further tweak the configuration file! [press any key to continue]");
fflush(stdout);
getchar();
getchar();
return;
}
|