add stats beside filename

This commit is contained in:
Night Kaly 2024-03-11 21:54:14 +00:00
parent 2acda7768c
commit b497735ee6
No known key found for this signature in database
GPG key ID: 8E829D3381CFEBBE
3 changed files with 72 additions and 10 deletions

52
ccc.c
View file

@ -4,6 +4,8 @@
#include <string.h>
#include <linux/limits.h>
#include <dirent.h> /* directories etc. */
#include <sys/stat.h>
#include <time.h>
#include <ncurses.h>
#include "file.h"
@ -21,6 +23,7 @@ typedef struct {
/* functions' definitions */
void list_files(char *path);
char *get_file_stat(char *filename);
void highlight_current_line();
void show_file_content();
void edit_file();
@ -166,10 +169,15 @@ void list_files(char *path)
}
/* can't be strncmp as that would filter out the dotfiles */
if (strcmp(filename, ".") && strcmp(filename, "..")) {
add_file(filename);
mvwprintw(windows[0].window, count + 1, 1, "%s", filename);
char *stats = get_file_stat(filename);
long index = add_file(filename, stats);
char *line = get_line(index);
mvwprintw(windows[0].window, count + 1, 1, "%s", line);
free(stats);
free(line);
count++;
}
free(filename);
}
closedir(dp);
wrefresh(windows[0].window);
@ -178,13 +186,40 @@ void list_files(char *path)
}
}
/*
* Get file's last modified time and size
*/
char *get_file_stat(char *filename) {
struct stat file_stat;
stat(filename, &file_stat);
char *time = memalloc(20 * sizeof(char));
strftime(time, 20, "%Y-%m-%d %H:%M", localtime(&file_stat.st_mtime)); /* format last modified time to a string */
double bytes = file_stat.st_size;
char *size = memalloc(25 * sizeof(char)); /* max 25 chars due to long, space, suffix and nul */
int unit = 0;
const char* units[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB"};
while (bytes > 1024) {
bytes /= 1024;
unit++;
}
sprintf(size, "%.*f%s", unit, bytes, units[unit]);
char *total_stat = memalloc(45 * sizeof(char));
snprintf(total_stat, 45, "%-18s %-10s", time, size);
free(time);
free(size);
return total_stat;
}
/*
* Highlight current line by reversing the color
*/
void highlight_current_line()
{
char cwd[PATH_MAX];
char *filename;
for (long i = 0; i < files_len(); i++) {
if (i == current_selection) {
@ -196,11 +231,13 @@ void highlight_current_line()
wprintw(windows[3].window, "(%ld/%ld) %s", i + 1, files_len(),
getcwd(cwd, sizeof(cwd)));
}
/* print the actual filename */
filename = get_filename(i);
mvwprintw(windows[0].window, i + 1, 1, "%s", filename);
/* print the actual filename and stats */
char *line = get_line(i);
mvwprintw(windows[0].window, i + 1, 1, "%s", line);
wattroff(windows[0].window, A_REVERSE);
wattroff(windows[0].window, COLOR_PAIR(1));
free(line);
}
wrefresh(windows[0].window);
@ -228,6 +265,7 @@ void show_file_content()
fread(buffer, 1, length, file);
mvwprintw(windows[2].window, 0, 0, "%s", buffer);
wrefresh(windows[2].window);
free(buffer);
} else {
wclear(windows[2].window);
}
@ -255,7 +293,7 @@ void edit_file() {
system(command);
reset_prog_mode(); /* return to previous tty mode */
refresh(); /* store the screen contents */
//execle(editor, filename);
free(filename);
}
}

26
file.c
View file

@ -1,10 +1,12 @@
#include <string.h>
#include <stdlib.h>
#include "util.h"
/* files in a link list data structure */
typedef struct file {
char *name;
char *stats;
/* put some more useful stat here */
struct file *next;
} file;
@ -25,15 +27,17 @@ long files_len()
return count;
}
long add_file(char *filename)
long add_file(char *filename, char *stats)
{
file *current = files;
file *new_file = memalloc(sizeof(file));
char *buf = strdup(filename);
if (!buf) {
char *buf2 = strdup(stats);
if (buf == NULL || buf2 == NULL) {
perror("ccc");
}
new_file->name = buf;
new_file->stats = buf2;
new_file->next = NULL;
if (current == NULL) {
files = new_file;
@ -76,3 +80,21 @@ char *get_filename(long index)
return NULL;
}
}
char *get_line(long index)
{
file *file = get_file(index);
if (file != NULL) {
char *name = strdup(file->name);
char *stats = strdup(file->stats);
if (name == NULL || stats == NULL) {
perror("ccc");
}
size_t length = strlen(name) + strlen(stats) + 2; /* one for space and one for nul */
char *line = memalloc(length * sizeof(char));
snprintf(line, length, "%s %s", stats, name);
return line;
} else {
return NULL;
}
}

4
file.h
View file

@ -3,13 +3,15 @@
typedef struct file {
char *name;
char *stats;
// put some more useful stat here
struct file *next;
} file;
long files_len();
long add_file(char *filename);
long add_file(char *filename, char *time);
file *get_file(long index);
char *get_filename(long index);
char *get_line(long index);
#endif