aboutsummaryrefslogtreecommitdiffstats
path: root/ui/src/state/models.ts
blob: c1d97f4bbbc620c8c9a022abd26061939618d963 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import Database from "@tauri-apps/plugin-sql";

export class User {
  username: string | null;

  constructor(username: string) {
    this.username = username;
  }

  isLoggedIn(): boolean {
    return this.username !== "" && this.username !== null;
  }
}

export const DefaultUser: User = new User("");

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

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[];
}

// Not yet complete. Not all types are defined here.
// Gonna hold off until the settings refactoring.
export interface Settings {
  auto_sync: boolean;
  update_check: boolean;
  sync_address: string;
  sync_frequency: string;
  db_path: string;
  record_store_path: string;
  key_path: string;
  session_path: string;
  shell_up_key_binding: boolean;
  inline_height: number;
  invert: boolean;
  show_preview: boolean;
  max_preview_height: number;
  show_help: boolean;
  show_tabs: boolean;
  word_chars: string;
  scroll_context_lines: number;
  history_format: string;
  prefers_reduced_motion: boolean;
  store_failed: boolean;
  secrets_filter: boolean;
  workspaces: boolean;
  ctrl_n_shortcuts: boolean;
  network_connect_timeout: number;
  network_timeout: number;
  local_timeout: number;
  enter_accept: boolean;
  smart_sort: boolean;
  sync: Sync;
}

interface Sync {
  records: boolean;
}

// Define other interfaces (Dialect, Timezone, Style, SearchMode, FilterMode, ExitMode, KeymapMode, CursorStyle, WordJumpMode, RegexSet, Stats) accordingly.

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,
        ),
    ),
  };
}