File: common.rs

package info (click to toggle)
rust-gnuplot 0.0.39-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 532 kB
  • sloc: makefile: 2
file content (106 lines) | stat: -rw-r--r-- 1,872 bytes parent folder | download
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
// This file is released into Public Domain.
use argparse_rs::*;
use gnuplot::*;
use std::env;

#[derive(Copy, Clone)]
pub struct BetterIterator<'l, T: 'l>
{
	idx: usize,
	slice: &'l [T],
}

impl<'l, T: 'l> Iterator for BetterIterator<'l, T>
{
	type Item = &'l T;
	fn next(&mut self) -> Option<&'l T>
	{
		let ret = self.slice.get(self.idx);
		self.idx += 1;
		ret
	}
}

pub trait BetterIteratorExt<'l, T>
{
	fn iter2(self) -> BetterIterator<'l, T>;
}

impl<'l, T: 'l> BetterIteratorExt<'l, T> for &'l [T]
{
	fn iter2(self) -> BetterIterator<'l, T>
	{
		BetterIterator {
			idx: 0,
			slice: self,
		}
	}
}

pub struct Common
{
	pub no_show: bool,
	pub term: Option<String>,
	pub extension: String,
}

impl Common
{
	pub fn new() -> Option<Common>
	{
		let arg_vec: Vec<_> = env::args().collect();

		let mut args = ArgParser::new(arg_vec[0].clone());

		args.add_opt(
			"no-show",
			Some("false"),
			'n',
			false,
			"do not run the gnuplot process.",
			ArgType::Flag,
		);
		args.add_opt(
			"terminal",
			None,
			't',
			false,
			"specify what terminal to use for gnuplot.",
			ArgType::Option,
		);
		args.add_opt(
			"extension",
			None,
			'e',
			false,
			"specify what extension the output file should have. Default: 'out'",
			ArgType::Option,
		);

		let res = args.parse(arg_vec.iter()).unwrap();

		if res.get("help").unwrap_or(false)
		{
			args.help();
			return None;
		}

		Some(Common {
			no_show: res.get("no-show").unwrap(),
			term: res.get::<String>("terminal").map(|s| s.to_string()),
			extension: res.get::<String>("extension").unwrap_or("out".to_string()),
		})
	}

	pub fn show(&self, fg: &mut Figure, filename: &str)
	{
		self.term.as_ref().map(|t| {
			fg.set_terminal(&t, &format!("{}.{}", filename, self.extension));
		});
		if !self.no_show
		{
			fg.show().unwrap();
		}
		fg.echo_to_file(&format!("{}.gnuplot", filename));
	}
}