mirror of
https://github.com/fairyglade/ly.git
synced 2026-03-25 09:46:06 +00:00
Move back custom widgets into main project
Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
@@ -1,110 +0,0 @@
|
||||
const std = @import("std");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const keyboard = @import("../keyboard.zig");
|
||||
const TerminalBuffer = @import("../TerminalBuffer.zig");
|
||||
const Widget = @import("../Widget.zig");
|
||||
const generic = @import("generic.zig");
|
||||
|
||||
const MessageLabel = generic.CyclableLabel(Message, Message);
|
||||
|
||||
const InfoLine = @This();
|
||||
|
||||
const Message = struct {
|
||||
width: usize,
|
||||
text: []const u8,
|
||||
bg: u32,
|
||||
fg: u32,
|
||||
};
|
||||
|
||||
label: MessageLabel,
|
||||
|
||||
pub fn init(
|
||||
allocator: Allocator,
|
||||
buffer: *TerminalBuffer,
|
||||
width: usize,
|
||||
arrow_fg: u32,
|
||||
arrow_bg: u32,
|
||||
) InfoLine {
|
||||
return .{
|
||||
.label = MessageLabel.init(
|
||||
allocator,
|
||||
buffer,
|
||||
drawItem,
|
||||
null,
|
||||
null,
|
||||
width,
|
||||
true,
|
||||
arrow_fg,
|
||||
arrow_bg,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *InfoLine) void {
|
||||
self.label.deinit();
|
||||
}
|
||||
|
||||
pub fn widget(self: *InfoLine) Widget {
|
||||
return Widget.init(
|
||||
"InfoLine",
|
||||
self,
|
||||
deinit,
|
||||
null,
|
||||
draw,
|
||||
null,
|
||||
handle,
|
||||
null,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn addMessage(self: *InfoLine, text: []const u8, bg: u32, fg: u32) !void {
|
||||
if (text.len == 0) return;
|
||||
|
||||
try self.label.addItem(.{
|
||||
.width = TerminalBuffer.strWidth(text),
|
||||
.text = text,
|
||||
.bg = bg,
|
||||
.fg = fg,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn clearRendered(self: InfoLine, allocator: Allocator) !void {
|
||||
// Draw over the area
|
||||
const spaces = try allocator.alloc(u8, self.label.width - 2);
|
||||
defer allocator.free(spaces);
|
||||
|
||||
@memset(spaces, ' ');
|
||||
|
||||
TerminalBuffer.drawText(
|
||||
spaces,
|
||||
self.label.component_pos.x + 2,
|
||||
self.label.component_pos.y,
|
||||
TerminalBuffer.Color.DEFAULT,
|
||||
TerminalBuffer.Color.DEFAULT,
|
||||
);
|
||||
}
|
||||
|
||||
fn draw(self: *InfoLine) void {
|
||||
self.label.draw();
|
||||
}
|
||||
|
||||
fn handle(self: *InfoLine, maybe_key: ?keyboard.Key, insert_mode: bool) !void {
|
||||
self.label.handle(maybe_key, insert_mode);
|
||||
}
|
||||
|
||||
fn drawItem(label: *MessageLabel, message: Message, x: usize, y: usize, width: usize) void {
|
||||
if (message.width == 0) return;
|
||||
|
||||
const x_offset = if (label.text_in_center and width >= message.width) (width - message.width) / 2 else 0;
|
||||
|
||||
label.cursor = message.width + x_offset;
|
||||
TerminalBuffer.drawConfinedText(
|
||||
message.text,
|
||||
x + x_offset,
|
||||
y,
|
||||
width,
|
||||
message.fg,
|
||||
message.bg,
|
||||
);
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
const std = @import("std");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const ly_core = @import("ly-core");
|
||||
const Environment = ly_core.Environment;
|
||||
|
||||
const keyboard = @import("../keyboard.zig");
|
||||
const TerminalBuffer = @import("../TerminalBuffer.zig");
|
||||
const Widget = @import("../Widget.zig");
|
||||
const generic = @import("generic.zig");
|
||||
const UserList = @import("UserList.zig");
|
||||
|
||||
const Env = struct {
|
||||
// TODO: Remove dependency on Environment
|
||||
environment: Environment,
|
||||
index: usize,
|
||||
};
|
||||
const EnvironmentLabel = generic.CyclableLabel(Env, *UserList);
|
||||
|
||||
const Session = @This();
|
||||
|
||||
label: EnvironmentLabel,
|
||||
user_list: *UserList,
|
||||
|
||||
pub fn init(
|
||||
allocator: Allocator,
|
||||
buffer: *TerminalBuffer,
|
||||
user_list: *UserList,
|
||||
width: usize,
|
||||
text_in_center: bool,
|
||||
fg: u32,
|
||||
bg: u32,
|
||||
) Session {
|
||||
return .{
|
||||
.label = EnvironmentLabel.init(
|
||||
allocator,
|
||||
buffer,
|
||||
drawItem,
|
||||
sessionChanged,
|
||||
user_list,
|
||||
width,
|
||||
text_in_center,
|
||||
fg,
|
||||
bg,
|
||||
),
|
||||
.user_list = user_list,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Session) void {
|
||||
for (self.label.list.items) |*env| {
|
||||
if (env.environment.entry_ini) |*entry_ini| entry_ini.deinit();
|
||||
self.label.allocator.free(env.environment.file_name);
|
||||
}
|
||||
|
||||
self.label.deinit();
|
||||
}
|
||||
|
||||
pub fn widget(self: *Session) Widget {
|
||||
return Widget.init(
|
||||
"Session",
|
||||
self,
|
||||
deinit,
|
||||
null,
|
||||
draw,
|
||||
null,
|
||||
handle,
|
||||
null,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn addEnvironment(self: *Session, environment: Environment) !void {
|
||||
const env = Env{ .environment = environment, .index = self.label.list.items.len };
|
||||
|
||||
try self.label.addItem(env);
|
||||
addedSession(env, self.user_list);
|
||||
}
|
||||
|
||||
fn draw(self: *Session) void {
|
||||
self.label.draw();
|
||||
}
|
||||
|
||||
fn handle(self: *Session, maybe_key: ?keyboard.Key, insert_mode: bool) !void {
|
||||
self.label.handle(maybe_key, insert_mode);
|
||||
}
|
||||
|
||||
fn addedSession(env: Env, user_list: *UserList) void {
|
||||
const user = user_list.label.list.items[user_list.label.current];
|
||||
if (!user.first_run) return;
|
||||
|
||||
user.session_index.* = env.index;
|
||||
}
|
||||
|
||||
fn sessionChanged(env: Env, maybe_user_list: ?*UserList) void {
|
||||
if (maybe_user_list) |user_list| {
|
||||
user_list.label.list.items[user_list.label.current].session_index.* = env.index;
|
||||
}
|
||||
}
|
||||
|
||||
fn drawItem(label: *EnvironmentLabel, env: Env, x: usize, y: usize, width: usize) void {
|
||||
if (width < 3) return;
|
||||
|
||||
const length = @min(TerminalBuffer.strWidth(env.environment.name), width - 3);
|
||||
if (length == 0) return;
|
||||
|
||||
const x_offset = if (label.text_in_center and width >= length) (width - length) / 2 else 0;
|
||||
|
||||
label.cursor = length + x_offset;
|
||||
TerminalBuffer.drawConfinedText(
|
||||
env.environment.name,
|
||||
x + x_offset,
|
||||
y,
|
||||
width,
|
||||
label.fg,
|
||||
label.bg,
|
||||
);
|
||||
}
|
||||
@@ -1,141 +0,0 @@
|
||||
const std = @import("std");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const ly_core = @import("ly-core");
|
||||
const SavedUsers = ly_core.SavedUsers;
|
||||
|
||||
const keyboard = @import("../keyboard.zig");
|
||||
const TerminalBuffer = @import("../TerminalBuffer.zig");
|
||||
const Widget = @import("../Widget.zig");
|
||||
const generic = @import("generic.zig");
|
||||
const Session = @import("Session.zig");
|
||||
|
||||
const StringList = std.ArrayListUnmanaged([]const u8);
|
||||
pub const User = struct {
|
||||
name: []const u8,
|
||||
session_index: *usize,
|
||||
allocated_index: bool,
|
||||
first_run: bool,
|
||||
};
|
||||
const UserLabel = generic.CyclableLabel(User, *Session);
|
||||
|
||||
const UserList = @This();
|
||||
|
||||
label: UserLabel,
|
||||
|
||||
pub fn init(
|
||||
allocator: Allocator,
|
||||
buffer: *TerminalBuffer,
|
||||
usernames: StringList,
|
||||
// TODO: Remove dependency on SavedUsers
|
||||
saved_users: *SavedUsers,
|
||||
session: *Session,
|
||||
width: usize,
|
||||
text_in_center: bool,
|
||||
fg: u32,
|
||||
bg: u32,
|
||||
) !UserList {
|
||||
var user_list = UserList{
|
||||
.label = UserLabel.init(
|
||||
allocator,
|
||||
buffer,
|
||||
drawItem,
|
||||
usernameChanged,
|
||||
session,
|
||||
width,
|
||||
text_in_center,
|
||||
fg,
|
||||
bg,
|
||||
),
|
||||
};
|
||||
|
||||
for (usernames.items) |username| {
|
||||
if (username.len == 0) continue;
|
||||
|
||||
var maybe_session_index: ?*usize = null;
|
||||
var first_run = true;
|
||||
for (saved_users.user_list.items) |*saved_user| {
|
||||
if (std.mem.eql(u8, username, saved_user.username)) {
|
||||
maybe_session_index = &saved_user.session_index;
|
||||
first_run = saved_user.first_run;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var allocated_index = false;
|
||||
if (maybe_session_index == null) {
|
||||
maybe_session_index = try allocator.create(usize);
|
||||
maybe_session_index.?.* = 0;
|
||||
allocated_index = true;
|
||||
}
|
||||
|
||||
try user_list.label.addItem(.{
|
||||
.name = username,
|
||||
.session_index = maybe_session_index.?,
|
||||
.allocated_index = allocated_index,
|
||||
.first_run = first_run,
|
||||
});
|
||||
}
|
||||
|
||||
return user_list;
|
||||
}
|
||||
|
||||
pub fn deinit(self: *UserList) void {
|
||||
for (self.label.list.items) |user| {
|
||||
if (user.allocated_index) {
|
||||
self.label.allocator.destroy(user.session_index);
|
||||
}
|
||||
}
|
||||
|
||||
self.label.deinit();
|
||||
}
|
||||
|
||||
pub fn widget(self: *UserList) Widget {
|
||||
return Widget.init(
|
||||
"UserList",
|
||||
self,
|
||||
deinit,
|
||||
null,
|
||||
draw,
|
||||
null,
|
||||
handle,
|
||||
null,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn getCurrentUsername(self: UserList) []const u8 {
|
||||
return self.label.list.items[self.label.current].name;
|
||||
}
|
||||
|
||||
fn draw(self: *UserList) void {
|
||||
self.label.draw();
|
||||
}
|
||||
|
||||
fn handle(self: *UserList, maybe_key: ?keyboard.Key, insert_mode: bool) !void {
|
||||
self.label.handle(maybe_key, insert_mode);
|
||||
}
|
||||
|
||||
fn usernameChanged(user: User, maybe_session: ?*Session) void {
|
||||
if (maybe_session) |session| {
|
||||
session.label.current = @min(user.session_index.*, session.label.list.items.len - 1);
|
||||
}
|
||||
}
|
||||
|
||||
fn drawItem(label: *UserLabel, user: User, x: usize, y: usize, width: usize) void {
|
||||
if (width < 3) return;
|
||||
|
||||
const length = @min(TerminalBuffer.strWidth(user.name), width - 3);
|
||||
if (length == 0) return;
|
||||
|
||||
const x_offset = if (label.text_in_center and width >= length) (width - length) / 2 else 0;
|
||||
|
||||
label.cursor = length + x_offset;
|
||||
TerminalBuffer.drawConfinedText(
|
||||
user.name,
|
||||
x + x_offset,
|
||||
y,
|
||||
width,
|
||||
label.fg,
|
||||
label.bg,
|
||||
);
|
||||
}
|
||||
@@ -9,8 +9,5 @@ pub const Widget = @import("Widget.zig");
|
||||
pub const BigLabel = @import("components/BigLabel.zig");
|
||||
pub const CenteredBox = @import("components/CenteredBox.zig");
|
||||
pub const CyclableLabel = @import("components/generic.zig").CyclableLabel;
|
||||
pub const InfoLine = @import("components/InfoLine.zig");
|
||||
pub const Label = @import("components/Label.zig");
|
||||
pub const Session = @import("components/Session.zig");
|
||||
pub const Text = @import("components/Text.zig");
|
||||
pub const UserList = @import("components/UserList.zig");
|
||||
|
||||
Reference in New Issue
Block a user