aboutsummaryrefslogtreecommitdiffstats
path: root/ui/src/state/models.ts
blob: b92d64a6300fb1e9e5a201dc6c01ef2aa74d60ed (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
import Database from "@tauri-apps/plugin-sql";

export interface User {
  username: string;
}

export const DefaultUser: User = {
  username: "",
};

export interface HomeInfo {
  historyCount: number;
  recordCount: number;
  lastSyncTime: Date;
}

export const DefaultHomeInfo: HomeInfo = {
  historyCount: 0,
  recordCount: 0,
  lastSyncTime: new Date(),
};

export class ShellHistory {
  id: string;
  timestamp: number;
  command: string;
  user: string;
  host: string;
  cwd: string;
  duration: number;
  exit: number;

  /// Pass a row straight from the database to this
  constructor(
    id: string,
    timestamp: number,
    command: string,
    hostuser: string,
    cwd: string,
    duration: number,
    exit: number,
  ) {
    this.id = id;
    this.timestamp = timestamp;
    this.command = command;

    let [host, user] = hostuser.split(":");
    this.user = user;
    this.host = host;

    this.cwd = cwd;
    this.duration = duration;
    this.exit = exit;
  }
}

export interface Alias {
  name: string;
  value: string;
}

export interface Var {
  name: string;
  value: string;
  export: boolean;
}

export interface InspectHistory {
  other: ShellHistory[];
}

export async function inspectCommandHistory(
  h: ShellHistory,
): Promise<InspectHistory> {
  const db = await Database.load(
    "sqlite:/Users/ellie/.local/share/atuin/history.db",
  );

  let other: any[] = await db.select(
    "select * from history where command=?1 order by timestamp desc",
    [h.command],
  );
  console.log(other);

  return {
    other: other.map(
      (i) =>
        new ShellHistory(
          i.id,
          i.timestamp,
          i.command,
          i.hostname,
          i.cwd,
          i.duration,
          i.exit,
        ),
    ),
  };
}

export async function inspectDirectoryHistory(
  h: ShellHistory,
): Promise<InspectHistory> {
  const db = await Database.load(
    "sqlite:/Users/ellie/.local/share/atuin/history.db",
  );

  let other: any[] = await db.select(
    "select * from history where cwd=?1 order by timestamp desc",
    [h.cwd],
  );
  console.log(other);

  return {
    other: other.map(
      (i) =>
        new ShellHistory(
          i.id,
          i.timestamp,
          i.command,
          i.hostname,
          i.cwd,
          i.duration,
          i.exit,
        ),
    ),
  };
}