commit
0c9ebad916
3 changed files with 160 additions and 31 deletions
148
ccc.c
148
ccc.c
|
@ -24,7 +24,8 @@ typedef struct {
|
||||||
|
|
||||||
/* functions' definitions */
|
/* functions' definitions */
|
||||||
void list_files(const char *path);
|
void list_files(const char *path);
|
||||||
char *get_file_stat(char *filename);
|
long long get_directory_size(const char *path);
|
||||||
|
long add_file_stat(char *filename);
|
||||||
void highlight_current_line();
|
void highlight_current_line();
|
||||||
void show_file_content();
|
void show_file_content();
|
||||||
void edit_file();
|
void edit_file();
|
||||||
|
@ -64,11 +65,12 @@ int main(int argc, char** argv)
|
||||||
endwin();
|
endwin();
|
||||||
die("ccc: Color is not supported in your terminal.\n");
|
die("ccc: Color is not supported in your terminal.\n");
|
||||||
} else {
|
} else {
|
||||||
|
use_default_colors();
|
||||||
start_color();
|
start_color();
|
||||||
}
|
}
|
||||||
|
|
||||||
init_pair(1, COLOR_BLUE, COLOR_BLACK); /* foreground, background */
|
init_pair(1, COLOR_BLUE, -1); /* foreground, background */
|
||||||
init_pair(2, COLOR_CYAN, COLOR_BLACK); /* active color */
|
init_pair(2, COLOR_CYAN, -1); /* active color */
|
||||||
|
|
||||||
half_width = COLS / 2;
|
half_width = COLS / 2;
|
||||||
init_windows();
|
init_windows();
|
||||||
|
@ -119,17 +121,14 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
/* enter directory/open a file */
|
/* enter directory/open a file */
|
||||||
case 'l':;
|
case 'l':;
|
||||||
char *filename = get_filepath(current_selection);
|
file *file = get_file(current_selection);
|
||||||
if (filename != NULL) {
|
if (file != NULL) {
|
||||||
struct stat file_stat;
|
|
||||||
stat(filename, &file_stat);
|
|
||||||
|
|
||||||
/* check if it is directory or regular file */
|
/* check if it is directory or regular file */
|
||||||
if (S_ISDIR(file_stat.st_mode)) {
|
if (strncmp(file->type, "DIR", 3) == 0) {
|
||||||
/* change cwd to directory */
|
/* change cwd to directory */
|
||||||
strcpy(cwd, filename);
|
strcpy(cwd, file->path);
|
||||||
clear_files();
|
clear_files();
|
||||||
list_files(filename);
|
list_files(cwd);
|
||||||
current_selection = 0;
|
current_selection = 0;
|
||||||
highlight_current_line();
|
highlight_current_line();
|
||||||
}
|
}
|
||||||
|
@ -158,6 +157,7 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
highlight_current_line();
|
highlight_current_line();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* jump down */
|
/* jump down */
|
||||||
case '\x04':
|
case '\x04':
|
||||||
if ((current_selection + JUMP_NUM) < (files_len() - 1))
|
if ((current_selection + JUMP_NUM) < (files_len() - 1))
|
||||||
|
@ -167,6 +167,7 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
highlight_current_line();
|
highlight_current_line();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* go down */
|
/* go down */
|
||||||
case 'j':
|
case 'j':
|
||||||
if (current_selection < (files_len() - 1))
|
if (current_selection < (files_len() - 1))
|
||||||
|
@ -174,11 +175,13 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
highlight_current_line();
|
highlight_current_line();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* jump to the bottom */
|
/* jump to the bottom */
|
||||||
case 'G':
|
case 'G':
|
||||||
current_selection = (files_len() - 1);
|
current_selection = (files_len() - 1);
|
||||||
highlight_current_line();
|
highlight_current_line();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* jump to the top */
|
/* jump to the top */
|
||||||
case 'g':
|
case 'g':
|
||||||
second = getch();
|
second = getch();
|
||||||
|
@ -200,12 +203,15 @@ int main(int argc, char** argv)
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
delwin(windows[i].window);
|
delwin(windows[i].window);
|
||||||
}
|
}
|
||||||
|
endwin();
|
||||||
init_windows();
|
init_windows();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
clear_files();
|
||||||
|
clear_marked();
|
||||||
endwin();
|
endwin();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -220,8 +226,7 @@ void list_files(const char *path)
|
||||||
struct dirent *ep;
|
struct dirent *ep;
|
||||||
|
|
||||||
draw_border_title(directory_border, true);
|
draw_border_title(directory_border, true);
|
||||||
dp = opendir(path);
|
if ((dp = opendir(path)) != NULL) {
|
||||||
if (dp != NULL) {
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
/* clear directory window to ready for printing */
|
/* clear directory window to ready for printing */
|
||||||
wclear(directory_content);
|
wclear(directory_content);
|
||||||
|
@ -234,19 +239,16 @@ void list_files(const char *path)
|
||||||
|
|
||||||
/* can't be strncmp as that would filter out the dotfiles */
|
/* can't be strncmp as that would filter out the dotfiles */
|
||||||
if (strcmp(filename, ".") && strcmp(filename, "..")) {
|
if (strcmp(filename, ".") && strcmp(filename, "..")) {
|
||||||
|
|
||||||
/* construct full file path */
|
/* construct full file path */
|
||||||
filename[0] = '\0';
|
filename[0] = '\0';
|
||||||
strcat(filename, cwd);
|
strcat(filename, cwd);
|
||||||
strcat(filename, "/");
|
strcat(filename, "/");
|
||||||
strcat(filename, ep->d_name); /* add file name */
|
strcat(filename, ep->d_name); /* add file name */
|
||||||
|
|
||||||
char *stats = get_file_stat(filename);
|
long index = add_file_stat(filename);
|
||||||
long index = add_file(filename, stats);
|
|
||||||
char *line = get_line(index);
|
char *line = get_line(index);
|
||||||
|
|
||||||
mvwprintw(directory_content, count, 0, "%s", line);
|
mvwprintw(directory_content, count, 0, "%s", line);
|
||||||
free(stats);
|
|
||||||
free(line);
|
free(line);
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
@ -259,20 +261,66 @@ void list_files(const char *path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long long get_directory_size(const char *path)
|
||||||
|
{
|
||||||
|
DIR *dp;
|
||||||
|
struct dirent *ep;
|
||||||
|
struct stat statbuf;
|
||||||
|
long long total_size = 0;
|
||||||
|
|
||||||
|
if ((dp = opendir(path)) != NULL) {
|
||||||
|
while ((ep = readdir(dp)) != NULL) {
|
||||||
|
if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
/* build full path of entry */
|
||||||
|
char full_path[PATH_MAX];
|
||||||
|
snprintf(full_path, sizeof(full_path), "%s/%s", path, ep->d_name);
|
||||||
|
|
||||||
|
if (lstat(full_path, &statbuf) == -1) {
|
||||||
|
perror("lstat");
|
||||||
|
closedir(dp);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
/* recursively calculate its size if it is directory */
|
||||||
|
if (S_ISDIR(statbuf.st_mode)) {
|
||||||
|
total_size += get_directory_size(full_path);
|
||||||
|
} else {
|
||||||
|
/* else add the size of the file to the total */
|
||||||
|
total_size += statbuf.st_size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
closedir(dp);
|
||||||
|
} else {
|
||||||
|
perror("ccc");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return total_size;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Get file's last modified time and size
|
* Get file's last modified time, size, type
|
||||||
|
* Add that file into list
|
||||||
*/
|
*/
|
||||||
char *get_file_stat(char *filename)
|
long add_file_stat(char *filepath)
|
||||||
{
|
{
|
||||||
struct stat file_stat;
|
struct stat file_stat;
|
||||||
stat(filename, &file_stat);
|
stat(filepath, &file_stat);
|
||||||
|
|
||||||
|
/* get last modified time */
|
||||||
char *time = memalloc(20 * sizeof(char));
|
char *time = memalloc(20 * sizeof(char));
|
||||||
/* 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 */
|
||||||
double bytes = file_stat.st_size;
|
double bytes = file_stat.st_size;
|
||||||
char *size = memalloc(25 * sizeof(char)); /* max 25 chars due to long, space, suffix and nul */
|
if (S_ISDIR(file_stat.st_mode)) {
|
||||||
|
bytes = (double) get_directory_size(filepath);
|
||||||
|
}
|
||||||
|
/* max 25 chars due to long, space, suffix and nul */
|
||||||
|
char *size = memalloc(25 * sizeof(char));
|
||||||
int unit = 0;
|
int unit = 0;
|
||||||
const char* units[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB"};
|
const char* units[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB"};
|
||||||
while (bytes > 1024) {
|
while (bytes > 1024) {
|
||||||
|
@ -281,13 +329,37 @@ char *get_file_stat(char *filename)
|
||||||
}
|
}
|
||||||
sprintf(size, "%.*f%s", unit, bytes, units[unit]);
|
sprintf(size, "%.*f%s", unit, bytes, units[unit]);
|
||||||
|
|
||||||
|
/* get file type */
|
||||||
|
char *type = memalloc(4 * sizeof(char)); /* 3 chars for type */
|
||||||
|
if (S_ISDIR(file_stat.st_mode)) {
|
||||||
|
strcpy(type, "DIR"); /* directory */
|
||||||
|
} else if (S_ISREG(file_stat.st_mode)) {
|
||||||
|
strcpy(type, "REG"); /* regular file */
|
||||||
|
} else if (S_ISLNK(file_stat.st_mode)) {
|
||||||
|
strcpy(type, "LNK"); /* symbolic link */
|
||||||
|
} else if (S_ISCHR(file_stat.st_mode)) {
|
||||||
|
strcpy(type, "CHR"); /* character device */
|
||||||
|
} else if (S_ISSOCK(file_stat.st_mode)) {
|
||||||
|
strcpy(type, "SOC"); /* socket */
|
||||||
|
} else if (S_ISBLK(file_stat.st_mode)) {
|
||||||
|
strcpy(type, "BLK"); /* block device */
|
||||||
|
} else if (S_ISFIFO(file_stat.st_mode)) {
|
||||||
|
strcpy(type, "FIF"); /* FIFO */
|
||||||
|
}
|
||||||
|
/* don't know how to handle socket, block device, character device and FIFO */
|
||||||
|
|
||||||
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';
|
||||||
|
|
||||||
free(time);
|
free(time);
|
||||||
free(size);
|
free(size);
|
||||||
return total_stat;
|
|
||||||
|
long index = add_file(filepath, total_stat, type);
|
||||||
|
|
||||||
|
free(total_stat);
|
||||||
|
free(type);
|
||||||
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -295,22 +367,42 @@ char *get_file_stat(char *filename)
|
||||||
*/
|
*/
|
||||||
void highlight_current_line()
|
void highlight_current_line()
|
||||||
{
|
{
|
||||||
for (long i = 0; i < files_len(); i++) {
|
long overflow = 0;
|
||||||
if (i == current_selection) {
|
if (current_selection > LINES - 4) {
|
||||||
|
/* overflown */
|
||||||
|
overflow = current_selection - (LINES - 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* calculate range of files to show */
|
||||||
|
long range = files_len();
|
||||||
|
if (range > LINES - 3) {
|
||||||
|
/* if there is more files than lines available to display*/
|
||||||
|
/* shrink range to avaiable lines to display */
|
||||||
|
/* with overflow to keep number of iterations to be constant */
|
||||||
|
range = LINES - 3 + overflow;
|
||||||
|
}
|
||||||
|
|
||||||
|
wclear(directory_content);
|
||||||
|
long line_count = 0;
|
||||||
|
for (long i = overflow; i < range; i++) {
|
||||||
|
if ((overflow == 0 && i == current_selection) || (overflow != 0 && i == current_selection)) {
|
||||||
wattron(directory_content, A_REVERSE);
|
wattron(directory_content, A_REVERSE);
|
||||||
wattron(directory_content, COLOR_PAIR(1));
|
wattron(directory_content, COLOR_PAIR(1));
|
||||||
|
|
||||||
/* update the panel */
|
/* update the panel */
|
||||||
wclear(panel);
|
wclear(panel);
|
||||||
wprintw(panel, "(%ld/%ld) %s", i + 1, files_len(), cwd);
|
wprintw(panel, "(%ld/%ld) %s", current_selection + 1, files_len(), cwd);
|
||||||
}
|
}
|
||||||
/* print the actual filename and stats */
|
/* print the actual filename and stats */
|
||||||
char *line = get_line(i);
|
char *line = get_line(i);
|
||||||
mvwprintw(directory_content, i, 0, "%s", line);
|
if (overflow > 0)
|
||||||
|
mvwprintw(directory_content, line_count, 0, "%s", line);
|
||||||
|
else
|
||||||
|
mvwprintw(directory_content, i, 0, "%s", line);
|
||||||
wattroff(directory_content, A_REVERSE);
|
wattroff(directory_content, A_REVERSE);
|
||||||
wattroff(directory_content, COLOR_PAIR(1));
|
wattroff(directory_content, COLOR_PAIR(1));
|
||||||
free(line);
|
free(line);
|
||||||
|
line_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
wrefresh(directory_content);
|
wrefresh(directory_content);
|
||||||
|
@ -393,6 +485,8 @@ void init_windows()
|
||||||
draw_border_title(directory_border, true);
|
draw_border_title(directory_border, true);
|
||||||
draw_border_title(preview_border, false);
|
draw_border_title(preview_border, false);
|
||||||
|
|
||||||
|
scrollok(directory_content, true);
|
||||||
|
scrollok(preview_content, true);
|
||||||
/* window location y, x */
|
/* window location y, x */
|
||||||
windows[0] = (WIN_STRUCT) { directory_border, 0, 0, 0 };
|
windows[0] = (WIN_STRUCT) { directory_border, 0, 0, 0 };
|
||||||
windows[1] = (WIN_STRUCT) { directory_border, 0, 0, 0 };
|
windows[1] = (WIN_STRUCT) { directory_border, 0, 0, 0 };
|
||||||
|
|
38
file.c
38
file.c
|
@ -8,14 +8,16 @@
|
||||||
typedef struct file {
|
typedef struct file {
|
||||||
char *path;
|
char *path;
|
||||||
char *stats;
|
char *stats;
|
||||||
|
char *type;
|
||||||
/* put some more useful stat here */
|
/* put some more useful stat here */
|
||||||
struct file *next;
|
struct file *next;
|
||||||
} file;
|
} file;
|
||||||
|
|
||||||
file *files = NULL;
|
file *files = NULL;
|
||||||
|
file *marked = NULL;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* get length of files linked list
|
* Get length of files linked list
|
||||||
*/
|
*/
|
||||||
long files_len()
|
long files_len()
|
||||||
{
|
{
|
||||||
|
@ -28,6 +30,21 @@ long files_len()
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get length of marked files
|
||||||
|
*/
|
||||||
|
long marked_len()
|
||||||
|
{
|
||||||
|
file *current = marked;
|
||||||
|
int count = 0;
|
||||||
|
while (current != NULL) {
|
||||||
|
count++;
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void clear_files()
|
void clear_files()
|
||||||
{
|
{
|
||||||
file *tmp;
|
file *tmp;
|
||||||
|
@ -38,17 +55,29 @@ void clear_files()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
long add_file(char *filepath, char *stats)
|
void clear_marked()
|
||||||
|
{
|
||||||
|
file *tmp;
|
||||||
|
while (marked != NULL) {
|
||||||
|
tmp = marked;
|
||||||
|
files = marked->next;
|
||||||
|
free(tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
long add_file(char *filepath, char *stats, char *type)
|
||||||
{
|
{
|
||||||
file *current = files;
|
file *current = files;
|
||||||
file *new_file = memalloc(sizeof(file));
|
file *new_file = memalloc(sizeof(file));
|
||||||
char *buf = strdup(filepath);
|
char *buf = strdup(filepath);
|
||||||
char *buf2 = strdup(stats);
|
char *buf2 = strdup(stats);
|
||||||
if (buf == NULL || buf2 == NULL) {
|
char *buf3 = strdup(type);
|
||||||
|
if (buf == NULL || buf2 == NULL || buf3 == NULL) {
|
||||||
perror("ccc");
|
perror("ccc");
|
||||||
}
|
}
|
||||||
new_file->path = buf;
|
new_file->path = buf;
|
||||||
new_file->stats = buf2;
|
new_file->stats = buf2;
|
||||||
|
new_file->type = buf3;
|
||||||
new_file->next = NULL;
|
new_file->next = NULL;
|
||||||
if (current == NULL) {
|
if (current == NULL) {
|
||||||
files = new_file;
|
files = new_file;
|
||||||
|
@ -92,6 +121,9 @@ char *get_filepath(long index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Construct a formatted line for display
|
||||||
|
*/
|
||||||
char *get_line(long index)
|
char *get_line(long index)
|
||||||
{
|
{
|
||||||
file *file = get_file(index);
|
file *file = get_file(index);
|
||||||
|
|
5
file.h
5
file.h
|
@ -4,13 +4,16 @@
|
||||||
typedef struct file {
|
typedef struct file {
|
||||||
char *path;
|
char *path;
|
||||||
char *stats;
|
char *stats;
|
||||||
|
char *type;
|
||||||
// put some more useful stat here
|
// put some more useful stat here
|
||||||
struct file *next;
|
struct file *next;
|
||||||
} file;
|
} file;
|
||||||
|
|
||||||
long files_len();
|
long files_len();
|
||||||
|
long marked_len();
|
||||||
void clear_files();
|
void clear_files();
|
||||||
long add_file(char *filename, char *time);
|
void clear_marked();
|
||||||
|
long add_file(char *filename, char *time, 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