#[cfg(any(feature = "read-clipboard", feature = "viewer"))]
use crate::config::CFG;
use crate::content::Content;
use atty::{is, Stream};
#[cfg(feature = "read-clipboard")]
use copypasta::ClipboardContext;
#[cfg(feature = "read-clipboard")]
use copypasta::ClipboardProvider;
use lazy_static::lazy_static;
use log::LevelFilter;
use parse_hyperlinks::iterator::first_hyperlink;
use std::io;
use std::io::Read;
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(Debug, PartialEq, StructOpt)]
#[structopt(
name = "Tp-Note",
about = "Fast note taking with templates and filename synchronization."
)]
pub struct Args {
#[structopt(long, short = "a")]
pub add_header: bool,
#[structopt(long, short = "b")]
pub batch: bool,
#[structopt(long, short = "c")]
pub config: Option<String>,
#[structopt(long, short = "d")]
pub debug: Option<LevelFilter>,
#[structopt(long, short = "u")]
pub popup: bool,
#[structopt(long, short = "e")]
pub edit: bool,
#[structopt(long, short = "t")]
pub tty: bool,
#[structopt(long, short = "p")]
pub port: Option<u16>,
#[structopt(long, short = "n")]
pub no_filename_sync: bool,
#[structopt(long, short = "v")]
pub view: bool,
#[structopt(name = "PATH", parse(from_os_str))]
pub path: Option<PathBuf>,
#[structopt(long, short = "V")]
pub version: bool,
#[structopt(long, short = "x", parse(from_os_str))]
pub export: Option<PathBuf>,
}
lazy_static! {
pub static ref ARGS : Args = Args::from_args();
}
lazy_static! {
pub static ref LAUNCH_EDITOR: bool = {
!ARGS.batch && ARGS.export.is_none() && !ARGS.view
};
}
#[cfg(feature = "viewer")]
lazy_static! {
pub static ref LAUNCH_VIEWER: bool = {
!ARGS.batch && ARGS.export.is_none() && !*RUNS_ON_CONSOLE &&
(ARGS.view || ( !ARGS.edit && !CFG.arg_default.edit ))
};
}
#[cfg(not(feature = "viewer"))]
lazy_static! {
pub static ref LAUNCH_VIEWER: bool = {
false
};
}
lazy_static! {
pub static ref RUNS_ON_CONSOLE: bool = {
use crate::CFG;
#[cfg(target_family = "unix")]
if let Some(user) = std::env::var("USER")
.ok()
.and_then(|s: String| if s.is_empty() { None } else { Some(s) }) {
if user == "root" {
return true;
}
}
#[cfg(target_family = "unix")]
let display = std::env::var("DISPLAY")
.ok()
.and_then(|s: String| if s.is_empty() { None } else { Some(s) });
#[cfg(not(target_family = "unix"))]
let display = Some(String::new());
display.is_none() || ARGS.tty || CFG.arg_default.tty
};
}
lazy_static! {
pub static ref STDIN: Content = {
let mut buffer = String::new();
if !is(Stream::Stdin) {
let stdin = io::stdin();
let mut handle = stdin.lock();
let _ = handle.read_to_string(&mut buffer);
}
buffer.truncate(buffer.trim_end().len());
Content::from_input_with_cr(buffer)
};
}
lazy_static! {
pub static ref CLIPBOARD: Content = {
let mut buffer = String::new();
#[cfg(feature="read-clipboard")]
if CFG.clipboard.read_enabled && !ARGS.batch {
if let Ok(mut ctx) = ClipboardContext::new(){
if let Ok(s) = ctx.get_contents(){
buffer.push_str(&s);
}
}
};
buffer.truncate(buffer.trim_end().len());
Content::from_input_with_cr(buffer)
};
}
#[derive(Debug, PartialEq, Default)]
pub struct Hyperlink {
pub name: String,
pub target: String,
pub title: String,
}
impl Hyperlink {
pub fn from(input: &str) -> Option<Hyperlink> {
first_hyperlink(input).map(|(link_name, link_target, link_title)| Hyperlink {
name: link_name.to_string(),
target: link_target.to_string(),
title: link_title.to_string(),
})
}
}
#[cfg(test)]
mod tests {
use super::Hyperlink;
#[test]
fn test_parse_hyperlink() {
let input = r#"abc[Homepage](https://blog.getreu.net "My blog")abc"#;
let expected_output = Hyperlink {
name: "Homepage".to_string(),
target: "https://blog.getreu.net".to_string(),
title: "My blog".to_string(),
};
let output = Hyperlink::from(input);
assert_eq!(expected_output, output.unwrap());
let input = r#"abc[Homepage][home]abc
[home]: https://blog.getreu.net "My blog""#;
let expected_output = Hyperlink {
name: "Homepage".to_string(),
target: "https://blog.getreu.net".to_string(),
title: "My blog".to_string(),
};
let output = Hyperlink::from(input);
assert_eq!(expected_output, output.unwrap());
let input = "abc`Homepage <https://blog.getreu.net>`_\nabc";
let expected_output = Hyperlink {
name: "Homepage".to_string(),
target: "https://blog.getreu.net".to_string(),
title: "".to_string(),
};
let output = Hyperlink::from(input);
assert_eq!(expected_output, output.unwrap());
let input = "abc `Homepage<home_>`_ abc\n.. _home: https://blog.getreu.net\nabc";
let expected_output = Hyperlink {
name: "Homepage".to_string(),
target: "https://blog.getreu.net".to_string(),
title: "".to_string(),
};
let output = Hyperlink::from(input);
assert_eq!(expected_output, output.unwrap());
}
}