change list_files to populate_files and not print the file, only in highlight
add ftype to add_file_stat to add marked/file
This commit is contained in:
parent
373ce06175
commit
d29dd3ed5f
3 changed files with 42 additions and 16 deletions
32
ccc.c
32
ccc.c
|
@ -26,9 +26,9 @@ typedef struct {
|
||||||
/* functions' definitions */
|
/* functions' definitions */
|
||||||
void change_dir(const char *buf);
|
void change_dir(const char *buf);
|
||||||
int mkdir_p(const char *destdir);
|
int mkdir_p(const char *destdir);
|
||||||
void list_files(const char *path);
|
void populate_files(const char *path);
|
||||||
int get_directory_size(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf);
|
int get_directory_size(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf);
|
||||||
long add_file_stat(char *filename);
|
long add_file_stat(char *filepath, int type);
|
||||||
void highlight_current_line();
|
void highlight_current_line();
|
||||||
void show_file_content();
|
void show_file_content();
|
||||||
void edit_file();
|
void edit_file();
|
||||||
|
@ -87,7 +87,7 @@ int main(int argc, char** argv)
|
||||||
cwd = memalloc(PATH_MAX * sizeof(char));
|
cwd = memalloc(PATH_MAX * sizeof(char));
|
||||||
getcwd(cwd, PATH_MAX);
|
getcwd(cwd, PATH_MAX);
|
||||||
|
|
||||||
list_files(cwd);
|
populate_files(cwd);
|
||||||
highlight_current_line();
|
highlight_current_line();
|
||||||
|
|
||||||
/* set window name */
|
/* set window name */
|
||||||
|
@ -233,7 +233,7 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
/* mark files by space */
|
/* mark files by space */
|
||||||
case 32:
|
case 32:
|
||||||
;
|
add_file_stat(get_filepath(current_selection), 1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* escape */
|
/* escape */
|
||||||
|
@ -264,7 +264,7 @@ void change_dir(const char *buf)
|
||||||
{
|
{
|
||||||
strcpy(cwd, buf);
|
strcpy(cwd, buf);
|
||||||
clear_files();
|
clear_files();
|
||||||
list_files(cwd);
|
populate_files(cwd);
|
||||||
current_selection = 0;
|
current_selection = 0;
|
||||||
highlight_current_line();
|
highlight_current_line();
|
||||||
}
|
}
|
||||||
|
@ -318,17 +318,16 @@ int mkdir_p(const char *destdir)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Read the provided directory and list all files to window 0
|
* Read the provided directory and add all files in directory to linked list
|
||||||
* ep->d_name -> filename
|
* ep->d_name -> filename
|
||||||
*/
|
*/
|
||||||
void list_files(const char *path)
|
void populate_files(const char *path)
|
||||||
{
|
{
|
||||||
DIR *dp;
|
DIR *dp;
|
||||||
struct dirent *ep;
|
struct dirent *ep;
|
||||||
|
|
||||||
draw_border_title(directory_border, true);
|
draw_border_title(directory_border, true);
|
||||||
if ((dp = opendir(path)) != NULL) {
|
if ((dp = opendir(path)) != NULL) {
|
||||||
int count = 0;
|
|
||||||
/* clear directory window to ready for printing */
|
/* clear directory window to ready for printing */
|
||||||
wclear(directory_content);
|
wclear(directory_content);
|
||||||
|
|
||||||
|
@ -346,12 +345,7 @@ void list_files(const char *path)
|
||||||
strcat(filename, "/");
|
strcat(filename, "/");
|
||||||
strcat(filename, ep->d_name); /* add file name */
|
strcat(filename, ep->d_name); /* add file name */
|
||||||
|
|
||||||
long index = add_file_stat(filename);
|
add_file_stat(filename, 0);
|
||||||
char *line = get_line(index);
|
|
||||||
|
|
||||||
mvwprintw(directory_content, count, 0, "%s", line);
|
|
||||||
free(line);
|
|
||||||
count++;
|
|
||||||
}
|
}
|
||||||
free(filename);
|
free(filename);
|
||||||
}
|
}
|
||||||
|
@ -371,8 +365,9 @@ int get_directory_size(const char *fpath, const struct stat *sb, int typeflag, s
|
||||||
/*
|
/*
|
||||||
* Get file's last modified time, size, type
|
* Get file's last modified time, size, type
|
||||||
* Add that file into list
|
* Add that file into list
|
||||||
|
* ftype: 0->dir files, 0->marked
|
||||||
*/
|
*/
|
||||||
long add_file_stat(char *filepath)
|
long add_file_stat(char *filepath, int ftype)
|
||||||
{
|
{
|
||||||
struct stat file_stat;
|
struct stat file_stat;
|
||||||
if (stat(filepath, &file_stat) == -1) {
|
if (stat(filepath, &file_stat) == -1) {
|
||||||
|
@ -387,7 +382,6 @@ long add_file_stat(char *filepath)
|
||||||
/* format last modified time to a string */
|
/* format last modified time to a string */
|
||||||
strftime(time, 20, "%Y-%m-%d %H:%M", localtime(&file_stat.st_mtime));
|
strftime(time, 20, "%Y-%m-%d %H:%M", localtime(&file_stat.st_mtime));
|
||||||
|
|
||||||
|
|
||||||
/* get file size */
|
/* get file size */
|
||||||
double bytes = file_stat.st_size;
|
double bytes = file_stat.st_size;
|
||||||
if (S_ISDIR(file_stat.st_mode)) {
|
if (S_ISDIR(file_stat.st_mode)) {
|
||||||
|
@ -426,6 +420,11 @@ long add_file_stat(char *filepath)
|
||||||
}
|
}
|
||||||
/* don't know how to handle socket, block device, character device and FIFO */
|
/* don't know how to handle socket, block device, character device and FIFO */
|
||||||
|
|
||||||
|
if (ftype == 1) {
|
||||||
|
long index = add_marked(filepath, type);
|
||||||
|
free(type);
|
||||||
|
return index;
|
||||||
|
}
|
||||||
char *total_stat = memalloc(45 * sizeof(char));
|
char *total_stat = memalloc(45 * sizeof(char));
|
||||||
snprintf(total_stat, 45, "%-18s %-10s", time, size);
|
snprintf(total_stat, 45, "%-18s %-10s", time, size);
|
||||||
total_stat[strlen(total_stat)] = '\0';
|
total_stat[strlen(total_stat)] = '\0';
|
||||||
|
@ -440,6 +439,7 @@ long add_file_stat(char *filepath)
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Highlight current line by reversing the color
|
* Highlight current line by reversing the color
|
||||||
*/
|
*/
|
||||||
|
|
25
file.c
25
file.c
|
@ -92,6 +92,31 @@ long add_file(char *filepath, char *stats, char *type)
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long add_marked(char *filepath, char *type)
|
||||||
|
{
|
||||||
|
file *current = marked;
|
||||||
|
file *new_file = memalloc(sizeof(file));
|
||||||
|
char *buf = strdup(filepath);
|
||||||
|
char *buf2 = strdup(type);
|
||||||
|
if (buf == NULL || buf2 == NULL) {
|
||||||
|
perror("ccc");
|
||||||
|
}
|
||||||
|
new_file->path = buf;
|
||||||
|
new_file->type = buf2;
|
||||||
|
new_file->next = NULL;
|
||||||
|
if (current == NULL) {
|
||||||
|
files = new_file;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
long index = 1;
|
||||||
|
while (current->next != NULL) {
|
||||||
|
current = current->next;
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
current->next = new_file;
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
file *get_file(long index)
|
file *get_file(long index)
|
||||||
{
|
{
|
||||||
file *current = files;
|
file *current = files;
|
||||||
|
|
1
file.h
1
file.h
|
@ -14,6 +14,7 @@ long marked_len();
|
||||||
void clear_files();
|
void clear_files();
|
||||||
void clear_marked();
|
void clear_marked();
|
||||||
long add_file(char *filename, char *time, char *type);
|
long add_file(char *filename, char *time, char *type);
|
||||||
|
long add_marked(char *filepath, char *type);
|
||||||
file *get_file(long index);
|
file *get_file(long index);
|
||||||
char *get_filepath(long index);
|
char *get_filepath(long index);
|
||||||
char *get_line(long index);
|
char *get_line(long index);
|
||||||
|
|
Loading…
Reference in a new issue