Skip to content

Commit

Permalink
fix(gui): do not hardcode db path (atuinsh#2309)
Browse files Browse the repository at this point in the history
* feat(gui/backend): add cli_settings tauri command

* chore(gui/backend): overdue cargo fmt

* fix(gui): use configured db path, not hardcoded
  • Loading branch information
ellie authored Jul 25, 2024
1 parent 3cf5299 commit 128891f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
9 changes: 5 additions & 4 deletions ui/backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 25 additions & 11 deletions ui/backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ async fn login(username: String, password: String, key: String) -> Result<String
async fn logout() -> Result<(), String> {
let settings = Settings::new().map_err(|e| e.to_string())?;

atuin_client::logout::logout(&settings)
.map_err(|e| e.to_string())?;
atuin_client::logout::logout(&settings).map_err(|e| e.to_string())?;

Ok(())
}
Expand Down Expand Up @@ -154,11 +153,18 @@ async fn home_info() -> Result<HomeInfo, String> {
.await
.map_err(|e| e.to_string())?;


let history = db.list(None, None).await?;
let stats = stats::compute(&settings, &history, 10, 1).map_or(vec![], |stats|stats.top[0..5].to_vec()).iter().map(|(commands, count)| (commands.join(" "), *count as u64)).collect();
let recent = if history.len() > 5 {history[0..5].to_vec()} else {vec![]};
let recent = recent.into_iter().map(|h|h.into()).collect();
let stats = stats::compute(&settings, &history, 10, 1)
.map_or(vec![], |stats| stats.top[0..5].to_vec())
.iter()
.map(|(commands, count)| (commands.join(" "), *count as u64))
.collect();
let recent = if history.len() > 5 {
history[0..5].to_vec()
} else {
vec![]
};
let recent = recent.into_iter().map(|h| h.into()).collect();

let info = if !settings.logged_in() {
HomeInfo {
Expand Down Expand Up @@ -257,6 +263,12 @@ async fn prefix_search(query: &str) -> Result<Vec<String>, String> {
Ok(commands)
}

#[tauri::command]
async fn cli_settings() -> Result<Settings, String> {
let settings = Settings::new().map_err(|e| e.to_string())?;
Ok(settings)
}

fn show_window(app: &AppHandle) {
let windows = app.webview_windows();

Expand Down Expand Up @@ -284,6 +296,7 @@ fn main() {
logout,
register,
history_calendar,
cli_settings,
run::pty::pty_open,
run::pty::pty_write,
run::pty::pty_resize,
Expand All @@ -298,16 +311,17 @@ fn main() {
dotfiles::vars::delete_var,
dotfiles::vars::set_var,
])
.plugin(tauri_plugin_sql::Builder::default().add_migrations("sqlite:runbooks.db", run::migrations::migrations()).build())
.plugin(
tauri_plugin_sql::Builder::default()
.add_migrations("sqlite:runbooks.db", run::migrations::migrations())
.build(),
)
.plugin(tauri_plugin_http::init())
.plugin(tauri_plugin_single_instance::init(|app, args, cwd| {
let _ = show_window(app);

}))
.manage(state::AtuinState::default())
.setup(|app|{
Ok(())
})
.setup(|app| Ok(()))
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
8 changes: 3 additions & 5 deletions ui/backend/src/run/pty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,12 @@ pub(crate) async fn pty_kill(
let pty = state.pty_sessions.write().await.remove(&pid);

match pty {
Some(pty)=>{

pty.kill_child().await.map_err(|e|e.to_string())?;
Some(pty) => {
pty.kill_child().await.map_err(|e| e.to_string())?;
println!("RIP {pid:?}");
}
None=>{}
None => {}
}


Ok(())
}
11 changes: 5 additions & 6 deletions ui/src/state/models.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { invoke } from "@tauri-apps/api/core";
import Database from "@tauri-apps/plugin-sql";

export class User {
Expand Down Expand Up @@ -122,9 +123,8 @@ interface Sync {
export async function inspectCommandHistory(
h: ShellHistory,
): Promise<InspectHistory> {
const db = await Database.load(
"sqlite:/Users/ellie/.local/share/atuin/history.db",
);
const settings: Settings = await invoke("cli_settings");
const db = await Database.load("sqlite:" + settings.db_path);

let other: any[] = await db.select(
"select * from history where command=?1 order by timestamp desc",
Expand All @@ -151,9 +151,8 @@ export async function inspectCommandHistory(
export async function inspectDirectoryHistory(
h: ShellHistory,
): Promise<InspectHistory> {
const db = await Database.load(
"sqlite:/Users/ellie/.local/share/atuin/history.db",
);
const settings: Settings = await invoke("cli_settings");
const db = await Database.load("sqlite:" + settings.db_path);

let other: any[] = await db.select(
"select * from history where cwd=?1 order by timestamp desc",
Expand Down

0 comments on commit 128891f

Please sign in to comment.