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::{
|
use crate::app::states::skirmish_states::{
|
||||||
Players,
|
Players,
|
||||||
tasks::{Task, Tasks},
|
tasks::{Task, Tasks},
|
||||||
@@ -81,18 +83,23 @@ impl Unit for MinerUnit {
|
|||||||
&self.tasks
|
&self.tasks
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_tasks_formatted(&self) -> String {
|
fn get_tasks_formatted(&self) -> Vec<Line<'_>> {
|
||||||
// format!("{:?}", self.tasks)
|
Vec::from(
|
||||||
let mut output: String = String::new();
|
self.tasks
|
||||||
for task in &self.tasks {
|
.iter()
|
||||||
output.push(task.get_icon());
|
.enumerate()
|
||||||
output.push(' ');
|
.map(|(i, task)| {
|
||||||
output += &self.display_coords(task.get_path_front_coords());
|
Line::from_iter([
|
||||||
output += " -> ";
|
format!("{}. ", i + 1).gray(),
|
||||||
output += &self.display_coords(task.get_path_back_coords());
|
task.get_icon().gray(),
|
||||||
}
|
" ".gray(),
|
||||||
|
self.display_coords(task.get_path_front_coords()).green(),
|
||||||
output
|
" -> ".gray(),
|
||||||
|
self.display_coords(task.get_path_back_coords()).yellow(),
|
||||||
|
])
|
||||||
|
})
|
||||||
|
.collect::<Vec<Line<'_>>>(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_task(&mut self, task: Tasks) {
|
fn set_task(&mut self, task: Tasks) {
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ pub trait Unit {
|
|||||||
|
|
||||||
fn set_task(&mut self, task: Tasks);
|
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<'_>> {
|
fn base_text(&self) -> Vec<Line<'_>> {
|
||||||
let owner: Span<'_> = self.get_owner().get_span();
|
let owner: Span<'_> = self.get_owner().get_span();
|
||||||
@@ -40,7 +40,7 @@ pub trait Unit {
|
|||||||
let can_dig: bool = self.get_can_dig();
|
let can_dig: bool = self.get_can_dig();
|
||||||
let digging_power: f32 = self.get_digging_power();
|
let digging_power: f32 = self.get_digging_power();
|
||||||
let have_any_tasks: bool = self.get_tasks().is_empty();
|
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([
|
let mut lines: Vec<Line<'_>> = Vec::from([
|
||||||
Line::from_iter(["Owner: ".gray(), owner]),
|
Line::from_iter(["Owner: ".gray(), owner]),
|
||||||
@@ -63,7 +63,10 @@ pub trait Unit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !have_any_tasks {
|
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
|
lines
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
use once_cell::sync::Lazy;
|
|
||||||
|
|
||||||
use crate::app::states::skirmish_states::{
|
use crate::app::states::skirmish_states::{
|
||||||
Players,
|
Players,
|
||||||
tasks::Tasks,
|
tasks::Tasks,
|
||||||
units::{MinerUnit, Unit},
|
units::{MinerUnit, Unit},
|
||||||
};
|
};
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
use ratatui::text::Line;
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
static EMPTY_TASKS: Lazy<VecDeque<Tasks>> = Lazy::new(|| VecDeque::new());
|
static EMPTY_TASKS: Lazy<VecDeque<Tasks>> = Lazy::new(|| VecDeque::new());
|
||||||
@@ -67,7 +67,7 @@ impl Unit for Units {
|
|||||||
self.u().get_tasks()
|
self.u().get_tasks()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_tasks_formatted(&self) -> String {
|
fn get_tasks_formatted(&self) -> Vec<Line<'_>> {
|
||||||
self.u().get_tasks_formatted()
|
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())
|
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()
|
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) {
|
fn set_task(&mut self, task: Tasks) {
|
||||||
|
|||||||
Reference in New Issue
Block a user