dotfiles/scripts/vinput/src/main.zig

101 lines
2.6 KiB
Zig
Raw Normal View History

2022-10-16 20:31:31 +02:00
const std = @import("std");
const c = @import("ffi.zig").c;
const ClipboardConnection = @import("ClipboardConnection.zig");
2022-10-16 20:31:31 +02:00
pub const std_options = std.Options{
.log_level = .debug,
2024-03-20 20:45:59 +01:00
.logFn = @import("common").logFn,
2023-03-16 08:18:45 +01:00
};
2022-10-16 20:31:31 +02:00
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
2022-10-17 18:44:31 +02:00
if (std.os.argv.len == 1 or std.os.argv.len > 2) {
std.log.err(
\\ Invalid usage.
\\ Usage: {s} FILE_EXTENSION
, .{std.os.argv[0]});
return error.InvalidCli;
}
2022-10-16 20:31:31 +02:00
var alloc = gpa.allocator();
const filename = try std.fmt.allocPrint(
alloc,
2022-10-17 18:44:31 +02:00
"/tmp/vinput{}-{}.{s}",
.{ std.os.linux.getuid(), std.time.milliTimestamp(), std.os.argv[1] },
2022-10-16 20:31:31 +02:00
);
defer alloc.free(filename);
2023-07-01 19:20:02 +02:00
var cp = try ClipboardConnection.init();
defer cp.deinit();
{
const file = try std.fs.createFileAbsolute(filename, .{});
defer file.close();
std.log.info("telling compositor to write clipboard content into tmpfile...", .{});
try cp.getContent(file.handle);
}
2022-10-17 10:47:36 +02:00
//const editor_argv = [_][]const u8{
// "neovide",
2023-11-04 11:50:56 +01:00
// "--no-fork",
2023-10-25 20:02:43 +02:00
// "--wayland_app_id",
2022-10-17 18:44:31 +02:00
// "vinput-editor",
2022-10-17 10:47:36 +02:00
// filename,
//};
const editor_argv = [_][]const u8{
2023-10-25 20:02:43 +02:00
"foot",
"--app-id",
2022-10-17 18:44:31 +02:00
"vinput-editor",
2023-06-01 16:49:56 +02:00
"--",
2022-10-17 10:47:36 +02:00
"nvim",
"--cmd",
"let g:started_by_vinput=v:true",
2022-10-16 20:31:31 +02:00
filename,
};
2022-10-17 10:47:36 +02:00
std.log.info("invoking editor with command {s}", .{&editor_argv});
2022-10-16 20:31:31 +02:00
2022-10-17 10:47:36 +02:00
var nvide_child = std.ChildProcess.init(&editor_argv, alloc);
2022-10-16 20:31:31 +02:00
_ = try nvide_child.spawnAndWait();
const stat = std.fs.cwd().statFile(filename) catch |e| {
switch (e) {
error.FileNotFound => {
std.log.warn("tempfile doesn't exist; aborting", .{});
return;
},
else => return e,
}
};
mmap: {
if (stat.size == 0) {
std.log.info("empty file", .{});
break :mmap;
}
2022-10-16 20:31:31 +02:00
var tempfile = try std.fs.openFileAbsolute(filename, .{});
defer tempfile.close();
std.log.info("mmapping tempfile", .{});
const fcontent = try std.os.mmap(
null,
stat.size,
std.os.PROT.READ,
2024-02-10 17:40:22 +01:00
.{ .TYPE = .PRIVATE },
2022-10-16 20:31:31 +02:00
tempfile.handle,
0,
);
defer std.os.munmap(fcontent);
try cp.serveContent(std.mem.trim(u8, fcontent, " \n\r"));
2022-10-16 20:31:31 +02:00
}
std.log.info("deleting tempfile {s}", .{filename});
try std.fs.deleteFileAbsolute(filename);
}