Merge pull request #11 from piotr-marendowski/feature
add stats and add key binds and fix preview
This commit is contained in:
commit
6b2eb35601
3 changed files with 129 additions and 39 deletions
130
ccc.c
130
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"
|
||||||
|
@ -22,8 +24,10 @@ 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 init_windows();
|
void init_windows();
|
||||||
void draw_border_title(WINDOW *window, bool active);
|
void draw_border_title(WINDOW *window, bool active);
|
||||||
|
|
||||||
|
@ -31,7 +35,7 @@ void draw_border_title(WINDOW *window, bool active);
|
||||||
unsigned int focus = 0;
|
unsigned int focus = 0;
|
||||||
long current_selection = 0;
|
long current_selection = 0;
|
||||||
int half_width;
|
int half_width;
|
||||||
WIN_STRUCT windows[3];
|
WIN_STRUCT windows[4];
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
|
@ -142,6 +146,9 @@ int main(int argc, char** argv)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case 'e':
|
||||||
|
edit_file();
|
||||||
|
break;
|
||||||
case 27: /* esc */
|
case 27: /* esc */
|
||||||
break;
|
break;
|
||||||
case KEY_RESIZE:
|
case KEY_RESIZE:
|
||||||
|
@ -181,10 +188,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);
|
||||||
|
@ -193,13 +205,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) {
|
||||||
|
@ -207,19 +246,21 @@ void highlight_current_line()
|
||||||
wattron(windows[0].window, COLOR_PAIR(1));
|
wattron(windows[0].window, COLOR_PAIR(1));
|
||||||
|
|
||||||
/* update the panel */
|
/* update the panel */
|
||||||
wclear(windows[2].window);
|
wclear(windows[3].window);
|
||||||
wprintw(windows[2].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);
|
||||||
wrefresh(windows[2].window);
|
wrefresh(windows[3].window);
|
||||||
/* show file content every time cursor changes */
|
/* show file content every time cursor changes */
|
||||||
show_file_content();
|
show_file_content();
|
||||||
}
|
}
|
||||||
|
@ -231,51 +272,76 @@ void show_file_content()
|
||||||
{
|
{
|
||||||
FILE *file = fopen(get_filename((long) current_selection), "rb");
|
FILE *file = fopen(get_filename((long) current_selection), "rb");
|
||||||
if (file) {
|
if (file) {
|
||||||
wclear(windows[1].window);
|
wclear(windows[2].window);
|
||||||
draw_border_title(windows[1].window, true);
|
draw_border_title(windows[1].window, true);
|
||||||
|
|
||||||
fseek(file, 0, SEEK_END);
|
fseek(file, 0, SEEK_END);
|
||||||
/* check if file isn't empty */
|
|
||||||
long length = ftell(file);
|
long length = ftell(file);
|
||||||
if (length != 0) {
|
/* check if file isn't empty */
|
||||||
fseek(file, 0, SEEK_SET);
|
if (length != 0 && length < 4096) {
|
||||||
|
fseek(file, 0, SEEK_SET); /* set cursor back to start of file */
|
||||||
char *buffer = memalloc(length * sizeof(char));
|
char *buffer = memalloc(length * sizeof(char));
|
||||||
fread(buffer, 1, length, file);
|
fread(buffer, 1, length, file);
|
||||||
|
mvwprintw(windows[2].window, 0, 0, "%s", buffer);
|
||||||
mvwprintw(windows[1].window, 1, 1, "%s", buffer);
|
wrefresh(windows[2].window);
|
||||||
wrefresh(windows[1].window);
|
free(buffer);
|
||||||
} else {
|
} else {
|
||||||
wclear(windows[1].window);
|
wclear(windows[2].window);
|
||||||
}
|
}
|
||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Opens $EDITOR to edit the file
|
||||||
|
*/
|
||||||
|
void edit_file() {
|
||||||
|
char *editor = getenv("EDITOR");
|
||||||
|
if (editor == NULL) {
|
||||||
|
wclear(windows[3].window);
|
||||||
|
wprintw(windows[3].window, "Cannot get EDITOR variable, is it defined?");
|
||||||
|
wrefresh(windows[3].window);
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
def_prog_mode(); /* save the tty modes */
|
||||||
|
endwin(); /* end curses mode temporarily */
|
||||||
|
char *filename = get_filename(current_selection);
|
||||||
|
int length = strlen(editor) + strlen(filename) + 2; /* one for space one for nul */
|
||||||
|
char command[length];
|
||||||
|
snprintf(command, length, "%s %s", editor, filename);
|
||||||
|
system(command);
|
||||||
|
reset_prog_mode(); /* return to previous tty mode */
|
||||||
|
refresh(); /* store the screen contents */
|
||||||
|
free(filename);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void init_windows()
|
void init_windows()
|
||||||
{
|
{
|
||||||
/*-----------------------------+
|
/*------------------------------+
|
||||||
| | |
|
| ||-------------||
|
||||||
| | |
|
| || ||
|
||||||
| directory (0) | preview (1) |
|
| directory (0) || preview (1)||
|
||||||
| | |
|
| || ||
|
||||||
| | |
|
| || ||
|
||||||
| | |
|
| ||-------------||
|
||||||
+==========panel (2)==========*/
|
+==========panel (2)===========*/
|
||||||
|
|
||||||
/* lines, cols, y, x */
|
/* lines, cols, y, x */
|
||||||
WINDOW *directory = newwin(LINES, half_width, 0, 0 );
|
WINDOW *directory = newwin(LINES - PH, half_width, 0, 0 );
|
||||||
WINDOW *preview = newwin(LINES, half_width, 0, half_width);
|
WINDOW *preview_border = newwin(LINES - PH, half_width, 0, half_width );
|
||||||
|
WINDOW *preview_content = newwin(LINES - PH - 2, half_width - 2, 1, half_width + 1);
|
||||||
WINDOW *panel = newwin(PH, COLS, LINES - PH, 0 );
|
WINDOW *panel = newwin(PH, COLS, LINES - PH, 0 );
|
||||||
|
|
||||||
/* draw border around windows */
|
/* draw border around windows */
|
||||||
draw_border_title(directory, true);
|
draw_border_title(directory, true);
|
||||||
draw_border_title(preview, false);
|
draw_border_title(preview_content, false);
|
||||||
|
|
||||||
/* window location y, x */
|
/* window location y, x */
|
||||||
windows[0] = (WIN_STRUCT) { directory, 0, 0, 0 };
|
windows[0] = (WIN_STRUCT) { directory, 0, 0, 0 };
|
||||||
windows[1] = (WIN_STRUCT) { preview, 1, 0, half_width };
|
windows[1] = (WIN_STRUCT) { preview_border, 1, 0, half_width };
|
||||||
windows[2] = (WIN_STRUCT) { panel, 2, LINES - PH, 0 };
|
windows[2] = (WIN_STRUCT) { preview_content, 1, 0, half_width };
|
||||||
|
windows[3] = (WIN_STRUCT) { panel, 2, LINES - PH, 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
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