GitHub PullRequestReviewCommentEvent at 2014-08-05 10:16:14
Repository: cmus
Actor: flyingmutant
Payload: {"action":"created","comment":{"url":"https://api.github.com/repos/cmus/cmus/pulls/comments/15804095","id":15804095,"diff_hunk":"@@ -234,142 +235,371 @@ static inline int strnequal(const char *a, const char *b, size_t b_len)\n \treturn a && (strlen(a) == b_len) && (memcmp(a, b, b_len) == 0);\n }\n \n-static void print(char *str, int str_width, const char *format, const struct format_option *fopts)\n+static const struct format_option *find_fopt(const struct format_option *fopts, const char *key)\n {\n-\t/* format and str indices */\n-\tint s = 0, d = 0;\n+\tconst struct format_option *fo;\n+\tchar ch = strlen(key) == 1 ? *key : 0;\n+\tfor (fo = fopts; fo->type != 0; fo++) {\n+\t\tif ( (ch != 0 && fo->ch == ch) || strnequal(fo->str, key, strlen(key)) ) {\n+\t\t\treturn fo;\n+\t\t}\n+\t}\n+\treturn NULL;\n+}\n \n-\twhile (format[s]) {\n-\t\tconst struct format_option *fo;\n-\t\tint long_len = 0;\n-\t\tconst char *long_begin = NULL;\n-\t\tuchar u;\n+static const char *str_val(const char *key, const struct format_option *fopts, char *buf)\n+{\n+\tconst struct format_option *fo;\n+\tconst struct cmus_opt *opt;\n+\tconst char *val = NULL;\n+\n+\tfo = find_fopt(fopts, key);\n+\tif (fo) {\n+\t\tif (fo->type == FO_STR)\n+\t\t\tval = fo->fo_str;\n+\t} else {\n+\t\topt = option_find_silent(key);\n+\t\tif (opt) {\n+\t\t\topt->get(opt->id, buf);\n+\t\t\tval = buf;\n+\t\t}\n+\t}\n+\tif (!val)\n+\t\tval = \"\";\n+\treturn val;\n+}\n \n-\t\tu = u_get_char(format, &s);\n+static int int_val(const char *key, const struct format_option *fopts, char *buf)\n+{\n+\tconst struct format_option *fo;\n+\tint val = -1;\n+\n+\tfo = find_fopt(fopts, key);\n+\tif (fo) {\n+\t\tif (fo->type == FO_INT)\n+\t\t\tval = fo->fo_int;\n+\t}\n+\treturn val;\n+}\n+\n+static int format_eval_cond(struct expr* expr, const struct format_option *fopts) \n+{\n+\tif (!expr)\n+\t\treturn -1;\n+\tenum expr_type type = expr->type;\n+\tconst char *key;\n+\tconst struct format_option *fo;\n+\tconst struct cmus_opt *opt;\n+\tchar buf[512];\n+\n+\tif (expr->left) {\n+\t\tint left = format_eval_cond(expr->left, fopts);\n+\n+\t\tif (type == EXPR_AND)\n+\t\t\treturn left && format_eval_cond(expr->right, fopts);\n+\t\tif (type == EXPR_OR)\n+\t\t\treturn left || format_eval_cond(expr->right, fopts);\n+\t\t/* EXPR_NOT */\n+\t\treturn !left;\n+\t}\n+\n+\tkey = expr->key;\n+\tif (type == EXPR_STR) {\n+\t\tconst char *val = str_val(key, fopts, buf);\n+\t\tint res;\n+\t\t\n+\t\tres = glob_match(&expr->estr.glob_head, val);\n+\t\tif (expr->estr.op == SOP_EQ)\n+\t\t\treturn res;\n+\t\treturn !res;\n+\t} else if (type == EXPR_INT) {\n+\t\tint val = int_val(key, fopts, buf); \n+\t\tint res;\n+\n+\t\tres = val - expr->eint.val;\n+\t\tswitch (expr->eint.op) {\n+\t\tcase IOP_LT:\n+\t\t\treturn res < 0;\n+\t\tcase IOP_LE:\n+\t\t\treturn res <= 0;\n+\t\tcase IOP_EQ:\n+\t\t\treturn res == 0;\n+\t\tcase IOP_GE:\n+\t\t\treturn res >= 0;\n+\t\tcase IOP_GT:\n+\t\t\treturn res > 0;\n+\t\tcase IOP_NE:\n+\t\t\treturn res != 0;\n+\t\t}\n+\t} else if (type == EXPR_ID) {\n+\t\tint a = 0, b = 0;\n+\t\tconst char *sa, *sb;\n+\t\tint res = 0;\n+\t\tif ( (sa = str_val(key, fopts, buf)) ) {\n+\t\t\tif ( (sb = str_val(expr->eid.key, fopts, buf)) ) {","path":"format_print.c","position":336,"original_position":336,"commit_id":"afdae4c9ac621bd50c319f3a6c1ae2ac12b15dee","original_commit_id":"afdae4c9ac621bd50c319f3a6c1ae2ac12b15dee","user":{"login":"flyingmutant","id":73059,"avatar_url":"https://avatars.githubusercontent.com/u/73059?v=2","gravatar_id":"334642c76bb6da9fb5206ba8abbfb0bc","url":"https://api.github.com/users/flyingmutant","html_url":"https://github.com/flyingmutant","followers_url":"https://api.github.com/users/flyingmutant/followers","following_url":"https://api.github.com/users/flyingmutant/following{/other_user}","gists_url":"https://api.github.com/users/flyingmutant/gists{/gist_id}","starred_url":"https://api.github.com/users/flyingmutant/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flyingmutant/subscriptions","organizations_url":"https://api.github.com/users/flyingmutant/orgs","repos_url":"https://api.github.com/users/flyingmutant/repos","events_url":"https://api.github.com/users/flyingmutant/events{/privacy}","received_events_url":"https://api.github.com/users/flyingmutant/received_events","type":"User","site_admin":false},"body":"No need to nest `if`s — `if (A && B)` will do here.","created_at":"2014-08-05T10:16:14Z","updated_at":"2014-08-05T10:16:14Z","html_url":"https://github.com/cmus/cmus/pull/151#discussion_r15804095","pull_request_url":"https://api.github.com/repos/cmus/cmus/pulls/151","_links":{"self":{"href":"https://api.github.com/repos/cmus/cmus/pulls/comments/15804095"},"html":{"href":"https://github.com/cmus/cmus/pull/151#discussion_r15804095"},"pull_request":{"href":"https://api.github.com/repos/cmus/cmus/pulls/151"}}},"pull_request":{"url":"https://api.github.com/repos/cmus/cmus/pulls/151","id":19080010,"html_url":"https://github.com/cmus/cmus/pull/151","diff_url":"https://github.com/cmus/cmus/pull/151.diff","patch_url":"https://github.com/cmus/cmus/pull/151.patch","issue_url":"https://api.github.com/repos/cmus/cmus/issues/151","number":151,"state":"open","title":"Add conditionals in format strings","user":{"login":"pavel-belikov","id":8167633,"avatar_url":"https://avatars.githubusercontent.com/u/8167633?v=2","gravatar_id":"70364a167f22d29cd34a826acf0fac23","url":"https://api.github.com/users/pavel-belikov","html_url":"https://github.com/pavel-belikov","followers_url":"https://api.github.com/users/pavel-belikov/followers","following_url":"https://api.github.com/users/pavel-belikov/following{/other_user}","gists_url":"https://api.github.com/users/pavel-belikov/gists{/gist_id}","starred_url":"https://api.github.com/users/pavel-belikov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pavel-belikov/subscriptions","organizations_url":"https://api.github.com/users/pavel-belikov/orgs","repos_url":"https://api.github.com/users/pavel-belikov/repos","events_url":"https://api.github.com/users/pavel-belikov/events{/privacy}","received_events_url":"https://api.github.com/users/pavel-belikov/received_events","type":"User","site_admin":false},"body":"Idea from #150. Syntax `%{if COND then A [else B] }`. \r\nI used `expr.c` for parsing. `format_print.c` was modified. It uses `gbuf` now: I think, it is more convenient.\r\nId's in `COND` can be options. Any unknown id is `false`. Id `stream` checks if `is_http_url( %{filename} )`.\r\nI haven't changed/removed/added default formats yet. ","created_at":"2014-07-30T10:09:50Z","updated_at":"2014-08-05T10:16:14Z","closed_at":null,"merged_at":null,"merge_commit_sha":"42e004be7c4bcbb34557ed0f408a96550c6d360f","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/cmus/cmus/pulls/151/commits","review_comments_url":"https://api.github.com/repos/cmus/cmus/pulls/151/comments","review_comment_url":"https://api.github.com/repos/cmus/cmus/pulls/comments/{number}","comments_url":"https://api.github.com/repos/cmus/cmus/issues/151/comments","statuses_url":"https://api.github.com/repos/cmus/cmus/statuses/afdae4c9ac621bd50c319f3a6c1ae2ac12b15dee","head":{"label":"pavel-belikov:conditional-format","ref":"conditional-format","sha":"afdae4c9ac621bd50c319f3a6c1ae2ac12b15dee","user":{"login":"pavel-belikov","id":8167633,"avatar_url":"https://avatars.githubusercontent.com/u/8167633?v=2","gravatar_id":"70364a167f22d29cd34a826acf0fac23","url":"https://api.github.com/users/pavel-belikov","html_url":"https://github.com/pavel-belikov","followers_url":"https://api.github.com/users/pavel-belikov/followers","following_url":"https://api.github.com/users/pavel-belikov/following{/other_user}","gists_url":"https://api.github.com/users/pavel-belikov/gists{/gist_id}","starred_url":"https://api.github.com/users/pavel-belikov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pavel-belikov/subscriptions","organizations_url":"https://api.github.com/users/pavel-belikov/orgs","repos_url":"https://api.github.com/users/pavel-belikov/repos","events_url":"https://api.github.com/users/pavel-belikov/events{/privacy}","received_events_url":"https://api.github.com/users/pavel-belikov/received_events","type":"User","site_admin":false},"repo":{"id":21910952,"name":"cmus","full_name":"pavel-belikov/cmus","owner":{"login":"pavel-belikov","id":8167633,"avatar_url":"https://avatars.githubusercontent.com/u/8167633?v=2","gravatar_id":"70364a167f22d29cd34a826acf0fac23","url":"https://api.github.com/users/pavel-belikov","html_url":"https://github.com/pavel-belikov","followers_url":"https://api.github.com/users/pavel-belikov/followers","following_url":"https://api.github.com/users/pavel-belikov/following{/other_user}","gists_url":"https://api.github.com/users/pavel-belikov/gists{/gist_id}","starred_url":"https://api.github.com/users/pavel-belikov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pavel-belikov/subscriptions","organizations_url":"https://api.github.com/users/pavel-belikov/orgs","repos_url":"https://api.github.com/users/pavel-belikov/repos","events_url":"https://api.github.com/users/pavel-belikov/events{/privacy}","received_events_url":"https://api.github.com/users/pavel-belikov/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/pavel-belikov/cmus","description":"Small, fast and powerful console music player for Unix-like operating systems.","fork":true,"url":"https://api.github.com/repos/pavel-belikov/cmus","forks_url":"https://api.github.com/repos/pavel-belikov/cmus/forks","keys_url":"https://api.github.com/repos/pavel-belikov/cmus/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pavel-belikov/cmus/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pavel-belikov/cmus/teams","hooks_url":"https://api.github.com/repos/pavel-belikov/cmus/hooks","issue_events_url":"https://api.github.com/repos/pavel-belikov/cmus/issues/events{/number}","events_url":"https://api.github.com/repos/pavel-belikov/cmus/events","assignees_url":"https://api.github.com/repos/pavel-belikov/cmus/assignees{/user}","branches_url":"https://api.github.com/repos/pavel-belikov/cmus/branches{/branch}","tags_url":"https://api.github.com/repos/pavel-belikov/cmus/tags","blobs_url":"https://api.github.com/repos/pavel-belikov/cmus/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pavel-belikov/cmus/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pavel-belikov/cmus/git/refs{/sha}","trees_url":"https://api.github.com/repos/pavel-belikov/cmus/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pavel-belikov/cmus/statuses/{sha}","languages_url":"https://api.github.com/repos/pavel-belikov/cmus/languages","stargazers_url":"https://api.github.com/repos/pavel-belikov/cmus/stargazers","contributors_url":"https://api.github.com/repos/pavel-belikov/cmus/contributors","subscribers_url":"https://api.github.com/repos/pavel-belikov/cmus/subscribers","subscription_url":"https://api.github.com/repos/pavel-belikov/cmus/subscription","commits_url":"https://api.github.com/repos/pavel-belikov/cmus/commits{/sha}","git_commits_url":"https://api.github.com/repos/pavel-belikov/cmus/git/commits{/sha}","comments_url":"https://api.github.com/repos/pavel-belikov/cmus/comments{/number}","issue_comment_url":"https://api.github.com/repos/pavel-belikov/cmus/issues/comments/{number}","contents_url":"https://api.github.com/repos/pavel-belikov/cmus/contents/{+path}","compare_url":"https://api.github.com/repos/pavel-belikov/cmus/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pavel-belikov/cmus/merges","archive_url":"https://api.github.com/repos/pavel-belikov/cmus/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pavel-belikov/cmus/downloads","issues_url":"https://api.github.com/repos/pavel-belikov/cmus/issues{/number}","pulls_url":"https://api.github.com/repos/pavel-belikov/cmus/pulls{/number}","milestones_url":"https://api.github.com/repos/pavel-belikov/cmus/milestones{/number}","notifications_url":"https://api.github.com/repos/pavel-belikov/cmus/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pavel-belikov/cmus/labels{/name}","releases_url":"https://api.github.com/repos/pavel-belikov/cmus/releases{/id}","created_at":"2014-07-16T17:49:54Z","updated_at":"2014-07-15T21:35:59Z","pushed_at":"2014-07-31T07:10:03Z","git_url":"git://github.com/pavel-belikov/cmus.git","ssh_url":"git@github.com:pavel-belikov/cmus.git","clone_url":"https://github.com/pavel-belikov/cmus.git","svn_url":"https://github.com/pavel-belikov/cmus","homepage":"https://cmus.github.io/","size":5586,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":false,"has_downloads":true,"has_wiki":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"cmus:master","ref":"master","sha":"425d1ace01387076606dab2d3ccc2e97befcd0d9","user":{"login":"cmus","id":3125757,"avatar_url":"https://avatars.githubusercontent.com/u/3125757?v=2","gravatar_id":"275397c2fbdb291171a40338d277e03a","url":"https://api.github.com/users/cmus","html_url":"https://github.com/cmus","followers_url":"https://api.github.com/users/cmus/followers","following_url":"https://api.github.com/users/cmus/following{/other_user}","gists_url":"https://api.github.com/users/cmus/gists{/gist_id}","starred_url":"https://api.github.com/users/cmus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cmus/subscriptions","organizations_url":"https://api.github.com/users/cmus/orgs","repos_url":"https://api.github.com/users/cmus/repos","events_url":"https://api.github.com/users/cmus/events{/privacy}","received_events_url":"https://api.github.com/users/cmus/received_events","type":"Organization","site_admin":false},"repo":{"id":7365136,"name":"cmus","full_name":"cmus/cmus","owner":{"login":"cmus","id":3125757,"avatar_url":"https://avatars.githubusercontent.com/u/3125757?v=2","gravatar_id":"275397c2fbdb291171a40338d277e03a","url":"https://api.github.com/users/cmus","html_url":"https://github.com/cmus","followers_url":"https://api.github.com/users/cmus/followers","following_url":"https://api.github.com/users/cmus/following{/other_user}","gists_url":"https://api.github.com/users/cmus/gists{/gist_id}","starred_url":"https://api.github.com/users/cmus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cmus/subscriptions","organizations_url":"https://api.github.com/users/cmus/orgs","repos_url":"https://api.github.com/users/cmus/repos","events_url":"https://api.github.com/users/cmus/events{/privacy}","received_events_url":"https://api.github.com/users/cmus/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/cmus/cmus","description":"Small, fast and powerful console music player for Unix-like operating systems.","fork":false,"url":"https://api.github.com/repos/cmus/cmus","forks_url":"https://api.github.com/repos/cmus/cmus/forks","keys_url":"https://api.github.com/repos/cmus/cmus/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cmus/cmus/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cmus/cmus/teams","hooks_url":"https://api.github.com/repos/cmus/cmus/hooks","issue_events_url":"https://api.github.com/repos/cmus/cmus/issues/events{/number}","events_url":"https://api.github.com/repos/cmus/cmus/events","assignees_url":"https://api.github.com/repos/cmus/cmus/assignees{/user}","branches_url":"https://api.github.com/repos/cmus/cmus/branches{/branch}","tags_url":"https://api.github.com/repos/cmus/cmus/tags","blobs_url":"https://api.github.com/repos/cmus/cmus/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cmus/cmus/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cmus/cmus/git/refs{/sha}","trees_url":"https://api.github.com/repos/cmus/cmus/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cmus/cmus/statuses/{sha}","languages_url":"https://api.github.com/repos/cmus/cmus/languages","stargazers_url":"https://api.github.com/repos/cmus/cmus/stargazers","contributors_url":"https://api.github.com/repos/cmus/cmus/contributors","subscribers_url":"https://api.github.com/repos/cmus/cmus/subscribers","subscription_url":"https://api.github.com/repos/cmus/cmus/subscription","commits_url":"https://api.github.com/repos/cmus/cmus/commits{/sha}","git_commits_url":"https://api.github.com/repos/cmus/cmus/git/commits{/sha}","comments_url":"https://api.github.com/repos/cmus/cmus/comments{/number}","issue_comment_url":"https://api.github.com/repos/cmus/cmus/issues/comments/{number}","contents_url":"https://api.github.com/repos/cmus/cmus/contents/{+path}","compare_url":"https://api.github.com/repos/cmus/cmus/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cmus/cmus/merges","archive_url":"https://api.github.com/repos/cmus/cmus/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cmus/cmus/downloads","issues_url":"https://api.github.com/repos/cmus/cmus/issues{/number}","pulls_url":"https://api.github.com/repos/cmus/cmus/pulls{/number}","milestones_url":"https://api.github.com/repos/cmus/cmus/milestones{/number}","notifications_url":"https://api.github.com/repos/cmus/cmus/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cmus/cmus/labels{/name}","releases_url":"https://api.github.com/repos/cmus/cmus/releases{/id}","created_at":"2012-12-29T10:05:50Z","updated_at":"2014-08-05T07:09:43Z","pushed_at":"2014-07-25T07:35:22Z","git_url":"git://github.com/cmus/cmus.git","ssh_url":"git@github.com:cmus/cmus.git","clone_url":"https://github.com/cmus/cmus.git","svn_url":"https://github.com/cmus/cmus","homepage":"https://cmus.github.io/","size":7770,"stargazers_count":330,"watchers_count":330,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"forks_count":55,"mirror_url":null,"open_issues_count":30,"forks":55,"open_issues":30,"watchers":330,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/cmus/cmus/pulls/151"},"html":{"href":"https://github.com/cmus/cmus/pull/151"},"issue":{"href":"https://api.github.com/repos/cmus/cmus/issues/151"},"comments":{"href":"https://api.github.com/repos/cmus/cmus/issues/151/comments"},"review_comments":{"href":"https://api.github.com/repos/cmus/cmus/pulls/151/comments"},"review_comment":{"href":"https://api.github.com/repos/cmus/cmus/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/cmus/cmus/pulls/151/commits"},"statuses":{"href":"https://api.github.com/repos/cmus/cmus/statuses/afdae4c9ac621bd50c319f3a6c1ae2ac12b15dee"}}}}