Lines
71.23 %
Functions
78.57 %
Branches
100 %
mod log_file;
use clap::Parser;
use cli_core::start_server;
use sqlx::types::Uuid;
use std::sync::Mutex;
use tracing::level_filters::LevelFilter;
use tui::{
App, CliConfig, ConsoleEval, Keys, LocalTransport, install_panic_hook, parse_colors, run_loop,
};
use tvui::{ColorDepth, Theme};
#[derive(Parser, Debug)]
#[command(name = "nomisync-tui", about = "Nomisync interactive TUI")]
struct Cli {
#[arg(short = 'u', long)]
userid: Uuid,
#[arg(short = 'd', long)]
database: Option<String>,
#[arg(long)]
loglevel: Option<LevelFilter>,
/// Key bindings preset: cua (the default), emacs or vim. Overrides the `tui-keymap` config key.
#[arg(long, value_parser = parse_keymap)]
keymap: Option<String>,
/// Colour depth: auto, rgb, 256 or 16. Overrides the `tui-colors` config key.
#[arg(long, default_value = "auto")]
colors: Colors,
/// Theme preset: dn or turbo. Overrides the `tui-theme` config key.
#[arg(long, value_parser = parse_theme)]
theme: Option<String>,
}
/// The `--colors` value: `None` is `auto`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Colors(Option<ColorDepth>);
impl std::str::FromStr for Colors {
type Err = String;
fn from_str(s: &str) -> Result<Colors, String> {
parse_colors(s).map(Colors)
fn parse_theme(s: &str) -> Result<String, String> {
Theme::preset(s)
.map(|_| s.to_string())
.map_err(|err| err.message)
impl Cli {
fn cli_config(&self) -> CliConfig {
CliConfig {
preset: self.theme.clone(),
depth: self.colors.0,
keymap: self.keymap.clone(),
fn detected_depth() -> ColorDepth {
let term = std::env::var("TERM").ok();
let colorterm = std::env::var("COLORTERM").ok();
ColorDepth::detect(term.as_deref(), colorterm.as_deref())
fn parse_keymap(s: &str) -> Result<String, String> {
Keys::preset(s).map(|_| s.to_string())
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
let log = log_file::open_from_env().map_err(std::io::Error::other)?;
server::telemetry::init_logging_to(cli.loglevel, LevelFilter::WARN, Mutex::new(log), false)?;
let mut app =
App::new(cli.userid, cli.cli_config(), detected_depth()).map_err(std::io::Error::other)?;
start_server(cli.database, None).await?;
install_panic_hook();
let handle = tokio::runtime::Handle::current();
match ConsoleEval::connect(&handle, rpc::ScriptCtx::new(cli.userid), rpc::Session::new) {
Ok(eval) => app.attach_console(eval),
Err(err) => tracing::warn!("console eval unavailable: {err}"),
// The transport is dropped, and the terminal restored, before an error is printed.
let loop_result = {
let mut transport = LocalTransport::new()?;
run_loop(&mut transport, &mut app)
loop_result?;
Ok(())
#[cfg(test)]
mod tests {
use super::*;
const USER: &str = "00000000-0000-0000-0000-000000000000";
fn parse(extra: &[&str]) -> Result<Cli, clap::Error> {
let args = ["nomisync-tui", "-u", USER]
.into_iter()
.chain(extra.iter().copied());
Cli::try_parse_from(args)
#[test]
fn the_cli_parses_with_and_without_colors() {
let default = parse(&[]).expect("parses");
assert_eq!(default.colors, Colors(None));
assert_eq!(default.cli_config().depth, None);
let forced = parse(&["--colors", "16"]).expect("parses");
assert_eq!(forced.colors, Colors(Some(ColorDepth::Ansi16)));
assert_eq!(
parse(&["--colors", "auto"]).expect("parses").colors,
Colors(None)
);
assert_eq!(forced.cli_config().depth, Some(ColorDepth::Ansi16));
assert!(parse(&["--colors", "8"]).is_err());
fn the_cli_parses_the_theme_flag() {
assert_eq!(parse(&[]).expect("parses").theme, None);
let turbo = parse(&["--theme", "turbo"]).expect("parses");
assert_eq!(turbo.cli_config().preset.as_deref(), Some("turbo"));
assert!(parse(&["--theme", "dn"]).is_ok());
assert!(parse(&["--theme", "bogus"]).is_err());
fn the_cli_parses_the_keymap_flag() {
assert_eq!(parse(&[]).expect("parses").keymap, None);
for name in ["cua", "emacs", "vim"] {
let cli = parse(&["--keymap", name]).expect("parses");
assert_eq!(cli.keymap.as_deref(), Some(name));
assert_eq!(cli.cli_config().keymap.as_deref(), Some(name));
assert!(parse(&["--keymap", "bogus"]).is_err());
assert!(parse(&["--edit-mode", "vim"]).is_err());