aboutsummaryrefslogtreecommitdiffstats
path: root/crates/common/src/utils.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--crates/common/src/utils.rs (renamed from crates/turtle/src/atuin_common/utils.rs)69
1 files changed, 40 insertions, 29 deletions
diff --git a/crates/turtle/src/atuin_common/utils.rs b/crates/common/src/utils.rs
index dbe9dfbc..b50328a9 100644
--- a/crates/turtle/src/atuin_common/utils.rs
+++ b/crates/common/src/utils.rs
@@ -4,11 +4,29 @@ use std::path::{Path, PathBuf};
use uuid::Uuid;
-pub(crate) fn uuid_v7() -> Uuid {
+#[must_use]
+pub fn get_hostname() -> String {
+ env::var("ATUIN_HOST_NAME")
+ .unwrap_or_else(|_| whoami::hostname().unwrap_or_else(|_| "unknown-host".to_string()))
+}
+
+#[must_use]
+pub fn get_username() -> String {
+ env::var("ATUIN_HOST_USER")
+ .unwrap_or_else(|_| whoami::username().unwrap_or_else(|_| "unknown-user".to_string()))
+}
+
+/// Returns a pair of the hostname and username, separated by a colon.
+#[must_use]
+pub fn get_host_user() -> String {
+ format!("{}:{}", get_hostname(), get_username())
+}
+
+pub fn uuid_v7() -> Uuid {
Uuid::now_v7()
}
-pub(crate) fn has_git_dir(path: &str) -> bool {
+pub fn has_git_dir(path: &str) -> bool {
let mut gitdir = PathBuf::from(path);
gitdir.push(".git");
@@ -50,7 +68,7 @@ fn resolve_git_worktree(path: &Path) -> Option<PathBuf> {
// detect if any parent dir has a git repo in it
// I really don't want to bring in libgit for something simple like this
// If we start to do anything more advanced, then perhaps
-pub(crate) fn in_git_repo(path: &str) -> Option<PathBuf> {
+pub fn in_git_repo(path: &str) -> Option<PathBuf> {
let mut gitdir = PathBuf::from(path);
while gitdir.parent().is_some() && !has_git_dir(gitdir.to_str().unwrap()) {
@@ -73,41 +91,47 @@ pub(crate) fn in_git_repo(path: &str) -> Option<PathBuf> {
// I don't want to use ProjectDirs, it puts config in awkward places on
// mac. Data too. Seems to be more intended for GUI apps.
-pub(crate) fn home_dir() -> PathBuf {
+#[must_use]
+pub fn home_dir() -> PathBuf {
directories::BaseDirs::new()
.map(|d| d.home_dir().to_path_buf())
.expect("could not determine home directory")
}
-pub(crate) fn config_dir() -> PathBuf {
+#[must_use]
+pub fn config_dir() -> PathBuf {
let config_dir =
env::var("XDG_CONFIG_HOME").map_or_else(|_| home_dir().join(".config"), PathBuf::from);
- config_dir.join("atuin")
+ config_dir.join("turtle")
}
-pub(crate) fn data_dir() -> PathBuf {
+#[must_use]
+pub fn data_dir() -> PathBuf {
let data_dir = env::var("XDG_DATA_HOME")
.map_or_else(|_| home_dir().join(".local").join("share"), PathBuf::from);
- data_dir.join("atuin")
+ data_dir.join("turtle")
}
-pub(crate) fn runtime_dir() -> PathBuf {
- env::var("XDG_RUNTIME_DIR").map_or_else(|_| data_dir(), PathBuf::from)
+#[must_use]
+pub fn daemon_socket_path() -> PathBuf {
+ runtime_dir().join("turtle.sock")
}
-pub(crate) fn logs_dir() -> PathBuf {
- home_dir().join(".atuin").join("logs")
+#[must_use]
+pub fn runtime_dir() -> PathBuf {
+ env::var("XDG_RUNTIME_DIR").map_or_else(|_| data_dir(), PathBuf::from)
}
-pub(crate) fn get_current_dir() -> String {
+#[must_use]
+pub fn get_current_dir() -> String {
// Prefer PWD environment variable over cwd if available to better support symbolic links
env::var("PWD").unwrap_or_else(|_| {
env::current_dir().map_or_else(|_| String::new(), |dir| dir.display().to_string())
})
}
-pub(crate) fn broken_symlink<P: Into<PathBuf>>(path: P) -> bool {
+pub fn broken_symlink<P: Into<PathBuf>>(path: P) -> bool {
let path = path.into();
path.is_symlink() && !path.exists()
}
@@ -118,7 +142,7 @@ pub(crate) fn broken_symlink<P: Into<PathBuf>>(path: P) -> bool {
/// Intended to help prevent control characters being printed and interpreted by the terminal when
/// printing history as well as to ensure the commands that appear in the interactive search
/// reflect the actual command run rather than just the printable characters.
-pub(crate) trait Escapable: AsRef<str> {
+pub trait Escapable: AsRef<str> {
fn escape_control(&self) -> Cow<'_, str> {
if self.as_ref().contains(|c: char| c.is_ascii_control()) {
let mut remaining = self.as_ref();
@@ -146,7 +170,7 @@ impl<T: AsRef<str>> Escapable for T {}
#[cfg(test)]
mod tests {
- use super::{Cow, Escapable, Uuid, env, in_git_repo, uuid_v7};
+ use super::{Cow, Uuid, env, in_git_repo, uuid_v7};
use std::collections::HashSet;
@@ -241,17 +265,4 @@ mod tests {
std::fs::remove_dir_all(&tmp).unwrap();
}
-
- #[test]
- fn dumb_random_test() {
- // Obviously not a test of randomness, but make sure we haven't made some
- // catastrophic error
-
- assert_ne!(crypto_random_string::<1>(), crypto_random_string::<1>());
- assert_ne!(crypto_random_string::<2>(), crypto_random_string::<2>());
- assert_ne!(crypto_random_string::<4>(), crypto_random_string::<4>());
- assert_ne!(crypto_random_string::<8>(), crypto_random_string::<8>());
- assert_ne!(crypto_random_string::<16>(), crypto_random_string::<16>());
- assert_ne!(crypto_random_string::<32>(), crypto_random_string::<32>());
- }
}