1
0
Fork 0

Move to memory.

This commit is contained in:
namedkitten 2020-07-09 11:44:36 +01:00
parent dab150abf0
commit b4353adc0c
3 changed files with 14 additions and 11 deletions

View file

@ -13,7 +13,7 @@ pub const Bar = struct {
var thread = try std.Thread.spawn(w, Widget.start);
}
var thread = try std.Thread.spawn(self, Bar.process);
std.time.sleep(100000 * std.time.ns_per_ms);
std.time.sleep(10000 * std.time.ns_per_ms);
self.running = false;
std.time.sleep(1000 * std.time.ns_per_ms);
return;

View file

@ -18,10 +18,15 @@ pub fn main() !void {
var allocator = &dbgAlloc.allocator;
var bar = barImpl.InitBar(allocator);
var br = Bar.init(&bar);
var arena = std.heap.ArenaAllocator.init(allocator);
var arenacator = &arena.allocator;
const widgets = [_]*Widget{
&Widget.init(&textWidget.New("owo", "potato")),
&Widget.init(&weatherWidget.New(&br, "London")),
&Widget.init(&weatherWidget.New(arenacator, &br, "London")),
};
bar.widgets = widgets[0..];
try br.start();
arena.deinit();
}

View file

@ -8,6 +8,7 @@ const colour = @import("../../formatting/colour.zig").colour;
const DebugAllocator = @import("../../debug_allocator.zig");
pub const WeatherWidget = struct {
allocator: *std.mem.Allocator,
bar: *Bar,
weather_api_url: []const u8,
info: ?Info,
@ -32,15 +33,11 @@ pub const WeatherWidget = struct {
}
pub fn start(self: *WeatherWidget) anyerror!void {
var buffer: [@sizeOf(u8) * 1024 * 8]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buffer);
var allocator = &fba.allocator;
while (true) {
std.time.sleep(2000 * std.time.ns_per_ms);
std.debug.print("Starting Weather Widget.\n", .{});
var file = try net.tcpConnectToHost(allocator, "api.openweathermap.org", 80);
var file = try net.tcpConnectToHost(self.allocator, "api.openweathermap.org", 80);
std.debug.print("Connected to OpenWeatherMap.\n", .{});
var read_buffer: [512]u8 = undefined;
@ -105,15 +102,15 @@ pub const WeatherWidget = struct {
} else if (temp == 18) {
tempColour = "yellow";
}
var arena = std.heap.ArenaAllocator.init(allocator);
var arena = std.heap.ArenaAllocator.init(self.allocator);
var arenacator = &arena.allocator;
if (self.info != null) {
allocator.free(self.info.?.full_text);
self.allocator.free(self.info.?.full_text);
}
self.info = Info{
.name = "weather",
.full_text = try std.fmt.allocPrint(allocator, "{} {}{}{} {}", .{
.full_text = try std.fmt.allocPrint(self.allocator, "{} {}{}{} {}", .{
colour(arenacator, "accentlight", "weather"),
colour(arenacator, tempColour, try std.fmt.allocPrint(arenacator, "{}", .{temp})),
colour(arenacator, "accentlight", "°"),
@ -128,8 +125,9 @@ pub const WeatherWidget = struct {
}
};
pub inline fn New(bar: *Bar, comptime location: []const u8) WeatherWidget {
pub inline fn New(allocator: *std.mem.Allocator, bar: *Bar, comptime location: []const u8) WeatherWidget {
return WeatherWidget{
.allocator = allocator,
.bar = bar,
.weather_api_url = "/data/2.5/weather?q=" ++ location ++ "&appid=dcea3595afe693d1c17846141f58ea10&units=metric",
.info = null,