1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
//! Contains miscellaneous helper utilities.
use std::cmp::Ordering;
pub use cxx::Exception;
use terminal_size::{terminal_size, Height, Width};
use crate::config;
use crate::raw::util::raw;
/// Get the terminal's height, i.e. the number of rows it has.
///
/// # Returns:
/// * The terminal height, or `24` if it cannot be determined.
pub fn terminal_height() -> usize {
if let Some((_, Height(rows))) = terminal_size() {
usize::from(rows)
} else {
24
}
}
/// Get the terminal's width, i.e. the number of columns it has.
///
/// # Returns:
/// * The terminal width, or `80` if it cannot be determined.
pub fn terminal_width() -> usize {
if let Some((Width(cols), _)) = terminal_size() {
usize::from(cols)
} else {
80
}
}
/// Compares two package versions, `ver1` and `ver2`. The returned enum variant
/// applies to the first version passed in.
///
/// # Examples
/// ```
/// use rust_apt::util::cmp_versions;
/// use std::cmp::Ordering;
///
/// let ver1 = "5.0";
/// let ver2 = "6.0";
/// let result = cmp_versions(ver1, ver2);
///
/// assert_eq!(Ordering::Less, result);
/// ```
pub fn cmp_versions(ver1: &str, ver2: &str) -> Ordering {
let result = raw::cmp_versions(ver1.to_owned(), ver2.to_owned());
match result {
_ if result < 0 => Ordering::Less,
_ if result == 0 => Ordering::Equal,
_ => Ordering::Greater,
}
}
/// Disk Space that `apt` will use for a transaction.
pub enum DiskSpace {
/// Additional Disk Space required.
Require(u64),
/// Disk Space that will be freed
Free(u64),
}
/// Numeral System for unit conversion.
pub enum NumSys {
/// Base 2 | 1024 | KibiByte (KiB)
Binary,
/// Base 10 | 1000 | KiloByte (KB)
Decimal,
}
/// Converts bytes into human readable output.
///
/// ```
/// use rust_apt::new_cache;
/// use rust_apt::util::{unit_str, NumSys};
/// let cache = new_cache!().unwrap();
/// let pkg = cache.get("apt").unwrap();
/// let version = pkg.candidate().unwrap();
///
/// println!("{}", unit_str(version.size(), NumSys::Decimal));
/// ```
pub fn unit_str(val: u64, base: NumSys) -> String {
let val = val as f64;
let (num, tera, giga, mega, kilo) = match base {
NumSys::Binary => (1024.0_f64, "TiB", "GiB", "MiB", "KiB"),
NumSys::Decimal => (1000.0_f64, "TB", "GB", "MB", "KB"),
};
let powers = [
(num.powi(4), tera),
(num.powi(3), giga),
(num.powi(2), mega),
(num, kilo),
];
for (divisor, unit) in powers {
if val > divisor {
return format!("{:.2} {unit}", val / divisor);
}
}
format!("{val} B")
}
/// Converts seconds into a human readable time string.
pub fn time_str(seconds: u64) -> String {
if seconds > 60 * 60 * 24 {
return format!(
"{}d {}h {}min {}s",
seconds / 60 / 60 / 24,
(seconds / 60 / 60) % 24,
(seconds / 60) % 60,
seconds % 60,
);
}
if seconds > 60 * 60 {
return format!(
"{}h {}min {}s",
(seconds / 60 / 60) % 24,
(seconds / 60) % 60,
seconds % 60,
);
}
if seconds > 60 {
return format!("{}min {}s", (seconds / 60) % 60, seconds % 60,);
}
format!("{seconds}s")
}
/// Get an APT-styled progress bar.
///
/// # Returns:
/// * [`String`] representing the progress bar.
///
/// # Example:
/// ```
/// use rust_apt::util::get_apt_progress_string;
/// let progress = get_apt_progress_string(0.5, 10);
/// assert_eq!(progress, "[####....]");
/// ```
pub fn get_apt_progress_string(percent: f32, output_width: u32) -> String {
raw::get_apt_progress_string(percent, output_width)
}
/// Lock the APT lockfile.
/// This should be done before modifying any APT files
/// such as with [`crate::cache::Cache::update`]
/// and then [`apt_unlock`] should be called after.
///
/// This Function Requires root
///
/// If [`apt_lock`] is called `n` times, [`apt_unlock`] must also be called `n`
/// times to release all acquired locks.
///
/// # Known Error Messages:
/// * `E:Could not open lock file /var/lib/dpkg/lock-frontend - open (13:
/// Permission denied)`
/// * `E:Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend),
/// are you root?`
pub fn apt_lock() -> Result<(), Exception> {
config::init_config_system();
raw::apt_lock()
}
/// Unlock the APT lockfile.
pub fn apt_unlock() {
config::init_config_system();
raw::apt_unlock()
}
/// Unlock the Dpkg lockfile.
/// This should be done before manually running
/// [`crate::cache::Cache::do_install`]
/// and then [`apt_unlock_inner`] should be called after.
///
/// This Function Requires root
pub fn apt_lock_inner() -> Result<(), Exception> {
config::init_config_system();
raw::apt_lock_inner()
}
/// Unlock the Dpkg lockfile.
pub fn apt_unlock_inner() {
config::init_config_system();
raw::apt_unlock_inner()
}
/// Checks if any locks are currently active for the lockfile. Note that this
/// will only return [`true`] if the current process has an active lock, calls
/// to [`apt_lock`] will return an [`Exception`] if another process has an
/// active lock.
pub fn apt_is_locked() -> bool {
config::init_config_system();
raw::apt_is_locked()
}