print marked files with cyan

introduce free file method

remove strip from makefile as -s already did
This commit is contained in:
Night Kaly 2024-03-15 18:28:37 +00:00
parent e7640b5768
commit e15caee8d0
No known key found for this signature in database
GPG key ID: 8E829D3381CFEBBE
4 changed files with 111 additions and 14 deletions

View file

@ -18,7 +18,6 @@ SRC = ccc.c util.c file.c $(CONF)
$(TARGET): $(SRC) $(TARGET): $(SRC)
$(CC) $(CFLAGS) $(LDFLAGS) $(SRC) -o $@ $(CC) $(CFLAGS) $(LDFLAGS) $(SRC) -o $@
strip $(TARGET)
dist: dist:
mkdir -p $(TARGET)-$(VERSION) mkdir -p $(TARGET)-$(VERSION)

36
ccc.c
View file

@ -84,7 +84,7 @@ int main(int argc, char** argv)
init_pair(4, COLOR_YELLOW, -1); /* BLK */ init_pair(4, COLOR_YELLOW, -1); /* BLK */
init_pair(5, COLOR_BLUE, -1); /* DIR */ init_pair(5, COLOR_BLUE, -1); /* DIR */
init_pair(6, COLOR_MAGENTA, -1); /* */ init_pair(6, COLOR_MAGENTA, -1); /* */
init_pair(7, COLOR_CYAN, -1); /* */ init_pair(7, COLOR_CYAN, -1); /* MARKED FILES */
init_pair(8, COLOR_WHITE, -1); /* REG */ init_pair(8, COLOR_WHITE, -1); /* REG */
half_width = COLS / 2; half_width = COLS / 2;
@ -239,7 +239,7 @@ int main(int argc, char** argv)
break; break;
/* mark files by space */ /* mark files by space */
case SPACE: case SPACE:;
add_file_stat(get_filepath(current_selection), 1); add_file_stat(get_filepath(current_selection), 1);
highlight_current_line(); highlight_current_line();
break; break;
@ -440,13 +440,17 @@ long add_file_stat(char *filepath, int ftype)
if (ftype == 1) { if (ftype == 1) {
long index = add_marked(filepath, type); long index = add_marked(filepath, type);
if (index == -1) {
}
free(type); free(type);
free(size); free(size);
free(time); free(time);
return index; if (index != -1) {
/* just marked */
return index;
}
/* already marked */
return -1;
/* -1 does nothing, just function required to return something */
} }
char *total_stat = memalloc(45 * sizeof(char)); char *total_stat = memalloc(45 * sizeof(char));
snprintf(total_stat, 45, "%-18s %-10s", time, size); snprintf(total_stat, 45, "%-18s %-10s", time, size);
@ -513,16 +517,28 @@ void highlight_current_line()
/* print the actual filename and stats */ /* print the actual filename and stats */
char *line = get_line(i); char *line = get_line(i);
int color = get_color(i); int color = get_color(i);
/* check is file marked for action */
/* print the whole directory with colors */ bool marked = in_marked(get_filepath(i));
wattron(directory_content, COLOR_PAIR(color)); if (marked) {
/* show file is selected */
wattron(directory_content, COLOR_PAIR(7));
} else {
/* print the whole directory with default colors */
wattron(directory_content, COLOR_PAIR(color));
}
if (overflow > 0) if (overflow > 0)
mvwprintw(directory_content, line_count, 0, "%s", line); mvwprintw(directory_content, line_count, 0, "%s", line);
else else
mvwprintw(directory_content, i, 0, "%s", line); mvwprintw(directory_content, i, 0, "%s", line);
if (marked) {
wattroff(directory_content, COLOR_PAIR(7));
} else {
wattroff(directory_content, COLOR_PAIR(color));
}
wattroff(directory_content, A_REVERSE); wattroff(directory_content, A_REVERSE);
wattroff(directory_content, COLOR_PAIR(color));
free(line); free(line);
line_count++; line_count++;
} }

84
file.c
View file

@ -1,5 +1,6 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
#include <libgen.h> #include <libgen.h>
#include "util.h" #include "util.h"
@ -45,13 +46,24 @@ long marked_len()
} }
void free_file(file *toremove)
{
if (toremove->type != NULL)
free(toremove->type);
if (toremove->path != NULL)
free(toremove->path);
if (toremove->stats != NULL)
free(toremove->stats);
free(toremove);
}
void clear_files() void clear_files()
{ {
file *tmp; file *tmp;
while (files != NULL) { while (files != NULL) {
tmp = files; tmp = files;
files = files->next; files = files->next;
free(tmp); free_file(tmp);
} }
} }
@ -61,7 +73,7 @@ void clear_marked()
while (marked != NULL) { while (marked != NULL) {
tmp = marked; tmp = marked;
files = marked->next; files = marked->next;
free(tmp); free_file(tmp);
} }
} }
@ -96,6 +108,29 @@ long add_file(char *filepath, char *stats, char *type, int color)
return index; return index;
} }
void remove_marked(file *marked_file)
{
/* If the head node itself is marked for removal */
if (marked == marked_file) {
marked = marked->next;
free_file(marked_file);
return;
}
/* Search for the marked file node in the list */
file* temp = marked;
while (temp != NULL && temp->next != marked_file) {
temp = temp->next;
}
/* If the marked file node is found, remove it from the list */
if (temp != NULL) {
temp->next = marked_file->next;
free_file(marked_file);
}
}
long add_marked(char *filepath, char *type) long add_marked(char *filepath, char *type)
{ {
file *current = marked; file *current = marked;
@ -107,20 +142,63 @@ long add_marked(char *filepath, char *type)
} }
new_file->path = buf; new_file->path = buf;
new_file->type = buf2; new_file->type = buf2;
new_file->stats = NULL;
new_file->color = 0;
new_file->next = NULL; new_file->next = NULL;
if (current == NULL) { if (current == NULL) {
marked = new_file; marked = new_file;
return 0; return 0;
} }
long index = 1; long index = 1;
while (current->next != NULL) { while (current->next != NULL) {
if (strcmp(current->path, new_file->path) == 0) {
remove_marked(current);
free_file(new_file);
return -1;
}
current = current->next; current = current->next;
index++; index++;
} }
if (strcmp(current->path, new_file->path) == 0){
remove_marked(current);
free_file(new_file);
return -1;
}
current->next = new_file; current->next = new_file;
return index; return index;
} }
file *get_marked(long index)
{
file *current = marked;
if (index == 0) {
return current;
}
if (index > files_len()) {
return NULL;
}
for (long i = 0; i < index; i++) {
current = current->next;
}
return current;
}
bool in_marked(char *path)
{
file *tmp = marked;
if (tmp == NULL)
return false;
while (tmp != NULL) {
if (strcmp(path, tmp->path) == 0) {
return true;
}
tmp = tmp->next;
}
return false;
}
file *get_file(long index) file *get_file(long index)
{ {
file *current = files; file *current = files;
@ -160,7 +238,7 @@ int get_color(long index)
} }
return color; return color;
} else { } else {
return 8; /* white */ return 8; /* white */
} }
} }

4
file.h
View file

@ -11,10 +11,14 @@ typedef struct file {
long files_len(); long files_len();
long marked_len(); long marked_len();
void free_file(file *toremove);
void clear_files(); void clear_files();
void clear_marked(); void clear_marked();
long add_file(char *filepath, char *stats, char *type, int color); long add_file(char *filepath, char *stats, char *type, int color);
void remove_marked(file *marked_file);
long add_marked(char *filepath, char *type); long add_marked(char *filepath, char *type);
file *get_marked(long index);
bool in_marked(char *path);
file *get_file(long index); file *get_file(long index);
char *get_filepath(long index); char *get_filepath(long index);
int get_color(long index); int get_color(long index);