Repository navigation
Expand file tree
/
Copy pathcmdline.cpp
More file actions
128 lines (109 loc) · 4.2 KB
/
Copy pathcmdline.cpp
File metadata and controls
128 lines (109 loc) · 4.2 KB
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
#include "cmdline.h"
#include <iostream>
#include "globals.h"
#include "common/str.h"
#include "common/platform.h"
#include "magic_enum/magic_enum.hpp"
#if PLATFORM_WINDOWS
#include <Windows.h>
#endif
using namespace std;
using namespace grey::common;
cmdline::cmdline(){
#if PLATFORM_WINDOWS
// Attach to the parent process's console or create a new one
if(!::AttachConsole(ATTACH_PARENT_PROCESS)) {
AllocConsole();
}
#endif
// Redirect standard input, output, and error streams to the console
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
}
int cmdline::exec(const std::string& command, const std::string& data) {
// browser-related queries
if(data.starts_with("list|")) return exec_list();
if(data.starts_with("get default|")) return exec_get_default();
if(data.starts_with("set default ")) return exec_set_default(data);
wcout << L"Unknown command: " << str::to_wstr(command) << endl;
return 1;
}
int cmdline::exec_list() {
// list all browsers and profiles
wcout << L"browsers: " << g_state.browsers.size() << endl << endl;
for(const auto& b : g_state.browsers) {
string engine_name{magic_enum::enum_name(b.engine)};
wcout << str::to_wstr(b.name) << endl;
wcout << L" cmd: " << str::to_wstr(b.open_cmd) << endl;
wcout << L" engine: " << str::to_wstr(engine_name) << endl;
wcout << L" hidden: " << (b.is_hidden ? L"yes" : L"no") << endl;
if(!b.icon_path.empty()) {
wcout << L" icon: " << str::to_wstr(b.icon_path) << endl;
}
wcout << L" profiles: " << b.profiles.size() << endl;
for(const auto& p : b.profiles) {
wcout << L" > name: " << str::to_wstr(p.name) << endl;
if(!p.launch_arg.empty()) {
wcout << L" args: " << str::to_wstr(p.launch_arg) << endl;
}
if(!p.user_arg.empty()) {
wcout << L" uargs: " << str::to_wstr(p.user_arg) << endl;
}
wcout << L" hidden: " << (p.is_hidden ? L"yes" : L"no") << endl;
if(!p.icon_path.empty()) {
wcout << L" icon: " << str::to_wstr(p.icon_path) << endl;
}
if(!p.user_icon_path.empty()) {
wcout << L" uicon: " << str::to_wstr(p.user_icon_path) << endl;
}
wcout << L" incognito: " << (p.is_incognito ? L"yes" : L"no") << endl;
}
wcout << endl;
}
return 0;
}
int cmdline::exec_get_default() {
optional<bt::profile_selection> ps = bt::browser::get_default(g_state.browsers);
if(ps) {
wcout << str::to_wstr(ps->b().name) << L"." << str::to_wstr(ps->p().name) << endl;
} else {
wcout << L"no default browser" << endl;
}
return 0;
}
int cmdline::exec_set_default(const std::string& data) {
// Data is in the following form: "set default <browser_name>.<profile_name>|suffix".
// Suffix is optional.
// Extract browser_id and profile_id.
string browser_name;
string profile_name;
string t = data.substr(12); // skip "set default"
size_t pos = t.find('|');
if(pos != string::npos) {
t = t.substr(0, pos);
}
size_t dot = t.find('.');
if(dot != string::npos) {
browser_name = t.substr(0, dot);
profile_name = t.substr(dot + 1);
}
wcout << L"setting default browser to " << str::to_wstr(browser_name) << L"." << str::to_wstr(profile_name) << endl;
// find browser
for(const auto& b : g_state.browsers) {
if(b.name == browser_name) {
wcout << L"found browser: " << str::to_wstr(b.name) << endl;
// find profile
for(const auto& p : b.profiles) {
if(p.name == profile_name) {
wcout << L"found profile: " << str::to_wstr(p.name) << endl;
bt::browser::set_default(g_state.browsers, p);
wcout << L"default profile set and saved." << endl;
return 0;
}
}
}
}
wcout << L"browser or profile not found" << endl;
return 1;
}