Skip to content

Commit

Permalink
command line support for "bt browser"
Browse files Browse the repository at this point in the history
  • Loading branch information
aloneguid committed Feb 20, 2025
1 parent d690924 commit 416797f
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: 'Build'

env:
VERSION: 4.2.5
VERSION: 4.3.0
BUILD_TYPE: Release
ARCH: x64
VCPKG_CONFIG: Release
Expand Down
1 change: 1 addition & 0 deletions bt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ find_package(tinyxml2 CONFIG REQUIRED)
find_package(Lua REQUIRED)

file(GLOB core_src CONFIGURE_DEPENDS
"cmdline.cpp"
"app/*.cpp"
"app/ui/*.cpp"
"app/pipeline/*.cpp"
Expand Down
12 changes: 6 additions & 6 deletions bt/bt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "app/rule_hit_log.h"
#include "app/app_log.h"
#include "app/url_opener.h"
#include "cmdline.h"
//#include "app/systray.h"

//ui
Expand Down Expand Up @@ -120,15 +121,14 @@ void execute(const string& data) {
string command_data;
string command = get_command(data, command_data);
if(!command.empty()) {
/*if(command == "tray") {
// launch "systray" app
bt::systray systray;
systray.run();
return;
} else */if(command == "pick") {
if(command == "pick") {
// force-invoke the picker
force_picker = true;
clean_data = command_data;
} else if(command == "browser") {
cmdline c;
c.exec(command, command_data);
return;
}
}

Expand Down
133 changes: 133 additions & 0 deletions bt/cmdline.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include "cmdline.h"
#include <Windows.h>
#include <iostream>
#include "globals.h"
#include "str.h"

using namespace std;

cmdline::cmdline(){
// Attach to the parent process's console or create a new one
if(!::AttachConsole(ATTACH_PARENT_PROCESS)) {
AllocConsole();
}

// 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_config.browsers.size() << endl << endl;

for(const auto& b : g_config.browsers) {
wcout << str::to_wstr(b->id) << endl;
wcout << L" name: " << str::to_wstr(b->name) << endl;
wcout << L" cmd: " << str::to_wstr(b->open_cmd) << endl;
wcout << L" system: " << (b->is_system ? L"yes" : L"no") << endl;
wcout << L" hidden: " << (b->is_hidden ? L"yes" : L"no") << endl;
if(b->is_system || b->is_chromium || b->is_firefox) {
wcout << L" features: ";
if(b->is_system) {
wcout << L"system ";
}
if(b->is_chromium) {
wcout << L"chromium ";
}
if(b->is_firefox) {
wcout << L"firefox ";
}
wcout << endl;
}
if(!b->icon_path.empty()) {
wcout << L" icon: " << str::to_wstr(b->icon_path) << endl;
}

wcout << L" profiles: " << b->instances.size() << endl;
for(const auto& p : b->instances) {
wcout << L" > id: " << str::to_wstr(p->id) << endl;
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() {

auto def = bt::browser::get_default(g_config.browsers, g_config.default_profile_long_id);

wcout << str::to_wstr(def->b->id) << L"." << str::to_wstr(def->id) << endl;

return 0;
}

int cmdline::exec_set_default(const std::string& data) {
// data is in the following form: "set default <browser_id>.<profile_id>|suffix"
// suffix is optional
// extract browser_id and profile_id
string browser_id;
string profile_id;

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_id = t.substr(0, dot);
profile_id = t.substr(dot + 1);
}

wcout << L"setting default browser to " << str::to_wstr(browser_id) << L"." << str::to_wstr(profile_id) << endl;

// find browser
for(const auto& b : g_config.browsers) {
if(b->id == browser_id) {
wcout << L"found browser: " << str::to_wstr(b->name) << endl;
// find profile
for(const auto& p : b->instances) {
if(p->id == profile_id) {
wcout << L"found profile: " << str::to_wstr(p->name) << endl;
g_config.default_profile_long_id = p->long_id();
g_config.commit();
wcout << L"default profile set and saved." << endl;
return 0;
}
}
}
}

wcout << L"browser or profile not found" << endl;
return 1;
}
14 changes: 14 additions & 0 deletions bt/cmdline.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include <string>

class cmdline {
public:
cmdline();

int exec(const std::string& command, const std::string& data);

private:
int exec_list();
int exec_get_default();
int exec_set_default(const std::string& data);
};
4 changes: 4 additions & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.3.0

This version enhances command line support, specifically allowing you to use terminal to list browsers and profiles comfigured in Browser Tamer, get default browser and set default browser. This is useful for scripting and automation. Read more in the documentation.

## 4.2.5

### Bugs fixed
Expand Down
93 changes: 93 additions & 0 deletions wrs/topics/commandline.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,96 @@ If you want to open the picker for any URL, you can use the `pick` command.
```shell
bt.exe pick https://www.google.com
```

## Terminal commands

You can also use terminal commands to control %product%, this does not pop up any UI.

### List browsers and profiles

```shell
bt browser list
```

Returns a long list of all currently configured browsers and profiles.

```
browsers: 3
msedge
name: Microsoft Edge
cmd: C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe
system: yes
hidden: no
features: system chromium
profiles: 3
> id: Default
name: Personal
args: "url" "--profile-directory=Default" --no-default-browser-check
hidden: no
icon: C:\Users\alone\AppData\Local\Microsoft\Edge\User Data\Default\Edge Profile Picture.png
incognito: no
> id: Profile 1
name: Work
args: "url" "--profile-directory=Profile 1" --no-default-browser-check
hidden: no
icon: C:\Users\alone\AppData\Local\Microsoft\Edge\User Data\Profile 1\Edge Profile Picture.png
incognito: no
> id: InPrivate
name: InPrivate
args: "url" --inprivate
hidden: no
incognito: yes
firefox
name: Mozilla Firefox
cmd: C:\Program Files\Mozilla Firefox\firefox.exe
system: yes
hidden: no
features: system firefox
profiles: 4
> id: Profile0
name: No container
args: "url" -P "default-release"
hidden: no
incognito: no
> id: Profile0+c_2
name: Isoline
args: "ext+bt:container=Isoline&url=url" -P "default-release"
hidden: no
incognito: no
> id: private
name: Private
args: -private-window "url"
hidden: no
icon: C:\Program Files\Mozilla Firefox\firefox.exe
incognito: yes
aadb3a96-6f1b-444b-955d-ac5f7d451f7f
name: Tor Browser
cmd: C:\software\tor\Browser\firefox.exe
system: no
hidden: no
profiles: 1
> id: default
name: Tor Browser
hidden: no
incognito: no
```
{collapsible="true" collapsed-title="Example output"}

### Get default browser

```shell
bt browser get default
```

Will print the default browser in format `<browser_id>.<profile_id>`. You can view these ids using `bt browser list` commands.

### Set default browser

```shell
bt browser set default <browser_id>.<profile_id>
```

Will set the default browser to the specified browser and profile. You need to specify correct browser and profile ids, which again can be retreived from `bt browser list` command.

0 comments on commit 416797f

Please sign in to comment.