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
//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
//                    Version 2, December 2004
//
// Copyleft (ↄ) meh. <meh@schizofreni.co> | http://meh.schizofreni.co
//
// Everyone is permitted to copy and distribute verbatim or modified
// copies of this license document, and changing it is allowed as long
// as the name is changed.
//
//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
//   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
//
//  0. You just DO WHAT THE FUCK YOU WANT TO.

use std::collections::HashMap;
use std::env;
use std::path::{Path, PathBuf};
use std::fs::{self, File};
use std::io::Read;

use capability::{Capability, Value};
use names;
use error::{self, Error};
use nom::IResult;
use parser::compiled;

/// A capability database.
#[derive(Eq, PartialEq, Clone, Debug, Default)]
pub struct Database {
	name:        String,
	aliases:     Vec<String>,
	description: String,

	inner: HashMap<String, Value>,
}

impl Database {
	/// Create a new database from the given values.
	pub fn new(name: String, aliases: Vec<String>, description: String, inner: HashMap<String, Value>) -> Self {
		Database {
			name:        name,
			aliases:     aliases,
			description: description,

			inner: inner,
		}
	}

	/// Load a database from the current environment.
	pub fn from_env() -> error::Result<Self> {
		if let Ok(name) = env::var("TERM") {
			Self::from_name(name)
		}
		else {
			Err(Error::NotFound)
		}
	}

	/// Load a database for the given name.
	pub fn from_name<N: AsRef<str>>(name: N) -> error::Result<Self> {
		let name  = name.as_ref();
		let first = name.chars().next().ok_or(Error::NotFound)?;

		let mut search = Vec::<PathBuf>::new();

		if let Some(dir) = env::var_os("TERMINFO") {
			search.push(dir.into());
		}

		if let Ok(dirs) = env::var("TERMINFO_DIRS") {
			for dir in dirs.split(':') {
				if dir.is_empty() {
					search.push("/usr/share/terminfo".into());
				}
				else {
					search.push(dir.into());
				}
			}
		}
		else {
			if let Some(mut home) = env::home_dir() {
				home.push(".terminfo");
				search.push(home.into());
			}

			search.push("/etc/terminfo".into());
			search.push("/lib/terminfo".into());
			search.push("/usr/share/terminfo".into());
			search.push("/boot/system/data/terminfo".into());
		}

		for path in search {
			if fs::metadata(&path).is_err() {
				continue;
			}

			// Check standard location.
			{
				let mut path = path.clone();
				path.push(first.to_string());
				path.push(name);

				if fs::metadata(&path).is_ok() {
					return Self::from_path(path);
				}
			}

			// Check non-standard location.
			{
				let mut path = path.clone();
				path.push(format!("{:x}", first as usize));
				path.push(name);

				if fs::metadata(&path).is_ok() {
					return Self::from_path(path);
				}
			}
		}

		Err(Error::NotFound)
	}

	/// Load a database from the given path.
	pub fn from_path<P: AsRef<Path>>(path: P) -> error::Result<Self> {
		let mut file = File::open(path)?;
		let mut buffer = Vec::new();
		file.read_to_end(&mut buffer)?;

		Self::from_buffer(buffer)
	}

	/// Load a database from a buffer.
	pub fn from_buffer<T: AsRef<[u8]>>(buffer: T) -> error::Result<Self> {
		if let IResult::Done(_, database) = compiled::parse(buffer.as_ref()) {
			Ok(database.into())
		}
		else {
			Err(Error::Parse)
		}
	}

	/// The terminal name.
	pub fn name(&self) -> &str {
		&self.name
	}

	/// The terminal aliases.
	pub fn aliases(&self) -> &[String] {
		&self.aliases
	}

	/// The terminal description.
	pub fn description(&self) -> &str {
		&self.description
	}

	/// Get a capability.
	///
	/// ## Example
	///
	/// ```
	/// use terminfo::{Database, capability as cap};
	///
	/// let info        = Database::from_env().unwrap();
	/// let colors: i16 = info.get::<cap::MaxColors>().unwrap().into();
	/// ```
	pub fn get<'a, C: Capability<'a>>(&'a self) -> Option<C> {
		C::parse(self.inner.get(C::name()))
	}

	/// Get a capability by name.
	///
	/// This interface only makes sense for extended capabilities since they
	/// don't have standardized types.
	///
	/// ## Example
	///
	/// ```
	/// use terminfo::Database;
	///
	/// let info      = Database::from_env().unwrap();
	/// let truecolor = info.raw("Tc").is_some();
	/// ```
	pub fn raw<S: AsRef<str>>(&self, name: S) -> Option<&Value> {
		let name = name.as_ref();

		self.inner.get(name).or_else(||
			names::ALIASES.get(name).and_then(|&name| self.inner.get(name)))
	}
}