Merge pull request #9 from piotr-marendowski/develop

suckless code style
This commit is contained in:
Night Kaly 2024-03-10 10:55:31 +00:00 committed by GitHub
commit 18d459a595
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 55 additions and 79 deletions

97
ccc.c
View file

@ -36,7 +36,7 @@ int main(int argc, char** argv)
{ {
char cwd[PATH_MAX]; char cwd[PATH_MAX];
// check if it is interactive shell /* check if it is interactive shell */
if (!isatty(STDIN_FILENO)) if (!isatty(STDIN_FILENO))
die("ccc: No tty detected. ccc requires an interactive shell to run.\n"); die("ccc: No tty detected. ccc requires an interactive shell to run.\n");
@ -47,18 +47,16 @@ int main(int argc, char** argv)
noecho(); noecho();
curs_set(0); curs_set(0);
// check terminal has colors /* check terminal has colors */
if (!has_colors()) if (!has_colors()) {
{
endwin(); endwin();
die("ccc: Color is not supported in your terminal.\n"); die("ccc: Color is not supported in your terminal.\n");
} } else {
else {
start_color(); start_color();
} }
init_pair(1, COLOR_BLUE, COLOR_BLACK); // foreground, background init_pair(1, COLOR_BLUE, COLOR_BLACK); /* foreground, background */
init_pair(2, COLOR_CYAN, COLOR_BLACK); // active color init_pair(2, COLOR_CYAN, COLOR_BLACK); /* active color */
refresh(); refresh();
half_width = COLS / 2; half_width = COLS / 2;
@ -67,18 +65,15 @@ int main(int argc, char** argv)
highlight_current_line(); highlight_current_line();
int ch; int ch;
while (1) while (1) {
{ if (COLS < 80 || LINES < 24) {
if (COLS < 80 || LINES < 24)
{
endwin(); endwin();
die("ccc: Terminal size needs to be at least 80x24\n"); die("ccc: Terminal size needs to be at least 80x24\n");
} }
ch = getch(); ch = getch();
if (ch == 'q') if (ch == 'q')
break; break;
switch (ch) switch (ch) {
{
case '.': case '.':
list_cwd_files(); list_cwd_files();
break; break;
@ -103,18 +98,17 @@ int main(int argc, char** argv)
current_selection = files_len() - 1; current_selection = files_len() - 1;
highlight_current_line(); highlight_current_line();
break; break;
case 27: // esc case 27: /* esc */
break; break;
case KEY_RESIZE: case KEY_RESIZE:
for (int i = 0; i < 2; i++) for (int i = 0; i < 2; i++) {
{
delwin(windows[i].window); delwin(windows[i].window);
} }
init_windows(); init_windows();
break; break;
} }
} }
endwin(); // End curses endwin(); /* End curses */
return 0; return 0;
} }
@ -129,22 +123,18 @@ void list_cwd_files()
struct dirent *ep; struct dirent *ep;
dp = opendir(cwd); dp = opendir(cwd);
if (dp != NULL) if (dp != NULL) {
{
int count = 0; int count = 0;
while ((ep = readdir(dp)) != NULL) while ((ep = readdir(dp)) != NULL) {
{
char *filename = strdup(ep->d_name); char *filename = strdup(ep->d_name);
if (!filename) if (filename == NULL) {
{ /* memory allocation failed */
// memory allocation failed
perror("ccc"); perror("ccc");
fprintf(stderr, "ccc: Cannot read filename %s.", ep->d_name); 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(filename, "..")) if (strcmp(filename, ".") && strcmp(filename, "..")) {
{
add_file(filename); add_file(filename);
mvwprintw(windows[0].window, count + 1, 1, "%s", filename); mvwprintw(windows[0].window, count + 1, 1, "%s", filename);
count++; count++;
@ -152,8 +142,7 @@ void list_cwd_files()
} }
closedir(dp); closedir(dp);
wrefresh(windows[0].window); wrefresh(windows[0].window);
} } else {
else {
perror("ccc"); perror("ccc");
} }
} }
@ -163,21 +152,19 @@ void list_cwd_files()
*/ */
void highlight_current_line() void highlight_current_line()
{ {
for (long 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); char *name = get_filename(i);
mvwprintw(windows[0].window, i + 1, 1, "%s", name); // print actual file name 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); // refresh to see the changes wrefresh(windows[0].window); /* refresh to see the changes */
show_file_content(); // show file content every time cursor changes show_file_content(); /* show file content every time cursor changes */
} }
/* /*
@ -186,13 +173,13 @@ void highlight_current_line()
void show_file_content() 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[1].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);
long length = ftell(file); long length = ftell(file);
fseek(file, 0, SEEK_SET); fseek(file, 0, SEEK_SET);
char a[100];
char *buffer = memalloc(length * sizeof(char)); char *buffer = memalloc(length * sizeof(char));
fread(buffer, 1, length, file); fread(buffer, 1, length, file);
fclose(file); fclose(file);
@ -213,11 +200,11 @@ void init_windows()
* |-----------------------------/ * |-----------------------------/
*/ */
// create windows /* create windows */
WINDOW *directory = newwin(LINES, half_width, 0, 0); WINDOW *directory = newwin(LINES, half_width, 0, 0);
WINDOW *preview = newwin(LINES, half_width, 0, half_width); WINDOW *preview = newwin(LINES, half_width, 0, half_width);
// 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, false);
@ -231,31 +218,31 @@ 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 /* turn on color depends on active */
if (active) { if (active) {
wattron(window, COLOR_PAIR(2)); wattron(window, COLOR_PAIR(2));
} else { } else {
wattron(window, COLOR_PAIR(1)); wattron(window, COLOR_PAIR(1));
} }
// draw top border /* draw top border */
mvwaddch(window, 0, 0, ACS_ULCORNER); // upper left corner mvwaddch(window, 0, 0, ACS_ULCORNER); /* upper left corner */
mvwhline(window, 0, 1, ACS_HLINE, half_width - 2); // top horizontal line mvwhline(window, 0, 1, ACS_HLINE, half_width - 2); /* top horizontal line */
mvwaddch(window, 0, half_width - 1, ACS_URCORNER); // upper right corner mvwaddch(window, 0, half_width - 1, ACS_URCORNER); /* upper right corner */
// draw side border /* draw side border */
mvwvline(window, 1, 0, ACS_VLINE, LINES - 2); // left vertical line mvwvline(window, 1, 0, ACS_VLINE, LINES - 2); /* left vertical line */
mvwvline(window, 1, half_width - 1, ACS_VLINE, LINES - 2); // right vertical line mvwvline(window, 1, half_width - 1, ACS_VLINE, LINES - 2); /* right vertical line */
// draw bottom border /* draw bottom border */
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 /* turn color off after turning it on */
if (active) { if (active) {
wattroff(window, COLOR_PAIR(2)); wattroff(window, COLOR_PAIR(2));
} else { } else {
wattroff(window, COLOR_PAIR(1)); wattroff(window, COLOR_PAIR(1));
} }
wrefresh(window); // Refresh the window to see the colored border and title wrefresh(window); /* Refresh the window to see the colored border and title */
} }

35
file.c
View file

@ -2,10 +2,10 @@
#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;
// put some more useful stat here /* put some more useful stat here */
struct file *next; struct file *next;
} file; } file;
@ -18,8 +18,7 @@ long files_len()
{ {
file *current = files; file *current = files;
int count = 0; int count = 0;
while (current != NULL) while (current != NULL) {
{
count++; count++;
current = current->next; current = current->next;
} }
@ -31,20 +30,17 @@ long add_file(char *filename)
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) if (!buf) {
{
perror("ccc"); perror("ccc");
} }
new_file->name = buf; new_file->name = buf;
new_file->next = NULL; new_file->next = NULL;
if (current == NULL) if (current == NULL) {
{
files = new_file; files = new_file;
return 0; return 0;
} }
long index = 1; long index = 1;
while (current->next != NULL) while (current->next != NULL) {
{
current = current->next; current = current->next;
index++; index++;
} }
@ -55,16 +51,13 @@ long add_file(char *filename)
file *get_file(long index) file *get_file(long index)
{ {
file *current = files; file *current = files;
if (index == 0) if (index == 0) {
{
return current; return current;
} }
if (index > files_len()) if (index > files_len()) {
{
return NULL; return NULL;
} }
for (long i = 0; i < index; i++) for (long i = 0; i < index; i++) {
{
current = current->next; current = current->next;
} }
return current; return current;
@ -73,17 +66,13 @@ file *get_file(long index)
char *get_filename(long index) char *get_filename(long index)
{ {
file *file = get_file(index); file *file = get_file(index);
if (file != NULL) if (file != NULL) {
{
char *name = strdup(file->name); char *name = strdup(file->name);
if (!name) if (!name) {
{
perror("ccc"); perror("ccc");
} }
return name; return name;
} } else {
else
{
return NULL; return NULL;
} }
} }

2
util.c
View file

@ -3,7 +3,7 @@
void die(char *reason) void die(char *reason)
{ {
fprintf(stderr, reason); fprintf(stderr, "%s\n", reason);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }