diff options
Diffstat (limited to 'src/render/mod.rs')
-rw-r--r-- | src/render/mod.rs | 183 |
1 files changed, 104 insertions, 79 deletions
diff --git a/src/render/mod.rs b/src/render/mod.rs index a824bd6..8575e9b 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -3,103 +3,128 @@ use systemd::unit; use horrorshow::prelude::*; use horrorshow::Raw; -fn render_in_page<'a>(content: Box<RenderBox + 'a>) -> String { - (html!{ - : Raw("<!DOCTYPE html>"); - html { - head { - title: "Systemhttpd"; - link(rel="stylesheet", type="text/css", href="/static/main.css"); +#[derive(Debug)] +pub struct Renderer { + pub user: Option<String> +} + +impl Renderer { + fn render_in_page<'a>(&self, content: Box<RenderBox + 'a>) -> String { + info!("Rendering page with context: {:?}", self); + let login_box: Box<RenderBox> = match self.user { + Some(ref user) => box_html!{ + : user; + : " ("; + a(href="logout") { // TODO get base url from context + : "log out" + } + : ")"; + }, + None => box_html! { + a(href="login") { // TODO Get base url from context + : "Login" + } } - body { - nav { - ul { - li { - p { : "SystemHttpd" } + }; + + (html!{ + : Raw("<!DOCTYPE html>"); + html { + head { + title: "Systemhttpd"; + link(rel="stylesheet", type="text/css", href="/static/main.css"); + } + body { + nav { + ul { + li { + p { : "SystemHttpd" } + } } - } - ul(class="right") { - li { - p { : "Login" } + ul(class="right") { + li { + p { + : login_box + } + } } } - } - main { - : content + main { + : content + } } } - } - }).into_string().unwrap() -} + }).into_string().unwrap() + } -pub fn login_page() -> String { - render_in_page(box_html! { - h1 { : "Log in" } - form(method="post") { - p { : "Username" } - input(type="text", name="username") {} - p { : "Password" } - input(type="text", name="password") {} - p {} - input(type="submit", value="Log in") {} - } - }) -} + pub fn login_page(&self) -> String { + self.render_in_page(box_html! { + h1 { : "Log in" } + form(method="post") { + p { : "Username" } + input(type="text", name="username") {} + p { : "Password" } + input(type="text", name="password") {} + p {} + input(type="submit", value="Log in") {} + } + }) + } -fn unit_table<'a>(units: &'a [&unit::Unit]) -> Box<RenderBox + 'a> { - fn render_unit<'a>(unit: &'a unit::Unit) -> Box<RenderBox + 'a> { - box_html! { - tr { - td { - a(href=format_args!("/status/{}", &unit.name)) { - : &unit.name + fn unit_table<'a>(&self, units: &'a [&unit::Unit]) -> Box<RenderBox + 'a> { + fn render_unit<'a>(unit: &'a unit::Unit) -> Box<RenderBox + 'a> { + box_html! { + tr { + td { + a(href=format_args!("/status/{}", &unit.name)) { + : &unit.name + } + } + td { + : format_args!("{} ({})", + &unit.active_state, + &unit.sub_state) } - } - td { - : format_args!("{} ({})", - &unit.active_state, - &unit.sub_state) } } } - } - box_html! { - table { - tr { - th { - : "Unit" + box_html! { + table { + tr { + th { + : "Unit" + } + th { + : "Active" + } } - th { - : "Active" + @ for unit in units { + : render_unit(unit) } } - @ for unit in units { - : render_unit(unit) - } } } -} -pub fn system_status(sections: &[(String, Vec<&unit::Unit>)]) -> String { - let b = - box_html! { - @ for &(ref type_, ref units) in sections { - h1 { - : type_ - } - : unit_table(&units) - } - }; - render_in_page(b) -} + pub fn system_status(&self, sections: &[(String, Vec<&unit::Unit>)]) + -> String { + self.render_in_page(box_html! { + @ for &(ref type_, ref units) in sections { + h1 { + : type_ + } + : self.unit_table(&units) + } + }) + } -pub fn unit_status(unit: &unit::Unit, log: &str) -> String { - render_in_page(box_html! { - h1 { :&unit.name } - p { : format_args!("{} ({})", &unit.active_state, &unit.sub_state) } - pre { - : log - } - }) + pub fn unit_status(&self, unit: &unit::Unit, log: &str) -> String { + self.render_in_page(box_html! { + h1 { :&unit.name } + p { : format_args!("{} ({})", &unit.active_state, &unit.sub_state) } + pre { + : log + } + }) + } } |