add: spellcheck utility implementation using ispell
This commit is contained in:
121
src/main.zig
Normal file
121
src/main.zig
Normal file
@@ -0,0 +1,121 @@
|
||||
const std = @import("std");
|
||||
const heap = std.heap;
|
||||
const os = std.os;
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
var area = heap.ArenaAllocator.init(gpa.allocator());
|
||||
defer area.deinit();
|
||||
|
||||
const allocator = area.allocator();
|
||||
const stdin = std.io.getStdIn().reader();
|
||||
const stat = try std.io.getStdIn().stat();
|
||||
var word: []const u8 = undefined;
|
||||
|
||||
switch (stat.kind) {
|
||||
.named_pipe => {
|
||||
if (try stdin.readUntilDelimiterOrEofAlloc(allocator, '\n', 1024)) |input| {
|
||||
word = input;
|
||||
}
|
||||
},
|
||||
else => {
|
||||
var arg_iterator = try std.process.argsWithAllocator(allocator);
|
||||
// skip own executable name
|
||||
_ = arg_iterator.next();
|
||||
const next = arg_iterator.next();
|
||||
if (next) |input| {
|
||||
word = input;
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
// start ispell process
|
||||
var process = std.ChildProcess.init(&.{ "ispell", "-a" }, allocator);
|
||||
process.stdin_behavior = .Pipe;
|
||||
process.stdout_behavior = .Pipe;
|
||||
process.stderr_behavior = .Pipe;
|
||||
|
||||
var stdout = std.ArrayList(u8).init(allocator);
|
||||
var stderr = std.ArrayList(u8).init(allocator);
|
||||
defer {
|
||||
stdout.deinit();
|
||||
stderr.deinit();
|
||||
}
|
||||
|
||||
try process.spawn();
|
||||
// stdin behavior is .Pipe, hence we 'pipe' input into the process after spawning it
|
||||
try process.stdin.?.writeAll(word);
|
||||
// telling the process that the input is complete
|
||||
process.stdin.?.close();
|
||||
// collecting the resulting output
|
||||
try process.collectOutput(&stdout, &stderr, 1024);
|
||||
// erease pipe content -> assign to null
|
||||
process.stdin = null;
|
||||
_ = try process.wait();
|
||||
var output = try stdout.toOwnedSlice();
|
||||
// currently we do not care about stderr outputs
|
||||
_ = try stderr.toOwnedSlice();
|
||||
|
||||
// extract the second line of the output as the first line is only information about ispell
|
||||
var content: []u8 = try allocator.alloc(u8, 1024);
|
||||
var idx: usize = 0;
|
||||
var start_idx: usize = 0;
|
||||
while (idx < output.len) : (idx += 1) {
|
||||
const char = output[idx];
|
||||
if (char == '\n') {
|
||||
start_idx = idx + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
std.mem.copy(u8, content, output[start_idx .. output.len - 1]);
|
||||
defer allocator.free(content);
|
||||
|
||||
// prepare for writing output to stdout
|
||||
const stdout_file = std.io.getStdOut().writer();
|
||||
var bw = std.io.bufferedWriter(stdout_file);
|
||||
const stdout_writer = bw.writer();
|
||||
|
||||
if (content[0] == '*' or content[0] == '+') {
|
||||
// given word was correct so just return the input as no replacement is necessary
|
||||
try stdout_writer.print("{s}", .{word});
|
||||
} else {
|
||||
// there was a suggestion made by ispell
|
||||
// just select the first one (maybe I can make the user select an option?)
|
||||
idx = 2 + word.len + 1;
|
||||
start_idx = idx;
|
||||
var end_idx = start_idx;
|
||||
// get selection count
|
||||
while (content[end_idx] != ' ') : (end_idx += 1) {}
|
||||
const suggestion_count = try std.fmt.parseInt(u8, content[start_idx..end_idx], 10);
|
||||
var suggestion_idx: usize = 0;
|
||||
var suggestions: [][]u8 = try allocator.alloc([]u8, suggestion_count);
|
||||
defer {
|
||||
for (suggestions) |suggestion| {
|
||||
allocator.free(suggestion);
|
||||
}
|
||||
allocator.free(suggestions);
|
||||
}
|
||||
idx = idx + 3 + 2;
|
||||
start_idx = idx;
|
||||
end_idx = start_idx + 1;
|
||||
// extract all selections
|
||||
while (suggestion_idx < suggestion_count) {
|
||||
// get index of the next suggestion
|
||||
while (content[end_idx] != ',') : (end_idx += 1) {
|
||||
if (end_idx == content.len - 1 or content[end_idx] == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
const size: usize = end_idx - start_idx;
|
||||
var suggestion: []u8 = try allocator.alloc(u8, size);
|
||||
std.mem.copy(u8, suggestion, content[start_idx..end_idx]);
|
||||
suggestions[suggestion_idx] = suggestion;
|
||||
suggestion_idx += 1;
|
||||
start_idx = end_idx + 2;
|
||||
end_idx = start_idx + 1;
|
||||
}
|
||||
try stdout_writer.print("{s}", .{suggestions[0]});
|
||||
}
|
||||
try bw.flush(); // don't forget to flush!
|
||||
}
|
||||
Reference in New Issue
Block a user