add stats beside filename
This commit is contained in:
parent
2acda7768c
commit
b497735ee6
3 changed files with 72 additions and 10 deletions
52
ccc.c
52
ccc.c
|
@ -4,6 +4,8 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <linux/limits.h>
|
#include <linux/limits.h>
|
||||||
#include <dirent.h> /* directories etc. */
|
#include <dirent.h> /* directories etc. */
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <time.h>
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
|
|
||||||
#include "file.h"
|
#include "file.h"
|
||||||
|
@ -21,6 +23,7 @@ typedef struct {
|
||||||
|
|
||||||
/* functions' definitions */
|
/* functions' definitions */
|
||||||
void list_files(char *path);
|
void list_files(char *path);
|
||||||
|
char *get_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();
|
||||||
|
@ -166,10 +169,15 @@ void list_files(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, "..")) {
|
||||||
add_file(filename);
|
char *stats = get_file_stat(filename);
|
||||||
mvwprintw(windows[0].window, count + 1, 1, "%s", 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++;
|
count++;
|
||||||
}
|
}
|
||||||
|
free(filename);
|
||||||
}
|
}
|
||||||
closedir(dp);
|
closedir(dp);
|
||||||
wrefresh(windows[0].window);
|
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
|
* Highlight current line by reversing the color
|
||||||
*/
|
*/
|
||||||
void highlight_current_line()
|
void highlight_current_line()
|
||||||
{
|
{
|
||||||
char cwd[PATH_MAX];
|
char cwd[PATH_MAX];
|
||||||
char *filename;
|
|
||||||
|
|
||||||
for (long i = 0; i < files_len(); i++) {
|
for (long i = 0; i < files_len(); i++) {
|
||||||
if (i == current_selection) {
|
if (i == current_selection) {
|
||||||
|
@ -196,11 +231,13 @@ void highlight_current_line()
|
||||||
wprintw(windows[3].window, "(%ld/%ld) %s", i + 1, files_len(),
|
wprintw(windows[3].window, "(%ld/%ld) %s", i + 1, files_len(),
|
||||||
getcwd(cwd, sizeof(cwd)));
|
getcwd(cwd, sizeof(cwd)));
|
||||||
}
|
}
|
||||||
/* print the actual filename */
|
/* print the actual filename and stats */
|
||||||
filename = get_filename(i);
|
char *line = get_line(i);
|
||||||
mvwprintw(windows[0].window, i + 1, 1, "%s", filename);
|
mvwprintw(windows[0].window, i + 1, 1, "%s", line);
|
||||||
|
|
||||||
wattroff(windows[0].window, A_REVERSE);
|
wattroff(windows[0].window, A_REVERSE);
|
||||||
wattroff(windows[0].window, COLOR_PAIR(1));
|
wattroff(windows[0].window, COLOR_PAIR(1));
|
||||||
|
free(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
wrefresh(windows[0].window);
|
wrefresh(windows[0].window);
|
||||||
|
@ -228,6 +265,7 @@ void show_file_content()
|
||||||
fread(buffer, 1, length, file);
|
fread(buffer, 1, length, file);
|
||||||
mvwprintw(windows[2].window, 0, 0, "%s", buffer);
|
mvwprintw(windows[2].window, 0, 0, "%s", buffer);
|
||||||
wrefresh(windows[2].window);
|
wrefresh(windows[2].window);
|
||||||
|
free(buffer);
|
||||||
} else {
|
} else {
|
||||||
wclear(windows[2].window);
|
wclear(windows[2].window);
|
||||||
}
|
}
|
||||||
|
@ -255,7 +293,7 @@ void edit_file() {
|
||||||
system(command);
|
system(command);
|
||||||
reset_prog_mode(); /* return to previous tty mode */
|
reset_prog_mode(); /* return to previous tty mode */
|
||||||
refresh(); /* store the screen contents */
|
refresh(); /* store the screen contents */
|
||||||
//execle(editor, filename);
|
free(filename);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
26
file.c
26
file.c
|
@ -1,10 +1,12 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
/* files in a link list data structure */
|
/* files in a link list data structure */
|
||||||
typedef struct file {
|
typedef struct file {
|
||||||
char *name;
|
char *name;
|
||||||
|
char *stats;
|
||||||
/* put some more useful stat here */
|
/* put some more useful stat here */
|
||||||
struct file *next;
|
struct file *next;
|
||||||
} file;
|
} file;
|
||||||
|
@ -25,15 +27,17 @@ long files_len()
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
long add_file(char *filename)
|
long add_file(char *filename, char *stats)
|
||||||
{
|
{
|
||||||
file *current = files;
|
file *current = files;
|
||||||
file *new_file = memalloc(sizeof(file));
|
file *new_file = memalloc(sizeof(file));
|
||||||
char *buf = strdup(filename);
|
char *buf = strdup(filename);
|
||||||
if (!buf) {
|
char *buf2 = strdup(stats);
|
||||||
|
if (buf == NULL || buf2 == NULL) {
|
||||||
perror("ccc");
|
perror("ccc");
|
||||||
}
|
}
|
||||||
new_file->name = buf;
|
new_file->name = buf;
|
||||||
|
new_file->stats = buf2;
|
||||||
new_file->next = NULL;
|
new_file->next = NULL;
|
||||||
if (current == NULL) {
|
if (current == NULL) {
|
||||||
files = new_file;
|
files = new_file;
|
||||||
|
@ -76,3 +80,21 @@ char *get_filename(long index)
|
||||||
return NULL;
|
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
4
file.h
|
@ -3,13 +3,15 @@
|
||||||
|
|
||||||
typedef struct file {
|
typedef struct file {
|
||||||
char *name;
|
char *name;
|
||||||
|
char *stats;
|
||||||
// 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 add_file(char *filename);
|
long add_file(char *filename, char *time);
|
||||||
file *get_file(long index);
|
file *get_file(long index);
|
||||||
char *get_filename(long index);
|
char *get_filename(long index);
|
||||||
|
char *get_line(long index);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue