commit
45b24bfd5f
4 changed files with 129 additions and 22 deletions
2
Makefile
2
Makefile
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
TARGET = ccc
|
TARGET = ccc
|
||||||
MANPAGE = ccc.1
|
MANPAGE = ccc.1
|
||||||
SRC = ccc.c util.c
|
SRC = ccc.c util.c file.c
|
||||||
|
|
||||||
# Flags
|
# Flags
|
||||||
LDFLAGS = $(shell pkg-config --libs ncurses)
|
LDFLAGS = $(shell pkg-config --libs ncurses)
|
||||||
|
|
45
ccc.c
45
ccc.c
|
@ -6,6 +6,7 @@
|
||||||
#include <dirent.h> /* directories etc. */
|
#include <dirent.h> /* directories etc. */
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
|
|
||||||
|
#include "file.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
#define ESC 0x1B /* \e or \033 */
|
#define ESC 0x1B /* \e or \033 */
|
||||||
|
@ -17,18 +18,17 @@ typedef struct {
|
||||||
int x;
|
int x;
|
||||||
} WIN_STRUCT;
|
} WIN_STRUCT;
|
||||||
|
|
||||||
|
|
||||||
/* functions' definitions */
|
/* functions' definitions */
|
||||||
void list_cwd_files();
|
void list_cwd_files();
|
||||||
void highlight_current_line();
|
void highlight_current_line();
|
||||||
void show_file_content();
|
void show_file_content();
|
||||||
long files_len();
|
|
||||||
void init_windows();
|
void init_windows();
|
||||||
void draw_border_title(WINDOW *window, bool active);
|
void draw_border_title(WINDOW *window, bool active);
|
||||||
|
|
||||||
/* global variables */
|
/* global variables */
|
||||||
unsigned int focus = 0;
|
unsigned int focus = 0;
|
||||||
int current_selection = 0;
|
int current_selection = 0;
|
||||||
char **files;
|
|
||||||
int half_width;
|
int half_width;
|
||||||
WIN_STRUCT windows[2];
|
WIN_STRUCT windows[2];
|
||||||
|
|
||||||
|
@ -114,6 +114,9 @@ int main(int argc, char** argv)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* read current directory and list all files to window 0
|
||||||
|
*/
|
||||||
void list_cwd_files()
|
void list_cwd_files()
|
||||||
{
|
{
|
||||||
char cwd[PATH_MAX];
|
char cwd[PATH_MAX];
|
||||||
|
@ -130,21 +133,20 @@ void list_cwd_files()
|
||||||
char *filename = strdup(ep->d_name);
|
char *filename = strdup(ep->d_name);
|
||||||
if (!filename)
|
if (!filename)
|
||||||
{
|
{
|
||||||
|
// memory allocation failed
|
||||||
perror("ccc");
|
perror("ccc");
|
||||||
fprintf(stderr, "ccc: Cannot read filename %s.", filename);
|
fprintf(stderr, "ccc: Cannot read filename %s.", ep->d_name);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
// can't be strncmp as that filter out dotfiles
|
// can't be strncmp as that filter out dotfiles
|
||||||
if (strcmp(filename, ".") && strcmp(ep->d_name, ".."))
|
if (strcmp(filename, ".") && strcmp(filename, ".."))
|
||||||
{
|
{
|
||||||
files = rememalloc(files, (count + 1) * sizeof(char *));
|
add_file(filename);
|
||||||
files[count] = filename;
|
|
||||||
mvwprintw(windows[0].window, count + 1, 1, "%s", filename);
|
mvwprintw(windows[0].window, count + 1, 1, "%s", filename);
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
closedir(dp);
|
closedir(dp);
|
||||||
files[count] = NULL;
|
|
||||||
wrefresh(windows[0].window);
|
wrefresh(windows[0].window);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -152,28 +154,34 @@ void list_cwd_files()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* highlight current line by reversing the color
|
||||||
|
*/
|
||||||
void highlight_current_line()
|
void highlight_current_line()
|
||||||
{
|
{
|
||||||
for (int i = 0; i < files_len(); i++)
|
for (long i = 0; i < files_len(); i++)
|
||||||
{
|
{
|
||||||
if (i == current_selection)
|
if (i == current_selection)
|
||||||
{
|
{
|
||||||
wattron(windows[0].window, A_REVERSE);
|
wattron(windows[0].window, A_REVERSE);
|
||||||
wattron(windows[0].window, COLOR_PAIR(1));
|
wattron(windows[0].window, COLOR_PAIR(1));
|
||||||
}
|
}
|
||||||
|
char *name = get_filename(i);
|
||||||
mvwprintw(windows[0].window, i + 1, 1, "%s", files[i]);
|
mvwprintw(windows[0].window, i + 1, 1, "%s", name); // print actual file name
|
||||||
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
wrefresh(windows[0].window);
|
wrefresh(windows[0].window); // refresh to see the changes
|
||||||
show_file_content();
|
show_file_content(); // show file content every time cursor changes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* get file content into buffer and show it to preview window
|
||||||
|
*/
|
||||||
void show_file_content()
|
void show_file_content()
|
||||||
{
|
{
|
||||||
FILE *file = fopen(files[current_selection], "rb");
|
FILE *file = fopen(get_filename((long) current_selection), "rb");
|
||||||
if (file)
|
if (file)
|
||||||
{
|
{
|
||||||
fseek(file, 0, SEEK_END);
|
fseek(file, 0, SEEK_END);
|
||||||
|
@ -187,14 +195,6 @@ void show_file_content()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
long files_len() {
|
|
||||||
long i = 0;
|
|
||||||
while (files[i] != NULL) {
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
void init_windows()
|
void init_windows()
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
|
@ -225,6 +225,7 @@ void init_windows()
|
||||||
*/
|
*/
|
||||||
void draw_border_title(WINDOW *window, bool active)
|
void draw_border_title(WINDOW *window, bool active)
|
||||||
{
|
{
|
||||||
|
// turn on color depends on active
|
||||||
if (active) {
|
if (active) {
|
||||||
wattron(window, COLOR_PAIR(2));
|
wattron(window, COLOR_PAIR(2));
|
||||||
} else {
|
} else {
|
||||||
|
@ -243,6 +244,8 @@ void draw_border_title(WINDOW *window, bool active)
|
||||||
mvwaddch(window, LINES - 1, 0, ACS_LLCORNER); // lower left corner
|
mvwaddch(window, LINES - 1, 0, ACS_LLCORNER); // lower left corner
|
||||||
mvwhline(window, LINES - 1, 1, ACS_HLINE, half_width - 2); // bottom horizontal line
|
mvwhline(window, LINES - 1, 1, ACS_HLINE, half_width - 2); // bottom horizontal line
|
||||||
mvwaddch(window, LINES - 1, half_width - 1, ACS_LRCORNER); // lower right corner
|
mvwaddch(window, LINES - 1, half_width - 1, ACS_LRCORNER); // lower right corner
|
||||||
|
|
||||||
|
// turn color off after turning it on
|
||||||
if (active) {
|
if (active) {
|
||||||
wattroff(window, COLOR_PAIR(2));
|
wattroff(window, COLOR_PAIR(2));
|
||||||
} else {
|
} else {
|
||||||
|
|
89
file.c
Normal file
89
file.c
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
// files in a link list data structure
|
||||||
|
typedef struct file {
|
||||||
|
char *name;
|
||||||
|
// put some more useful stat here
|
||||||
|
struct file *next;
|
||||||
|
} file;
|
||||||
|
|
||||||
|
file *files = NULL;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* get length of files linked list
|
||||||
|
*/
|
||||||
|
long files_len()
|
||||||
|
{
|
||||||
|
file *current = files;
|
||||||
|
int count = 0;
|
||||||
|
while (current != NULL)
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
long add_file(char *filename)
|
||||||
|
{
|
||||||
|
file *current = files;
|
||||||
|
file *new_file = memalloc(sizeof(file));
|
||||||
|
char *buf = strdup(filename);
|
||||||
|
if (!buf)
|
||||||
|
{
|
||||||
|
perror("ccc");
|
||||||
|
}
|
||||||
|
new_file->name = buf;
|
||||||
|
new_file->next = NULL;
|
||||||
|
if (current == NULL)
|
||||||
|
{
|
||||||
|
files = new_file;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
long index = 1;
|
||||||
|
while (current->next != NULL)
|
||||||
|
{
|
||||||
|
current = current->next;
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
current->next = new_file;
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
file *get_file(long index)
|
||||||
|
{
|
||||||
|
file *current = files;
|
||||||
|
if (index == 0)
|
||||||
|
{
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
if (index > files_len())
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
for (long i = 0; i < index; i++)
|
||||||
|
{
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_filename(long index)
|
||||||
|
{
|
||||||
|
file *file = get_file(index);
|
||||||
|
if (file != NULL)
|
||||||
|
{
|
||||||
|
char *name = strdup(file->name);
|
||||||
|
if (!name)
|
||||||
|
{
|
||||||
|
perror("ccc");
|
||||||
|
}
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
15
file.h
Normal file
15
file.h
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
#ifndef FILE_H_
|
||||||
|
#define FILE_H_
|
||||||
|
|
||||||
|
typedef struct file {
|
||||||
|
char *name;
|
||||||
|
// put some more useful stat here
|
||||||
|
struct file *next;
|
||||||
|
} file;
|
||||||
|
|
||||||
|
long files_len();
|
||||||
|
long add_file(char *filename);
|
||||||
|
file *get_file(long index);
|
||||||
|
char *get_filename(long index);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in a new issue