Skip to content

Commit 9feea04

Browse files
committed
feat: implement basic search feature
1 parent 4782d7f commit 9feea04

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

src/cli.zig

+49-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,44 @@
11
const std = @import("std");
22
const cli = @import("zig-cli");
3-
const data = @import("./const.zig");
4-
const GitmojiConfig = data.GitmojiConfig;
3+
const data = @import("./data.zig");
4+
const GitmojiConfig = @import("./models.zig").GitmojiConfig;
55

66
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
77
const allocator = gpa.allocator();
88

9+
var args_definition = struct {
10+
keyword: []const u8 = undefined,
11+
}{};
12+
13+
fn contains_subsequence(haystack: []const u8, needle: []const u8) bool {
14+
const needle_len = needle.len;
15+
const haystack_len = haystack.len;
16+
for (0..haystack_len - needle_len - 1) |i| {
17+
if (std.mem.eql(u8, haystack[i .. i + needle_len], needle)) {
18+
return true;
19+
}
20+
}
21+
return false;
22+
}
23+
924
fn run_list() !void {
10-
const data_list = [_]GitmojiConfig{ GitmojiConfig{ .emoji = "🎨", .description = "Improve structure / format of the code." }, GitmojiConfig{ .emoji = "⚡️", .description = "Improve performance." }, GitmojiConfig{ .emoji = "🔥", .description = "Remove code or files." }, GitmojiConfig{ .emoji = "🐛", .description = "Fix a bug." }, GitmojiConfig{ .emoji = "🚑️", .description = "Critical hotfix." }, GitmojiConfig{ .emoji = "✨", .description = "Introduce new features." }, GitmojiConfig{ .emoji = "📝", .description = "Add or update documentation." }, GitmojiConfig{ .emoji = "🚀", .description = "Deploy stuff." }, GitmojiConfig{ .emoji = "💄", .description = "Add or update the UI and style files." }, GitmojiConfig{ .emoji = "🎉", .description = "Begin a project." }, GitmojiConfig{ .emoji = "✅", .description = "Add, update, or pass tests." }, GitmojiConfig{ .emoji = "🔒️", .description = "Fix security or privacy issues." }, GitmojiConfig{ .emoji = "🔐", .description = "Add or update secrets." }, GitmojiConfig{ .emoji = "🔖", .description = "Release / Version tags." }, GitmojiConfig{ .emoji = "🚨", .description = "Fix compiler / linter warnings." }, GitmojiConfig{ .emoji = "🚧", .description = "Work in progress." }, GitmojiConfig{ .emoji = "💚", .description = "Fix CI Build." }, GitmojiConfig{ .emoji = "⬇️", .description = "Downgrade dependencies." }, GitmojiConfig{ .emoji = "⬆️", .description = "Upgrade dependencies." }, GitmojiConfig{ .emoji = "📌", .description = "Pin dependencies to specific versions." }, GitmojiConfig{ .emoji = "👷", .description = "Add or update CI build system." }, GitmojiConfig{ .emoji = "📈", .description = "Add or update analytics or track code." }, GitmojiConfig{ .emoji = "♻️", .description = "Refactor code." }, GitmojiConfig{ .emoji = "➕", .description = "Add a dependency." }, GitmojiConfig{ .emoji = "➖", .description = "Remove a dependency." }, GitmojiConfig{ .emoji = "🔧", .description = "Add or update configuration files." }, GitmojiConfig{ .emoji = "🔨", .description = "Add or update development scripts." }, GitmojiConfig{ .emoji = "🌐", .description = "Internationalization and localization." }, GitmojiConfig{ .emoji = "✏️", .description = "Fix typos." }, GitmojiConfig{ .emoji = "💩", .description = "Write bad code that needs to be improved." }, GitmojiConfig{ .emoji = "⏪️", .description = "Revert changes." }, GitmojiConfig{ .emoji = "🔀", .description = "Merge branches." }, GitmojiConfig{ .emoji = "📦️", .description = "Add or update compiled files or packages." }, GitmojiConfig{ .emoji = "👽️", .description = "Update code due to external API changes." }, GitmojiConfig{ .emoji = "🚚", .description = "Move or r ename resources (e.g.: files, paths, routes)." }, GitmojiConfig{ .emoji = "📄", .description = "Add or update license." }, GitmojiConfig{ .emoji = "💥", .description = "Introduce breaking changes." }, GitmojiConfig{ .emoji = "🍱", .description = "Add or update assets." }, GitmojiConfig{ .emoji = "♿️", .description = "Improve accessibility." }, GitmojiConfig{ .emoji = "💡", .description = "Add or update comments in source code." }, GitmojiConfig{ .emoji = "🍻", .description = "Write code drunkenly." }, GitmojiConfig{ .emoji = "💬", .description = "Add or update text and literals." }, GitmojiConfig{ .emoji = "🗃️", .description = "Perform database related changes." }, GitmojiConfig{ .emoji = "🔊", .description = "Add or update logs." }, GitmojiConfig{ .emoji = "🔇", .description = "Remove logs." }, GitmojiConfig{ .emoji = "👥", .description = "Add or update contributor(s)." }, GitmojiConfig{ .emoji = "🚸", .description = "Improve user experience / usability." }, GitmojiConfig{ .emoji = "🏗️", .description = "Make architectural changes." }, GitmojiConfig{ .emoji = "📱", .description = "Work on responsive design." }, GitmojiConfig{ .emoji = "🤡", .description = "Mock things." }, GitmojiConfig{ .emoji = "🥚", .description = "Add or update an easter egg." }, GitmojiConfig{ .emoji = "🙈", .description = "Add or update a .gitignore file." }, GitmojiConfig{ .emoji = "📸", .description = "Add or update snapshots." }, GitmojiConfig{ .emoji = "⚗️", .description = "Perform experiments." }, GitmojiConfig{ .emoji = "🔍️", .description = "Improve SEO." }, GitmojiConfig{ .emoji = "🏷️", .description = "Add or update types." }, GitmojiConfig{ .emoji = "🌱", .description = "Add or update seed files." }, GitmojiConfig{ .emoji = "🚩", .description = "Add, update, or remove feature flags." }, GitmojiConfig{ .emoji = "🥅", .description = "Catch errors." }, GitmojiConfig{ .emoji = "💫", .description = "Add or update animations and transitions." }, GitmojiConfig{ .emoji = "🗑️", .description = "Deprecate code that needs to be cleaned up." }, GitmojiConfig{ .emoji = "🛂", .description = "Work on code related to authorization, roles and permissions." }, GitmojiConfig{ .emoji = "🩹", .description = "Simple fix for a non-critical issue." }, GitmojiConfig{ .emoji = "🧐", .description = "Data exploration/inspection." }, GitmojiConfig{ .emoji = "⚰️", .description = "Remove dead code." }, GitmojiConfig{ .emoji = "🧪", .description = "Add a failing test." }, GitmojiConfig{ .emoji = "👔", .description = "Add or update business logic." }, GitmojiConfig{ .emoji = "🩺", .description = "Add or update healthcheck." }, GitmojiConfig{ .emoji = "🧱", .description = "Infrastructure related changes." }, GitmojiConfig{ .emoji = "🧑‍💻", .description = "Improve developer experience." }, GitmojiConfig{ .emoji = "💸", .description = "Add sponsorships or money related infrastructure." }, GitmojiConfig{ .emoji = "🧵", .description = "Add or update code related to multithreading or concurrency." }, GitmojiConfig{ .emoji = "🦺", .description = "Add or update code related to validation." } };
25+
const data_list = data.data_list;
1126

1227
for (data_list) |gitmoji| {
1328
std.log.info("{s} - {s}", .{ gitmoji.emoji, gitmoji.description });
1429
}
1530
}
1631

1732
fn run_search() !void {
18-
std.log.info("Not implemented yet !", .{});
33+
const args = &args_definition;
34+
const keyword = args.keyword;
35+
std.log.info("Searching for {s}...", .{keyword});
36+
const original_list = data.data_list;
37+
for (original_list) |gitmoji| {
38+
if (contains_subsequence(gitmoji.description, keyword)) {
39+
std.log.info("{s} - {s}", .{ gitmoji.emoji, gitmoji.description });
40+
}
41+
}
1942
}
2043

2144
fn list_command() !cli.Command {
@@ -26,12 +49,11 @@ fn list_command() !cli.Command {
2649
};
2750
}
2851

29-
fn search_command() !cli.Command {
30-
return cli.Command{
31-
.name = "search",
32-
.description = cli.Description{ .one_line = "Searches for a commit emoji based on a keyword." },
33-
.target = cli.CommandTarget{ .action = cli.CommandAction{ .exec = run_search } },
34-
};
52+
fn free_args_definition() void {
53+
allocator.free(args_definition.keyword);
54+
if (gpa.deinit() == .leak) {
55+
@panic("args definition leaked");
56+
}
3557
}
3658

3759
pub fn main_cli() cli.AppRunner.Error!cli.ExecFn {
@@ -43,7 +65,20 @@ pub fn main_cli() cli.AppRunner.Error!cli.ExecFn {
4365
.target = cli.CommandTarget{
4466
.subcommands = &.{
4567
try list_command(),
46-
try search_command(),
68+
cli.Command{
69+
.name = "search",
70+
.description = cli.Description{ .one_line = "Searches for a commit emoji based on a keyword." },
71+
.options = &[_]cli.Option{
72+
cli.Option{
73+
.long_name = "keyword",
74+
.short_alias = 'k',
75+
.required = true,
76+
.help = "The keyword to search for in the list.",
77+
.value_ref = alloc.mkRef(&args_definition.keyword),
78+
},
79+
},
80+
.target = cli.CommandTarget{ .action = cli.CommandAction{ .exec = run_search } },
81+
},
4782
},
4883
},
4984
};
@@ -53,5 +88,8 @@ pub fn main_cli() cli.AppRunner.Error!cli.ExecFn {
5388
.author = "GRK",
5489
.command = main_command,
5590
};
91+
92+
free_args_definition();
93+
5694
return alloc.getAction(&app);
5795
}

src/data.zig

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const models = @import("models.zig");
2+
const GitmojiConfig = models.GitmojiConfig;
3+
4+
pub const data_list = [_]GitmojiConfig{ GitmojiConfig{ .emoji = "🎨", .description = "Improve structure / format of the code." }, GitmojiConfig{ .emoji = "⚡️", .description = "Improve performance." }, GitmojiConfig{ .emoji = "🔥", .description = "Remove code or files." }, GitmojiConfig{ .emoji = "🐛", .description = "Fix a bug." }, GitmojiConfig{ .emoji = "🚑️", .description = "Critical hotfix." }, GitmojiConfig{ .emoji = "✨", .description = "Introduce new features." }, GitmojiConfig{ .emoji = "📝", .description = "Add or update documentation." }, GitmojiConfig{ .emoji = "🚀", .description = "Deploy stuff." }, GitmojiConfig{ .emoji = "💄", .description = "Add or update the UI and style files." }, GitmojiConfig{ .emoji = "🎉", .description = "Begin a project." }, GitmojiConfig{ .emoji = "✅", .description = "Add, update, or pass tests." }, GitmojiConfig{ .emoji = "🔒️", .description = "Fix security or privacy issues." }, GitmojiConfig{ .emoji = "🔐", .description = "Add or update secrets." }, GitmojiConfig{ .emoji = "🔖", .description = "Release / Version tags." }, GitmojiConfig{ .emoji = "🚨", .description = "Fix compiler / linter warnings." }, GitmojiConfig{ .emoji = "🚧", .description = "Work in progress." }, GitmojiConfig{ .emoji = "💚", .description = "Fix CI Build." }, GitmojiConfig{ .emoji = "⬇️", .description = "Downgrade dependencies." }, GitmojiConfig{ .emoji = "⬆️", .description = "Upgrade dependencies." }, GitmojiConfig{ .emoji = "📌", .description = "Pin dependencies to specific versions." }, GitmojiConfig{ .emoji = "👷", .description = "Add or update CI build system." }, GitmojiConfig{ .emoji = "📈", .description = "Add or update analytics or track code." }, GitmojiConfig{ .emoji = "♻️", .description = "Refactor code." }, GitmojiConfig{ .emoji = "➕", .description = "Add a dependency." }, GitmojiConfig{ .emoji = "➖", .description = "Remove a dependency." }, GitmojiConfig{ .emoji = "🔧", .description = "Add or update configuration files." }, GitmojiConfig{ .emoji = "🔨", .description = "Add or update development scripts." }, GitmojiConfig{ .emoji = "🌐", .description = "Internationalization and localization." }, GitmojiConfig{ .emoji = "✏️", .description = "Fix typos." }, GitmojiConfig{ .emoji = "💩", .description = "Write bad code that needs to be improved." }, GitmojiConfig{ .emoji = "⏪️", .description = "Revert changes." }, GitmojiConfig{ .emoji = "🔀", .description = "Merge branches." }, GitmojiConfig{ .emoji = "📦️", .description = "Add or update compiled files or packages." }, GitmojiConfig{ .emoji = "👽️", .description = "Update code due to external API changes." }, GitmojiConfig{ .emoji = "🚚", .description = "Move or r ename resources (e.g.: files, paths, routes)." }, GitmojiConfig{ .emoji = "📄", .description = "Add or update license." }, GitmojiConfig{ .emoji = "💥", .description = "Introduce breaking changes." }, GitmojiConfig{ .emoji = "🍱", .description = "Add or update assets." }, GitmojiConfig{ .emoji = "♿️", .description = "Improve accessibility." }, GitmojiConfig{ .emoji = "💡", .description = "Add or update comments in source code." }, GitmojiConfig{ .emoji = "🍻", .description = "Write code drunkenly." }, GitmojiConfig{ .emoji = "💬", .description = "Add or update text and literals." }, GitmojiConfig{ .emoji = "🗃️", .description = "Perform database related changes." }, GitmojiConfig{ .emoji = "🔊", .description = "Add or update logs." }, GitmojiConfig{ .emoji = "🔇", .description = "Remove logs." }, GitmojiConfig{ .emoji = "👥", .description = "Add or update contributor(s)." }, GitmojiConfig{ .emoji = "🚸", .description = "Improve user experience / usability." }, GitmojiConfig{ .emoji = "🏗️", .description = "Make architectural changes." }, GitmojiConfig{ .emoji = "📱", .description = "Work on responsive design." }, GitmojiConfig{ .emoji = "🤡", .description = "Mock things." }, GitmojiConfig{ .emoji = "🥚", .description = "Add or update an easter egg." }, GitmojiConfig{ .emoji = "🙈", .description = "Add or update a .gitignore file." }, GitmojiConfig{ .emoji = "📸", .description = "Add or update snapshots." }, GitmojiConfig{ .emoji = "⚗️", .description = "Perform experiments." }, GitmojiConfig{ .emoji = "🔍️", .description = "Improve SEO." }, GitmojiConfig{ .emoji = "🏷️", .description = "Add or update types." }, GitmojiConfig{ .emoji = "🌱", .description = "Add or update seed files." }, GitmojiConfig{ .emoji = "🚩", .description = "Add, update, or remove feature flags." }, GitmojiConfig{ .emoji = "🥅", .description = "Catch errors." }, GitmojiConfig{ .emoji = "💫", .description = "Add or update animations and transitions." }, GitmojiConfig{ .emoji = "🗑️", .description = "Deprecate code that needs to be cleaned up." }, GitmojiConfig{ .emoji = "🛂", .description = "Work on code related to authorization, roles and permissions." }, GitmojiConfig{ .emoji = "🩹", .description = "Simple fix for a non-critical issue." }, GitmojiConfig{ .emoji = "🧐", .description = "Data exploration/inspection." }, GitmojiConfig{ .emoji = "⚰️", .description = "Remove dead code." }, GitmojiConfig{ .emoji = "🧪", .description = "Add a failing test." }, GitmojiConfig{ .emoji = "👔", .description = "Add or update business logic." }, GitmojiConfig{ .emoji = "🩺", .description = "Add or update healthcheck." }, GitmojiConfig{ .emoji = "🧱", .description = "Infrastructure related changes." }, GitmojiConfig{ .emoji = "🧑‍💻", .description = "Improve developer experience." }, GitmojiConfig{ .emoji = "💸", .description = "Add sponsorships or money related infrastructure." }, GitmojiConfig{ .emoji = "🧵", .description = "Add or update code related to multithreading or concurrency." }, GitmojiConfig{ .emoji = "🦺", .description = "Add or update code related to validation." } };
File renamed without changes.

0 commit comments

Comments
 (0)