summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Hawthorn <john.hawthorn@gmail.com>2015-11-06 21:45:02 -0800
committerJohn Hawthorn <john.hawthorn@gmail.com>2015-11-07 18:19:14 -0800
commitafef1c979c0955a7bced155ea91984acda067429 (patch)
tree14af5919cf5ff24280cf49d16d4b1ead58599861
parent4ad97c86ab6de5890b07f15751f96a96587a234e (diff)
Apply clang-format to all files
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.
-rw-r--r--.clang-format6
-rw-r--r--Makefile5
-rw-r--r--choices.c45
-rw-r--r--config.h14
-rw-r--r--fzy.c177
-rw-r--r--fzytest.c49
-rw-r--r--match.c93
-rw-r--r--tty.c52
-rw-r--r--tty.h18
9 files changed, 232 insertions, 227 deletions
diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..c429e97
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,6 @@
+IndentWidth: 8
+UseTab: Always
+BreakBeforeBraces: Attach
+IndentCaseLabels: true
+AllowShortFunctionsOnASingleLine: false
+ColumnLimit: 100
diff --git a/Makefile b/Makefile
index 8ad1f7e..9018404 100644
--- a/Makefile
+++ b/Makefile
@@ -31,7 +31,10 @@ install: fzy
mkdir -p $(DESTDIR)$(MANDIR)/man1
cp fzy.1 $(DESTDIR)$(MANDIR)/man1/
+fmt:
+ clang-format -i *.c *.h
+
clean:
$(RM) fzy fzytest *.o
-.PHONY: test all clean install
+.PHONY: test all clean install fmt
diff --git a/choices.c b/choices.c
index 2a27d10..455aacc 100644
--- a/choices.c
+++ b/choices.c
@@ -10,18 +10,18 @@ static int cmpchoice(const void *_idx1, const void *_idx2) {
const struct scored_result *a = _idx1;
const struct scored_result *b = _idx2;
- if(a->score == b->score)
+ if (a->score == b->score)
return 0;
- else if(a->score < b->score)
+ else if (a->score < b->score)
return 1;
else
return -1;
}
-static void choices_resize(choices_t *c, int new_capacity){
+static void choices_resize(choices_t *c, int new_capacity) {
c->strings = realloc(c->strings, new_capacity * sizeof(const char *));
- if(!c->strings){
+ if (!c->strings) {
fprintf(stderr, "Error: Can't allocate memory\n");
abort();
}
@@ -29,13 +29,13 @@ static void choices_resize(choices_t *c, int new_capacity){
c->capacity = new_capacity;
}
-static void choices_reset_search(choices_t *c){
+static void choices_reset_search(choices_t *c) {
free(c->results);
c->selection = c->available = 0;
c->results = NULL;
}
-void choices_init(choices_t *c){
+void choices_init(choices_t *c) {
c->strings = NULL;
c->results = NULL;
c->capacity = c->size = 0;
@@ -43,36 +43,36 @@ void choices_init(choices_t *c){
choices_resize(c, INITIAL_CAPACITY);
}
-void choices_free(choices_t *c){
+void choices_free(choices_t *c) {
free(c->strings);
free(c->results);
}
-void choices_add(choices_t *c, const char *choice){
+void choices_add(choices_t *c, const char *choice) {
/* Previous search is now invalid */
choices_reset_search(c);
- if(c->size == c->capacity){
+ if (c->size == c->capacity) {
choices_resize(c, c->capacity * 2);
}
c->strings[c->size++] = choice;
}
-size_t choices_available(choices_t *c){
+size_t choices_available(choices_t *c) {
return c->available;
}
-void choices_search(choices_t *c, const char *search){
+void choices_search(choices_t *c, const char *search) {
choices_reset_search(c);
c->results = malloc(c->size * sizeof(struct scored_result));
- if(!c->results){
+ if (!c->results) {
fprintf(stderr, "Error: Can't allocate memory\n");
abort();
}
- for(size_t i = 0; i < c->size; i++){
- if(has_match(search, c->strings[i])){
+ for (size_t i = 0; i < c->size; i++) {
+ if (has_match(search, c->strings[i])) {
c->results[c->available].str = c->strings[i];
c->results[c->available].score = match(search, c->strings[i]);
c->available++;
@@ -82,24 +82,23 @@ void choices_search(choices_t *c, const char *search){
qsort(c->results, c->available, sizeof(struct scored_result), cmpchoice);
}
-const char *choices_get(choices_t *c, size_t n){
- if(n < c->available){
+const char *choices_get(choices_t *c, size_t n) {
+ if (n < c->available) {
return c->results[n].str;
- }else{
+ } else {
return NULL;
}
}
-double choices_getscore(choices_t *c, size_t n){
+double choices_getscore(choices_t *c, size_t n) {
return c->results[n].score;
}
-void choices_prev(choices_t *c){
- if(c->available)
+void choices_prev(choices_t *c) {
+ if (c->available)
c->selection = (c->selection + c->available - 1) % c->available;
}
-void choices_next(choices_t *c){
- if(c->available)
+void choices_next(choices_t *c) {
+ if (c->available)
c->selection = (c->selection + 1) % c->available;
}
-
diff --git a/config.h b/config.h
index 8fa5284..98cc915 100644
--- a/config.h
+++ b/config.h
@@ -1,10 +1,10 @@
#define TTY_COLOR_HIGHLIGHT TTY_COLOR_YELLOW
-#define SCORE_GAP_LEADING -0.005
-#define SCORE_GAP_TRAILING -0.005
-#define SCORE_GAP_INNER -0.01
+#define SCORE_GAP_LEADING -0.005
+#define SCORE_GAP_TRAILING -0.005
+#define SCORE_GAP_INNER -0.01
#define SCORE_MATCH_CONSECUTIVE 1.0
-#define SCORE_MATCH_SLASH 0.9
-#define SCORE_MATCH_WORD 0.8
-#define SCORE_MATCH_CAPITAL 0.7
-#define SCORE_MATCH_DOT 0.6
+#define SCORE_MATCH_SLASH 0.9
+#define SCORE_MATCH_WORD 0.8
+#define SCORE_MATCH_CAPITAL 0.7
+#define SCORE_MATCH_DOT 0.6
diff --git a/fzy.c b/fzy.c
index dbefa45..5d3bf15 100644
--- a/fzy.c
+++ b/fzy.c
@@ -18,15 +18,15 @@ size_t scrolloff = 1;
const char *prompt = "> ";
-void read_choices(choices_t *c){
+void read_choices(choices_t *c) {
const char *line;
char buf[4096];
- while(fgets(buf, sizeof buf, stdin)){
+ while (fgets(buf, sizeof buf, stdin)) {
char *nl;
- if((nl = strchr(buf, '\n')))
+ if ((nl = strchr(buf, '\n')))
*nl = '\0';
- if(!(line = strdup(buf))){
+ if (!(line = strdup(buf))) {
fprintf(stderr, "Cannot allocate memory");
abort();
}
@@ -38,42 +38,42 @@ void read_choices(choices_t *c){
int search_size;
char search[SEARCH_SIZE_MAX + 1] = {0};
-void clear(tty_t *tty){
+void clear(tty_t *tty) {
tty_setcol(tty, 0);
size_t line = 0;
- while(line++ < num_lines){
+ while (line++ < num_lines) {
tty_newline(tty);
}
- tty_moveup(tty, line-1);
+ tty_moveup(tty, line - 1);
tty_flush(tty);
}
-void draw_match(tty_t *tty, const char *choice, int selected){
+void draw_match(tty_t *tty, const char *choice, int selected) {
int n = strlen(search);
size_t positions[n + 1];
- for(int i = 0; i < n + 1; i++)
+ for (int i = 0; i < n + 1; i++)
positions[i] = -1;
double score = match_positions(search, choice, &positions[0]);
size_t maxwidth = tty_getwidth(tty);
- if(flag_show_scores)
+ if (flag_show_scores)
tty_printf(tty, "(%5.2f) ", score);
- if(selected)
+ if (selected)
tty_setinvert(tty);
- for(size_t i = 0, p = 0; choice[i] != '\0'; i++){
- if(i+1 < maxwidth){
- if(positions[p] == i){
+ for (size_t i = 0, p = 0; choice[i] != '\0'; i++) {
+ if (i + 1 < maxwidth) {
+ if (positions[p] == i) {
tty_setfg(tty, TTY_COLOR_HIGHLIGHT);
p++;
- }else{
+ } else {
tty_setfg(tty, TTY_COLOR_NORMAL);
}
tty_printf(tty, "%c", choice[i]);
- }else{
+ } else {
tty_printf(tty, "$");
break;
}
@@ -81,23 +81,23 @@ void draw_match(tty_t *tty, const char *choice, int selected){
tty_setnormal(tty);
}
-void draw(tty_t *tty, choices_t *choices){
+void draw(tty_t *tty, choices_t *choices) {
size_t start = 0;
size_t current_selection = choices->selection;
- if(current_selection + scrolloff >= num_lines){
+ if (current_selection + scrolloff >= num_lines) {
start = current_selection + scrolloff - num_lines + 1;
- if(start + num_lines >= choices_available(choices)){
+ if (start + num_lines >= choices_available(choices)) {
start = choices_available(choices) - num_lines;
}
}
tty_setcol(tty, 0);
tty_printf(tty, "%s%s", prompt, search);
tty_clearline(tty);
- for(size_t i = start; i < start + num_lines; i++){
+ for (size_t i = start; i < start + num_lines; i++) {
tty_printf(tty, "\n");
tty_clearline(tty);
const char *choice = choices_get(choices, i);
- if(choice){
+ if (choice) {
draw_match(tty, choice, i == choices->selection);
}
}
@@ -106,12 +106,12 @@ void draw(tty_t *tty, choices_t *choices){
tty_flush(tty);
}
-void emit(choices_t *choices){
+void emit(choices_t *choices) {
const char *selection = choices_get(choices, choices->selection);
- if(selection){
+ if (selection) {
/* output the selected result */
printf("%s\n", selection);
- }else{
+ } else {
/* No match, output the query instead */
printf("%s\n", search);
}
@@ -119,98 +119,97 @@ void emit(choices_t *choices){
exit(EXIT_SUCCESS);
}
-void run(tty_t *tty, choices_t *choices){
+void run(tty_t *tty, choices_t *choices) {
choices_search(choices, search);
char ch;
do {
draw(tty, choices);
ch = tty_getchar(tty);
- if(isprint(ch)){
- if(search_size < SEARCH_SIZE_MAX){
+ if (isprint(ch)) {
+ if (search_size < SEARCH_SIZE_MAX) {
search[search_size++] = ch;
search[search_size] = '\0';
choices_search(choices, search);
}
- }else if(ch == 127 || ch == 8){ /* DEL || backspace */
- if(search_size)
+ } else if (ch == 127 || ch == 8) { /* DEL || backspace */
+ if (search_size)
search[--search_size] = '\0';
choices_search(choices, search);
- }else if(ch == 21){ /* C-U */
+ } else if (ch == 21) { /* C-U */
search_size = 0;
search[0] = '\0';
choices_search(choices, search);
- }else if(ch == 23){ /* C-W */
- if(search_size)
+ } else if (ch == 23) { /* C-W */
+ if (search_size)
search[--search_size] = '\0';
- while(search_size && !isspace(search[--search_size]))
+ while (search_size && !isspace(search[--search_size]))
search[search_size] = '\0';
choices_search(choices, search);
- }else if(ch == 14){ /* C-N */
+ } else if (ch == 14) { /* C-N */
choices_next(choices);
- }else if(ch == 16){ /* C-P */
+ } else if (ch == 16) { /* C-P */
choices_prev(choices);
- }else if(ch == 9){ /* TAB */
+ } else if (ch == 9) { /* TAB */
strncpy(search, choices_get(choices, choices->selection), SEARCH_SIZE_MAX);
search_size = strlen(search);
choices_search(choices, search);
- }else if(ch == 3 || ch == 4){ /* ^C || ^D */
+ } else if (ch == 3 || ch == 4) { /* ^C || ^D */
clear(tty);
tty_close(tty);
exit(EXIT_FAILURE);
- }else if(ch == 10){ /* Enter */
+ } else if (ch == 10) { /* Enter */
clear(tty);
/* ttyout should be flushed before outputting on stdout */
tty_close(tty);
emit(choices);
- }else if(ch == 27){ /* ESC */
+ } else if (ch == 27) { /* ESC */
ch = tty_getchar(tty);
- if(ch == '[' || ch == 'O'){
+ if (ch == '[' || ch == 'O') {
ch = tty_getchar(tty);
- if(ch == 'A'){ /* UP ARROW */
+ if (ch == 'A') { /* UP ARROW */
choices_prev(choices);
- }else if(ch == 'B'){ /* DOWN ARROW */
+ } else if (ch == 'B') { /* DOWN ARROW */
choices_next(choices);
}
}
}
- }while(1);
+ } while (1);
}
-static const char *usage_str = ""
-"Usage: fzy [OPTION]...\n"
-" -l, --lines=LINES Specify how many lines of results to show (default 10)\n"
-" -p, --prompt=PROMPT Input prompt (default '> ')\n"
-" -e, --show-matches=QUERY Output the sorted matches of QUERY\n"
-" -t, --tty=TTY Specify file to use as TTY device (default /dev/tty)\n"
-" -s, --show-scores Show the scores of each match\n"
-" -h, --help Display this help and exit\n"
-" -v, --version Output version information and exit\n";
+static const char *usage_str =
+ ""
+ "Usage: fzy [OPTION]...\n"
+ " -l, --lines=LINES Specify how many lines of results to show (default 10)\n"
+ " -p, --prompt=PROMPT Input prompt (default '> ')\n"
+ " -e, --show-matches=QUERY Output the sorted matches of QUERY\n"
+ " -t, --tty=TTY Specify file to use as TTY device (default /dev/tty)\n"
+ " -s, --show-scores Show the scores of each match\n"
+ " -h, --help Display this help and exit\n"
+ " -v, --version Output version information and exit\n";
-void usage(const char *argv0){
+void usage(const char *argv0) {
fprintf(stderr, usage_str, argv0);
}
-static struct option longopts[] = {
- { "show-matches", required_argument, NULL, 'e' },
- { "lines", required_argument, NULL, 'l' },
- { "tty", required_argument, NULL, 't' },
- { "prompt", required_argument, NULL, 'p' },
- { "show-scores", no_argument, NULL, 's' },
- { "version", no_argument, NULL, 'v' },
- { "benchmark", no_argument, NULL, 'b' },
- { "help", no_argument, NULL, 'h' },
- { NULL, 0, NULL, 0 }
-};
+static struct option longopts[] = {{"show-matches", required_argument, NULL, 'e'},
+ {"lines", required_argument, NULL, 'l'},
+ {"tty", required_argument, NULL, 't'},
+ {"prompt", required_argument, NULL, 'p'},
+ {"show-scores", no_argument, NULL, 's'},
+ {"version", no_argument, NULL, 'v'},
+ {"benchmark", no_argument, NULL, 'b'},
+ {"help", no_argument, NULL, 'h'},
+ {NULL, 0, NULL, 0}};
-int main(int argc, char *argv[]){
+int main(int argc, char *argv[]) {
int benchmark = 0;
const char *initial_query = NULL;
const char *tty_filename = "/dev/tty";
char c;
- while((c = getopt_long(argc, argv, "vhse:l:t:p:", longopts, NULL)) != -1){
- switch(c){
+ while ((c = getopt_long(argc, argv, "vhse:l:t:p:", longopts, NULL)) != -1) {
+ switch (c) {
case 'v':
printf("%s " VERSION " (c) 2014 John Hawthorn\n", argv[0]);
exit(EXIT_SUCCESS);
@@ -229,27 +228,25 @@ int main(int argc, char *argv[]){
case 'p':
prompt = optarg;
break;
- case 'l':
- {
- int l;
- if(!strcmp(optarg, "max")){
- l = INT_MAX;
- }else if(sscanf(optarg, "%d", &l) != 1 || l < 3){
- fprintf(stderr, "Invalid format for --lines: %s\n", optarg);
- fprintf(stderr, "Must be integer in range 3..\n");
- usage(argv[0]);
- exit(EXIT_FAILURE);
- }
- num_lines = l;
+ case 'l': {
+ int l;
+ if (!strcmp(optarg, "max")) {
+ l = INT_MAX;
+ } else if (sscanf(optarg, "%d", &l) != 1 || l < 3) {
+ fprintf(stderr, "Invalid format for --lines: %s\n", optarg);
+ fprintf(stderr, "Must be integer in range 3..\n");
+ usage(argv[0]);
+ exit(EXIT_FAILURE);
}
- break;
+ num_lines = l;
+ } break;
case 'h':
default:
usage(argv[0]);
exit(EXIT_SUCCESS);
}
}
- if(optind != argc){
+ if (optind != argc) {
usage(argv[0]);
exit(EXIT_FAILURE);
}
@@ -258,29 +255,29 @@ int main(int argc, char *argv[]){
choices_init(&choices);
read_choices(&choices);
- if(num_lines > choices.size)
+ if (num_lines > choices.size)
num_lines = choices.size;
- if(benchmark){
- if(!initial_query){
+ if (benchmark) {
+ if (!initial_query) {
fprintf(stderr, "Must specify -e/--show-matches with --benchmark\n");
exit(EXIT_FAILURE);
}
- for(int i = 0; i < 100; i++)
+ for (int i = 0; i < 100; i++)
choices_search(&choices, initial_query);
- }else if(initial_query){
+ } else if (initial_query) {
choices_search(&choices, initial_query);
- for(size_t i = 0; i < choices_available(&choices); i++){
- if(flag_show_scores)
+ for (size_t i = 0; i < choices_available(&choices); i++) {
+ if (flag_show_scores)
printf("%f\t", choices_getscore(&choices, i));
printf("%s\n", choices_get(&choices, i));
}
- }else{
+ } else {
/* interactive */
tty_t tty;
tty_init(&tty, tty_filename);
- if(num_lines + 1 > tty_getheight(&tty))
+ if (num_lines + 1 > tty_getheight(&tty))
num_lines = tty_getheight(&tty) - 1;
run(&tty, &choices);
diff --git a/fzytest.c b/fzytest.c
index fc6f202..f7d5fae 100644
--- a/fzytest.c
+++ b/fzytest.c
@@ -7,22 +7,23 @@
int testsrun = 0, testsfailed = 0, assertionsrun = 0;
-#define assert(x) \
- if(++assertionsrun && !(x)){ \
- fprintf(stderr, "test \"%s\" failed\n assert(%s) was false\n at %s:%i\n\n", __func__, #x, __FILE__ ,__LINE__); \
- raise(SIGTRAP); \
- testsfailed++; \
- return; \
+#define assert(x) \
+ if (++assertionsrun && !(x)) { \
+ fprintf(stderr, "test \"%s\" failed\n assert(%s) was false\n at %s:%i\n\n", \
+ __func__, #x, __FILE__, __LINE__); \
+ raise(SIGTRAP); \
+ testsfailed++; \
+ return; \
}
#define assert_streq(a, b) assert(!strcmp(a, b))
-void runtest(void (*test)()){
+void runtest(void (*test)()) {
testsrun++;
test();
}
-void test_match(){
+void test_match() {
assert(has_match("a", "a"));
assert(has_match("a", "ab"));
assert(has_match("a", "ba"));
@@ -38,7 +39,7 @@ void test_match(){
assert(has_match("", "a"));
}
-void test_scoring(){
+void test_scoring() {
/* App/Models/Order is better than App/MOdels/zRder */
assert(match("amor", "app/models/order") > match("amor", "app/models/zrder"));
@@ -65,7 +66,7 @@ void test_scoring(){
assert(match("abc", " a b c ") > match("abc", " a b c "));
}
-void test_positions_1(){
+void test_positions_1() {
size_t positions[3];
match_positions("amo", "app/models/foo", positions);
assert(positions[0] == 0);
@@ -73,7 +74,7 @@ void test_positions_1(){
assert(positions[2] == 5);
}
-void test_positions_2(){
+void test_positions_2() {
/*
* We should prefer matching the 'o' in order, since it's the beginning
* of a word.
@@ -85,21 +86,21 @@ void test_positions_2(){
assert(positions[2] == 11);
}
-void test_positions_3(){
+void test_positions_3() {
size_t positions[2];
match_positions("as", "tags", positions);
assert(positions[0] == 1);
assert(positions[1] == 3);
}
-void test_positions_4(){
+void test_positions_4() {
size_t positions[2];
match_positions("as", "examples.txt", positions);
assert(positions[0] == 2);
assert(positions[1] == 7);
}
-void test_positions_exact(){
+void test_positions_exact() {
size_t positions[3];
match_positions("foo", "foo", positions);
assert(positions[0] == 0);
@@ -107,7 +108,7 @@ void test_positions_exact(){
assert(positions[2] == 2);
}
-void test_choices_empty(){
+void test_choices_empty() {
choices_t choices;
choices_init(&choices);
assert(choices.size == 0);
@@ -123,7 +124,7 @@ void test_choices_empty(){
choices_free(&choices);
}
-void test_choices_1(){
+void test_choices_1() {
choices_t choices;
choices_init(&choices);
choices_add(&choices, "tags");
@@ -148,7 +149,7 @@ void test_choices_1(){
choices_free(&choices);
}
-void test_choices_2(){
+void test_choices_2() {
choices_t choices;
choices_init(&choices);
choices_add(&choices, "tags");
@@ -198,7 +199,7 @@ void test_choices_2(){
choices_free(&choices);
}
-void test_choices_without_search(){
+void test_choices_without_search() {
/* Before a search is run, it should return no results */
choices_t choices;
@@ -219,17 +220,17 @@ void test_choices_without_search(){
choices_free(&choices);
}
-void summary(){
+void summary() {
printf("%i tests, %i assertions, %i failures\n", testsrun, assertionsrun, testsfailed);
}
-static void ignore_signal(int signum){
- (void) signum;
+static void ignore_signal(int signum) {
+ (void)signum;
}
-int main(int argc, char *argv[]){
- (void) argc;
- (void) argv;
+int main(int argc, char *argv[]) {
+ (void)argc;
+ (void)argv;
/* We raise sigtrap on all assertion failures.
* If we have no debugger running, we should ignore it */
diff --git a/match.c b/match.c
index f915110..3eafda1 100644
--- a/match.c
+++ b/match.c
@@ -9,16 +9,16 @@
#include "config.h"
-char *strcasechr(const char *s, char c){
+char *strcasechr(const char *s, char c) {
const char accept[3] = {c, toupper(c), 0};
return strpbrk(s, accept);
}
-int has_match(const char *needle, const char *haystack){
- while(*needle){
+int has_match(const char *needle, const char *haystack) {
+ while (*needle) {
char nch = *needle++;
- if(!(haystack = strcasechr(haystack, nch))){
+ if (!(haystack = strcasechr(haystack, nch))) {
return 0;
}
haystack++;
@@ -29,22 +29,22 @@ int has_match(const char *needle, const char *haystack){
#define max(a, b) (((a) > (b)) ? (a) : (b))
/* print one of the internal matrices */
-void mat_print(score_t *mat, const char *needle, const char *haystack){
+void mat_print(score_t *mat, const char *needle, const char *haystack) {
int n = strlen(needle);
int m = strlen(haystack);
int i, j;
fprintf(stderr, " ");
- for(j = 0; j < m; j++){
+ for (j = 0; j < m; j++) {
fprintf(stderr, " %c", haystack[j]);
}
fprintf(stderr, "\n");
- for(i = 0; i < n; i++){
+ for (i = 0; i < n; i++) {
fprintf(stderr, " %c |", needle[i]);
- for(j = 0; j < m; j++){
- score_t val = mat[i*m + j];
- if(val == SCORE_MIN){
+ for (j = 0; j < m; j++) {
+ score_t val = mat[i * m + j];
+ if (val == SCORE_MIN) {
fprintf(stderr, " -inf");
- }else{
+ } else {
fprintf(stderr, " % .2f", val);
}
}
@@ -53,14 +53,14 @@ void mat_print(score_t *mat, const char *needle, const char *haystack){
fprintf(stderr, "\n\n");
}
-score_t calculate_score(const char *needle, const char *haystack, size_t *positions){
- if(!*haystack || !*needle)
+score_t calculate_score(const char *needle, const char *haystack, size_t *positions) {
+ if (!*haystack || !*needle)
return SCORE_MIN;
int n = strlen(needle);
int m = strlen(haystack);
- if(m > 1024){
+ if (m > 1024) {
/*
* Unreasonably large candidate: return no score
* 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
/* Which positions are beginning of words */
char last_ch = '\0';
- for(int i = 0; i < m; i++){
+ for (int i = 0; i < m; i++) {
char ch = haystack[i];
score_t score = 0;
- if(isalnum(ch)){
- if(!last_ch || last_ch == '/'){
+ if (isalnum(ch)) {
+ if (!last_ch || last_ch == '/') {
score = SCORE_MATCH_SLASH;
- }else if(last_ch == '-' ||
- last_ch == '_' ||
- last_ch == ' ' ||
- (last_ch >= '0' && last_ch <= '9')){
+ } else if (last_ch == '-' || last_ch == '_' || last_ch == ' ' ||
+ (last_ch >= '0' && last_ch <= '9')) {
score = SCORE_MATCH_WORD;
- }else if(last_ch >= 'a' && last_ch <= 'z' &&
- ch >= 'A' && ch <= 'Z'){
+ } else if (last_ch >= 'a' && last_ch <= 'z' && ch >= 'A' && ch <= 'Z') {
/* CamelCase */
score = SCORE_MATCH_CAPITAL;
- }else if(last_ch == '.'){
+ } else if (last_ch == '.') {
score = SCORE_MATCH_DOT;
}
}
@@ -104,21 +101,20 @@ score_t calculate_score(const char *needle, const char *haystack, size_t *positi
last_ch = ch;
}
- for(int i = 0; i < n; i++){
+ for (int i = 0; i < n; i++) {
score_t prev_score = SCORE_MIN;
- score_t gap_score = i == n-1 ? SCORE_GAP_TRAILING : SCORE_GAP_INNER;
- for(int j = 0; j < m; j++){
+ score_t gap_score = i == n - 1 ? SCORE_GAP_TRAILING : SCORE_GAP_INNER;
+ for (int j = 0; j < m; j++) {
score_t score = SCORE_MIN;
- if(tolower(needle[i]) == tolower(haystack[j])){
- if(!i){
+ if (tolower(needle[i]) == tolower(haystack[j])) {
+ if (!i) {
score = (j * SCORE_GAP_LEADING) + match_bonus[j];
- }else if(j){
+ } else if (j) {
score = max(
- M[i-1][j-1] + match_bonus[j],
+ M[i - 1][j - 1] + match_bonus[j],
- /* consecutive match, doesn't stack with match_bonus */
- D[i-1][j-1] + SCORE_MATCH_CONSECUTIVE
- );
+ /* consecutive match, doesn't stack with match_bonus */
+ D[i - 1][j - 1] + SCORE_MATCH_CONSECUTIVE);
}
}
D[i][j] = score;
@@ -134,10 +130,10 @@ score_t calculate_score(const char *needle, const char *haystack, size_t *positi
#endif
/* backtrace to find the positions of optimal matching */
- if(positions){
+ if (positions) {
int match_required = 0;
- for(int i = n-1, j = m-1; i >= 0; i--){
- for(; j >= 0; j--){
+ for (int i = n - 1, j = m - 1; i >= 0; i--) {
+ for (; j >= 0; j--) {
/*
* There may be multiple paths which result in
* the optimal weight.
@@ -146,12 +142,15 @@ score_t calculate_score(const char *needle, const char *haystack, size_t *positi
* we encounter, the latest in the candidate
* string.
*/
- if(D[i][j] != SCORE_MIN && (match_required || D[i][j] == M[i][j])){
+ if (D[i][j] != SCORE_MIN &&
+ (match_required || D[i][j] == M[i][j])) {
/* If this score was determined using
* SCORE_MATCH_CONSECUTIVE, the
* previous character MUST be a match
*/
- match_required = i && j && M[i][j] == D[i-1][j-1] + SCORE_MATCH_CONSECUTIVE;
+ match_required =
+ i && j &&
+ M[i][j] == D[i - 1][j - 1] + SCORE_MATCH_CONSECUTIVE;
positions[i] = j--;
break;
}
@@ -159,24 +158,24 @@ score_t calculate_score(const char *needle, const char *haystack, size_t *positi
}
}
- return M[n-1][m-1];
+ return M[n - 1][m - 1];
}
-score_t match_positions(const char *needle, const char *haystack, size_t *positions){
- if(!*needle){
+score_t match_positions(const char *needle, const char *haystack, size_t *positions) {
+ if (!*needle) {
return SCORE_MAX;
- }else if(!strcasecmp(needle, haystack)){
- if(positions){
+ } else if (!strcasecmp(needle, haystack)) {
+ if (positions) {
int n = strlen(needle);
- for(int i = 0; i < n; i++)
+ for (int i = 0; i < n; i++)
positions[i] = i;
}
return SCORE_MAX;
- }else{
+ } else {
return calculate_score(needle, haystack, positions);
}
}
-score_t match(const char *needle, const char *haystack){
+score_t match(const char *needle, const char *haystack) {
return match_positions(needle, haystack, NULL);
}
diff --git a/tty.c b/tty.c
index 7e1c3d8..5293627 100644
--- a/tty.c
+++ b/tty.c
@@ -8,23 +8,23 @@
#include "tty.h"
-void tty_reset(tty_t *tty){
+void tty_reset(tty_t *tty) {
tcsetattr(tty->fdin, TCSANOW, &tty->original_termios);
}
-void tty_close(tty_t *tty){
+void tty_close(tty_t *tty) {
tty_reset(tty);
fclose(tty->fout);
close(tty->fdin);
}
-void tty_init(tty_t *tty, const char *tty_filename){
+void tty_init(tty_t *tty, const char *tty_filename) {
tty->fdin = open(tty_filename, O_RDONLY);
tty->fout = fopen(tty_filename, "w");
- if(setvbuf(tty->fout, NULL, _IOFBF, 4096))
+ if (setvbuf(tty->fout, NULL, _IOFBF, 4096))
perror("setvbuf");
- if(tcgetattr(tty->fdin, &tty->original_termios))
+ if (tcgetattr(tty->fdin, &tty->original_termios))
perror("tcgetattr");
struct termios new_termios = tty->original_termios;
@@ -37,7 +37,7 @@ void tty_init(tty_t *tty, const char *tty_filename){
*/
new_termios.c_lflag &= ~(ICANON | ECHO | ISIG);
- if(tcsetattr(tty->fdin, TCSANOW, &new_termios))
+ if (tcsetattr(tty->fdin, TCSANOW, &new_termios))
perror("tcsetattr");
tty_getwinsz(tty);
@@ -45,82 +45,82 @@ void tty_init(tty_t *tty, const char *tty_filename){
tty_setnormal(tty);
}
-void tty_getwinsz(tty_t *tty){
+void tty_getwinsz(tty_t *tty) {
struct winsize ws;
- if(ioctl(fileno(tty->fout), TIOCGWINSZ, &ws) == -1){
+ if (ioctl(fileno(tty->fout), TIOCGWINSZ, &ws) == -1) {
tty->maxwidth = 80;
tty->maxheight = 25;
- }else{
+ } else {
tty->maxwidth = ws.ws_col;
tty->maxheight = ws.ws_row;
}
}
-char tty_getchar(tty_t *tty){
+char tty_getchar(tty_t *tty) {
char ch;
int size = read(tty->fdin, &ch, 1);
- if(size < 0){
+ if (size < 0) {
perror("error reading from tty");
exit(EXIT_FAILURE);
- }else if(size == 0){
+ } else if (size == 0) {
/* EOF */
exit(EXIT_FAILURE);
- }else{
+ } else {
return ch;
}
}
-static void tty_sgr(tty_t *tty, int code){
+static void tty_sgr(tty_t *tty, int code) {
tty_printf(tty, "%c%c%im", 0x1b, '[', code);
}
-void tty_setfg(tty_t *tty, int fg){
- if(tty->fgcolor != fg){
+void tty_setfg(tty_t *tty, int fg) {
+ if (tty->fgcolor != fg) {
tty_sgr(tty, 30 + fg);
tty->fgcolor = fg;
}
}
-void tty_setinvert(tty_t *tty){
+void tty_setinvert(tty_t *tty) {
tty_sgr(tty, 7);
}
-void tty_setnormal(tty_t *tty){
+void tty_setnormal(tty_t *tty) {
tty_sgr(tty, 0);
tty->fgcolor = 9;
}
-void tty_newline(tty_t *tty){
+void tty_newline(tty_t *tty) {
tty_printf(tty, "%c%cK\n", 0x1b, '[');
}
-void tty_clearline(tty_t *tty){
+void tty_clearline(tty_t *tty) {
tty_printf(tty, "%c%cK", 0x1b, '[');
}
-void tty_setcol(tty_t *tty, int col){
+void tty_setcol(tty_t *tty, int col) {
tty_printf(tty, "%c%c%iG", 0x1b, '[', col + 1);
}
-void tty_moveup(tty_t *tty, int i){
+void tty_moveup(tty_t *tty, int i) {
tty_printf(tty, "%c%c%iA", 0x1b, '[', i);
}
-void tty_printf(tty_t *tty, const char *fmt, ...){
+void tty_printf(tty_t *tty, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(tty->fout, fmt, args);
va_end(args);
}
-void tty_flush(tty_t *tty){
+void tty_flush(tty_t *tty) {
fflush(tty->fout);
}
-size_t tty_getwidth(tty_t *tty){
+size_t tty_getwidth(tty_t *tty) {
return tty->maxwidth;
}
-size_t tty_getheight(tty_t *tty){
+size_t tty_getheight(tty_t *tty) {
return tty->maxheight;
}
diff --git a/tty.h b/tty.h
index 76c5d03..b5a9343 100644
--- a/tty.h
+++ b/tty.h
@@ -3,7 +3,7 @@
#include <termios.h>
-typedef struct{
+typedef struct {
int fdin;
FILE *fout;
struct termios original_termios;
@@ -22,15 +22,15 @@ void tty_setfg(tty_t *tty, int fg);
void tty_setinvert(tty_t *tty);
void tty_setnormal(tty_t *tty);
-#define TTY_COLOR_BLACK 0
-#define TTY_COLOR_RED 1
-#define TTY_COLOR_GREEN 2
-#define TTY_COLOR_YELLOW 3
-#define TTY_COLOR_BLUE 4
+#define TTY_COLOR_BLACK 0
+#define TTY_COLOR_RED 1
+#define TTY_COLOR_GREEN 2
+#define TTY_COLOR_YELLOW 3
+#define TTY_COLOR_BLUE 4
#define TTY_COLOR_MAGENTA 5
-#define TTY_COLOR_CYAN 6
-#define TTY_COLOR_WHITE 7
-#define TTY_COLOR_NORMAL 9
+#define TTY_COLOR_CYAN 6
+#define TTY_COLOR_WHITE 7
+#define TTY_COLOR_NORMAL 9
/* tty_newline
* Move cursor to the beginning of the next line, clearing to the end of the