Use printf
This commit is contained in:
parent
7ca0b4924a
commit
7fb42381ee
1 changed files with 30 additions and 43 deletions
73
ccc.c
73
ccc.c
|
@ -101,7 +101,6 @@ void wpprintw(const char *fmt, ...);
|
||||||
void move_cursor(int row, int col);
|
void move_cursor(int row, int col);
|
||||||
int readch(void);
|
int readch(void);
|
||||||
int get_window_size(int *row, int *col);
|
int get_window_size(int *row, int *col);
|
||||||
void bprintf(const char *fmt, ...);
|
|
||||||
|
|
||||||
/* global variables */
|
/* global variables */
|
||||||
long sel_file = 0;
|
long sel_file = 0;
|
||||||
|
@ -170,7 +169,7 @@ int main(int argc, char **argv)
|
||||||
/* initialize screen, don't print special chars,
|
/* initialize screen, don't print special chars,
|
||||||
* make ctrl + c work, don't show cursor
|
* make ctrl + c work, don't show cursor
|
||||||
* enable arrow keys */
|
* enable arrow keys */
|
||||||
bprintf("\033[?1049h\033[2J\033[?25l");
|
printf("\033[?1049h\033[2J\033[?25l");
|
||||||
tcgetattr(STDIN_FILENO, &oldt);
|
tcgetattr(STDIN_FILENO, &oldt);
|
||||||
newt = oldt;
|
newt = oldt;
|
||||||
/* Disable canonical mode and echo */
|
/* Disable canonical mode and echo */
|
||||||
|
@ -228,7 +227,7 @@ void cleanup(void)
|
||||||
free(marked);
|
free(marked);
|
||||||
/* Restore old terminal settings */
|
/* Restore old terminal settings */
|
||||||
tcsetattr(STDIN_FILENO, TCSAFLUSH, &oldt);
|
tcsetattr(STDIN_FILENO, TCSAFLUSH, &oldt);
|
||||||
bprintf("\033[2J\033[?1049l\033[?25h");
|
printf("\033[2J\033[?1049l\033[?25h");
|
||||||
}
|
}
|
||||||
|
|
||||||
void replace_home(char *str)
|
void replace_home(char *str)
|
||||||
|
@ -555,7 +554,7 @@ void show_file_content(void)
|
||||||
char *line = get_line(files_visit, i, 0, show_icons);
|
char *line = get_line(files_visit, i, 0, show_icons);
|
||||||
int color = files_visit->items[i].color;
|
int color = files_visit->items[i].color;
|
||||||
move_cursor(i + 1, half_width);
|
move_cursor(i + 1, half_width);
|
||||||
bprintf("\033[K\033[%dm%s\033[m\n", color, line);
|
printf("\033[K\033[%dm%s\033[m\n", color, line);
|
||||||
free(line);
|
free(line);
|
||||||
}
|
}
|
||||||
arraylist_free(files_visit);
|
arraylist_free(files_visit);
|
||||||
|
@ -563,8 +562,8 @@ void show_file_content(void)
|
||||||
}
|
}
|
||||||
FILE *file = fopen(current_file.path, "r");
|
FILE *file = fopen(current_file.path, "r");
|
||||||
if (!file) {
|
if (!file) {
|
||||||
/* bprintf("Unable to read %s", current_file.name ? current_file.name : "unknown"); */
|
/* printf("Unable to read %s", current_file.name ? current_file.name : "unknown"); */
|
||||||
bprintf("Unable to read unknown");
|
printf("Unable to read unknown");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -572,7 +571,7 @@ void show_file_content(void)
|
||||||
/* Check if its binary */
|
/* Check if its binary */
|
||||||
while ((c = fgetc(file)) != EOF) {
|
while ((c = fgetc(file)) != EOF) {
|
||||||
if (c == '\0') {
|
if (c == '\0') {
|
||||||
bprintf("binary");
|
printf("binary");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -602,7 +601,7 @@ void show_file_content(void)
|
||||||
size_t buflen = strlen(buffer);
|
size_t buflen = strlen(buffer);
|
||||||
if (buffer[0] == '\0' || strspn(buffer, " \t") == buflen) {
|
if (buffer[0] == '\0' || strspn(buffer, " \t") == buflen) {
|
||||||
move_cursor(row++, half_width);
|
move_cursor(row++, half_width);
|
||||||
bprintf("\n");
|
printf("\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
move_cursor(row++, half_width);
|
move_cursor(row++, half_width);
|
||||||
|
@ -620,16 +619,16 @@ void show_file_content(void)
|
||||||
if (length < sizeof(ansiseq)) {
|
if (length < sizeof(ansiseq)) {
|
||||||
strncpy(ansiseq, buffer + start, length);
|
strncpy(ansiseq, buffer + start, length);
|
||||||
ansiseq[length] = '\0';
|
ansiseq[length] = '\0';
|
||||||
bprintf("%s", ansiseq);
|
printf("%s", ansiseq);
|
||||||
/* Update the index to the character after the sequence */
|
/* Update the index to the character after the sequence */
|
||||||
i += length - 1;
|
i += length - 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
bprintf("%c", buffer[i]);
|
printf("%c", buffer[i]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
bprintf("%c", buffer[i]);
|
printf("%c", buffer[i]);
|
||||||
len++;
|
len++;
|
||||||
}
|
}
|
||||||
if (len && (len) % (cols - half_width + 1) == 0) {
|
if (len && (len) % (cols - half_width + 1) == 0) {
|
||||||
|
@ -657,10 +656,10 @@ void list_files(void)
|
||||||
if (range == 0) {
|
if (range == 0) {
|
||||||
for (int i = 0; i < rows - 1; i++) {
|
for (int i = 0; i < rows - 1; i++) {
|
||||||
move_cursor(i + 1, 1);
|
move_cursor(i + 1, 1);
|
||||||
bprintf("\033[K");
|
printf("\033[K");
|
||||||
}
|
}
|
||||||
move_cursor(1, half_width);
|
move_cursor(1, half_width);
|
||||||
bprintf("empty directory");
|
printf("empty directory");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -677,7 +676,7 @@ void list_files(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
move_cursor(1, 1);
|
move_cursor(1, 1);
|
||||||
bprintf("\033[2J");
|
printf("\033[2J");
|
||||||
|
|
||||||
int max_flen = 0;
|
int max_flen = 0;
|
||||||
for (long i = overflow; i < range; i++) {
|
for (long i = overflow; i < range; i++) {
|
||||||
|
@ -709,7 +708,7 @@ void list_files(void)
|
||||||
int is_marked = arraylist_search(marked, files->items[i].path, 0) != -1;
|
int is_marked = arraylist_search(marked, files->items[i].path, 0) != -1;
|
||||||
move_cursor(i - overflow + 1, 1);
|
move_cursor(i - overflow + 1, 1);
|
||||||
if (is_marked) color = MAR_COLOR;
|
if (is_marked) color = MAR_COLOR;
|
||||||
bprintf("\033[30m\033[%dm%s\033[m\n",
|
printf("\033[30m\033[%dm%s\033[m\n",
|
||||||
is_selected ? color + 10 : color, line);
|
is_selected ? color + 10 : color, line);
|
||||||
|
|
||||||
free(line);
|
free(line);
|
||||||
|
@ -755,7 +754,7 @@ char *get_panel_string(char *prompt)
|
||||||
char *buf = memalloc(bufsize);
|
char *buf = memalloc(bufsize);
|
||||||
size_t buflen = 0;
|
size_t buflen = 0;
|
||||||
buf[0] = '\0';
|
buf[0] = '\0';
|
||||||
bprintf("\033[?25h");
|
printf("\033[?25h");
|
||||||
while (1) {
|
while (1) {
|
||||||
wpprintw("%s%s", prompt, buf);
|
wpprintw("%s%s", prompt, buf);
|
||||||
int c = readch();
|
int c = readch();
|
||||||
|
@ -766,12 +765,12 @@ char *get_panel_string(char *prompt)
|
||||||
} else if (c == '\033') {
|
} else if (c == '\033') {
|
||||||
wpprintw("");
|
wpprintw("");
|
||||||
free(buf);
|
free(buf);
|
||||||
bprintf("\033[?25l");
|
printf("\033[?25l");
|
||||||
return NULL;
|
return NULL;
|
||||||
} else if (c == '\r') {
|
} else if (c == '\r') {
|
||||||
wpprintw("");
|
wpprintw("");
|
||||||
if (buflen != 0) {
|
if (buflen != 0) {
|
||||||
bprintf("\033[?25l");
|
printf("\033[?25l");
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
} else if (!iscntrl(c) && c < 128) {
|
} else if (!iscntrl(c) && c < 128) {
|
||||||
|
@ -787,7 +786,7 @@ char *get_panel_string(char *prompt)
|
||||||
if (buf[0] == '~')
|
if (buf[0] == '~')
|
||||||
replace_home(buf);
|
replace_home(buf);
|
||||||
|
|
||||||
bprintf("\033[?25l");
|
printf("\033[?25l");
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -950,9 +949,9 @@ void prev_dir(const Arg *arg)
|
||||||
|
|
||||||
void show_help(const Arg *arg)
|
void show_help(const Arg *arg)
|
||||||
{
|
{
|
||||||
bprintf("\033[2J");
|
printf("\033[2J");
|
||||||
move_cursor(1, 1);
|
move_cursor(1, 1);
|
||||||
bprintf(
|
printf(
|
||||||
"h/left/backspace: go to parent dir\n"
|
"h/left/backspace: go to parent dir\n"
|
||||||
"j/down: scroll down\n"
|
"j/down: scroll down\n"
|
||||||
"k/up: scroll up\n"
|
"k/up: scroll up\n"
|
||||||
|
@ -1110,7 +1109,7 @@ void toggle_executable(const Arg *arg)
|
||||||
|
|
||||||
void start_shell(const Arg *arg)
|
void start_shell(const Arg *arg)
|
||||||
{
|
{
|
||||||
bprintf("\033[2J\033[?25h");
|
printf("\033[2J\033[?25h");
|
||||||
move_cursor(1, 1);
|
move_cursor(1, 1);
|
||||||
char shell[PATH_MAX];
|
char shell[PATH_MAX];
|
||||||
char *shellenv = getenv("SHELL");
|
char *shellenv = getenv("SHELL");
|
||||||
|
@ -1127,7 +1126,7 @@ void start_shell(const Arg *arg)
|
||||||
} else if (pid > 0) {
|
} else if (pid > 0) {
|
||||||
/* Parent process */
|
/* Parent process */
|
||||||
waitpid(pid, NULL, 0);
|
waitpid(pid, NULL, 0);
|
||||||
bprintf("\033[?25l");
|
printf("\033[?25l");
|
||||||
} else {
|
} else {
|
||||||
/* Fork failed */
|
/* Fork failed */
|
||||||
wpprintw("fork failed: %s", strerror(errno));
|
wpprintw("fork failed: %s", strerror(errno));
|
||||||
|
@ -1144,7 +1143,7 @@ void yank_clipboard(const Arg *arg)
|
||||||
} else if (pid > 0) {
|
} else if (pid > 0) {
|
||||||
/* Parent process */
|
/* Parent process */
|
||||||
waitpid(pid, NULL, 0);
|
waitpid(pid, NULL, 0);
|
||||||
bprintf("\033[?25l");
|
printf("\033[?25l");
|
||||||
} else {
|
} else {
|
||||||
/* Fork failed */
|
/* Fork failed */
|
||||||
wpprintw("fork failed: %s", strerror(errno));
|
wpprintw("fork failed: %s", strerror(errno));
|
||||||
|
@ -1214,7 +1213,7 @@ void open_detached(const Arg *arg)
|
||||||
|
|
||||||
void view_file_attr(const Arg *arg)
|
void view_file_attr(const Arg *arg)
|
||||||
{
|
{
|
||||||
bprintf("\033[2J");
|
printf("\033[2J");
|
||||||
move_cursor(1, 1);
|
move_cursor(1, 1);
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
|
@ -1233,7 +1232,7 @@ void view_file_attr(const Arg *arg)
|
||||||
|
|
||||||
void show_history(const Arg *arg)
|
void show_history(const Arg *arg)
|
||||||
{
|
{
|
||||||
bprintf("\033[2J");
|
printf("\033[2J");
|
||||||
move_cursor(1, 1);
|
move_cursor(1, 1);
|
||||||
char history_path[PATH_MAX];
|
char history_path[PATH_MAX];
|
||||||
strcpy(history_path, "~/.cache/ccc/history");
|
strcpy(history_path, "~/.cache/ccc/history");
|
||||||
|
@ -1243,8 +1242,7 @@ void show_history(const Arg *arg)
|
||||||
int row = 1;
|
int row = 1;
|
||||||
while (fgets(buffer, sizeof(buffer), history_file) && row <= rows - 1) {
|
while (fgets(buffer, sizeof(buffer), history_file) && row <= rows - 1) {
|
||||||
move_cursor(row++, 1);
|
move_cursor(row++, 1);
|
||||||
bprintf(buffer);
|
printf("%s", buffer);
|
||||||
|
|
||||||
}
|
}
|
||||||
fclose(history_file);
|
fclose(history_file);
|
||||||
readch();
|
readch();
|
||||||
|
@ -1328,12 +1326,12 @@ void wpprintw(const char *fmt, ...)
|
||||||
|
|
||||||
move_cursor(rows, 1);
|
move_cursor(rows, 1);
|
||||||
/* Clear line and print formatted string */
|
/* Clear line and print formatted string */
|
||||||
bprintf("\033[K%s", buffer);
|
printf("\033[K%s", buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void move_cursor(int row, int col)
|
void move_cursor(int row, int col)
|
||||||
{
|
{
|
||||||
bprintf("\033[%d;%dH", row, col);
|
printf("\033[%d;%dH", row, col);
|
||||||
}
|
}
|
||||||
|
|
||||||
int readch(void)
|
int readch(void)
|
||||||
|
@ -1395,7 +1393,7 @@ int get_cursor_position(int *rows, int *cols)
|
||||||
{
|
{
|
||||||
char buf[32];
|
char buf[32];
|
||||||
unsigned int i = 0;
|
unsigned int i = 0;
|
||||||
bprintf("\033[6n");
|
printf("\033[6n");
|
||||||
while (i < sizeof(buf) - 1) {
|
while (i < sizeof(buf) - 1) {
|
||||||
if (read(STDIN_FILENO, &buf[i], 1) != 1) {
|
if (read(STDIN_FILENO, &buf[i], 1) != 1) {
|
||||||
break;
|
break;
|
||||||
|
@ -1420,7 +1418,7 @@ int get_window_size(int *row, int *col)
|
||||||
struct winsize ws;
|
struct winsize ws;
|
||||||
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
|
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
|
||||||
/* Can't get window size */
|
/* Can't get window size */
|
||||||
bprintf("\033[999C\033[999B");
|
printf("\033[999C\033[999B");
|
||||||
return get_cursor_position(row, col);
|
return get_cursor_position(row, col);
|
||||||
} else {
|
} else {
|
||||||
*col = ws.ws_col;
|
*col = ws.ws_col;
|
||||||
|
@ -1428,14 +1426,3 @@ int get_window_size(int *row, int *col)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* printf, but write to STDOUT_FILENO
|
|
||||||
*/
|
|
||||||
void bprintf(const char *fmt, ...)
|
|
||||||
{
|
|
||||||
va_list args;
|
|
||||||
va_start(args, fmt);
|
|
||||||
vprintf(fmt, args);
|
|
||||||
va_end(args);
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue