generated from GarandPLG/rust-flake-template
Return task queue as styled Line vectors
Change `get_tasks_formatted` to return a `Vec<Line<'_>>` instead of a plain `String`. Update `MinerUnit` to construct colored task lines, modify the `Unit` trait and `Units` implementations to use the new return type, and add the required `ratatui` imports. Adjust `base_text` handling to render each task line separately.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use ratatui::{style::Stylize, text::Line};
|
||||
|
||||
use crate::app::states::skirmish_states::{
|
||||
Players,
|
||||
tasks::{Task, Tasks},
|
||||
@@ -81,18 +83,23 @@ impl Unit for MinerUnit {
|
||||
&self.tasks
|
||||
}
|
||||
|
||||
fn get_tasks_formatted(&self) -> String {
|
||||
// format!("{:?}", self.tasks)
|
||||
let mut output: String = String::new();
|
||||
for task in &self.tasks {
|
||||
output.push(task.get_icon());
|
||||
output.push(' ');
|
||||
output += &self.display_coords(task.get_path_front_coords());
|
||||
output += " -> ";
|
||||
output += &self.display_coords(task.get_path_back_coords());
|
||||
}
|
||||
|
||||
output
|
||||
fn get_tasks_formatted(&self) -> Vec<Line<'_>> {
|
||||
Vec::from(
|
||||
self.tasks
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, task)| {
|
||||
Line::from_iter([
|
||||
format!("{}. ", i + 1).gray(),
|
||||
task.get_icon().gray(),
|
||||
" ".gray(),
|
||||
self.display_coords(task.get_path_front_coords()).green(),
|
||||
" -> ".gray(),
|
||||
self.display_coords(task.get_path_back_coords()).yellow(),
|
||||
])
|
||||
})
|
||||
.collect::<Vec<Line<'_>>>(),
|
||||
)
|
||||
}
|
||||
|
||||
fn set_task(&mut self, task: Tasks) {
|
||||
|
||||
@@ -30,7 +30,7 @@ pub trait Unit {
|
||||
|
||||
fn set_task(&mut self, task: Tasks);
|
||||
|
||||
fn get_tasks_formatted(&self) -> String;
|
||||
fn get_tasks_formatted(&self) -> Vec<Line<'_>>;
|
||||
|
||||
fn base_text(&self) -> Vec<Line<'_>> {
|
||||
let owner: Span<'_> = self.get_owner().get_span();
|
||||
@@ -40,7 +40,7 @@ pub trait Unit {
|
||||
let can_dig: bool = self.get_can_dig();
|
||||
let digging_power: f32 = self.get_digging_power();
|
||||
let have_any_tasks: bool = self.get_tasks().is_empty();
|
||||
let tasks: String = self.get_tasks_formatted();
|
||||
let tasks: Vec<Line<'_>> = self.get_tasks_formatted();
|
||||
|
||||
let mut lines: Vec<Line<'_>> = Vec::from([
|
||||
Line::from_iter(["Owner: ".gray(), owner]),
|
||||
@@ -63,7 +63,10 @@ pub trait Unit {
|
||||
}
|
||||
|
||||
if !have_any_tasks {
|
||||
lines.push(Line::from_iter(["Tasks Queue: ".gray(), tasks.green()]));
|
||||
lines.push(Line::from("Tasks Queue: ".gray()));
|
||||
for task in tasks {
|
||||
lines.push(task);
|
||||
}
|
||||
}
|
||||
|
||||
lines
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
use crate::app::states::skirmish_states::{
|
||||
Players,
|
||||
tasks::Tasks,
|
||||
units::{MinerUnit, Unit},
|
||||
};
|
||||
use once_cell::sync::Lazy;
|
||||
use ratatui::text::Line;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
static EMPTY_TASKS: Lazy<VecDeque<Tasks>> = Lazy::new(|| VecDeque::new());
|
||||
@@ -67,7 +67,7 @@ impl Unit for Units {
|
||||
self.u().get_tasks()
|
||||
}
|
||||
|
||||
fn get_tasks_formatted(&self) -> String {
|
||||
fn get_tasks_formatted(&self) -> Vec<Line<'_>> {
|
||||
self.u().get_tasks_formatted()
|
||||
}
|
||||
|
||||
@@ -117,9 +117,9 @@ impl Unit for Option<Units> {
|
||||
self.as_ref().map_or(&EMPTY_TASKS, |u| u.get_tasks())
|
||||
}
|
||||
|
||||
fn get_tasks_formatted(&self) -> String {
|
||||
fn get_tasks_formatted(&self) -> Vec<Line<'_>> {
|
||||
self.as_ref()
|
||||
.map_or("".to_string(), |u| u.get_tasks_formatted())
|
||||
.map_or(Vec::new(), |u| u.get_tasks_formatted())
|
||||
}
|
||||
|
||||
fn set_task(&mut self, task: Tasks) {
|
||||
|
||||
Reference in New Issue
Block a user