open file with argument

This commit is contained in:
Night Kaly 2024-03-31 00:20:29 +00:00
parent 93c1e5f8c2
commit da2e1ee144
No known key found for this signature in database
GPG key ID: 8E829D3381CFEBBE
3 changed files with 23 additions and 8 deletions

13
ccc.c
View file

@ -40,6 +40,7 @@ void draw_border_title(WINDOW *window, bool active);
/* global variables */
unsigned int focus = 0;
long current_selection = 0;
bool to_open_file = false;
bool dirs_size = DIRS_SIZE;
bool show_hidden = SHOW_HIDDEN;
bool file_details = SHOW_DETAILS;
@ -69,10 +70,13 @@ int main(int argc, char** argv)
die("Error from lstat");
}
/* chdir to directory from argument */
if(S_ISDIR(st.st_mode) && chdir(argv[1])) {
if (S_ISDIR(st.st_mode) && chdir(argv[1])) {
perror("ccc");
die("Error from chdir");
}
if (S_ISREG(st.st_mode)) {
to_open_file = true;
}
}
/* check if it is interactive shell */
@ -117,7 +121,12 @@ int main(int argc, char** argv)
getcwd(cwd, PATH_MAX);
p_cwd = memalloc(PATH_MAX * sizeof(char));
start_ccc();
populate_files(cwd, 0);
if (to_open_file) {
current_selection = arraylist_search(files, argv[1], true);
highlight_current_line();
}
int ch, ch2;
while (1) {
@ -735,7 +744,7 @@ void highlight_current_line()
char *line = get_line(files, i, file_details);
int color = files->items[i].color;
/* check is file marked for action */
bool is_marked = arraylist_includes(marked, files->items[i].path);
bool is_marked = arraylist_search(marked, files->items[i].path, false) != -1;
if (is_marked) {
/* show file is selected */
wattron(directory_content, COLOR_PAIR(7));

16
file.c
View file

@ -34,15 +34,21 @@ void arraylist_free(ArrayList *list)
list->length = 0;
}
bool arraylist_includes(ArrayList *list, char *path)
long arraylist_search(ArrayList *list, char *path, bool bname)
{
for (int i = 0; i < list->length; i++) {
if (strcmp(list->items[i].path, path) == 0) {
return true;
for (long i = 0; i < list->length; i++) {
if (!bname && strcmp(list->items[i].path, path) == 0) {
return i;
}
if (bname) {
char *fbname = basename(list->items[i].path);
if (fbname != NULL && strcmp(fbname, path) == 0) {
return i;
}
}
}
return false;
return -1;
}
void arraylist_remove(ArrayList *list, long index)

2
file.h
View file

@ -19,7 +19,7 @@ typedef struct ArrayList {
ArrayList *arraylist_init(size_t capacity);
void arraylist_free(ArrayList *list);
bool arraylist_includes(ArrayList *list, char *path);
long arraylist_search(ArrayList *list, char *path, bool bname);
void arraylist_remove(ArrayList *list, long index);
void arraylist_add(ArrayList *list, char *filepath, char *stats, char *type, wchar_t *icon, int color, bool marked, bool force);
char *get_line(ArrayList *list, long index, bool detail);