use crate::config::CFG;
#[cfg(feature = "read-clipboard")]
use arboard::Clipboard;
use atty::{is, Stream};
use lazy_static::lazy_static;
use log::LevelFilter;
use std::env;
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;
pub const ENV_VAR_TPNOTE_CONFIG: &str = "TPNOTE_CONFIG";
pub const ENV_VAR_TPNOTE_EDITOR: &str = "TPNOTE_EDITOR";
#[cfg(feature = "viewer")]
pub const ENV_VAR_TPNOTE_BROWSER: &str = "TPNOTE_BROWSER";
#[cfg(target_family = "unix")]
const ENV_VAR_USER: &str = "USER";
#[cfg(target_family = "unix")]
const ENV_VAR_DISPLAY: &str = "DISPLAY";
#[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 = "l")]
pub force_lang: Option<String>,
#[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() &&
env::var(ENV_VAR_TPNOTE_EDITOR) != Ok(String::new()) &&
(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
&& env::var(ENV_VAR_TPNOTE_BROWSER) != Ok(String::new())
))
};
}
#[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(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(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) = Clipboard::new(){
if let Ok(s) = ctx.get_text(){
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))
);
}
lazy_static! {
pub static ref DOC_PATH: Result<PathBuf, std::io::Error> =
if let Some(p) = &ARGS.path {
p.canonicalize()
} else {
env::current_dir()
};
}