Some style improvements in ccc.c

This commit is contained in:
Piotr Marendowski 2024-03-09 19:24:38 +01:00
parent 670612290e
commit 9e3de3d265

28
ccc.c
View file

@ -3,11 +3,13 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <linux/limits.h> #include <linux/limits.h>
#include <dirent.h> #include <dirent.h> /* directories etc. */
#include <ncurses.h> #include <ncurses.h>
#include "util.h" #include "util.h"
#define ESC 0x1B
typedef struct { typedef struct {
WINDOW *window; WINDOW *window;
int location; int location;
@ -15,6 +17,7 @@ typedef struct {
int x; int x;
} WIN_STRUCT; } WIN_STRUCT;
/* 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();
@ -22,6 +25,7 @@ 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 */
unsigned int focus = 0; unsigned int focus = 0;
int current_selection = 0; int current_selection = 0;
char **files; char **files;
@ -30,30 +34,33 @@ WIN_STRUCT windows[2];
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
// 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");
}
initscr(); initscr();
noecho(); noecho();
curs_set(0); curs_set(0);
// check terminal have 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");
} }
start_color(); else {
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();
halfx = COLS / 2; halfx = COLS / 2;
init_windows(); init_windows();
list_cwd_files(); list_cwd_files();
highlight_current_line(); highlight_current_line();
int ch; int ch;
while (1) while (1)
{ {
@ -99,7 +106,6 @@ int main(int argc, char** argv)
} }
} }
endwin(); // End curses endwin(); // End curses
return EXIT_SUCCESS;
return 0; return 0;
} }
@ -109,6 +115,7 @@ void list_cwd_files()
getcwd(cwd, sizeof(cwd)); getcwd(cwd, sizeof(cwd));
DIR *dp; DIR *dp;
struct dirent *ep; struct dirent *ep;
dp = opendir(cwd); dp = opendir(cwd);
if (dp != NULL) if (dp != NULL)
{ {
@ -133,8 +140,7 @@ void list_cwd_files()
files[count] = NULL; files[count] = NULL;
wrefresh(windows[0].window); wrefresh(windows[0].window);
} }
else else {
{
perror("ccc"); perror("ccc");
} }
} }
@ -148,10 +154,12 @@ void highlight_current_line()
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));
} }
mvwprintw(windows[0].window, i + 1, 1, "%s", files[i]); mvwprintw(windows[0].window, i + 1, 1, "%s", files[i]);
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);
show_file_content(); show_file_content();
} }