Skip to content

Commit

Permalink
Add workspace mode, enable if in git repo
Browse files Browse the repository at this point in the history
  • Loading branch information
ellie committed Jun 15, 2023
1 parent 0c75cfb commit ed89e1c
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 7 deletions.
17 changes: 16 additions & 1 deletion atuin-client/src/database.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{env, path::Path, str::FromStr};
use std::{
env,
path::{Path, PathBuf},
str::FromStr,
};

use async_trait::async_trait;
use atuin_common::utils;
Expand All @@ -25,6 +29,7 @@ pub struct Context {
pub cwd: String,
pub hostname: String,
pub host_id: String,
pub git_root: Option<PathBuf>,
}

#[derive(Default, Clone)]
Expand Down Expand Up @@ -52,12 +57,14 @@ pub fn current_context() -> Context {
);
let cwd = utils::get_current_dir();
let host_id = Settings::host_id().expect("failed to load host ID");
let git_root = utils::in_git_repo(cwd.as_str());

Context {
session,
hostname,
cwd,
host_id,
git_root,
}
}

Expand Down Expand Up @@ -260,6 +267,7 @@ impl Database for Sqlite {
FilterMode::Host => query.and_where_eq("hostname", quote(&context.hostname)),
FilterMode::Session => query.and_where_eq("session", quote(&context.session)),
FilterMode::Directory => query.and_where_eq("cwd", quote(&context.cwd)),
FilterMode::Workspace => query.and_where_like_any("cwd", context.cwd.clone()),
};

if unique {
Expand Down Expand Up @@ -379,11 +387,18 @@ impl Database for Sqlite {
sql.order_desc("timestamp");
}

let git_root = if let Some(git_root) = context.git_root.clone() {
git_root.to_str().unwrap_or("/").to_string()
} else {
context.cwd.clone()
};

match filter {
FilterMode::Global => &mut sql,
FilterMode::Host => sql.and_where_eq("hostname", quote(&context.hostname)),
FilterMode::Session => sql.and_where_eq("session", quote(&context.session)),
FilterMode::Directory => sql.and_where_eq("cwd", quote(&context.cwd)),
FilterMode::Workspace => sql.and_where_like_left("cwd", git_root),
};

let orig_query = query;
Expand Down
6 changes: 6 additions & 0 deletions atuin-client/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ pub enum FilterMode {

#[serde(rename = "directory")]
Directory = 3,

#[serde(rename = "workspace")]
Workspace = 4,
}

impl FilterMode {
Expand All @@ -78,6 +81,7 @@ impl FilterMode {
FilterMode::Host => "HOST",
FilterMode::Session => "SESSION",
FilterMode::Directory => "DIRECTORY",
FilterMode::Workspace => "WORKSPACE",
}
}
}
Expand Down Expand Up @@ -160,6 +164,7 @@ pub struct Settings {
pub history_filter: RegexSet,
#[serde(with = "serde_regex", default = "RegexSet::empty")]
pub cwd_filter: RegexSet,
pub workspaces: bool,

// This is automatically loaded when settings is created. Do not set in
// config! Keep secrets and settings apart.
Expand Down Expand Up @@ -369,6 +374,7 @@ impl Settings {
.set_default("scroll_context_lines", 1)?
.set_default("shell_up_key_binding", false)?
.set_default("session_token", "")?
.set_default("workspaces", false)?
.add_source(
Environment::with_prefix("atuin")
.prefix_separator("_")
Expand Down
27 changes: 26 additions & 1 deletion atuin-common/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::env;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use chrono::{Months, NaiveDate};
use rand::RngCore;
Expand Down Expand Up @@ -46,6 +46,31 @@ pub fn uuid_v4() -> String {
Uuid::new_v4().as_simple().to_string()
}

pub fn has_git_dir(path: &str) -> bool {
let mut gitdir = PathBuf::from(path);
gitdir.push(".git");

gitdir.exists()
}

// 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 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()) {
gitdir.pop();
}

// No parent? then we hit root, finding no git
if gitdir.parent().is_some() {
return Some(gitdir);
}

return None;
}

// TODO: more reliable, more tested
// 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.
Expand Down
2 changes: 1 addition & 1 deletion atuin/src/command/client/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use runtime_format::{FormatKey, FormatKeyError, ParseSegment, ParsedFmt};
use atuin_client::{
database::{current_context, Database},
history::History,
settings::Settings,
settings::{FilterMode, Settings},
};

#[cfg(feature = "sync")]
Expand Down
11 changes: 9 additions & 2 deletions atuin/src/command/client/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,21 @@ async fn run_non_interactive(
let context = current_context();

let opt_filter = OptFilters {
cwd: dir,
cwd: dir.clone(),
..filter_options
};

let dir = dir.unwrap_or("/".to_string());
let filter_mode = if settings.workspaces && utils::has_git_dir(dir.as_str()) {
FilterMode::Workspace
} else {
settings.filter_mode
};

let results = db
.search(
settings.search_mode,
settings.filter_mode,
filter_mode,
&context,
query.join(" ").as_str(),
opt_filter,
Expand Down
7 changes: 5 additions & 2 deletions atuin/src/command/client/search/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
time::Duration,
};

use atuin_common::utils;
use crossterm::{
event::{self, Event, KeyCode, KeyEvent, KeyModifiers, MouseEvent},
execute, terminal,
Expand Down Expand Up @@ -586,14 +587,16 @@ pub async fn history(
search_mode: settings.search_mode,
search: SearchState {
input,
context,
filter_mode: if settings.shell_up_key_binding {
filter_mode: if settings.workspaces && context.git_root.is_some() {
FilterMode::Workspace
} else if settings.shell_up_key_binding {
settings
.filter_mode_shell_up_key_binding
.unwrap_or(settings.filter_mode)
} else {
settings.filter_mode
},
context,
},
engine: engines::engine(settings.search_mode),
results_len: 0,
Expand Down
1 change: 1 addition & 0 deletions atuin/src/command/client/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl Cmd {
} else {
self.period.join(" ")
};

let history = if words.as_str() == "all" {
db.list(FilterMode::Global, &context, None, false).await?
} else if words.trim() == "today" {
Expand Down

0 comments on commit ed89e1c

Please sign in to comment.