Merge pull request #6 from piotr-marendowski/develop

Develop
This commit is contained in:
Night Kaly 2024-03-09 19:46:46 +00:00 committed by GitHub
commit 45b24bfd5f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 129 additions and 22 deletions

View file

@ -3,7 +3,7 @@
TARGET = ccc
MANPAGE = ccc.1
SRC = ccc.c util.c
SRC = ccc.c util.c file.c
# Flags
LDFLAGS = $(shell pkg-config --libs ncurses)

45
ccc.c
View file

@ -6,6 +6,7 @@
#include <dirent.h> /* directories etc. */
#include <ncurses.h>
#include "file.h"
#include "util.h"
#define ESC 0x1B /* \e or \033 */
@ -17,18 +18,17 @@ typedef struct {
int x;
} WIN_STRUCT;
/* functions' definitions */
void list_cwd_files();
void highlight_current_line();
void show_file_content();
long files_len();
void init_windows();
void draw_border_title(WINDOW *window, bool active);
/* global variables */
unsigned int focus = 0;
int current_selection = 0;
char **files;
int half_width;
WIN_STRUCT windows[2];
@ -114,6 +114,9 @@ int main(int argc, char** argv)
return 0;
}
/*
* read current directory and list all files to window 0
*/
void list_cwd_files()
{
char cwd[PATH_MAX];
@ -130,21 +133,20 @@ void list_cwd_files()
char *filename = strdup(ep->d_name);
if (!filename)
{
// memory allocation failed
perror("ccc");
fprintf(stderr, "ccc: Cannot read filename %s.", filename);
fprintf(stderr, "ccc: Cannot read filename %s.", ep->d_name);
exit(EXIT_FAILURE);
}
// 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 *));
files[count] = filename;
add_file(filename);
mvwprintw(windows[0].window, count + 1, 1, "%s", filename);
count++;
}
}
closedir(dp);
files[count] = NULL;
wrefresh(windows[0].window);
}
else {
@ -152,28 +154,34 @@ void list_cwd_files()
}
}
/*
* highlight current line by reversing the color
*/
void highlight_current_line()
{
for (int i = 0; i < files_len(); i++)
for (long i = 0; i < files_len(); i++)
{
if (i == current_selection)
{
wattron(windows[0].window, A_REVERSE);
wattron(windows[0].window, COLOR_PAIR(1));
}
mvwprintw(windows[0].window, i + 1, 1, "%s", files[i]);
char *name = get_filename(i);
mvwprintw(windows[0].window, i + 1, 1, "%s", name); // print actual file name
wattroff(windows[0].window, A_REVERSE);
wattroff(windows[0].window, COLOR_PAIR(1));
}
wrefresh(windows[0].window);
show_file_content();
wrefresh(windows[0].window); // refresh to see the changes
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()
{
FILE *file = fopen(files[current_selection], "rb");
FILE *file = fopen(get_filename((long) current_selection), "rb");
if (file)
{
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()
{
/*
@ -225,6 +225,7 @@ void init_windows()
*/
void draw_border_title(WINDOW *window, bool active)
{
// turn on color depends on active
if (active) {
wattron(window, COLOR_PAIR(2));
} else {
@ -243,6 +244,8 @@ void draw_border_title(WINDOW *window, bool active)
mvwaddch(window, LINES - 1, 0, ACS_LLCORNER); // lower left corner
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
// turn color off after turning it on
if (active) {
wattroff(window, COLOR_PAIR(2));
} else {

89
file.c Normal file
View 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
View 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