Skip to content

Commit 86fa082

Browse files
committed
allow locking of session and dir
1 parent ac251cd commit 86fa082

File tree

8 files changed

+140
-15
lines changed

8 files changed

+140
-15
lines changed

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ enum-map = "2.1.0"
1414
log = "0.4"
1515
textwrap = "0.15"
1616
humantime = "2.1.0"
17+
once_cell = "1.19.0"
1718

1819
[dev-dependencies]
1920
regex = "*"

src/focus.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use once_cell::sync::Lazy;
2+
use std::sync::{Mutex, MutexGuard};
3+
4+
struct AppState {
5+
focus_session: Option<String>,
6+
focus_dir: Option<String>,
7+
}
8+
static APP_STATE: Lazy<Mutex<AppState>> = Lazy::new(|| {
9+
Mutex::new(AppState{
10+
focus_session : None,
11+
focus_dir: None,
12+
})
13+
});
14+
15+
16+
fn access_app_state() -> MutexGuard<'static, AppState> {
17+
APP_STATE.lock().unwrap()
18+
}
19+
20+
pub fn focus_session(session: &str){
21+
let mut app_state = access_app_state();
22+
app_state.focus_session = Some(session.to_string());
23+
}
24+
25+
pub fn get_focus_session() -> Option<String>{
26+
let app_state = access_app_state();
27+
return match app_state.focus_session.as_ref() {
28+
Some(v) => return Some(v.to_string()),
29+
None => None,
30+
};
31+
}
32+
33+
pub fn reset_focus_session(){
34+
let mut app_state = access_app_state();
35+
app_state.focus_session = None;
36+
}
37+
38+
pub fn focus_dir(dir: &str){
39+
let mut app_state = access_app_state();
40+
app_state.focus_dir = Some(dir.to_string());
41+
}
42+
43+
pub fn get_focus_dir() -> Option<String>{
44+
let app_state = access_app_state();
45+
return match app_state.focus_dir.as_ref() {
46+
Some(v) => return Some(v.to_string()),
47+
None => None,
48+
};
49+
}
50+
51+
pub fn reset_focus_dir(){
52+
let mut app_state = access_app_state();
53+
app_state.focus_dir = None;
54+
}

src/history.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ impl History {
3939
pub fn command(&self) -> &String {
4040
return &self.cmd;
4141
}
42+
pub fn session(&self) -> String {
43+
return self.session.to_string();
44+
}
45+
pub fn dir(&self) -> String {
46+
return self.dir.to_string();
47+
}
4248
}
4349

4450
impl History {

src/main.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
extern crate skim;
22
mod environment;
3+
mod focus;
34
mod history;
45
mod location;
56
mod query;
67
mod title;
78

89
use crate::environment::*;
10+
use crate::focus::focus_dir;
11+
use crate::focus::focus_session;
12+
use crate::focus::get_focus_dir;
13+
use crate::focus::get_focus_session;
14+
use crate::focus::reset_focus_dir;
15+
use crate::focus::reset_focus_session;
916
use crate::history::History;
1017
use crate::location::Location;
1118
use crate::query::build_query_string;
@@ -99,6 +106,8 @@ fn show_history(thequery: String) -> Result<String, String> {
99106
"f3:abort",
100107
"f4:abort",
101108
"f5:abort",
109+
"f6:abort",
110+
"f7:abort",
102111
"ctrl-r:abort",
103112
"ctrl-u:half-page-up",
104113
"ctrl-d:half-page-down",
@@ -170,6 +179,28 @@ fn process_result(
170179
Key::F(5) => {
171180
*grouped = !*grouped;
172181
}
182+
Key::F(6) => {
183+
if get_focus_session().is_none() {
184+
focus_session(
185+
&((*sel.selected_items[0]).as_any().downcast_ref::<History>())
186+
.unwrap()
187+
.session(),
188+
);
189+
} else {
190+
reset_focus_session();
191+
}
192+
}
193+
Key::F(7) => {
194+
if get_focus_dir().is_none() {
195+
focus_dir(
196+
&((*sel.selected_items[0]).as_any().downcast_ref::<History>())
197+
.unwrap()
198+
.dir(),
199+
);
200+
} else {
201+
reset_focus_dir();
202+
}
203+
}
173204
Key::Ctrl('r') => {
174205
*loc = match *loc {
175206
Location::Session => Location::Directory,
@@ -205,7 +236,7 @@ fn main() -> Result<()> {
205236
}(args);
206237

207238
if query == "--version" {
208-
println!("v0.8.7");
239+
println!("v0.8.8");
209240
std::process::exit(1);
210241
}
211242

src/query.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use crate::environment::*;
22
use crate::location::Location;
3+
use crate::focus::get_focus_session;
4+
use crate::focus::get_focus_dir;
5+
36

47
pub fn build_query_string(theloc: &Location, grouped: bool) -> String {
58
let mut query = String::from("select history.id as id, commands.argv as cmd,");
@@ -26,11 +29,27 @@ pub fn build_query_string(theloc: &Location, grouped: bool) -> String {
2629
};
2730
match theloc {
2831
Location::Session => {
29-
query.push_str(&format!(" session == {} and", &get_current_session_id()));
32+
let session = get_focus_session().unwrap_or(get_current_session_id());
33+
query.push_str(&format!(" session == {} and", session));
3034
}
3135

3236
Location::Directory => {
33-
query.push_str(&format!(" places.dir like '{}' and", &get_current_dir()));
37+
let session = get_focus_dir().unwrap_or(get_current_dir());
38+
query.push_str(&format!(" places.dir like '{}' and", session));
39+
}
40+
41+
_ => {}
42+
};
43+
match theloc {
44+
Location::Machine | Location::Everywhere => {
45+
let session = get_focus_session();
46+
let dir = get_focus_dir();
47+
if dir.is_some() {
48+
query.push_str(&format!(" places.dir like '{}' and", dir.unwrap()));
49+
}
50+
if session.is_some() {
51+
query.push_str(&format!(" session == {} and", session.unwrap()));
52+
}
3453
}
3554

3655
_ => {}

src/title.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use crate::environment::*;
2+
use crate::focus::get_focus_dir;
3+
use crate::focus::get_focus_session;
24
use crate::location::Location;
35
use enum_map::enum_map;
46

@@ -12,6 +14,15 @@ pub fn generate_title(location: &Location) -> String {
1214
};
1315
}(&location);
1416

17+
let format_extra_info = |info: Option<String>, title: &str| -> String {
18+
return match info {
19+
Some(ri) => format!("{}: {} ", &title, &ri,),
20+
None => String::from(""),
21+
};
22+
};
23+
let focus_session = format_extra_info(get_focus_session(), "Session");
24+
let focus_dir = format_extra_info(get_focus_dir(), "Directory");
25+
1526
let location_map = enum_map! {
1627
Location::Session => "Session location history",
1728
Location::Directory => "Directory location history",
@@ -22,28 +33,30 @@ pub fn generate_title(location: &Location) -> String {
2233
let header_map = enum_map! {
2334
Location::Session =>
2435
" ┏━━━━━━━━━━━┱─────────────┬────────┬──────────────┐
25-
┃F1: Session┃F2: Directory│F3: Host│F4: Everywhere│ F5: Toggle group
26-
━┛ ┗━━━━━━━━━━━━━┷━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━",
36+
┃F1: Session┃F2: Directory│F3: Host│F4: Everywhere│ F5: Toggle group, F6: Lock Session, F7: Lock Dir
37+
━┛ ┗━━━━━━━━━━━━━┷━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━",
2738
Location::Directory =>
2839
" ┌───────────┲━━━━━━━━━━━━━┱────────┬──────────────┐
29-
│F1: Session┃F2: Directory┃F3: Host│F4: Everywhere│ F5: Toggle group
30-
━┷━━━━━━━━━━━┛ ┗━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━",
40+
│F1: Session┃F2: Directory┃F3: Host│F4: Everywhere│ F5: Toggle group, F6: Lock Session, F7: Lock Dir
41+
━┷━━━━━━━━━━━┛ ┗━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━",
3142

3243
Location::Machine =>
3344
" ┌───────────┬─────────────┲━━━━━━━━┱──────────────┐
34-
│F1: Session│F2: Directory┃F3: Host┃F4: Everywhere│ F5: Toggle group
35-
━┷━━━━━━━━━━━┷━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━",
45+
│F1: Session│F2: Directory┃F3: Host┃F4: Everywhere│ F5: Toggle group, F6: Lock Session, F7: Lock Dir
46+
━┷━━━━━━━━━━━┷━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━",
3647

3748
Location::Everywhere =>
3849
" ┌───────────┬─────────────┬────────┲━━━━━━━━━━━━━━┓
39-
│F1: Session│F2: Directory│F3: Host┃F4: Everywhere┃ F5: Toggle group
40-
━┷━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━┛ ┗━━━━━━━━━━━━━━━━━",
50+
│F1: Session│F2: Directory│F3: Host┃F4: Everywhere┃ F5: Toggle group, F6: Lock Session, F7: Lock Dir
51+
━┷━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━┛ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━",
4152
};
4253

4354
let title = format!(
44-
"{} {}\n{}\n",
55+
"{} {} {}{}\n{}\n",
4556
&location_map[location.clone()],
4657
&extra_info,
58+
&focus_session,
59+
&focus_dir,
4760
&header_map[location.clone()],
4861
);
4962
return title.to_string();

zsh-histdb-skim.zsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ XDG_BIN_PATH=${XDG_DATA_HOME:-$HOME/.local/share}/zsh-histdb-skim/
22
BIN_DIR=${HISTDB_SKIM_PATH:-${XDG_BIN_PATH}}
33
BIN_PATH=${BIN_DIR}/zsh-histdb-skim
44

5-
HISTB_SKIM_VERSION="v0.8.7"
5+
HISTB_SKIM_VERSION="v0.8.8"
66

77
histdb-skim-get-os(){
88
UNAME_STR=`uname -a`

0 commit comments

Comments
 (0)