Compare commits

..

No commits in common. "65c06ff53fa94958665b5f0caf68c1879a5c2f0f" and "04bfc4d76a98b178a17990ff60e052878065b29b" have entirely different histories.

7 changed files with 66 additions and 71 deletions

2
TODO
View file

@ -1,2 +0,0 @@
keybinding in config with struct and enum
cli help page documentation

81
ccc.c
View file

@ -13,35 +13,11 @@
#include <sys/stat.h>
#include <sys/ioctl.h>
#include "config.h"
#include "file.h"
#include "icons.h"
#include "util.h"
/* Keybindings */
enum keys {
CTRLD = 0x04,
ENTER = 0xD,
CTRLU = 0x15,
SPACE = 0x20,
TILDE = 0x7E,
BACKSPACE = 127,
ARROW_LEFT = 1000,
ARROW_RIGHT,
ARROW_UP,
ARROW_DOWN,
DEL_KEY,
HOME_KEY,
END_KEY,
PAGE_UP,
PAGE_DOWN,
};
typedef struct {
int key;
} key;
#define PATH_MAX 4096 /* Max length of path */
#include "config.h"
void handle_sigwinch(int ignore);
void cleanup(void);
void show_help(void);
@ -75,8 +51,8 @@ void bprintf(const char *fmt, ...);
/* global variables */
unsigned int focus = 0;
long sel_file = 0;
int file_picker = 1;
int to_open_file = 0;
bool file_picker = false;
bool to_open_file = false;
char *argv_cp;
char *cwd;
char *p_cwd; /* previous cwd */
@ -94,7 +70,7 @@ int main(int argc, char **argv)
{
if (argc == 3) {
if (strncmp(argv[2], "-p", 2) == 0)
file_picker = 1;
file_picker = true;
}
if (argc <= 3 && argc != 1) {
if (strncmp(argv[1], "-h", 2) == 0)
@ -112,14 +88,12 @@ int main(int argc, char **argv)
} else if (S_ISREG(st.st_mode)) {
argv_cp = estrdup(argv[1]);
char *last_slash = strrchr(argv_cp, '/');
if (last_slash) {
*last_slash = '\0';
if (chdir(argv[1])) {
perror("ccc");
die("Error from chdir");
}
*last_slash = '\0';
if (chdir(argv[1])) {
perror("ccc");
die("Error from chdir");
}
to_open_file = 1;
to_open_file = true;
}
}
@ -164,7 +138,7 @@ int main(int argc, char **argv)
handle_sigwinch(-1);
if (to_open_file) {
sel_file = arraylist_search(files, argv_cp, 1);
sel_file = arraylist_search(files, argv_cp, true);
list_files();
}
@ -623,7 +597,8 @@ void add_file_stat(char *filename, char *path, int ftype)
if (stat(path, &file_stat) == -1) {
/* can't be triggered? */
if (errno == EACCES)
arraylist_add(files, filename, path, NULL, REG, NULL, DEF_COLOR, 0, 0);
arraylist_add(files, filename, path, NULL, REG, NULL, DEF_COLOR, false,
false);
}
int type;
@ -673,8 +648,8 @@ void add_file_stat(char *filename, char *path, int ftype)
/* If file is to be marked */
if (ftype == 1 || ftype == 2) {
/* Force if user is marking all files */
int force = ftype == 2 ? 1 : 0;
arraylist_add(marked, filename, path, NULL, type, icon_str, DEF_COLOR, 1,
bool force = ftype == 2 ? true : false;
arraylist_add(marked, filename, path, NULL, type, icon_str, DEF_COLOR, true,
force);
/* free type and return without allocating more stuff */
return;
@ -690,7 +665,7 @@ void add_file_stat(char *filename, char *path, int ftype)
double bytes = file_stat.st_size;
if (dirs_size) {
/* dirs_size is 1, so calculate disk usage */
/* dirs_size is true, so calculate disk usage */
if (S_ISDIR(file_stat.st_mode)) {
/* at most 15 fd opened */
total_dir_size = 0;
@ -727,9 +702,9 @@ void add_file_stat(char *filename, char *path, int ftype)
/* DIR if color is blue */
if (color == 34)
arraylist_add(tmp1, filename, path, total_stat, type, icon_str, color, 0, 0);
arraylist_add(tmp1, filename, path, total_stat, type, icon_str, color, false, false);
else
arraylist_add(tmp2, filename, path, total_stat, type, icon_str, color, 0, 0);
arraylist_add(tmp2, filename, path, total_stat, type, icon_str, color, false, false);
free(time);
free(size);
@ -813,7 +788,7 @@ void list_files(void)
char *line = get_line(files, i, show_details, show_icons);
int color = files->items[i].color;
/* check is file marked for action */
int is_marked = arraylist_search(marked, files->items[i].path, 0) != -1;
bool is_marked = arraylist_search(marked, files->items[i].path, false) != -1;
move_cursor(i + 1, 1);
if (is_marked) color = 32;
bprintf("\033[30m\033[%dm%s\033[0m\n",
@ -829,13 +804,13 @@ void list_files(void)
size_t get_effective_length(const char *str)
{
size_t length = 0;
int in_ansi_sequence = 0;
bool in_ansi_sequence = false;
while (*str) {
if (*str == '\033') {
in_ansi_sequence = 1;
in_ansi_sequence = true;
} else if (in_ansi_sequence && *str == 'm') {
in_ansi_sequence = 0;
in_ansi_sequence = false;
} else if (!in_ansi_sequence) {
length++;
}
@ -847,17 +822,17 @@ size_t get_effective_length(const char *str)
void print_chunk(const char *str, size_t start, size_t end)
{
int in_ansi_sequence = 0;
bool in_ansi_sequence = false;
size_t printed_length = 0;
for (size_t i = start; i < end; i++) {
if (str[i] == '\033') {
in_ansi_sequence = 1;
in_ansi_sequence = true;
}
if (in_ansi_sequence) {
putchar(str[i]);
if (str[i] == 'm') {
in_ansi_sequence = 0;
in_ansi_sequence = false;
}
} else {
putchar(str[i]);
@ -881,7 +856,7 @@ void show_file_content(void)
ArrayList *files_visit;
populate_files(current_file.name, 0, &files_visit);
for (long i = 0; i < files_visit->length; i++) {
char *line = get_line(files_visit, i, 0, show_icons);
char *line = get_line(files_visit, i, false, show_icons);
int color = files_visit->items[i].color;
move_cursor(i + 1, half_width);
bprintf("\033[K\033[%dm%s\033[m\n", color, line);
@ -944,17 +919,17 @@ void show_file_content(void)
/* Find the actual end position in the string to avoid cutting ANSI sequences */
size_t actual_end = offset;
size_t visible_count = 0;
int in_ansi_sequence = 0;
bool in_ansi_sequence = false;
while (visible_count < chunk_size && buffer[actual_end] != '\0') {
if (buffer[actual_end] == '\033') {
in_ansi_sequence = 1;
in_ansi_sequence = true;
}
if (!in_ansi_sequence) {
visible_count++;
}
if (in_ansi_sequence && buffer[actual_end] == 'm') {
in_ansi_sequence = 0;
in_ansi_sequence = false;
}
actual_end++;
}

View file

@ -1,3 +1,6 @@
#define PATH_MAX 4096 /* Max length of path */
/* Settings */
static int panel_height = 1; /* Panel height */
static int jump_num = 14; /* Length of ctrl + u/d jump */
static int decimal_place = 1; /* Number of decimal places size can be shown */
@ -48,8 +51,22 @@ static char last_d[PATH_MAX] = "~/.cache/ccc/.ccc_d";
/* Will create this directory if doesn't exist! */
static char trash_dir[PATH_MAX] = "~/.cache/ccc/trash/";
/*
key keybindings[] = {
/* Keybindings */
#define CTRLD 0x04
#define ENTER 0xD
#define CTRLU 0x15
#define SPACE 0x20
#define TILDE 0x7E
}
*/
enum keys {
BACKSPACE = 127,
ARROW_LEFT = 1000,
ARROW_RIGHT,
ARROW_UP,
ARROW_DOWN,
DEL_KEY,
HOME_KEY,
END_KEY,
PAGE_UP,
PAGE_DOWN
};

9
file.c
View file

@ -1,5 +1,6 @@
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "util.h"
#include "file.h"
@ -33,9 +34,9 @@ void arraylist_free(ArrayList *list)
/*
* Check if the file is in the arraylist
* Treat filepath as base name if bname is 1
* Treat filepath as base name if bname is true
*/
long arraylist_search(ArrayList *list, char *filepath, int bname)
long arraylist_search(ArrayList *list, char *filepath, bool bname)
{
for (long i = 0; i < list->length; i++) {
if (!bname && strcmp(list->items[i].path, filepath) == 0) {
@ -70,7 +71,7 @@ void arraylist_remove(ArrayList *list, long index)
/*
* Force will not remove duplicate marked files, instead it just skip adding
*/
void arraylist_add(ArrayList *list, char *name, char *path, char *stats, int type, char *icon, int color, int marked, int force)
void arraylist_add(ArrayList *list, char *name, char *path, char *stats, int type, char *icon, int color, bool marked, bool force)
{
file new_file = { name, path, type, stats, icon, color };
@ -104,7 +105,7 @@ void arraylist_add(ArrayList *list, char *name, char *path, char *stats, int typ
/*
* Construct a formatted line for display
*/
char *get_line(ArrayList *list, long index, int detail, int icons)
char *get_line(ArrayList *list, long index, bool detail, bool icons)
{
file file = list->items[index];

7
file.h
View file

@ -2,6 +2,7 @@
#define FILE_H_
#include <stdio.h>
#include <stdbool.h>
enum ftypes {
REG,
@ -30,9 +31,9 @@ typedef struct ArrayList {
ArrayList *arraylist_init(size_t capacity);
void arraylist_free(ArrayList *list);
long arraylist_search(ArrayList *list, char *filepath, int bname);
long arraylist_search(ArrayList *list, char *filepath, bool bname);
void arraylist_remove(ArrayList *list, long index);
void arraylist_add(ArrayList *list, char *name, char *path, char *stats, int type, char *icon, int color, int marked, int force);
char *get_line(ArrayList *list, long index, int detail, int icons);
void arraylist_add(ArrayList *list, char *name, char *path, char *stats, int type, char *icon, int color, bool marked, bool force);
char *get_line(ArrayList *list, long index, bool detail, bool icons);
#endif

View file

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "icons.h"
#include "util.h"
@ -152,9 +153,9 @@ void hashtable_print(void)
}
/* Gets hashed name and tries to store the icon struct in that place */
int hashtable_add(icon *p)
bool hashtable_add(icon *p)
{
if (p == NULL) return 0;
if (p == NULL) return false;
int index = hash(p->name);
int initial_index = index;
@ -162,11 +163,11 @@ int hashtable_add(icon *p)
while (hash_table[index] != NULL) {
index = (index + 1) % TABLE_SIZE; /* move to next item */
/* the hash table is full as no available index back to initial index, cannot fit new item */
if (index == initial_index) return 0;
if (index == initial_index) return false;
}
hash_table[index] = p;
return 1;
return true;
}
/* Rehashes the name and then looks in this spot, if found returns icon */

View file

@ -1,6 +1,8 @@
#ifndef ICONS_H_
#define ICONS_H_
#include <stdbool.h>
#define MAX_NAME 30
#define TABLE_SIZE 100
@ -12,7 +14,7 @@ typedef struct {
unsigned int hash(char *name);
void hashtable_init(void);
void hashtable_print(void);
int hashtable_add(icon *p);
bool hashtable_add(icon *p);
icon *hashtable_search(char *name);
#endif