Skip to content

Commit

Permalink
Implement window cloning ability (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
jzbor committed Jun 4, 2024
1 parent 149d34a commit 2c18861
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,22 @@ kermit [-h] [-v] [-d] [-c config] [-t title] [-w workdir] [-e command]

## Default Key Bindings

| Key | Action |
|----------------------------------------|-----------------------------------|
| `ctrl` + `alt` + `c` | Copy to clipboard |
| `ctrl` + `alt` + `v` | Paste from clipboard |
| `ctrl` + `alt` + `t`/`return` | Open a new tab |
| `ctrl` + `alt` + `r` | Reload configuration file |
| `ctrl` + `alt` + `d` | Load default configuration |
| `ctrl` + `alt` + `q` | Exit the terminal |
| `ctrl` + `alt` + `k`/`up` | Increase font size |
| `ctrl` + `alt` + `j`/`down` | Decrease font size |
| `ctrl` + `alt` + `equals`/`plus` | Reset font size to default |
| `ctrl` + `alt` + `<num>` | Switch to tab with number `<num>` |
| `ctrl` + `alt` + `l`/`right`/`pageup` | Switch to next tab |
| `ctrl` + `alt` + `h`/`left`/`pagedown` | Switch to previous tab |
| `ctrl` + `alt` + `backspace` | Close the current tab |
| Key | Action |
|----------------------------------------|------------------------------------|
| `ctrl` + `alt` + `c` | Copy to clipboard |
| `ctrl` + `alt` + `v` | Paste from clipboard |
| `ctrl` + `alt` + `t`/`return` | Open a new tab |
| `ctrl` + `alt` + `n` | Open a new window |
| `ctrl` + `alt` + `r` | Reload configuration file |
| `ctrl` + `alt` + `d` | Load default configuration |
| `ctrl` + `alt` + `q` | Exit the terminal |
| `ctrl` + `alt` + `k`/`up` | Increase font size |
| `ctrl` + `alt` + `j`/`down` | Decrease font size |
| `ctrl` + `alt` + `equals`/`plus` | Reset font size to default |
| `ctrl` + `alt` + `<num>` | Switch to tab with number `<num>` |
| `ctrl` + `alt` + `l`/`right`/`pageup` | Switch to next tab |
| `ctrl` + `alt` + `h`/`left`/`pagedown` | Switch to previous tab |
| `ctrl` + `alt` + `backspace` | Close the current tab |

The default modifiers (`ctrl` + `alt`) can be set to `ctrl` + `shift` using the config file.
Key bindings can be overridden by custom key bindings.
Expand Down
31 changes: 30 additions & 1 deletion src/kermit.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ static gboolean debugMessages = FALSE; /* Boolean value for -d argument */
static gboolean closeTab = FALSE; /* Close the tab on child-exited signal */
static va_list vargs; /* Hold information about variable arguments */
static GdkRGBA termPalette[TERM_PALETTE_SIZE]; /* Terminal colors */
typedef struct { /* Key bindings struct */
static char **args; /* Save args the terminal was launched with*/
typedef struct KeyBindings { /* Key bindings struct */
gboolean internal;
char *key;
char *cmd;
Expand All @@ -86,6 +87,7 @@ static DefaultBindings defaultKeyBindings[] = { /* Preconfigured default key
{ .bind = { .key = "c", .cmd = "copy", .internal = TRUE } },
{ .bind = { .key = "v", .cmd = "paste", .internal = TRUE } },
{ .bind = { .key = "t", .cmd = "new-tab", .internal = TRUE } },
{ .bind = { .key = "n", .cmd = "new-window", .internal = TRUE } },
{ .bind = { .key = "return", .cmd = "new-tab", .internal = TRUE } },
{ .bind = { .key = "r", .cmd = "reload-config", .internal = TRUE } },
{ .bind = { .key = "d", .cmd = "default-config", .internal = TRUE } },
Expand Down Expand Up @@ -144,6 +146,30 @@ static int connectSignals(GtkWidget *terminal) {
return 0;
}

/*!
* Opens a new window with the same working directory
*
* \param terminal
*/
static void termClone(VteTerminal *terminal) {
const char prefix[] = "file://";
const char *path = vte_terminal_get_current_directory_uri(terminal);
if (path && strncmp(prefix, path, strlen(prefix)) == 0) {
path += strlen(prefix);
}
if (fork() == 0) {
if (!path) {
fprintf(stderr, "Unable to fetch current working directory\n");
} else if (chdir(path) == -1) {
perror("Unable to change pwd for new terminal");
}
if (execvp(args[0], args) == -1) {
perror("Cloning the terminal failed");
_exit(errno);
}
}
}

/*
* Handle internal action
*
Expand All @@ -170,6 +196,8 @@ static gboolean termAction(GtkWidget *terminal, const char *action) {
} else if (strcmp(action, "new-tab") == 0) {
gtk_notebook_append_page(GTK_NOTEBOOK(notebook), getTerm(), NULL);
gtk_widget_show_all(window);
} else if (strcmp(action, "new-window") == 0) {
termClone(VTE_TERMINAL(terminal));
} else if (strcmp(action, "exit") == 0) {
gtk_main_quit();
} else if (strcmp(action, "inc-font-size") == 0) {
Expand Down Expand Up @@ -753,6 +781,7 @@ static void parseSettings() {
* \return 1 on exit
*/
static int parseArgs(int argc, char **argv) {
args = argv;
while ((opt = getopt(argc, argv, ":c:w:e:t:vdh")) != -1) {
switch (opt) {
case 'c':
Expand Down

0 comments on commit 2c18861

Please sign in to comment.