aboutsummaryrefslogtreecommitdiffstats
path: root/nix-conf/overlays/atuin-session-host-filter.patch
blob: 9606f6bd72e35ff02c7ba756301fe1f04abd533d (plain) (blame)
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
diff --git a/crates/atuin-client/src/database.rs b/crates/atuin-client/src/database.rs
index 7c63368..338e107 100644
--- a/crates/atuin-client/src/database.rs
+++ b/crates/atuin-client/src/database.rs
@@ -367,6 +367,16 @@ impl Database for Sqlite {
                     }
                     &mut query
                 }
+                FilterMode::SessionHost => {
+                    query.and_where_eq("session", quote(&context.session));
+                    if let Some(session_start) = session_start {
+                        query.or_where(format!(
+                            "hostname = {} and timestamp < {session_start}",
+                            quote(&context.hostname)
+                        ));
+                    }
+                    &mut query
+                }
                 FilterMode::Directory => query.and_where_eq("cwd", quote(&context.cwd)),
                 FilterMode::Workspace => query.and_where_like_left("cwd", &git_root),
             };
@@ -498,6 +508,16 @@ impl Database for Sqlite {
                 }
                 &mut sql
             }
+            FilterMode::SessionHost => {
+                sql.and_where_eq("session", quote(&context.session));
+                if let Some(session_start) = session_start {
+                    sql.or_where(format!(
+                        "lower(hostname) = {} and timestamp < {session_start}",
+                        quote(context.hostname.to_lowercase())
+                    ));
+                }
+                &mut sql
+            }
             FilterMode::Directory => sql.and_where_eq("cwd", quote(&context.cwd)),
             FilterMode::Workspace => sql.and_where_like_left("cwd", git_root),
         };
diff --git a/crates/atuin-client/src/settings.rs b/crates/atuin-client/src/settings.rs
index 745bd2f..4580db3 100644
--- a/crates/atuin-client/src/settings.rs
+++ b/crates/atuin-client/src/settings.rs
@@ -114,6 +114,9 @@ pub enum FilterMode {

     #[serde(rename = "session-preload")]
     SessionPreload = 5,
+
+    #[serde(rename = "session-host")]
+    SessionHost = 6,
 }

 impl FilterMode {
@@ -125,6 +128,7 @@ impl FilterMode {
             FilterMode::Directory => "DIRECTORY",
             FilterMode::Workspace => "WORKSPACE",
             FilterMode::SessionPreload => "SESSION+",
+            FilterMode::SessionHost => "SESSION*",
         }
     }
 }
@@ -774,6 +778,7 @@ impl Default for Search {
                 FilterMode::Host,
                 FilterMode::Session,
                 FilterMode::SessionPreload,
+                FilterMode::SessionHost,
                 FilterMode::Workspace,
                 FilterMode::Directory,
             ],
@@ -1461,6 +1466,7 @@ impl Settings {
                     "workspace",
                     "directory",
                     "session-preload",
+                    "session-host",
                 ],
             )?
             .set_default("theme.name", "default")?
diff --git a/crates/atuin-daemon/src/client.rs b/crates/atuin-daemon/src/client.rs
index 2f492f6..2fbb5d0 100644
--- a/crates/atuin-daemon/src/client.rs
+++ b/crates/atuin-daemon/src/client.rs
@@ -229,6 +229,7 @@ impl From<FilterMode> for RpcFilterMode {
             FilterMode::Directory => RpcFilterMode::Directory,
             FilterMode::Workspace => RpcFilterMode::Workspace,
             FilterMode::SessionPreload => RpcFilterMode::SessionPreload,
+            FilterMode::SessionHost => RpcFilterMode::Session,
         }
     }
 }
diff --git a/crates/atuin/src/command/client/search/engines/skim.rs b/crates/atuin/src/command/client/search/engines/skim.rs
index 7d9feb4..01a2c15 100644
--- a/crates/atuin/src/command/client/search/engines/skim.rs
+++ b/crates/atuin/src/command/client/search/engines/skim.rs
@@ -131,6 +131,51 @@ async fn fuzzy_search(
                     }
                 }
             }
+            // SessionHost: include current session + host history from before session start
+            FilterMode::SessionHost => {
+                let is_current_session = {
+                    history
+                        .session
+                        .as_bytes()
+                        .chunks(32)
+                        .any(|chunk| chunk == context.session.as_bytes())
+                };
+
+                if !is_current_session {
+                    let is_host_history = history
+                        .hostname
+                        .split(',')
+                        .contains(&context.hostname.as_str());
+                    if !is_host_history {
+                        continue;
+                    }
+
+                    let Ok(uuid) = uuid::Uuid::parse_str(&context.session) else {
+                        log::warn!("failed to parse session id '{}'", context.session);
+                        continue;
+                    };
+                    let Some(timestamp) = uuid.get_timestamp() else {
+                        log::warn!(
+                            "failed to get timestamp from uuid '{}'",
+                            uuid.as_hyphenated()
+                        );
+                        continue;
+                    };
+                    let (seconds, nanos) = timestamp.to_unix();
+                    let Ok(session_start) = time::OffsetDateTime::from_unix_timestamp_nanos(
+                        i128::from(seconds) * 1_000_000_000 + i128::from(nanos),
+                    ) else {
+                        log::warn!(
+                            "failed to create OffsetDateTime from second: {seconds}, nanosecond: {nanos}"
+                        );
+                        continue;
+                    };
+
+                    if history.timestamp >= session_start {
+                        continue;
+                    }
+                }
+            }
             // we aggregate directory by ':' separating them
             FilterMode::Directory if history.cwd.split(':').contains(&context.cwd.as_str()) => {}
             FilterMode::Workspace if history.cwd.split(':').contains(&git_root) => {}