use crate::config::CFG;
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 std::io;
use std::io::Read;
use std::path::PathBuf;
use structopt::StructOpt;
use tpnote_lib::config::LocalLinkKind;
use tpnote_lib::content::Content;
use tpnote_lib::content::ContentString;
#[derive(Debug, Eq, 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>,
#[structopt(long)]
pub export_link_rewriting: Option<LocalLinkKind>,
}
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.edit || !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: ContentString = {
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());
<ContentString as Content>::from_string_with_cr(buffer)
};
}
lazy_static! {
pub static ref CLIPBOARD: ContentString = {
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());
<ContentString as Content>::from_string_with_cr(buffer)
};
}
lazy_static! {
pub static ref HTML_EXPORT: Option<(PathBuf, LocalLinkKind)> = ARGS.export
.as_ref().map(|dir| (dir.to_path_buf(), ARGS.export_link_rewriting
.unwrap_or(CFG.arg_default.export_link_rewriting))
);
}