Apologies that this uses my preferred formatting style: mostly the same
as Linux, but without a break between function and brace. Adds spaces in
a few places they weren't before.
| ... | ... |
@@ -10,18 +10,18 @@ static int cmpchoice(const void *_idx1, const void *_idx2) {
|
| 10 | 10 |
const struct scored_result *a = _idx1; |
| 11 | 11 |
const struct scored_result *b = _idx2; |
| 12 | 12 |
|
| 13 |
- if(a->score == b->score) |
|
| 13 |
+ if (a->score == b->score) |
|
| 14 | 14 |
return 0; |
| 15 |
- else if(a->score < b->score) |
|
| 15 |
+ else if (a->score < b->score) |
|
| 16 | 16 |
return 1; |
| 17 | 17 |
else |
| 18 | 18 |
return -1; |
| 19 | 19 |
} |
| 20 | 20 |
|
| 21 |
-static void choices_resize(choices_t *c, int new_capacity){
|
|
| 21 |
+static void choices_resize(choices_t *c, int new_capacity) {
|
|
| 22 | 22 |
c->strings = realloc(c->strings, new_capacity * sizeof(const char *)); |
| 23 | 23 |
|
| 24 |
- if(!c->strings){
|
|
| 24 |
+ if (!c->strings) {
|
|
| 25 | 25 |
fprintf(stderr, "Error: Can't allocate memory\n"); |
| 26 | 26 |
abort(); |
| 27 | 27 |
} |
| ... | ... |
@@ -29,13 +29,13 @@ static void choices_resize(choices_t *c, int new_capacity){
|
| 29 | 29 |
c->capacity = new_capacity; |
| 30 | 30 |
} |
| 31 | 31 |
|
| 32 |
-static void choices_reset_search(choices_t *c){
|
|
| 32 |
+static void choices_reset_search(choices_t *c) {
|
|
| 33 | 33 |
free(c->results); |
| 34 | 34 |
c->selection = c->available = 0; |
| 35 | 35 |
c->results = NULL; |
| 36 | 36 |
} |
| 37 | 37 |
|
| 38 |
-void choices_init(choices_t *c){
|
|
| 38 |
+void choices_init(choices_t *c) {
|
|
| 39 | 39 |
c->strings = NULL; |
| 40 | 40 |
c->results = NULL; |
| 41 | 41 |
c->capacity = c->size = 0; |
| ... | ... |
@@ -43,36 +43,36 @@ void choices_init(choices_t *c){
|
| 43 | 43 |
choices_resize(c, INITIAL_CAPACITY); |
| 44 | 44 |
} |
| 45 | 45 |
|
| 46 |
-void choices_free(choices_t *c){
|
|
| 46 |
+void choices_free(choices_t *c) {
|
|
| 47 | 47 |
free(c->strings); |
| 48 | 48 |
free(c->results); |
| 49 | 49 |
} |
| 50 | 50 |
|
| 51 |
-void choices_add(choices_t *c, const char *choice){
|
|
| 51 |
+void choices_add(choices_t *c, const char *choice) {
|
|
| 52 | 52 |
/* Previous search is now invalid */ |
| 53 | 53 |
choices_reset_search(c); |
| 54 | 54 |
|
| 55 |
- if(c->size == c->capacity){
|
|
| 55 |
+ if (c->size == c->capacity) {
|
|
| 56 | 56 |
choices_resize(c, c->capacity * 2); |
| 57 | 57 |
} |
| 58 | 58 |
c->strings[c->size++] = choice; |
| 59 | 59 |
} |
| 60 | 60 |
|
| 61 |
-size_t choices_available(choices_t *c){
|
|
| 61 |
+size_t choices_available(choices_t *c) {
|
|
| 62 | 62 |
return c->available; |
| 63 | 63 |
} |
| 64 | 64 |
|
| 65 |
-void choices_search(choices_t *c, const char *search){
|
|
| 65 |
+void choices_search(choices_t *c, const char *search) {
|
|
| 66 | 66 |
choices_reset_search(c); |
| 67 | 67 |
|
| 68 | 68 |
c->results = malloc(c->size * sizeof(struct scored_result)); |
| 69 |
- if(!c->results){
|
|
| 69 |
+ if (!c->results) {
|
|
| 70 | 70 |
fprintf(stderr, "Error: Can't allocate memory\n"); |
| 71 | 71 |
abort(); |
| 72 | 72 |
} |
| 73 | 73 |
|
| 74 |
- for(size_t i = 0; i < c->size; i++){
|
|
| 75 |
- if(has_match(search, c->strings[i])){
|
|
| 74 |
+ for (size_t i = 0; i < c->size; i++) {
|
|
| 75 |
+ if (has_match(search, c->strings[i])) {
|
|
| 76 | 76 |
c->results[c->available].str = c->strings[i]; |
| 77 | 77 |
c->results[c->available].score = match(search, c->strings[i]); |
| 78 | 78 |
c->available++; |
| ... | ... |
@@ -82,24 +82,23 @@ void choices_search(choices_t *c, const char *search){
|
| 82 | 82 |
qsort(c->results, c->available, sizeof(struct scored_result), cmpchoice); |
| 83 | 83 |
} |
| 84 | 84 |
|
| 85 |
-const char *choices_get(choices_t *c, size_t n){
|
|
| 86 |
- if(n < c->available){
|
|
| 85 |
+const char *choices_get(choices_t *c, size_t n) {
|
|
| 86 |
+ if (n < c->available) {
|
|
| 87 | 87 |
return c->results[n].str; |
| 88 |
- }else{
|
|
| 88 |
+ } else {
|
|
| 89 | 89 |
return NULL; |
| 90 | 90 |
} |
| 91 | 91 |
} |
| 92 |
-double choices_getscore(choices_t *c, size_t n){
|
|
| 92 |
+double choices_getscore(choices_t *c, size_t n) {
|
|
| 93 | 93 |
return c->results[n].score; |
| 94 | 94 |
} |
| 95 | 95 |
|
| 96 |
-void choices_prev(choices_t *c){
|
|
| 97 |
- if(c->available) |
|
| 96 |
+void choices_prev(choices_t *c) {
|
|
| 97 |
+ if (c->available) |
|
| 98 | 98 |
c->selection = (c->selection + c->available - 1) % c->available; |
| 99 | 99 |
} |
| 100 | 100 |
|
| 101 |
-void choices_next(choices_t *c){
|
|
| 102 |
- if(c->available) |
|
| 101 |
+void choices_next(choices_t *c) {
|
|
| 102 |
+ if (c->available) |
|
| 103 | 103 |
c->selection = (c->selection + 1) % c->available; |
| 104 | 104 |
} |
| 105 |
- |
| ... | ... |
@@ -1,10 +1,10 @@ |
| 1 | 1 |
#define TTY_COLOR_HIGHLIGHT TTY_COLOR_YELLOW |
| 2 | 2 |
|
| 3 |
-#define SCORE_GAP_LEADING -0.005 |
|
| 4 |
-#define SCORE_GAP_TRAILING -0.005 |
|
| 5 |
-#define SCORE_GAP_INNER -0.01 |
|
| 3 |
+#define SCORE_GAP_LEADING -0.005 |
|
| 4 |
+#define SCORE_GAP_TRAILING -0.005 |
|
| 5 |
+#define SCORE_GAP_INNER -0.01 |
|
| 6 | 6 |
#define SCORE_MATCH_CONSECUTIVE 1.0 |
| 7 |
-#define SCORE_MATCH_SLASH 0.9 |
|
| 8 |
-#define SCORE_MATCH_WORD 0.8 |
|
| 9 |
-#define SCORE_MATCH_CAPITAL 0.7 |
|
| 10 |
-#define SCORE_MATCH_DOT 0.6 |
|
| 7 |
+#define SCORE_MATCH_SLASH 0.9 |
|
| 8 |
+#define SCORE_MATCH_WORD 0.8 |
|
| 9 |
+#define SCORE_MATCH_CAPITAL 0.7 |
|
| 10 |
+#define SCORE_MATCH_DOT 0.6 |
| ... | ... |
@@ -18,15 +18,15 @@ size_t scrolloff = 1; |
| 18 | 18 |
|
| 19 | 19 |
const char *prompt = "> "; |
| 20 | 20 |
|
| 21 |
-void read_choices(choices_t *c){
|
|
| 21 |
+void read_choices(choices_t *c) {
|
|
| 22 | 22 |
const char *line; |
| 23 | 23 |
char buf[4096]; |
| 24 |
- while(fgets(buf, sizeof buf, stdin)){
|
|
| 24 |
+ while (fgets(buf, sizeof buf, stdin)) {
|
|
| 25 | 25 |
char *nl; |
| 26 |
- if((nl = strchr(buf, '\n'))) |
|
| 26 |
+ if ((nl = strchr(buf, '\n'))) |
|
| 27 | 27 |
*nl = '\0'; |
| 28 | 28 |
|
| 29 |
- if(!(line = strdup(buf))){
|
|
| 29 |
+ if (!(line = strdup(buf))) {
|
|
| 30 | 30 |
fprintf(stderr, "Cannot allocate memory"); |
| 31 | 31 |
abort(); |
| 32 | 32 |
} |
| ... | ... |
@@ -38,42 +38,42 @@ void read_choices(choices_t *c){
|
| 38 | 38 |
int search_size; |
| 39 | 39 |
char search[SEARCH_SIZE_MAX + 1] = {0};
|
| 40 | 40 |
|
| 41 |
-void clear(tty_t *tty){
|
|
| 41 |
+void clear(tty_t *tty) {
|
|
| 42 | 42 |
tty_setcol(tty, 0); |
| 43 | 43 |
size_t line = 0; |
| 44 |
- while(line++ < num_lines){
|
|
| 44 |
+ while (line++ < num_lines) {
|
|
| 45 | 45 |
tty_newline(tty); |
| 46 | 46 |
} |
| 47 |
- tty_moveup(tty, line-1); |
|
| 47 |
+ tty_moveup(tty, line - 1); |
|
| 48 | 48 |
tty_flush(tty); |
| 49 | 49 |
} |
| 50 | 50 |
|
| 51 |
-void draw_match(tty_t *tty, const char *choice, int selected){
|
|
| 51 |
+void draw_match(tty_t *tty, const char *choice, int selected) {
|
|
| 52 | 52 |
int n = strlen(search); |
| 53 | 53 |
size_t positions[n + 1]; |
| 54 |
- for(int i = 0; i < n + 1; i++) |
|
| 54 |
+ for (int i = 0; i < n + 1; i++) |
|
| 55 | 55 |
positions[i] = -1; |
| 56 | 56 |
|
| 57 | 57 |
double score = match_positions(search, choice, &positions[0]); |
| 58 | 58 |
|
| 59 | 59 |
size_t maxwidth = tty_getwidth(tty); |
| 60 | 60 |
|
| 61 |
- if(flag_show_scores) |
|
| 61 |
+ if (flag_show_scores) |
|
| 62 | 62 |
tty_printf(tty, "(%5.2f) ", score); |
| 63 | 63 |
|
| 64 |
- if(selected) |
|
| 64 |
+ if (selected) |
|
| 65 | 65 |
tty_setinvert(tty); |
| 66 | 66 |
|
| 67 |
- for(size_t i = 0, p = 0; choice[i] != '\0'; i++){
|
|
| 68 |
- if(i+1 < maxwidth){
|
|
| 69 |
- if(positions[p] == i){
|
|
| 67 |
+ for (size_t i = 0, p = 0; choice[i] != '\0'; i++) {
|
|
| 68 |
+ if (i + 1 < maxwidth) {
|
|
| 69 |
+ if (positions[p] == i) {
|
|
| 70 | 70 |
tty_setfg(tty, TTY_COLOR_HIGHLIGHT); |
| 71 | 71 |
p++; |
| 72 |
- }else{
|
|
| 72 |
+ } else {
|
|
| 73 | 73 |
tty_setfg(tty, TTY_COLOR_NORMAL); |
| 74 | 74 |
} |
| 75 | 75 |
tty_printf(tty, "%c", choice[i]); |
| 76 |
- }else{
|
|
| 76 |
+ } else {
|
|
| 77 | 77 |
tty_printf(tty, "$"); |
| 78 | 78 |
break; |
| 79 | 79 |
} |
| ... | ... |
@@ -81,23 +81,23 @@ void draw_match(tty_t *tty, const char *choice, int selected){
|
| 81 | 81 |
tty_setnormal(tty); |
| 82 | 82 |
} |
| 83 | 83 |
|
| 84 |
-void draw(tty_t *tty, choices_t *choices){
|
|
| 84 |
+void draw(tty_t *tty, choices_t *choices) {
|
|
| 85 | 85 |
size_t start = 0; |
| 86 | 86 |
size_t current_selection = choices->selection; |
| 87 |
- if(current_selection + scrolloff >= num_lines){
|
|
| 87 |
+ if (current_selection + scrolloff >= num_lines) {
|
|
| 88 | 88 |
start = current_selection + scrolloff - num_lines + 1; |
| 89 |
- if(start + num_lines >= choices_available(choices)){
|
|
| 89 |
+ if (start + num_lines >= choices_available(choices)) {
|
|
| 90 | 90 |
start = choices_available(choices) - num_lines; |
| 91 | 91 |
} |
| 92 | 92 |
} |
| 93 | 93 |
tty_setcol(tty, 0); |
| 94 | 94 |
tty_printf(tty, "%s%s", prompt, search); |
| 95 | 95 |
tty_clearline(tty); |
| 96 |
- for(size_t i = start; i < start + num_lines; i++){
|
|
| 96 |
+ for (size_t i = start; i < start + num_lines; i++) {
|
|
| 97 | 97 |
tty_printf(tty, "\n"); |
| 98 | 98 |
tty_clearline(tty); |
| 99 | 99 |
const char *choice = choices_get(choices, i); |
| 100 |
- if(choice){
|
|
| 100 |
+ if (choice) {
|
|
| 101 | 101 |
draw_match(tty, choice, i == choices->selection); |
| 102 | 102 |
} |
| 103 | 103 |
} |
| ... | ... |
@@ -106,12 +106,12 @@ void draw(tty_t *tty, choices_t *choices){
|
| 106 | 106 |
tty_flush(tty); |
| 107 | 107 |
} |
| 108 | 108 |
|
| 109 |
-void emit(choices_t *choices){
|
|
| 109 |
+void emit(choices_t *choices) {
|
|
| 110 | 110 |
const char *selection = choices_get(choices, choices->selection); |
| 111 |
- if(selection){
|
|
| 111 |
+ if (selection) {
|
|
| 112 | 112 |
/* output the selected result */ |
| 113 | 113 |
printf("%s\n", selection);
|
| 114 |
- }else{
|
|
| 114 |
+ } else {
|
|
| 115 | 115 |
/* No match, output the query instead */ |
| 116 | 116 |
printf("%s\n", search);
|
| 117 | 117 |
} |
| ... | ... |
@@ -119,98 +119,97 @@ void emit(choices_t *choices){
|
| 119 | 119 |
exit(EXIT_SUCCESS); |
| 120 | 120 |
} |
| 121 | 121 |
|
| 122 |
-void run(tty_t *tty, choices_t *choices){
|
|
| 122 |
+void run(tty_t *tty, choices_t *choices) {
|
|
| 123 | 123 |
choices_search(choices, search); |
| 124 | 124 |
char ch; |
| 125 | 125 |
do {
|
| 126 | 126 |
draw(tty, choices); |
| 127 | 127 |
ch = tty_getchar(tty); |
| 128 |
- if(isprint(ch)){
|
|
| 129 |
- if(search_size < SEARCH_SIZE_MAX){
|
|
| 128 |
+ if (isprint(ch)) {
|
|
| 129 |
+ if (search_size < SEARCH_SIZE_MAX) {
|
|
| 130 | 130 |
search[search_size++] = ch; |
| 131 | 131 |
search[search_size] = '\0'; |
| 132 | 132 |
choices_search(choices, search); |
| 133 | 133 |
} |
| 134 |
- }else if(ch == 127 || ch == 8){ /* DEL || backspace */
|
|
| 135 |
- if(search_size) |
|
| 134 |
+ } else if (ch == 127 || ch == 8) { /* DEL || backspace */
|
|
| 135 |
+ if (search_size) |
|
| 136 | 136 |
search[--search_size] = '\0'; |
| 137 | 137 |
choices_search(choices, search); |
| 138 |
- }else if(ch == 21){ /* C-U */
|
|
| 138 |
+ } else if (ch == 21) { /* C-U */
|
|
| 139 | 139 |
search_size = 0; |
| 140 | 140 |
search[0] = '\0'; |
| 141 | 141 |
choices_search(choices, search); |
| 142 |
- }else if(ch == 23){ /* C-W */
|
|
| 143 |
- if(search_size) |
|
| 142 |
+ } else if (ch == 23) { /* C-W */
|
|
| 143 |
+ if (search_size) |
|
| 144 | 144 |
search[--search_size] = '\0'; |
| 145 |
- while(search_size && !isspace(search[--search_size])) |
|
| 145 |
+ while (search_size && !isspace(search[--search_size])) |
|
| 146 | 146 |
search[search_size] = '\0'; |
| 147 | 147 |
choices_search(choices, search); |
| 148 |
- }else if(ch == 14){ /* C-N */
|
|
| 148 |
+ } else if (ch == 14) { /* C-N */
|
|
| 149 | 149 |
choices_next(choices); |
| 150 |
- }else if(ch == 16){ /* C-P */
|
|
| 150 |
+ } else if (ch == 16) { /* C-P */
|
|
| 151 | 151 |
choices_prev(choices); |
| 152 |
- }else if(ch == 9){ /* TAB */
|
|
| 152 |
+ } else if (ch == 9) { /* TAB */
|
|
| 153 | 153 |
strncpy(search, choices_get(choices, choices->selection), SEARCH_SIZE_MAX); |
| 154 | 154 |
search_size = strlen(search); |
| 155 | 155 |
choices_search(choices, search); |
| 156 |
- }else if(ch == 3 || ch == 4){ /* ^C || ^D */
|
|
| 156 |
+ } else if (ch == 3 || ch == 4) { /* ^C || ^D */
|
|
| 157 | 157 |
clear(tty); |
| 158 | 158 |
tty_close(tty); |
| 159 | 159 |
exit(EXIT_FAILURE); |
| 160 |
- }else if(ch == 10){ /* Enter */
|
|
| 160 |
+ } else if (ch == 10) { /* Enter */
|
|
| 161 | 161 |
clear(tty); |
| 162 | 162 |
|
| 163 | 163 |
/* ttyout should be flushed before outputting on stdout */ |
| 164 | 164 |
tty_close(tty); |
| 165 | 165 |
|
| 166 | 166 |
emit(choices); |
| 167 |
- }else if(ch == 27){ /* ESC */
|
|
| 167 |
+ } else if (ch == 27) { /* ESC */
|
|
| 168 | 168 |
ch = tty_getchar(tty); |
| 169 |
- if(ch == '[' || ch == 'O'){
|
|
| 169 |
+ if (ch == '[' || ch == 'O') {
|
|
| 170 | 170 |
ch = tty_getchar(tty); |
| 171 |
- if(ch == 'A'){ /* UP ARROW */
|
|
| 171 |
+ if (ch == 'A') { /* UP ARROW */
|
|
| 172 | 172 |
choices_prev(choices); |
| 173 |
- }else if(ch == 'B'){ /* DOWN ARROW */
|
|
| 173 |
+ } else if (ch == 'B') { /* DOWN ARROW */
|
|
| 174 | 174 |
choices_next(choices); |
| 175 | 175 |
} |
| 176 | 176 |
} |
| 177 | 177 |
} |
| 178 |
- }while(1); |
|
| 178 |
+ } while (1); |
|
| 179 | 179 |
} |
| 180 | 180 |
|
| 181 |
-static const char *usage_str = "" |
|
| 182 |
-"Usage: fzy [OPTION]...\n" |
|
| 183 |
-" -l, --lines=LINES Specify how many lines of results to show (default 10)\n" |
|
| 184 |
-" -p, --prompt=PROMPT Input prompt (default '> ')\n" |
|
| 185 |
-" -e, --show-matches=QUERY Output the sorted matches of QUERY\n" |
|
| 186 |
-" -t, --tty=TTY Specify file to use as TTY device (default /dev/tty)\n" |
|
| 187 |
-" -s, --show-scores Show the scores of each match\n" |
|
| 188 |
-" -h, --help Display this help and exit\n" |
|
| 189 |
-" -v, --version Output version information and exit\n"; |
|
| 181 |
+static const char *usage_str = |
|
| 182 |
+ "" |
|
| 183 |
+ "Usage: fzy [OPTION]...\n" |
|
| 184 |
+ " -l, --lines=LINES Specify how many lines of results to show (default 10)\n" |
|
| 185 |
+ " -p, --prompt=PROMPT Input prompt (default '> ')\n" |
|
| 186 |
+ " -e, --show-matches=QUERY Output the sorted matches of QUERY\n" |
|
| 187 |
+ " -t, --tty=TTY Specify file to use as TTY device (default /dev/tty)\n" |
|
| 188 |
+ " -s, --show-scores Show the scores of each match\n" |
|
| 189 |
+ " -h, --help Display this help and exit\n" |
|
| 190 |
+ " -v, --version Output version information and exit\n"; |
|
| 190 | 191 |
|
| 191 |
-void usage(const char *argv0){
|
|
| 192 |
+void usage(const char *argv0) {
|
|
| 192 | 193 |
fprintf(stderr, usage_str, argv0); |
| 193 | 194 |
} |
| 194 | 195 |
|
| 195 |
-static struct option longopts[] = {
|
|
| 196 |
- { "show-matches", required_argument, NULL, 'e' },
|
|
| 197 |
- { "lines", required_argument, NULL, 'l' },
|
|
| 198 |
- { "tty", required_argument, NULL, 't' },
|
|
| 199 |
- { "prompt", required_argument, NULL, 'p' },
|
|
| 200 |
- { "show-scores", no_argument, NULL, 's' },
|
|
| 201 |
- { "version", no_argument, NULL, 'v' },
|
|
| 202 |
- { "benchmark", no_argument, NULL, 'b' },
|
|
| 203 |
- { "help", no_argument, NULL, 'h' },
|
|
| 204 |
- { NULL, 0, NULL, 0 }
|
|
| 205 |
-}; |
|
| 196 |
+static struct option longopts[] = {{"show-matches", required_argument, NULL, 'e'},
|
|
| 197 |
+ {"lines", required_argument, NULL, 'l'},
|
|
| 198 |
+ {"tty", required_argument, NULL, 't'},
|
|
| 199 |
+ {"prompt", required_argument, NULL, 'p'},
|
|
| 200 |
+ {"show-scores", no_argument, NULL, 's'},
|
|
| 201 |
+ {"version", no_argument, NULL, 'v'},
|
|
| 202 |
+ {"benchmark", no_argument, NULL, 'b'},
|
|
| 203 |
+ {"help", no_argument, NULL, 'h'},
|
|
| 204 |
+ {NULL, 0, NULL, 0}};
|
|
| 206 | 205 |
|
| 207 |
-int main(int argc, char *argv[]){
|
|
| 206 |
+int main(int argc, char *argv[]) {
|
|
| 208 | 207 |
int benchmark = 0; |
| 209 | 208 |
const char *initial_query = NULL; |
| 210 | 209 |
const char *tty_filename = "/dev/tty"; |
| 211 | 210 |
char c; |
| 212 |
- while((c = getopt_long(argc, argv, "vhse:l:t:p:", longopts, NULL)) != -1){
|
|
| 213 |
- switch(c){
|
|
| 211 |
+ while ((c = getopt_long(argc, argv, "vhse:l:t:p:", longopts, NULL)) != -1) {
|
|
| 212 |
+ switch (c) {
|
|
| 214 | 213 |
case 'v': |
| 215 | 214 |
printf("%s " VERSION " (c) 2014 John Hawthorn\n", argv[0]);
|
| 216 | 215 |
exit(EXIT_SUCCESS); |
| ... | ... |
@@ -229,27 +228,25 @@ int main(int argc, char *argv[]){
|
| 229 | 228 |
case 'p': |
| 230 | 229 |
prompt = optarg; |
| 231 | 230 |
break; |
| 232 |
- case 'l': |
|
| 233 |
- {
|
|
| 234 |
- int l; |
|
| 235 |
- if(!strcmp(optarg, "max")){
|
|
| 236 |
- l = INT_MAX; |
|
| 237 |
- }else if(sscanf(optarg, "%d", &l) != 1 || l < 3){
|
|
| 238 |
- fprintf(stderr, "Invalid format for --lines: %s\n", optarg); |
|
| 239 |
- fprintf(stderr, "Must be integer in range 3..\n"); |
|
| 240 |
- usage(argv[0]); |
|
| 241 |
- exit(EXIT_FAILURE); |
|
| 242 |
- } |
|
| 243 |
- num_lines = l; |
|
| 231 |
+ case 'l': {
|
|
| 232 |
+ int l; |
|
| 233 |
+ if (!strcmp(optarg, "max")) {
|
|
| 234 |
+ l = INT_MAX; |
|
| 235 |
+ } else if (sscanf(optarg, "%d", &l) != 1 || l < 3) {
|
|
| 236 |
+ fprintf(stderr, "Invalid format for --lines: %s\n", optarg); |
|
| 237 |
+ fprintf(stderr, "Must be integer in range 3..\n"); |
|
| 238 |
+ usage(argv[0]); |
|
| 239 |
+ exit(EXIT_FAILURE); |
|
| 244 | 240 |
} |
| 245 |
- break; |
|
| 241 |
+ num_lines = l; |
|
| 242 |
+ } break; |
|
| 246 | 243 |
case 'h': |
| 247 | 244 |
default: |
| 248 | 245 |
usage(argv[0]); |
| 249 | 246 |
exit(EXIT_SUCCESS); |
| 250 | 247 |
} |
| 251 | 248 |
} |
| 252 |
- if(optind != argc){
|
|
| 249 |
+ if (optind != argc) {
|
|
| 253 | 250 |
usage(argv[0]); |
| 254 | 251 |
exit(EXIT_FAILURE); |
| 255 | 252 |
} |
| ... | ... |
@@ -258,29 +255,29 @@ int main(int argc, char *argv[]){
|
| 258 | 255 |
choices_init(&choices); |
| 259 | 256 |
read_choices(&choices); |
| 260 | 257 |
|
| 261 |
- if(num_lines > choices.size) |
|
| 258 |
+ if (num_lines > choices.size) |
|
| 262 | 259 |
num_lines = choices.size; |
| 263 | 260 |
|
| 264 |
- if(benchmark){
|
|
| 265 |
- if(!initial_query){
|
|
| 261 |
+ if (benchmark) {
|
|
| 262 |
+ if (!initial_query) {
|
|
| 266 | 263 |
fprintf(stderr, "Must specify -e/--show-matches with --benchmark\n"); |
| 267 | 264 |
exit(EXIT_FAILURE); |
| 268 | 265 |
} |
| 269 |
- for(int i = 0; i < 100; i++) |
|
| 266 |
+ for (int i = 0; i < 100; i++) |
|
| 270 | 267 |
choices_search(&choices, initial_query); |
| 271 |
- }else if(initial_query){
|
|
| 268 |
+ } else if (initial_query) {
|
|
| 272 | 269 |
choices_search(&choices, initial_query); |
| 273 |
- for(size_t i = 0; i < choices_available(&choices); i++){
|
|
| 274 |
- if(flag_show_scores) |
|
| 270 |
+ for (size_t i = 0; i < choices_available(&choices); i++) {
|
|
| 271 |
+ if (flag_show_scores) |
|
| 275 | 272 |
printf("%f\t", choices_getscore(&choices, i));
|
| 276 | 273 |
printf("%s\n", choices_get(&choices, i));
|
| 277 | 274 |
} |
| 278 |
- }else{
|
|
| 275 |
+ } else {
|
|
| 279 | 276 |
/* interactive */ |
| 280 | 277 |
tty_t tty; |
| 281 | 278 |
tty_init(&tty, tty_filename); |
| 282 | 279 |
|
| 283 |
- if(num_lines + 1 > tty_getheight(&tty)) |
|
| 280 |
+ if (num_lines + 1 > tty_getheight(&tty)) |
|
| 284 | 281 |
num_lines = tty_getheight(&tty) - 1; |
| 285 | 282 |
|
| 286 | 283 |
run(&tty, &choices); |
| ... | ... |
@@ -7,22 +7,23 @@ |
| 7 | 7 |
|
| 8 | 8 |
int testsrun = 0, testsfailed = 0, assertionsrun = 0; |
| 9 | 9 |
|
| 10 |
-#define assert(x) \ |
|
| 11 |
- if(++assertionsrun && !(x)){ \
|
|
| 12 |
- fprintf(stderr, "test \"%s\" failed\n assert(%s) was false\n at %s:%i\n\n", __func__, #x, __FILE__ ,__LINE__); \ |
|
| 13 |
- raise(SIGTRAP); \ |
|
| 14 |
- testsfailed++; \ |
|
| 15 |
- return; \ |
|
| 10 |
+#define assert(x) \ |
|
| 11 |
+ if (++assertionsrun && !(x)) { \
|
|
| 12 |
+ fprintf(stderr, "test \"%s\" failed\n assert(%s) was false\n at %s:%i\n\n", \ |
|
| 13 |
+ __func__, #x, __FILE__, __LINE__); \ |
|
| 14 |
+ raise(SIGTRAP); \ |
|
| 15 |
+ testsfailed++; \ |
|
| 16 |
+ return; \ |
|
| 16 | 17 |
} |
| 17 | 18 |
|
| 18 | 19 |
#define assert_streq(a, b) assert(!strcmp(a, b)) |
| 19 | 20 |
|
| 20 |
-void runtest(void (*test)()){
|
|
| 21 |
+void runtest(void (*test)()) {
|
|
| 21 | 22 |
testsrun++; |
| 22 | 23 |
test(); |
| 23 | 24 |
} |
| 24 | 25 |
|
| 25 |
-void test_match(){
|
|
| 26 |
+void test_match() {
|
|
| 26 | 27 |
assert(has_match("a", "a"));
|
| 27 | 28 |
assert(has_match("a", "ab"));
|
| 28 | 29 |
assert(has_match("a", "ba"));
|
| ... | ... |
@@ -38,7 +39,7 @@ void test_match(){
|
| 38 | 39 |
assert(has_match("", "a"));
|
| 39 | 40 |
} |
| 40 | 41 |
|
| 41 |
-void test_scoring(){
|
|
| 42 |
+void test_scoring() {
|
|
| 42 | 43 |
/* App/Models/Order is better than App/MOdels/zRder */ |
| 43 | 44 |
assert(match("amor", "app/models/order") > match("amor", "app/models/zrder"));
|
| 44 | 45 |
|
| ... | ... |
@@ -65,7 +66,7 @@ void test_scoring(){
|
| 65 | 66 |
assert(match("abc", " a b c ") > match("abc", " a b c "));
|
| 66 | 67 |
} |
| 67 | 68 |
|
| 68 |
-void test_positions_1(){
|
|
| 69 |
+void test_positions_1() {
|
|
| 69 | 70 |
size_t positions[3]; |
| 70 | 71 |
match_positions("amo", "app/models/foo", positions);
|
| 71 | 72 |
assert(positions[0] == 0); |
| ... | ... |
@@ -73,7 +74,7 @@ void test_positions_1(){
|
| 73 | 74 |
assert(positions[2] == 5); |
| 74 | 75 |
} |
| 75 | 76 |
|
| 76 |
-void test_positions_2(){
|
|
| 77 |
+void test_positions_2() {
|
|
| 77 | 78 |
/* |
| 78 | 79 |
* We should prefer matching the 'o' in order, since it's the beginning |
| 79 | 80 |
* of a word. |
| ... | ... |
@@ -85,21 +86,21 @@ void test_positions_2(){
|
| 85 | 86 |
assert(positions[2] == 11); |
| 86 | 87 |
} |
| 87 | 88 |
|
| 88 |
-void test_positions_3(){
|
|
| 89 |
+void test_positions_3() {
|
|
| 89 | 90 |
size_t positions[2]; |
| 90 | 91 |
match_positions("as", "tags", positions);
|
| 91 | 92 |
assert(positions[0] == 1); |
| 92 | 93 |
assert(positions[1] == 3); |
| 93 | 94 |
} |
| 94 | 95 |
|
| 95 |
-void test_positions_4(){
|
|
| 96 |
+void test_positions_4() {
|
|
| 96 | 97 |
size_t positions[2]; |
| 97 | 98 |
match_positions("as", "examples.txt", positions);
|
| 98 | 99 |
assert(positions[0] == 2); |
| 99 | 100 |
assert(positions[1] == 7); |
| 100 | 101 |
} |
| 101 | 102 |
|
| 102 |
-void test_positions_exact(){
|
|
| 103 |
+void test_positions_exact() {
|
|
| 103 | 104 |
size_t positions[3]; |
| 104 | 105 |
match_positions("foo", "foo", positions);
|
| 105 | 106 |
assert(positions[0] == 0); |
| ... | ... |
@@ -107,7 +108,7 @@ void test_positions_exact(){
|
| 107 | 108 |
assert(positions[2] == 2); |
| 108 | 109 |
} |
| 109 | 110 |
|
| 110 |
-void test_choices_empty(){
|
|
| 111 |
+void test_choices_empty() {
|
|
| 111 | 112 |
choices_t choices; |
| 112 | 113 |
choices_init(&choices); |
| 113 | 114 |
assert(choices.size == 0); |
| ... | ... |
@@ -123,7 +124,7 @@ void test_choices_empty(){
|
| 123 | 124 |
choices_free(&choices); |
| 124 | 125 |
} |
| 125 | 126 |
|
| 126 |
-void test_choices_1(){
|
|
| 127 |
+void test_choices_1() {
|
|
| 127 | 128 |
choices_t choices; |
| 128 | 129 |
choices_init(&choices); |
| 129 | 130 |
choices_add(&choices, "tags"); |
| ... | ... |
@@ -148,7 +149,7 @@ void test_choices_1(){
|
| 148 | 149 |
choices_free(&choices); |
| 149 | 150 |
} |
| 150 | 151 |
|
| 151 |
-void test_choices_2(){
|
|
| 152 |
+void test_choices_2() {
|
|
| 152 | 153 |
choices_t choices; |
| 153 | 154 |
choices_init(&choices); |
| 154 | 155 |
choices_add(&choices, "tags"); |
| ... | ... |
@@ -198,7 +199,7 @@ void test_choices_2(){
|
| 198 | 199 |
choices_free(&choices); |
| 199 | 200 |
} |
| 200 | 201 |
|
| 201 |
-void test_choices_without_search(){
|
|
| 202 |
+void test_choices_without_search() {
|
|
| 202 | 203 |
/* Before a search is run, it should return no results */ |
| 203 | 204 |
|
| 204 | 205 |
choices_t choices; |
| ... | ... |
@@ -219,17 +220,17 @@ void test_choices_without_search(){
|
| 219 | 220 |
choices_free(&choices); |
| 220 | 221 |
} |
| 221 | 222 |
|
| 222 |
-void summary(){
|
|
| 223 |
+void summary() {
|
|
| 223 | 224 |
printf("%i tests, %i assertions, %i failures\n", testsrun, assertionsrun, testsfailed);
|
| 224 | 225 |
} |
| 225 | 226 |
|
| 226 |
-static void ignore_signal(int signum){
|
|
| 227 |
- (void) signum; |
|
| 227 |
+static void ignore_signal(int signum) {
|
|
| 228 |
+ (void)signum; |
|
| 228 | 229 |
} |
| 229 | 230 |
|
| 230 |
-int main(int argc, char *argv[]){
|
|
| 231 |
- (void) argc; |
|
| 232 |
- (void) argv; |
|
| 231 |
+int main(int argc, char *argv[]) {
|
|
| 232 |
+ (void)argc; |
|
| 233 |
+ (void)argv; |
|
| 233 | 234 |
|
| 234 | 235 |
/* We raise sigtrap on all assertion failures. |
| 235 | 236 |
* If we have no debugger running, we should ignore it */ |
| ... | ... |
@@ -9,16 +9,16 @@ |
| 9 | 9 |
|
| 10 | 10 |
#include "config.h" |
| 11 | 11 |
|
| 12 |
-char *strcasechr(const char *s, char c){
|
|
| 12 |
+char *strcasechr(const char *s, char c) {
|
|
| 13 | 13 |
const char accept[3] = {c, toupper(c), 0};
|
| 14 | 14 |
return strpbrk(s, accept); |
| 15 | 15 |
} |
| 16 | 16 |
|
| 17 |
-int has_match(const char *needle, const char *haystack){
|
|
| 18 |
- while(*needle){
|
|
| 17 |
+int has_match(const char *needle, const char *haystack) {
|
|
| 18 |
+ while (*needle) {
|
|
| 19 | 19 |
char nch = *needle++; |
| 20 | 20 |
|
| 21 |
- if(!(haystack = strcasechr(haystack, nch))){
|
|
| 21 |
+ if (!(haystack = strcasechr(haystack, nch))) {
|
|
| 22 | 22 |
return 0; |
| 23 | 23 |
} |
| 24 | 24 |
haystack++; |
| ... | ... |
@@ -29,22 +29,22 @@ int has_match(const char *needle, const char *haystack){
|
| 29 | 29 |
#define max(a, b) (((a) > (b)) ? (a) : (b)) |
| 30 | 30 |
|
| 31 | 31 |
/* print one of the internal matrices */ |
| 32 |
-void mat_print(score_t *mat, const char *needle, const char *haystack){
|
|
| 32 |
+void mat_print(score_t *mat, const char *needle, const char *haystack) {
|
|
| 33 | 33 |
int n = strlen(needle); |
| 34 | 34 |
int m = strlen(haystack); |
| 35 | 35 |
int i, j; |
| 36 | 36 |
fprintf(stderr, " "); |
| 37 |
- for(j = 0; j < m; j++){
|
|
| 37 |
+ for (j = 0; j < m; j++) {
|
|
| 38 | 38 |
fprintf(stderr, " %c", haystack[j]); |
| 39 | 39 |
} |
| 40 | 40 |
fprintf(stderr, "\n"); |
| 41 |
- for(i = 0; i < n; i++){
|
|
| 41 |
+ for (i = 0; i < n; i++) {
|
|
| 42 | 42 |
fprintf(stderr, " %c |", needle[i]); |
| 43 |
- for(j = 0; j < m; j++){
|
|
| 44 |
- score_t val = mat[i*m + j]; |
|
| 45 |
- if(val == SCORE_MIN){
|
|
| 43 |
+ for (j = 0; j < m; j++) {
|
|
| 44 |
+ score_t val = mat[i * m + j]; |
|
| 45 |
+ if (val == SCORE_MIN) {
|
|
| 46 | 46 |
fprintf(stderr, " -inf"); |
| 47 |
- }else{
|
|
| 47 |
+ } else {
|
|
| 48 | 48 |
fprintf(stderr, " % .2f", val); |
| 49 | 49 |
} |
| 50 | 50 |
} |
| ... | ... |
@@ -53,14 +53,14 @@ void mat_print(score_t *mat, const char *needle, const char *haystack){
|
| 53 | 53 |
fprintf(stderr, "\n\n"); |
| 54 | 54 |
} |
| 55 | 55 |
|
| 56 |
-score_t calculate_score(const char *needle, const char *haystack, size_t *positions){
|
|
| 57 |
- if(!*haystack || !*needle) |
|
| 56 |
+score_t calculate_score(const char *needle, const char *haystack, size_t *positions) {
|
|
| 57 |
+ if (!*haystack || !*needle) |
|
| 58 | 58 |
return SCORE_MIN; |
| 59 | 59 |
|
| 60 | 60 |
int n = strlen(needle); |
| 61 | 61 |
int m = strlen(haystack); |
| 62 | 62 |
|
| 63 |
- if(m > 1024){
|
|
| 63 |
+ if (m > 1024) {
|
|
| 64 | 64 |
/* |
| 65 | 65 |
* Unreasonably large candidate: return no score |
| 66 | 66 |
* If it is a valid match it will still be returned, it will |
| ... | ... |
@@ -79,23 +79,20 @@ score_t calculate_score(const char *needle, const char *haystack, size_t *positi |
| 79 | 79 |
|
| 80 | 80 |
/* Which positions are beginning of words */ |
| 81 | 81 |
char last_ch = '\0'; |
| 82 |
- for(int i = 0; i < m; i++){
|
|
| 82 |
+ for (int i = 0; i < m; i++) {
|
|
| 83 | 83 |
char ch = haystack[i]; |
| 84 | 84 |
|
| 85 | 85 |
score_t score = 0; |
| 86 |
- if(isalnum(ch)){
|
|
| 87 |
- if(!last_ch || last_ch == '/'){
|
|
| 86 |
+ if (isalnum(ch)) {
|
|
| 87 |
+ if (!last_ch || last_ch == '/') {
|
|
| 88 | 88 |
score = SCORE_MATCH_SLASH; |
| 89 |
- }else if(last_ch == '-' || |
|
| 90 |
- last_ch == '_' || |
|
| 91 |
- last_ch == ' ' || |
|
| 92 |
- (last_ch >= '0' && last_ch <= '9')){
|
|
| 89 |
+ } else if (last_ch == '-' || last_ch == '_' || last_ch == ' ' || |
|
| 90 |
+ (last_ch >= '0' && last_ch <= '9')) {
|
|
| 93 | 91 |
score = SCORE_MATCH_WORD; |
| 94 |
- }else if(last_ch >= 'a' && last_ch <= 'z' && |
|
| 95 |
- ch >= 'A' && ch <= 'Z'){
|
|
| 92 |
+ } else if (last_ch >= 'a' && last_ch <= 'z' && ch >= 'A' && ch <= 'Z') {
|
|
| 96 | 93 |
/* CamelCase */ |
| 97 | 94 |
score = SCORE_MATCH_CAPITAL; |
| 98 |
- }else if(last_ch == '.'){
|
|
| 95 |
+ } else if (last_ch == '.') {
|
|
| 99 | 96 |
score = SCORE_MATCH_DOT; |
| 100 | 97 |
} |
| 101 | 98 |
} |
| ... | ... |
@@ -104,21 +101,20 @@ score_t calculate_score(const char *needle, const char *haystack, size_t *positi |
| 104 | 101 |
last_ch = ch; |
| 105 | 102 |
} |
| 106 | 103 |
|
| 107 |
- for(int i = 0; i < n; i++){
|
|
| 104 |
+ for (int i = 0; i < n; i++) {
|
|
| 108 | 105 |
score_t prev_score = SCORE_MIN; |
| 109 |
- score_t gap_score = i == n-1 ? SCORE_GAP_TRAILING : SCORE_GAP_INNER; |
|
| 110 |
- for(int j = 0; j < m; j++){
|
|
| 106 |
+ score_t gap_score = i == n - 1 ? SCORE_GAP_TRAILING : SCORE_GAP_INNER; |
|
| 107 |
+ for (int j = 0; j < m; j++) {
|
|
| 111 | 108 |
score_t score = SCORE_MIN; |
| 112 |
- if(tolower(needle[i]) == tolower(haystack[j])){
|
|
| 113 |
- if(!i){
|
|
| 109 |
+ if (tolower(needle[i]) == tolower(haystack[j])) {
|
|
| 110 |
+ if (!i) {
|
|
| 114 | 111 |
score = (j * SCORE_GAP_LEADING) + match_bonus[j]; |
| 115 |
- }else if(j){
|
|
| 112 |
+ } else if (j) {
|
|
| 116 | 113 |
score = max( |
| 117 |
- M[i-1][j-1] + match_bonus[j], |
|
| 114 |
+ M[i - 1][j - 1] + match_bonus[j], |
|
| 118 | 115 |
|
| 119 |
- /* consecutive match, doesn't stack with match_bonus */ |
|
| 120 |
- D[i-1][j-1] + SCORE_MATCH_CONSECUTIVE |
|
| 121 |
- ); |
|
| 116 |
+ /* consecutive match, doesn't stack with match_bonus */ |
|
| 117 |
+ D[i - 1][j - 1] + SCORE_MATCH_CONSECUTIVE); |
|
| 122 | 118 |
} |
| 123 | 119 |
} |
| 124 | 120 |
D[i][j] = score; |
| ... | ... |
@@ -134,10 +130,10 @@ score_t calculate_score(const char *needle, const char *haystack, size_t *positi |
| 134 | 130 |
#endif |
| 135 | 131 |
|
| 136 | 132 |
/* backtrace to find the positions of optimal matching */ |
| 137 |
- if(positions){
|
|
| 133 |
+ if (positions) {
|
|
| 138 | 134 |
int match_required = 0; |
| 139 |
- for(int i = n-1, j = m-1; i >= 0; i--){
|
|
| 140 |
- for(; j >= 0; j--){
|
|
| 135 |
+ for (int i = n - 1, j = m - 1; i >= 0; i--) {
|
|
| 136 |
+ for (; j >= 0; j--) {
|
|
| 141 | 137 |
/* |
| 142 | 138 |
* There may be multiple paths which result in |
| 143 | 139 |
* the optimal weight. |
| ... | ... |
@@ -146,12 +142,15 @@ score_t calculate_score(const char *needle, const char *haystack, size_t *positi |
| 146 | 142 |
* we encounter, the latest in the candidate |
| 147 | 143 |
* string. |
| 148 | 144 |
*/ |
| 149 |
- if(D[i][j] != SCORE_MIN && (match_required || D[i][j] == M[i][j])){
|
|
| 145 |
+ if (D[i][j] != SCORE_MIN && |
|
| 146 |
+ (match_required || D[i][j] == M[i][j])) {
|
|
| 150 | 147 |
/* If this score was determined using |
| 151 | 148 |
* SCORE_MATCH_CONSECUTIVE, the |
| 152 | 149 |
* previous character MUST be a match |
| 153 | 150 |
*/ |
| 154 |
- match_required = i && j && M[i][j] == D[i-1][j-1] + SCORE_MATCH_CONSECUTIVE; |
|
| 151 |
+ match_required = |
|
| 152 |
+ i && j && |
|
| 153 |
+ M[i][j] == D[i - 1][j - 1] + SCORE_MATCH_CONSECUTIVE; |
|
| 155 | 154 |
positions[i] = j--; |
| 156 | 155 |
break; |
| 157 | 156 |
} |
| ... | ... |
@@ -159,24 +158,24 @@ score_t calculate_score(const char *needle, const char *haystack, size_t *positi |
| 159 | 158 |
} |
| 160 | 159 |
} |
| 161 | 160 |
|
| 162 |
- return M[n-1][m-1]; |
|
| 161 |
+ return M[n - 1][m - 1]; |
|
| 163 | 162 |
} |
| 164 | 163 |
|
| 165 |
-score_t match_positions(const char *needle, const char *haystack, size_t *positions){
|
|
| 166 |
- if(!*needle){
|
|
| 164 |
+score_t match_positions(const char *needle, const char *haystack, size_t *positions) {
|
|
| 165 |
+ if (!*needle) {
|
|
| 167 | 166 |
return SCORE_MAX; |
| 168 |
- }else if(!strcasecmp(needle, haystack)){
|
|
| 169 |
- if(positions){
|
|
| 167 |
+ } else if (!strcasecmp(needle, haystack)) {
|
|
| 168 |
+ if (positions) {
|
|
| 170 | 169 |
int n = strlen(needle); |
| 171 |
- for(int i = 0; i < n; i++) |
|
| 170 |
+ for (int i = 0; i < n; i++) |
|
| 172 | 171 |
positions[i] = i; |
| 173 | 172 |
} |
| 174 | 173 |
return SCORE_MAX; |
| 175 |
- }else{
|
|
| 174 |
+ } else {
|
|
| 176 | 175 |
return calculate_score(needle, haystack, positions); |
| 177 | 176 |
} |
| 178 | 177 |
} |
| 179 | 178 |
|
| 180 |
-score_t match(const char *needle, const char *haystack){
|
|
| 179 |
+score_t match(const char *needle, const char *haystack) {
|
|
| 181 | 180 |
return match_positions(needle, haystack, NULL); |
| 182 | 181 |
} |
| ... | ... |
@@ -8,23 +8,23 @@ |
| 8 | 8 |
|
| 9 | 9 |
#include "tty.h" |
| 10 | 10 |
|
| 11 |
-void tty_reset(tty_t *tty){
|
|
| 11 |
+void tty_reset(tty_t *tty) {
|
|
| 12 | 12 |
tcsetattr(tty->fdin, TCSANOW, &tty->original_termios); |
| 13 | 13 |
} |
| 14 | 14 |
|
| 15 |
-void tty_close(tty_t *tty){
|
|
| 15 |
+void tty_close(tty_t *tty) {
|
|
| 16 | 16 |
tty_reset(tty); |
| 17 | 17 |
fclose(tty->fout); |
| 18 | 18 |
close(tty->fdin); |
| 19 | 19 |
} |
| 20 | 20 |
|
| 21 |
-void tty_init(tty_t *tty, const char *tty_filename){
|
|
| 21 |
+void tty_init(tty_t *tty, const char *tty_filename) {
|
|
| 22 | 22 |
tty->fdin = open(tty_filename, O_RDONLY); |
| 23 | 23 |
tty->fout = fopen(tty_filename, "w"); |
| 24 |
- if(setvbuf(tty->fout, NULL, _IOFBF, 4096)) |
|
| 24 |
+ if (setvbuf(tty->fout, NULL, _IOFBF, 4096)) |
|
| 25 | 25 |
perror("setvbuf");
|
| 26 | 26 |
|
| 27 |
- if(tcgetattr(tty->fdin, &tty->original_termios)) |
|
| 27 |
+ if (tcgetattr(tty->fdin, &tty->original_termios)) |
|
| 28 | 28 |
perror("tcgetattr");
|
| 29 | 29 |
|
| 30 | 30 |
struct termios new_termios = tty->original_termios; |
| ... | ... |
@@ -37,7 +37,7 @@ void tty_init(tty_t *tty, const char *tty_filename){
|
| 37 | 37 |
*/ |
| 38 | 38 |
new_termios.c_lflag &= ~(ICANON | ECHO | ISIG); |
| 39 | 39 |
|
| 40 |
- if(tcsetattr(tty->fdin, TCSANOW, &new_termios)) |
|
| 40 |
+ if (tcsetattr(tty->fdin, TCSANOW, &new_termios)) |
|
| 41 | 41 |
perror("tcsetattr");
|
| 42 | 42 |
|
| 43 | 43 |
tty_getwinsz(tty); |
| ... | ... |
@@ -45,82 +45,82 @@ void tty_init(tty_t *tty, const char *tty_filename){
|
| 45 | 45 |
tty_setnormal(tty); |
| 46 | 46 |
} |
| 47 | 47 |
|
| 48 |
-void tty_getwinsz(tty_t *tty){
|
|
| 48 |
+void tty_getwinsz(tty_t *tty) {
|
|
| 49 | 49 |
struct winsize ws; |
| 50 |
- if(ioctl(fileno(tty->fout), TIOCGWINSZ, &ws) == -1){
|
|
| 50 |
+ if (ioctl(fileno(tty->fout), TIOCGWINSZ, &ws) == -1) {
|
|
| 51 | 51 |
tty->maxwidth = 80; |
| 52 | 52 |
tty->maxheight = 25; |
| 53 |
- }else{
|
|
| 53 |
+ } else {
|
|
| 54 | 54 |
tty->maxwidth = ws.ws_col; |
| 55 | 55 |
tty->maxheight = ws.ws_row; |
| 56 | 56 |
} |
| 57 | 57 |
} |
| 58 | 58 |
|
| 59 |
-char tty_getchar(tty_t *tty){
|
|
| 59 |
+char tty_getchar(tty_t *tty) {
|
|
| 60 | 60 |
char ch; |
| 61 | 61 |
int size = read(tty->fdin, &ch, 1); |
| 62 |
- if(size < 0){
|
|
| 62 |
+ if (size < 0) {
|
|
| 63 | 63 |
perror("error reading from tty");
|
| 64 | 64 |
exit(EXIT_FAILURE); |
| 65 |
- }else if(size == 0){
|
|
| 65 |
+ } else if (size == 0) {
|
|
| 66 | 66 |
/* EOF */ |
| 67 | 67 |
exit(EXIT_FAILURE); |
| 68 |
- }else{
|
|
| 68 |
+ } else {
|
|
| 69 | 69 |
return ch; |
| 70 | 70 |
} |
| 71 | 71 |
} |
| 72 | 72 |
|
| 73 |
-static void tty_sgr(tty_t *tty, int code){
|
|
| 73 |
+static void tty_sgr(tty_t *tty, int code) {
|
|
| 74 | 74 |
tty_printf(tty, "%c%c%im", 0x1b, '[', code); |
| 75 | 75 |
} |
| 76 | 76 |
|
| 77 |
-void tty_setfg(tty_t *tty, int fg){
|
|
| 78 |
- if(tty->fgcolor != fg){
|
|
| 77 |
+void tty_setfg(tty_t *tty, int fg) {
|
|
| 78 |
+ if (tty->fgcolor != fg) {
|
|
| 79 | 79 |
tty_sgr(tty, 30 + fg); |
| 80 | 80 |
tty->fgcolor = fg; |
| 81 | 81 |
} |
| 82 | 82 |
} |
| 83 | 83 |
|
| 84 |
-void tty_setinvert(tty_t *tty){
|
|
| 84 |
+void tty_setinvert(tty_t *tty) {
|
|
| 85 | 85 |
tty_sgr(tty, 7); |
| 86 | 86 |
} |
| 87 | 87 |
|
| 88 |
-void tty_setnormal(tty_t *tty){
|
|
| 88 |
+void tty_setnormal(tty_t *tty) {
|
|
| 89 | 89 |
tty_sgr(tty, 0); |
| 90 | 90 |
tty->fgcolor = 9; |
| 91 | 91 |
} |
| 92 | 92 |
|
| 93 |
-void tty_newline(tty_t *tty){
|
|
| 93 |
+void tty_newline(tty_t *tty) {
|
|
| 94 | 94 |
tty_printf(tty, "%c%cK\n", 0x1b, '['); |
| 95 | 95 |
} |
| 96 | 96 |
|
| 97 |
-void tty_clearline(tty_t *tty){
|
|
| 97 |
+void tty_clearline(tty_t *tty) {
|
|
| 98 | 98 |
tty_printf(tty, "%c%cK", 0x1b, '['); |
| 99 | 99 |
} |
| 100 | 100 |
|
| 101 |
-void tty_setcol(tty_t *tty, int col){
|
|
| 101 |
+void tty_setcol(tty_t *tty, int col) {
|
|
| 102 | 102 |
tty_printf(tty, "%c%c%iG", 0x1b, '[', col + 1); |
| 103 | 103 |
} |
| 104 | 104 |
|
| 105 |
-void tty_moveup(tty_t *tty, int i){
|
|
| 105 |
+void tty_moveup(tty_t *tty, int i) {
|
|
| 106 | 106 |
tty_printf(tty, "%c%c%iA", 0x1b, '[', i); |
| 107 | 107 |
} |
| 108 | 108 |
|
| 109 |
-void tty_printf(tty_t *tty, const char *fmt, ...){
|
|
| 109 |
+void tty_printf(tty_t *tty, const char *fmt, ...) {
|
|
| 110 | 110 |
va_list args; |
| 111 | 111 |
va_start(args, fmt); |
| 112 | 112 |
vfprintf(tty->fout, fmt, args); |
| 113 | 113 |
va_end(args); |
| 114 | 114 |
} |
| 115 | 115 |
|
| 116 |
-void tty_flush(tty_t *tty){
|
|
| 116 |
+void tty_flush(tty_t *tty) {
|
|
| 117 | 117 |
fflush(tty->fout); |
| 118 | 118 |
} |
| 119 | 119 |
|
| 120 |
-size_t tty_getwidth(tty_t *tty){
|
|
| 120 |
+size_t tty_getwidth(tty_t *tty) {
|
|
| 121 | 121 |
return tty->maxwidth; |
| 122 | 122 |
} |
| 123 | 123 |
|
| 124 |
-size_t tty_getheight(tty_t *tty){
|
|
| 124 |
+size_t tty_getheight(tty_t *tty) {
|
|
| 125 | 125 |
return tty->maxheight; |
| 126 | 126 |
} |
| ... | ... |
@@ -3,7 +3,7 @@ |
| 3 | 3 |
|
| 4 | 4 |
#include <termios.h> |
| 5 | 5 |
|
| 6 |
-typedef struct{
|
|
| 6 |
+typedef struct {
|
|
| 7 | 7 |
int fdin; |
| 8 | 8 |
FILE *fout; |
| 9 | 9 |
struct termios original_termios; |
| ... | ... |
@@ -22,15 +22,15 @@ void tty_setfg(tty_t *tty, int fg); |
| 22 | 22 |
void tty_setinvert(tty_t *tty); |
| 23 | 23 |
void tty_setnormal(tty_t *tty); |
| 24 | 24 |
|
| 25 |
-#define TTY_COLOR_BLACK 0 |
|
| 26 |
-#define TTY_COLOR_RED 1 |
|
| 27 |
-#define TTY_COLOR_GREEN 2 |
|
| 28 |
-#define TTY_COLOR_YELLOW 3 |
|
| 29 |
-#define TTY_COLOR_BLUE 4 |
|
| 25 |
+#define TTY_COLOR_BLACK 0 |
|
| 26 |
+#define TTY_COLOR_RED 1 |
|
| 27 |
+#define TTY_COLOR_GREEN 2 |
|
| 28 |
+#define TTY_COLOR_YELLOW 3 |
|
| 29 |
+#define TTY_COLOR_BLUE 4 |
|
| 30 | 30 |
#define TTY_COLOR_MAGENTA 5 |
| 31 |
-#define TTY_COLOR_CYAN 6 |
|
| 32 |
-#define TTY_COLOR_WHITE 7 |
|
| 33 |
-#define TTY_COLOR_NORMAL 9 |
|
| 31 |
+#define TTY_COLOR_CYAN 6 |
|
| 32 |
+#define TTY_COLOR_WHITE 7 |
|
| 33 |
+#define TTY_COLOR_NORMAL 9 |
|
| 34 | 34 |
|
| 35 | 35 |
/* tty_newline |
| 36 | 36 |
* Move cursor to the beginning of the next line, clearing to the end of the |