Merge pull request #13 from piotr-marendowski/feature

Feature
This commit is contained in:
Piotr Marendowski 2024-03-13 18:43:34 +00:00 committed by GitHub
commit 0c9ebad916
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 160 additions and 31 deletions

146
ccc.c
View file

@ -24,7 +24,8 @@ typedef struct {
/* functions' definitions */
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 show_file_content();
void edit_file();
@ -64,11 +65,12 @@ int main(int argc, char** argv)
endwin();
die("ccc: Color is not supported in your terminal.\n");
} else {
use_default_colors();
start_color();
}
init_pair(1, COLOR_BLUE, COLOR_BLACK); /* foreground, background */
init_pair(2, COLOR_CYAN, COLOR_BLACK); /* active color */
init_pair(1, COLOR_BLUE, -1); /* foreground, background */
init_pair(2, COLOR_CYAN, -1); /* active color */
half_width = COLS / 2;
init_windows();
@ -119,17 +121,14 @@ int main(int argc, char** argv)
/* enter directory/open a file */
case 'l':;
char *filename = get_filepath(current_selection);
if (filename != NULL) {
struct stat file_stat;
stat(filename, &file_stat);
file *file = get_file(current_selection);
if (file != NULL) {
/* 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 */
strcpy(cwd, filename);
strcpy(cwd, file->path);
clear_files();
list_files(filename);
list_files(cwd);
current_selection = 0;
highlight_current_line();
}
@ -158,6 +157,7 @@ int main(int argc, char** argv)
highlight_current_line();
break;
/* jump down */
case '\x04':
if ((current_selection + JUMP_NUM) < (files_len() - 1))
@ -167,6 +167,7 @@ int main(int argc, char** argv)
highlight_current_line();
break;
/* go down */
case 'j':
if (current_selection < (files_len() - 1))
@ -174,11 +175,13 @@ int main(int argc, char** argv)
highlight_current_line();
break;
/* jump to the bottom */
case 'G':
current_selection = (files_len() - 1);
highlight_current_line();
break;
/* jump to the top */
case 'g':
second = getch();
@ -200,12 +203,15 @@ int main(int argc, char** argv)
for (int i = 0; i < 2; i++) {
delwin(windows[i].window);
}
endwin();
init_windows();
break;
default:
break;
}
}
clear_files();
clear_marked();
endwin();
return 0;
}
@ -220,8 +226,7 @@ void list_files(const char *path)
struct dirent *ep;
draw_border_title(directory_border, true);
dp = opendir(path);
if (dp != NULL) {
if ((dp = opendir(path)) != NULL) {
int count = 0;
/* clear directory window to ready for printing */
wclear(directory_content);
@ -234,19 +239,16 @@ void list_files(const char *path)
/* can't be strncmp as that would filter out the dotfiles */
if (strcmp(filename, ".") && strcmp(filename, "..")) {
/* construct full file path */
filename[0] = '\0';
strcat(filename, cwd);
strcat(filename, "/");
strcat(filename, ep->d_name); /* add file name */
char *stats = get_file_stat(filename);
long index = add_file(filename, stats);
long index = add_file_stat(filename);
char *line = get_line(index);
mvwprintw(directory_content, count, 0, "%s", line);
free(stats);
free(line);
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;
stat(filename, &file_stat);
stat(filepath, &file_stat);
/* get last modified time */
char *time = memalloc(20 * sizeof(char));
/* format last modified time to a string */
strftime(time, 20, "%Y-%m-%d %H:%M", localtime(&file_stat.st_mtime));
/* get file 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;
const char* units[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB"};
while (bytes > 1024) {
@ -281,13 +329,37 @@ char *get_file_stat(char *filename)
}
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));
snprintf(total_stat, 45, "%-18s %-10s", time, size);
total_stat[strlen(total_stat)] = '\0';
free(time);
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()
{
for (long i = 0; i < files_len(); i++) {
if (i == current_selection) {
long overflow = 0;
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, COLOR_PAIR(1));
/* update the 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 */
char *line = get_line(i);
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, COLOR_PAIR(1));
free(line);
line_count++;
}
wrefresh(directory_content);
@ -393,6 +485,8 @@ void init_windows()
draw_border_title(directory_border, true);
draw_border_title(preview_border, false);
scrollok(directory_content, true);
scrollok(preview_content, true);
/* window location y, x */
windows[0] = (WIN_STRUCT) { directory_border, 0, 0, 0 };
windows[1] = (WIN_STRUCT) { directory_border, 0, 0, 0 };

38
file.c
View file

@ -8,14 +8,16 @@
typedef struct file {
char *path;
char *stats;
char *type;
/* put some more useful stat here */
struct file *next;
} file;
file *files = NULL;
file *marked = NULL;
/*
* get length of files linked list
* Get length of files linked list
*/
long files_len()
{
@ -28,6 +30,21 @@ long files_len()
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()
{
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 *new_file = memalloc(sizeof(file));
char *buf = strdup(filepath);
char *buf2 = strdup(stats);
if (buf == NULL || buf2 == NULL) {
char *buf3 = strdup(type);
if (buf == NULL || buf2 == NULL || buf3 == NULL) {
perror("ccc");
}
new_file->path = buf;
new_file->stats = buf2;
new_file->type = buf3;
new_file->next = NULL;
if (current == NULL) {
files = new_file;
@ -92,6 +121,9 @@ char *get_filepath(long index)
}
}
/*
* Construct a formatted line for display
*/
char *get_line(long index)
{
file *file = get_file(index);

5
file.h
View file

@ -4,13 +4,16 @@
typedef struct file {
char *path;
char *stats;
char *type;
// put some more useful stat here
struct file *next;
} file;
long files_len();
long marked_len();
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);
char *get_filepath(long index);
char *get_line(long index);