Move to memory.
This commit is contained in:
parent
dab150abf0
commit
b4353adc0c
|
@ -13,7 +13,7 @@ pub const Bar = struct {
|
||||||
var thread = try std.Thread.spawn(w, Widget.start);
|
var thread = try std.Thread.spawn(w, Widget.start);
|
||||||
}
|
}
|
||||||
var thread = try std.Thread.spawn(self, Bar.process);
|
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;
|
self.running = false;
|
||||||
std.time.sleep(1000 * std.time.ns_per_ms);
|
std.time.sleep(1000 * std.time.ns_per_ms);
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -18,10 +18,15 @@ pub fn main() !void {
|
||||||
var allocator = &dbgAlloc.allocator;
|
var allocator = &dbgAlloc.allocator;
|
||||||
var bar = barImpl.InitBar(allocator);
|
var bar = barImpl.InitBar(allocator);
|
||||||
var br = Bar.init(&bar);
|
var br = Bar.init(&bar);
|
||||||
|
|
||||||
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
||||||
|
var arenacator = &arena.allocator;
|
||||||
|
|
||||||
const widgets = [_]*Widget{
|
const widgets = [_]*Widget{
|
||||||
&Widget.init(&textWidget.New("owo", "potato")),
|
&Widget.init(&textWidget.New("owo", "potato")),
|
||||||
&Widget.init(&weatherWidget.New(&br, "London")),
|
&Widget.init(&weatherWidget.New(arenacator, &br, "London")),
|
||||||
};
|
};
|
||||||
bar.widgets = widgets[0..];
|
bar.widgets = widgets[0..];
|
||||||
try br.start();
|
try br.start();
|
||||||
|
arena.deinit();
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ const colour = @import("../../formatting/colour.zig").colour;
|
||||||
const DebugAllocator = @import("../../debug_allocator.zig");
|
const DebugAllocator = @import("../../debug_allocator.zig");
|
||||||
|
|
||||||
pub const WeatherWidget = struct {
|
pub const WeatherWidget = struct {
|
||||||
|
allocator: *std.mem.Allocator,
|
||||||
bar: *Bar,
|
bar: *Bar,
|
||||||
weather_api_url: []const u8,
|
weather_api_url: []const u8,
|
||||||
info: ?Info,
|
info: ?Info,
|
||||||
|
@ -32,15 +33,11 @@ pub const WeatherWidget = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start(self: *WeatherWidget) anyerror!void {
|
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) {
|
while (true) {
|
||||||
std.time.sleep(2000 * std.time.ns_per_ms);
|
std.time.sleep(2000 * std.time.ns_per_ms);
|
||||||
|
|
||||||
std.debug.print("Starting Weather Widget.\n", .{});
|
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", .{});
|
std.debug.print("Connected to OpenWeatherMap.\n", .{});
|
||||||
|
|
||||||
var read_buffer: [512]u8 = undefined;
|
var read_buffer: [512]u8 = undefined;
|
||||||
|
@ -105,15 +102,15 @@ pub const WeatherWidget = struct {
|
||||||
} else if (temp == 18) {
|
} else if (temp == 18) {
|
||||||
tempColour = "yellow";
|
tempColour = "yellow";
|
||||||
}
|
}
|
||||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
var arena = std.heap.ArenaAllocator.init(self.allocator);
|
||||||
var arenacator = &arena.allocator;
|
var arenacator = &arena.allocator;
|
||||||
if (self.info != null) {
|
if (self.info != null) {
|
||||||
allocator.free(self.info.?.full_text);
|
self.allocator.free(self.info.?.full_text);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.info = Info{
|
self.info = Info{
|
||||||
.name = "weather",
|
.name = "weather",
|
||||||
.full_text = try std.fmt.allocPrint(allocator, "{} {}{}{} {}", .{
|
.full_text = try std.fmt.allocPrint(self.allocator, "{} {}{}{} {}", .{
|
||||||
colour(arenacator, "accentlight", "weather"),
|
colour(arenacator, "accentlight", "weather"),
|
||||||
colour(arenacator, tempColour, try std.fmt.allocPrint(arenacator, "{}", .{temp})),
|
colour(arenacator, tempColour, try std.fmt.allocPrint(arenacator, "{}", .{temp})),
|
||||||
colour(arenacator, "accentlight", "°"),
|
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{
|
return WeatherWidget{
|
||||||
|
.allocator = allocator,
|
||||||
.bar = bar,
|
.bar = bar,
|
||||||
.weather_api_url = "/data/2.5/weather?q=" ++ location ++ "&appid=dcea3595afe693d1c17846141f58ea10&units=metric",
|
.weather_api_url = "/data/2.5/weather?q=" ++ location ++ "&appid=dcea3595afe693d1c17846141f58ea10&units=metric",
|
||||||
.info = null,
|
.info = null,
|
||||||
|
|
Loading…
Reference in a new issue