Skip to content

Commit d8dfb6b

Browse files
committed
finish adding IOContext to rest of atags
1 parent 36a1f4b commit d8dfb6b

File tree

1 file changed

+41
-53
lines changed

1 file changed

+41
-53
lines changed

src/tags_main.zig

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,25 +1131,24 @@ const CreatePool = struct {
11311131

11321132
ctx: *Context,
11331133
config: Config,
1134+
io: *IOContext,
11341135

11351136
const Self = @This();
11361137

1137-
pub fn init(ctx: *Context, config: Config) !Self {
1138-
return Self{ .ctx = ctx, .config = config };
1138+
pub fn init(ctx: *Context, config: Config, io: *IOContext) !Self {
1139+
return Self{ .ctx = ctx, .config = config, .io = io };
11391140
}
11401141

11411142
pub fn deinit(self: *Self) void {
11421143
_ = self;
11431144
}
11441145

11451146
pub fn run(self: *Self) !void {
1146-
var stdout = std.io.getStdOut().writer();
1147-
11481147
var pool = try self.ctx.createPool(self.config.title);
11491148
defer pool.deinit();
11501149

1151-
std.debug.print("pool created with id ", .{});
1152-
try stdout.print("{d}\n", .{pool.hash});
1150+
std.debug.print("pool created with id {d}", .{pool.hash.id});
1151+
try self.io.stdout().print("{d}\n", .{pool.hash});
11531152
}
11541153
};
11551154

@@ -1170,27 +1169,26 @@ const FetchPool = struct {
11701169

11711170
ctx: *Context,
11721171
config: Config,
1172+
io: *IOContext,
11731173

11741174
const Self = @This();
11751175

1176-
pub fn init(ctx: *Context, config: Config) !Self {
1177-
return Self{ .ctx = ctx, .config = config };
1176+
pub fn init(ctx: *Context, config: Config, io: *IOContext) !Self {
1177+
return Self{ .ctx = ctx, .config = config, .io = io };
11781178
}
11791179

11801180
pub fn deinit(self: *Self) void {
11811181
_ = self;
11821182
}
11831183

11841184
pub fn run(self: *Self) !void {
1185-
var stdout = std.io.getStdOut().writer();
1186-
11871185
var pool = (try self.ctx.fetchPool(self.config.pool_id)) orelse return error.PoolNotFound;
11881186
defer pool.deinit();
11891187

11901188
const file_hashes = try pool.fetchFiles(self.ctx.allocator);
11911189
defer self.ctx.allocator.free(file_hashes);
11921190

1193-
try stdout.print(
1191+
try self.io.stdout().print(
11941192
"pool '{s}' {s}\n",
11951193
.{ pool.title, pool.hash },
11961194
);
@@ -1199,9 +1197,9 @@ const FetchPool = struct {
11991197
var file = (try self.ctx.fetchFile(file_hash.id)).?;
12001198
defer file.deinit();
12011199

1202-
try stdout.print("- {s}", .{file.local_path});
1203-
try file.printTagsTo(self.ctx.allocator, stdout, .{});
1204-
try stdout.print("\n", .{});
1200+
try self.io.stdout().print("- {s}", .{file.local_path});
1201+
try file.printTagsTo(self.ctx.allocator, self.io.stdout(), .{});
1202+
try self.io.stdout().print("\n", .{});
12051203
}
12061204
}
12071205
};
@@ -1221,20 +1219,19 @@ const SearchPool = struct {
12211219

12221220
ctx: *Context,
12231221
config: Config,
1222+
io: *IOContext,
12241223

12251224
const Self = @This();
12261225

1227-
pub fn init(ctx: *Context, config: Config) !Self {
1228-
return Self{ .ctx = ctx, .config = config };
1226+
pub fn init(ctx: *Context, config: Config, io: *IOContext) !Self {
1227+
return Self{ .ctx = ctx, .config = config, .io = io };
12291228
}
12301229

12311230
pub fn deinit(self: *Self) void {
12321231
_ = self;
12331232
}
12341233

12351234
pub fn run(self: *Self) !void {
1236-
var stdout = std.io.getStdOut().writer();
1237-
12381235
var stmt = try self.ctx.db.prepare(
12391236
\\ select pool_hash
12401237
\\ from pools
@@ -1254,7 +1251,7 @@ const SearchPool = struct {
12541251
var pool = (try self.ctx.fetchPool(ID.new(pool_hash))).?;
12551252
defer pool.deinit();
12561253

1257-
try stdout.print(
1254+
try self.io.stdout().print(
12581255
"pool '{s}' {s}\n",
12591256
.{ pool.title, pool.hash },
12601257
);
@@ -1277,24 +1274,23 @@ const RemovePool = struct {
12771274

12781275
ctx: *Context,
12791276
config: Config,
1277+
io: *IOContext,
12801278

12811279
const Self = @This();
12821280

1283-
pub fn init(ctx: *Context, config: Config) !Self {
1284-
return Self{ .ctx = ctx, .config = config };
1281+
pub fn init(ctx: *Context, config: Config, io: *IOContext) !Self {
1282+
return Self{ .ctx = ctx, .config = config, .io = io };
12851283
}
12861284

12871285
pub fn deinit(self: *Self) void {
12881286
_ = self;
12891287
}
12901288

12911289
pub fn run(self: *Self) !void {
1292-
var stdout = std.io.getStdOut().writer();
1293-
12941290
var pool = (try self.ctx.fetchPool(self.config.pool_id)) orelse return error.PoolNotFound;
12951291
defer pool.deinit();
12961292

1297-
try stdout.print(
1293+
try self.io.stdout().print(
12981294
"pool '{s}' {s} will be removed\n",
12991295
.{ pool.title, pool.hash },
13001296
);
@@ -1323,23 +1319,22 @@ const CreateSource = struct {
13231319

13241320
ctx: *Context,
13251321
config: Config,
1322+
io: *IOContext,
13261323

13271324
const Self = @This();
13281325

1329-
pub fn init(ctx: *Context, config: Config) !Self {
1330-
return Self{ .ctx = ctx, .config = config };
1326+
pub fn init(ctx: *Context, config: Config, io: *IOContext) !Self {
1327+
return Self{ .ctx = ctx, .config = config, .io = io };
13311328
}
13321329

13331330
pub fn deinit(self: *Self) void {
13341331
_ = self;
13351332
}
13361333

13371334
pub fn run(self: *Self) !void {
1338-
var stdout = std.io.getStdOut().writer();
1339-
13401335
const source = try self.ctx.createTagSource(self.config.title, .{});
1341-
std.debug.print("source created with id ", .{});
1342-
try stdout.print("{d}\n", .{source.id});
1336+
std.debug.print("source created with id {d}", .{source.id});
1337+
try self.io.stdout().print("{d}\n", .{source.id});
13431338
}
13441339
};
13451340

@@ -1357,26 +1352,24 @@ const RemoveSource = struct {
13571352

13581353
ctx: *Context,
13591354
config: Config,
1355+
io: *IOContext,
13601356

13611357
const Self = @This();
13621358

1363-
pub fn init(ctx: *Context, config: Config) !Self {
1364-
return Self{ .ctx = ctx, .config = config };
1359+
pub fn init(ctx: *Context, config: Config, io: *IOContext) !Self {
1360+
return Self{ .ctx = ctx, .config = config, .io = io };
13651361
}
13661362

13671363
pub fn deinit(self: *Self) void {
13681364
_ = self;
13691365
}
13701366

13711367
pub fn run(self: *Self) !void {
1372-
var stdout = std.io.getStdOut().writer();
1373-
13741368
const source =
13751369
(try self.ctx.fetchTagSource(.external, self.config.id)) orelse return error.SourceNotFound;
13761370

13771371
try source.delete();
1378-
1379-
try stdout.print("ok\n", .{});
1372+
try self.io.stdout().print("ok\n", .{});
13801373
}
13811374
};
13821375

@@ -1389,20 +1382,19 @@ const ListSource = struct {
13891382

13901383
ctx: *Context,
13911384
config: void,
1385+
io: *IOContext,
13921386

13931387
const Self = @This();
13941388

1395-
pub fn init(ctx: *Context, config: void) !Self {
1396-
return Self{ .ctx = ctx, .config = config };
1389+
pub fn init(ctx: *Context, config: void, io: *IOContext) !Self {
1390+
return Self{ .ctx = ctx, .config = config, .io = io };
13971391
}
13981392

13991393
pub fn deinit(self: *Self) void {
14001394
_ = self;
14011395
}
14021396

14031397
pub fn run(self: *Self) !void {
1404-
const raw_stdout = std.io.getStdOut().writer();
1405-
14061398
var stmt = try self.ctx.db.prepare(
14071399
\\ select type, id, name
14081400
\\ from tag_sources
@@ -1416,18 +1408,14 @@ const ListSource = struct {
14161408
self.ctx.allocator.free(entries);
14171409
}
14181410

1419-
const BufferedFileWriter = std.io.BufferedWriter(4096, std.fs.File.Writer);
1420-
var buffered_stdout = BufferedFileWriter{ .unbuffered_writer = raw_stdout };
1421-
var stdout = buffered_stdout.writer();
1422-
14231411
for (entries) |row| {
1424-
try stdout.print(
1412+
try self.io.stdout().print(
14251413
"type={d} id={d}: name={s}\n",
14261414
.{ row.type, row.id, row.name },
14271415
);
14281416
}
14291417

1430-
try buffered_stdout.flush();
1418+
try self.io.flushStdout();
14311419
}
14321420
};
14331421

@@ -1613,38 +1601,38 @@ pub fn main() anyerror!void {
16131601
},
16141602

16151603
.CreatePool => |config| {
1616-
var self = try CreatePool.init(&ctx, config);
1604+
var self = try CreatePool.init(&ctx, config, &default_io);
16171605
defer self.deinit();
16181606
try self.run();
16191607
},
16201608
.FetchPool => |config| {
1621-
var self = try FetchPool.init(&ctx, config);
1609+
var self = try FetchPool.init(&ctx, config, &default_io);
16221610
defer self.deinit();
16231611
try self.run();
16241612
},
16251613
.SearchPool => |config| {
1626-
var self = try SearchPool.init(&ctx, config);
1614+
var self = try SearchPool.init(&ctx, config, &default_io);
16271615
defer self.deinit();
16281616
try self.run();
16291617
},
16301618
.RemovePool => |config| {
1631-
var self = try RemovePool.init(&ctx, config);
1619+
var self = try RemovePool.init(&ctx, config, &default_io);
16321620
defer self.deinit();
16331621
try self.run();
16341622
},
16351623

16361624
.CreateSource => |config| {
1637-
var self = try CreateSource.init(&ctx, config);
1625+
var self = try CreateSource.init(&ctx, config, &default_io);
16381626
defer self.deinit();
16391627
try self.run();
16401628
},
16411629
.ListSource => |config| {
1642-
var self = try ListSource.init(&ctx, config);
1630+
var self = try ListSource.init(&ctx, config, &default_io);
16431631
defer self.deinit();
16441632
try self.run();
16451633
},
16461634
.RemoveSource => |config| {
1647-
var self = try RemoveSource.init(&ctx, config);
1635+
var self = try RemoveSource.init(&ctx, config, &default_io);
16481636
defer self.deinit();
16491637
try self.run();
16501638
},

0 commit comments

Comments
 (0)