1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
use std::fmt;
use std::path::Path;
use geometry::Size;
use terminal::config::{ConfigPart, escape_config_string};


/// The `terminal` [configuration](http://foo.wyrd.name/en:bearlibterminal:reference:configuration#library_configuration) section repr.
///
/// See [`terminal::set()`](../fn.set.html).
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct Terminal {
	encoding: Option<String>,  //TODO: use an enum/validated struct?
}

/// The `window` [configuration](http://foo.wyrd.name/en:bearlibterminal:reference:configuration#library_configuration) section repr.
///
/// `None` values will not override current ones.
///
/// See [`terminal::set()`](../fn.set.html).
#[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>,
}

/// The `input` [configuration](http://foo.wyrd.name/en:bearlibterminal:reference:configuration#library_configuration) section repr.
///
/// `None` values will not override current ones.
///
/// See [`terminal::set()`](../fn.set.html).
#[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>,
}

/// The `output` [configuration](http://foo.wyrd.name/en:bearlibterminal:reference:configuration#library_configuration) section repr.
///
/// `None` values will not override current ones.
///
/// See [`terminal::set()`](../fn.set.html).
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct Output {
	postformatting: Option<bool>,
	vsync: Option<bool>,
}


/// The `log` [configuration](http://foo.wyrd.name/en:bearlibterminal:reference:configuration#library_configuration) section repr.
///
/// `None` values will not override current ones.
///
/// See [`terminal::set()`](../fn.set.html).
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct Log {
	file: Option<String>,
	level: Option<LogLevel>,
	mode: Option<LogMode>,
}


/// Possible cell size, `Auto` will make the size be selected based on the font.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum Cellsize {
	Auto,
	Sized(Size),
}

/// Logging levels, as specified [here](http://foo.wyrd.name/en:bearlibterminal:reference:configuration#library_configuration).
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum LogLevel {
	None,
	Fatal,
	Error,
	Warning,
	Info,
	Debug,
	Trace,
}

/// Log writing mode.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum LogMode {
	/// Reset the log each time.
	Truncate,
	/// Continue writing at the end.
	Append,
}


impl Terminal {
	/// Construct a new `terminal` [configuration](http://foo.wyrd.name/en:bearlibterminal:reference:configuration#library_configuration) section override
	/// segment with a specified used for unibyte strings, which is better left at default, as Rust uses UTF-8 for everything.
	///
	/// Default: `"utf8"`.
	pub fn new(encoding: String) -> Terminal {
		Terminal{
			encoding: Some(encoding),
		}
	}
}

impl Window {
	/// Construct a `window` [configuration](http://foo.wyrd.name/en:bearlibterminal:reference:configuration#library_configuration) section override segment
	/// with everything being equal to `None`.
	pub fn empty() -> Window {
		Window{
			size: None,
			cellsize: None,
			title: None,
			icon: None,
			resizeable: None,
			fullscreen: None,
		}
	}

	/// Window size in cells.
	///
	/// Default: `80x25`.
	pub fn size                (mut self, size: Size)         -> Self {self.size       = Some(size)                                       ; self}

	/// Size of all cells, in pixels.
	///
	/// Default: [`Cellsize::Auto`](enum.Cellsize.html#variant.Auto).
	pub fn cellsize            (mut self, cellsize: Cellsize) -> Self {self.cellsize   = Some(cellsize)                                   ; self}

	/// The terminal window's title.
	///
	/// Default: `"BearLibTerminal"`.
	pub fn title               (mut self, title: String)      -> Self {self.title      = Some(title)                                      ; self}

	/// The path of the icon used for the terminal window.
	///
	/// Default: none.
	pub fn icon<T: AsRef<Path>>(mut self, icon: T)            -> Self {self.icon       = Some(icon.as_ref().to_str().unwrap().to_string()); self}

	/// Whether the terminal window should be resizeable.
	///
	/// Default: `false`.
	pub fn resizeable          (mut self, resizeable: bool)   -> Self {self.resizeable = Some(resizeable)                                 ; self}

	/// Whether to enforce fullscreen mode.
	///
	/// Default: `false`.
	pub fn fullscreen          (mut self, fullscreen: bool)   -> Self {self.fullscreen = Some(fullscreen)                                 ; self}
}

impl Input {
	/// Construct an `input` [configuration](http://foo.wyrd.name/en:bearlibterminal:reference:configuration#library_configuration) section override segment
	/// with all elements equal to `None`.
	pub fn empty() -> Input {
		Input{
			precise_mouse: None,
			mouse_cursor: None,
			cursor_symbol: None,
			cursor_blink_rate: None,
		}
	}

	/// Whether to generate a mouse-move event when a mouse moves from one pixel to another as opposed to from one cell to another.
	///
	/// Default: `false`.
	pub fn precise_mouse    (mut self, precise_mouse: bool)    -> Self {self.precise_mouse     = Some(precise_mouse)    ; self}

	/// Whether to show the cursor.
	///
	/// Default: `true`.
	pub fn mouse_cursor     (mut self, mouse_cursor: bool)     -> Self {self.mouse_cursor      = Some(mouse_cursor)     ; self}

	/// The cursor symbol to blink in the read string function.
	///
	/// Default: `'_'` a.k.a. `0x5F`.
	pub fn cursor_symbol    (mut self, cursor_symbol: char)    -> Self {self.cursor_symbol     = Some(cursor_symbol)    ; self}

	/// Amount of time in milliseconds to blink the cursor symbol for.
	///
	/// Default: `500`.
	pub fn cursor_blink_rate(mut self, cursor_blink_rate: i32) -> Self {self.cursor_blink_rate = Some(cursor_blink_rate); self}
}

impl Output {
	/// Construct an `output` [configuration](http://foo.wyrd.name/en:bearlibterminal:reference:configuration#library_configuration) section override segment
	/// with all values equalling `None`
	pub fn clean() -> Output {
		Output{
			postformatting: None,
			vsync: None,
		}
	}

	/// Whether to process special tags in the [`print()`](../fn.print.html) function.
	///
	/// Default: `true`.
	pub fn postformatting(mut self, postformatting: bool) -> Self {self.postformatting = Some(postformatting); self}

	/// Toggle OpenGL VSync.
	///
	/// Default: `true`.
	pub fn vsync         (mut self, vsync: bool)          -> Self {self.vsync          = Some(vsync);          self}
}

impl Log {
	/// Construct an `log` [configuration](http://foo.wyrd.name/en:bearlibterminal:reference:configuration#library_configuration) section override segment
	/// with everything set to `None`
	pub fn empty() -> Log {
		Log{
			file: None,
			level: None,
			mode: None,
		}
	}

	/// The file to write the log to. Note, that, IME, it didn't work.
	///
	/// Default: `"bearlibterminal.log"`
	pub fn file (mut self, file: String)    -> Log {self.file  = Some(file) ; self}

	/// The minimal log level to print at.
	///
	/// Default: [`LogLevel::Error`](enum.LogLevel.html#variant.Error)
	pub fn level(mut self, level: LogLevel) -> Log {self.level = Some(level); self}

	/// How to write to the log file if one laready exists.
	///
	/// Default: [`LogMode::Truncate`](enum.LogMode.html#variant.Truncate)
	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",
		})
	}
}