use std::fmt;
use std::path::Path;
use geometry::Size;
use terminal::config::{ConfigPart, escape_config_string};
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct Terminal {
encoding: Option<String>,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct Window {
size: Option<Size>,
cellsize: Option<Cellsize>,
title: Option<String>,
icon: Option<String>,
resizeable: Option<bool>,
fullscreen: Option<bool>,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct Input {
precise_mouse: Option<bool>,
mouse_cursor: Option<bool>,
cursor_symbol: Option<char>,
cursor_blink_rate: Option<i32>,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct Output {
postformatting: Option<bool>,
vsync: Option<bool>,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct Log {
file: Option<String>,
level: Option<LogLevel>,
mode: Option<LogMode>,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum Cellsize {
Auto,
Sized(Size),
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum LogLevel {
None,
Fatal,
Error,
Warning,
Info,
Debug,
Trace,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum LogMode {
Truncate,
Append,
}
impl Terminal {
pub fn new(encoding: String) -> Terminal {
Terminal{
encoding: Some(encoding),
}
}
}
impl Window {
pub fn empty() -> Window {
Window{
size: None,
cellsize: None,
title: None,
icon: None,
resizeable: None,
fullscreen: None,
}
}
pub fn size (mut self, size: Size) -> Self {self.size = Some(size) ; self}
pub fn cellsize (mut self, cellsize: Cellsize) -> Self {self.cellsize = Some(cellsize) ; self}
pub fn title (mut self, title: String) -> Self {self.title = Some(title) ; self}
pub fn icon<T: AsRef<Path>>(mut self, icon: T) -> Self {self.icon = Some(icon.as_ref().to_str().unwrap().to_string()); self}
pub fn resizeable (mut self, resizeable: bool) -> Self {self.resizeable = Some(resizeable) ; self}
pub fn fullscreen (mut self, fullscreen: bool) -> Self {self.fullscreen = Some(fullscreen) ; self}
}
impl Input {
pub fn empty() -> Input {
Input{
precise_mouse: None,
mouse_cursor: None,
cursor_symbol: None,
cursor_blink_rate: None,
}
}
pub fn precise_mouse (mut self, precise_mouse: bool) -> Self {self.precise_mouse = Some(precise_mouse) ; self}
pub fn mouse_cursor (mut self, mouse_cursor: bool) -> Self {self.mouse_cursor = Some(mouse_cursor) ; self}
pub fn cursor_symbol (mut self, cursor_symbol: char) -> Self {self.cursor_symbol = Some(cursor_symbol) ; self}
pub fn cursor_blink_rate(mut self, cursor_blink_rate: i32) -> Self {self.cursor_blink_rate = Some(cursor_blink_rate); self}
}
impl Output {
pub fn clean() -> Output {
Output{
postformatting: None,
vsync: None,
}
}
pub fn postformatting(mut self, postformatting: bool) -> Self {self.postformatting = Some(postformatting); self}
pub fn vsync (mut self, vsync: bool) -> Self {self.vsync = Some(vsync); self}
}
impl Log {
pub fn empty() -> Log {
Log{
file: None,
level: None,
mode: None,
}
}
pub fn file (mut self, file: String) -> Log {self.file = Some(file) ; self}
pub fn level(mut self, level: LogLevel) -> Log {self.level = Some(level); self}
pub fn mode (mut self, mode: LogMode) -> Log {self.mode = Some(mode) ; self}
}
impl ConfigPart for Terminal {
fn to_config_str(&self) -> String {
match self.encoding {
Some(ref encoding) => format!("terminal.encoding={};", escape_config_string(&encoding)),
None => "".to_string(),
}
}
}
impl ConfigPart for Window {
fn to_config_str(&self) -> String {
if self.size.is_some() || self.cellsize.is_some() || self.title.is_some() || self.icon.is_some() || self.resizeable.is_some() ||
self.fullscreen.is_some() {
format!("window: {}, {}, {}, {}, {}, {};",
match self.size {
Some(ref size) => format!("size={}", size),
None => "".to_string(),
},
match self.cellsize {
Some(ref cellsize) =>
match cellsize {
&Cellsize::Sized(size) => format!("cellsize={}", size),
&Cellsize::Auto => "cellsize=auto".to_string(),
},
None => "".to_string(),
},
match self.title {
Some(ref title) => format!("title={}", escape_config_string(&title)),
None => "".to_string(),
},
match self.icon {
Some(ref icon) => format!("icon={}", escape_config_string(&icon)),
None => "".to_string(),
},
match self.resizeable {
Some(ref resizeable) => format!("resizeable={}", resizeable),
None => "".to_string(),
},
match self.fullscreen {
Some(ref fullscreen) => format!("fullscreen={}", fullscreen),
None => "".to_string(),
},
)
} else {
"".to_string()
}
}
}
impl ConfigPart for Input {
fn to_config_str(&self) -> String {
if self.precise_mouse.is_some() || self.mouse_cursor.is_some() || self.cursor_symbol.is_some() || self.cursor_blink_rate.is_some() {
format!("input: {}, {}, {}, {};",
match self.precise_mouse {
Some(ref precise_mouse) => format!("precise-mouse={}", precise_mouse),
None => "".to_string(),
},
match self.mouse_cursor {
Some(ref mouse_cursor) => format!("mouse-cursor={}", mouse_cursor),
None => "".to_string(),
},
match self.cursor_symbol {
Some(ref cursor_symbol) => format!("cursor-symbol=0x{:x}", *cursor_symbol as i8),
None => "".to_string(),
},
match self.cursor_blink_rate {
Some(ref cursor_blink_rate) => format!("cursor-blink-rate={}", cursor_blink_rate),
None => "".to_string(),
},
)
} else {
"".to_string()
}
}
}
impl ConfigPart for Output {
fn to_config_str(&self) -> String {
if self.postformatting.is_some() || self.vsync.is_some() {
format!("output: {}, {};",
match self.postformatting {
Some(ref postformatting) => format!("postformatting={}", postformatting),
None => "".to_string(),
},
match self.vsync {
Some(ref vsync) => format!("vsync={}", vsync),
None => "".to_string(),
},
)
} else {
"".to_string()
}
}
}
impl ConfigPart for Log {
fn to_config_str(&self) -> String {
if self.file.is_some() || self.level.is_some() || self.mode.is_some() {
format!("log: {}, {}, {};",
match self.file {
Some(ref file) => format!("file={}", escape_config_string(&file)),
None => "".to_string(),
},
match self.level {
Some(ref level) => format!("level={}", level),
None => "".to_string(),
},
match self.mode {
Some(ref mode) => format!("mode={}", mode),
None => "".to_string(),
},
)
} else {
"".to_string()
}
}
}
impl fmt::Display for LogLevel {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(match self {
&LogLevel::None => "none",
&LogLevel::Fatal => "fatal",
&LogLevel::Error => "error",
&LogLevel::Warning => "warning",
&LogLevel::Info => "info",
&LogLevel::Debug => "debug",
&LogLevel::Trace => "trace",
})
}
}
impl fmt::Display for LogMode {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(match self {
&LogMode::Truncate => "truncate",
&LogMode::Append => "append",
})
}
}