diff options
| author | Kunal Mohan <kunalmohan99@gmail.com> | 2021-05-16 16:42:50 +0530 |
|---|---|---|
| committer | Kunal Mohan <kunalmohan99@gmail.com> | 2021-05-16 21:41:56 +0530 |
| commit | 2038947a14d32d89da2bdbcaa1381c3015ac9c7a (patch) | |
| tree | 5a332470e32b380816098594a89d3c3db1f6eb53 /src/common/utils | |
| parent | handle error on termios initialization (#511) (diff) | |
| download | sl0ppij-2038947a14d32d89da2bdbcaa1381c3015ac9c7a.tar.gz sl0ppij-2038947a14d32d89da2bdbcaa1381c3015ac9c7a.tar.xz sl0ppij-2038947a14d32d89da2bdbcaa1381c3015ac9c7a.zip | |
Big refactor: separate crates for client, server and utilities
Diffstat (limited to 'src/common/utils')
| -rw-r--r-- | src/common/utils/consts.rs | 54 | ||||
| -rw-r--r-- | src/common/utils/logging.rs | 85 | ||||
| -rw-r--r-- | src/common/utils/mod.rs | 5 | ||||
| -rw-r--r-- | src/common/utils/shared.rs | 108 |
4 files changed, 0 insertions, 252 deletions
diff --git a/src/common/utils/consts.rs b/src/common/utils/consts.rs deleted file mode 100644 index 42843eb8..00000000 --- a/src/common/utils/consts.rs +++ /dev/null @@ -1,54 +0,0 @@ -//! Zellij program-wide constants. - -use crate::os_input_output::set_permissions; -use directories_next::ProjectDirs; -use lazy_static::lazy_static; -use nix::unistd::Uid; -use std::path::PathBuf; -use std::{env, fs}; - -pub const ZELLIJ_CONFIG_FILE_ENV: &str = "ZELLIJ_CONFIG_FILE"; -pub const ZELLIJ_CONFIG_DIR_ENV: &str = "ZELLIJ_CONFIG_DIR"; -pub const VERSION: &str = env!("CARGO_PKG_VERSION"); - -pub const SYSTEM_DEFAULT_CONFIG_DIR: &str = "/etc/zellij"; -pub const SYSTEM_DEFAULT_DATA_DIR_PREFIX: &str = system_default_data_dir(); - -const fn system_default_data_dir() -> &'static str { - if let Some(data_dir) = std::option_env!("PREFIX") { - data_dir - } else { - &"/usr" - } -} - -lazy_static! { - static ref UID: Uid = Uid::current(); - pub static ref SESSION_NAME: String = names::Generator::default().next().unwrap(); - pub static ref ZELLIJ_PROJ_DIR: ProjectDirs = - ProjectDirs::from("org", "Zellij Contributors", "Zellij").unwrap(); - pub static ref ZELLIJ_IPC_PIPE: PathBuf = { - let mut ipc_dir = env::var("ZELLIJ_SOCKET_DIR").map_or_else( - |_| { - ZELLIJ_PROJ_DIR - .runtime_dir() - .map_or_else(|| ZELLIJ_TMP_DIR.clone(), |p| p.to_owned()) - }, - PathBuf::from, - ); - ipc_dir.push(VERSION); - fs::create_dir_all(&ipc_dir).unwrap(); - set_permissions(&ipc_dir).unwrap(); - ipc_dir.push(&*SESSION_NAME); - ipc_dir - }; - pub static ref ZELLIJ_TMP_DIR: PathBuf = - PathBuf::from("/tmp/zellij-".to_string() + &format!("{}", *UID)); - pub static ref ZELLIJ_TMP_LOG_DIR: PathBuf = ZELLIJ_TMP_DIR.join("zellij-log"); - pub static ref ZELLIJ_TMP_LOG_FILE: PathBuf = ZELLIJ_TMP_LOG_DIR.join("log.txt"); -} - -pub const FEATURES: &[&str] = &[ - #[cfg(feature = "disable_automatic_asset_installation")] - "disable_automatic_asset_installation", -]; diff --git a/src/common/utils/logging.rs b/src/common/utils/logging.rs deleted file mode 100644 index 0dd382d6..00000000 --- a/src/common/utils/logging.rs +++ /dev/null @@ -1,85 +0,0 @@ -//! Zellij logging utility functions. - -use std::{ - fs, - io::{self, prelude::*}, - os::unix::io::RawFd, - path::{Path, PathBuf}, -}; - -use crate::os_input_output::set_permissions; -use crate::utils::consts::{ZELLIJ_TMP_LOG_DIR, ZELLIJ_TMP_LOG_FILE}; - -pub fn atomic_create_file(file_name: &Path) -> io::Result<()> { - let _ = fs::OpenOptions::new() - .append(true) - .create(true) - .open(file_name)?; - set_permissions(file_name) -} - -pub fn atomic_create_dir(dir_name: &Path) -> io::Result<()> { - let result = if let Err(e) = fs::create_dir(dir_name) { - if e.kind() == std::io::ErrorKind::AlreadyExists { - Ok(()) - } else { - Err(e) - } - } else { - Ok(()) - }; - if result.is_ok() { - set_permissions(dir_name)?; - } - result -} - -pub fn debug_log_to_file(mut message: String) -> io::Result<()> { - message.push('\n'); - debug_log_to_file_without_newline(message) -} - -pub fn debug_log_to_file_without_newline(message: String) -> io::Result<()> { - atomic_create_file(&*ZELLIJ_TMP_LOG_FILE)?; - let mut file = fs::OpenOptions::new() - .append(true) - .open(&*ZELLIJ_TMP_LOG_FILE)?; - file.write_all(message.as_bytes()) -} - -pub fn _debug_log_to_file_pid_3(message: String, pid: RawFd) -> io::Result<()> { - if pid == 3 { - debug_log_to_file(message) - } else { - Ok(()) - } -} - -pub fn _delete_log_file() -> io::Result<()> { - if fs::metadata(&*ZELLIJ_TMP_LOG_FILE).is_ok() { - fs::remove_file(&*ZELLIJ_TMP_LOG_FILE) - } else { - Ok(()) - } -} - -pub fn _delete_log_dir() -> io::Result<()> { - if fs::metadata(&*ZELLIJ_TMP_LOG_DIR).is_ok() { - fs::remove_dir_all(&*ZELLIJ_TMP_LOG_DIR) - } else { - Ok(()) - } -} - -pub fn debug_to_file(message: u8, pid: RawFd) -> io::Result<()> { - let mut path = PathBuf::new(); - path.push(&*ZELLIJ_TMP_LOG_DIR); - path.push(format!("zellij-{}.log", pid.to_string())); - - let mut file = fs::OpenOptions::new() - .append(true) - .create(true) - .open(&path)?; - set_permissions(&path)?; - file.write_all(&[message]) -} diff --git a/src/common/utils/mod.rs b/src/common/utils/mod.rs deleted file mode 100644 index 1cb4df27..00000000 --- a/src/common/utils/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -//! Zellij utilities. - -pub mod consts; -pub mod logging; -pub mod shared; diff --git a/src/common/utils/shared.rs b/src/common/utils/shared.rs deleted file mode 100644 index 26a2f90a..00000000 --- a/src/common/utils/shared.rs +++ /dev/null @@ -1,108 +0,0 @@ -//! Some general utility functions. - -use std::{iter, str::from_utf8}; - -use strip_ansi_escapes::strip; - -use colors_transform::{Color, Rgb}; -use zellij_tile::data::{Palette, PaletteColor, PaletteSource, Theme}; - -fn ansi_len(s: &str) -> usize { - from_utf8(&strip(s.as_bytes()).unwrap()) - .unwrap() - .chars() - .count() -} - -pub fn adjust_to_size(s: &str, rows: usize, columns: usize) -> String { - s.lines() - .map(|l| { - let actual_len = ansi_len(l); - if actual_len > columns { - let mut line = String::from(l); - line.truncate(columns); - line - } else { - [l, &str::repeat(" ", columns - ansi_len(l))].concat() - } - }) - .chain(iter::repeat(str::repeat(" ", columns))) - .take(rows) - .collect::<Vec<_>>() - .join("\n\r") -} - -// Colors -pub mod colors { - pub const WHITE: u8 = 255; - pub const GREEN: u8 = 154; - pub const GRAY: u8 = 238; - pub const BRIGHT_GRAY: u8 = 245; - pub const RED: u8 = 88; - pub const ORANGE: u8 = 166; - pub const BLACK: u8 = 16; -} - -pub fn _hex_to_rgb(hex: &str) -> (u8, u8, u8) { - let rgb = Rgb::from_hex_str(hex) - .expect("The passed argument must be a valid hex color") - .as_tuple(); - (rgb.0 as u8, rgb.1 as u8, rgb.2 as u8) -} - -pub fn default_palette() -> Palette { - Palette { - source: PaletteSource::Default, - theme: Theme::Dark, - fg: PaletteColor::EightBit(colors::BRIGHT_GRAY), - bg: PaletteColor::EightBit(colors::GRAY), - black: PaletteColor::EightBit(colors::BLACK), - red: PaletteColor::EightBit(colors::RED), - green: PaletteColor::EightBit(colors::GREEN), - yellow: PaletteColor::EightBit(colors::GRAY), - blue: PaletteColor::EightBit(colors::GRAY), - magenta: PaletteColor::EightBit(colors::GRAY), - cyan: PaletteColor::EightBit(colors::GRAY), - white: PaletteColor::EightBit(colors::WHITE), - orange: PaletteColor::EightBit(colors::ORANGE), - } -} - -// Dark magic -pub fn _detect_theme(bg: PaletteColor) -> Theme { - match bg { - PaletteColor::Rgb((r, g, b)) => { - // HSP, P stands for perceived brightness - let hsp: f64 = (0.299 * (r as f64 * r as f64) - + 0.587 * (g as f64 * g as f64) - + 0.114 * (b as f64 * b as f64)) - .sqrt(); - match hsp > 127.5 { - true => Theme::Light, - false => Theme::Dark, - } - } - _ => Theme::Dark, - } -} - -// (this was shamelessly copied from alacritty) -// -// This returns the current terminal version as a unique number based on the -// semver version. The different versions are padded to ensure that a higher semver version will -// always report a higher version number. -pub fn version_number(mut version: &str) -> usize { - if let Some(separator) = version.rfind('-') { - version = &version[..separator]; - } - - let mut version_number = 0; - - let semver_versions = version.split('.'); - for (i, semver_version) in semver_versions.rev().enumerate() { - let semver_number = semver_version.parse::<usize>().unwrap_or(0); - version_number += usize::pow(100, i as u32) * semver_number; - } - - version_number -} |
