mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-16 00:36:07 +03:00
Merge pull request #1830 from ghostty-org/xdg-title
xdg-terminal-exec invocations set title based on command
This commit is contained in:
@ -513,7 +513,27 @@ pub fn init(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.title) |title| try rt_surface.setTitle(title);
|
if (config.title) |title| {
|
||||||
|
try rt_surface.setTitle(title);
|
||||||
|
} else if ((comptime builtin.os.tag == .linux) and
|
||||||
|
config.@"_xdg-terminal-exec")
|
||||||
|
xdg: {
|
||||||
|
// For xdg-terminal-exec execution we special-case and set the window
|
||||||
|
// title to the command being executed. This allows window managers
|
||||||
|
// to set custom styling based on the command being executed.
|
||||||
|
const command = config.command orelse break :xdg;
|
||||||
|
if (command.len > 0) {
|
||||||
|
const title = alloc.dupeZ(u8, command) catch |err| {
|
||||||
|
log.warn(
|
||||||
|
"error copying command for title, title will not be set err={}",
|
||||||
|
.{err},
|
||||||
|
);
|
||||||
|
break :xdg;
|
||||||
|
};
|
||||||
|
defer alloc.free(title);
|
||||||
|
try rt_surface.setTitle(title);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Surface) void {
|
pub fn deinit(self: *Surface) void {
|
||||||
|
@ -1108,6 +1108,9 @@ _errors: ErrorList = .{},
|
|||||||
/// as loadTheme which has more details on why.
|
/// as loadTheme which has more details on why.
|
||||||
_replay_steps: std.ArrayListUnmanaged(Replay.Step) = .{},
|
_replay_steps: std.ArrayListUnmanaged(Replay.Step) = .{},
|
||||||
|
|
||||||
|
/// Set to true if Ghostty was executed as xdg-terminal-exec on Linux.
|
||||||
|
@"_xdg-terminal-exec": bool = false,
|
||||||
|
|
||||||
pub fn deinit(self: *Config) void {
|
pub fn deinit(self: *Config) void {
|
||||||
if (self._arena) |arena| arena.deinit();
|
if (self._arena) |arena| arena.deinit();
|
||||||
self.* = undefined;
|
self.* = undefined;
|
||||||
@ -1654,13 +1657,16 @@ pub fn loadCliArgs(self: *Config, alloc_gpa: Allocator) !void {
|
|||||||
// On Linux, we have a special case where if the executing
|
// On Linux, we have a special case where if the executing
|
||||||
// program is "xdg-terminal-exec" then we treat all CLI
|
// program is "xdg-terminal-exec" then we treat all CLI
|
||||||
// args as if they are a command to execute.
|
// args as if they are a command to execute.
|
||||||
if (comptime builtin.os.tag == .linux) xdg: {
|
//
|
||||||
if (!std.mem.eql(
|
// In this mode, we also behave slightly differently:
|
||||||
u8,
|
//
|
||||||
std.fs.path.basename(std.mem.sliceTo(std.os.argv[0], 0)),
|
// - The initial window title is set to the full command. This
|
||||||
"xdg-terminal-exec",
|
// can be used with window managers to modify positioning,
|
||||||
)) break :xdg;
|
// styling, etc. based on the command.
|
||||||
|
//
|
||||||
|
// See: https://github.com/Vladimir-csp/xdg-terminal-exec
|
||||||
|
if (comptime builtin.os.tag == .linux) {
|
||||||
|
if (internal_os.xdg.parseTerminalExec(std.os.argv)) |args| {
|
||||||
const arena_alloc = self._arena.?.allocator();
|
const arena_alloc = self._arena.?.allocator();
|
||||||
|
|
||||||
// First, we add an artificial "-e" so that if we
|
// First, we add an artificial "-e" so that if we
|
||||||
@ -1672,16 +1678,22 @@ pub fn loadCliArgs(self: *Config, alloc_gpa: Allocator) !void {
|
|||||||
// a command to execute.
|
// a command to execute.
|
||||||
var command = std.ArrayList(u8).init(arena_alloc);
|
var command = std.ArrayList(u8).init(arena_alloc);
|
||||||
errdefer command.deinit();
|
errdefer command.deinit();
|
||||||
for (std.os.argv[1..]) |arg_raw| {
|
for (args) |arg_raw| {
|
||||||
const arg = std.mem.sliceTo(arg_raw, 0);
|
const arg = std.mem.sliceTo(arg_raw, 0);
|
||||||
try self._replay_steps.append(arena_alloc, .{ .arg = try arena_alloc.dupe(u8, arg) });
|
try self._replay_steps.append(
|
||||||
|
arena_alloc,
|
||||||
|
.{ .arg = try arena_alloc.dupe(u8, arg) },
|
||||||
|
);
|
||||||
|
|
||||||
try command.appendSlice(arg);
|
try command.appendSlice(arg);
|
||||||
try command.append(' ');
|
try command.append(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.@"_xdg-terminal-exec" = true;
|
||||||
self.command = command.items[0 .. command.items.len - 1];
|
self.command = command.items[0 .. command.items.len - 1];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Parse the config from the CLI args
|
// Parse the config from the CLI args
|
||||||
var iter = try std.process.argsWithAllocator(alloc_gpa);
|
var iter = try std.process.argsWithAllocator(alloc_gpa);
|
||||||
|
@ -78,6 +78,23 @@ pub fn config(alloc: Allocator, opts: Options) ![]u8 {
|
|||||||
return error.NoHomeDir;
|
return error.NoHomeDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses the xdg-terminal-exec specification. This expects argv[0] to
|
||||||
|
/// be "xdg-terminal-exec".
|
||||||
|
pub fn parseTerminalExec(argv: []const [*:0]const u8) ?[]const [*:0]const u8 {
|
||||||
|
if (!std.mem.eql(
|
||||||
|
u8,
|
||||||
|
std.fs.path.basename(std.mem.sliceTo(argv[0], 0)),
|
||||||
|
"xdg-terminal-exec",
|
||||||
|
)) return null;
|
||||||
|
|
||||||
|
// We expect at least one argument
|
||||||
|
if (argv.len < 2) return &.{};
|
||||||
|
|
||||||
|
// If the first argument is "-e" we skip it.
|
||||||
|
const start: usize = if (std.mem.eql(u8, std.mem.sliceTo(argv[1], 0), "-e")) 2 else 1;
|
||||||
|
return argv[start..];
|
||||||
|
}
|
||||||
|
|
||||||
test {
|
test {
|
||||||
const testing = std.testing;
|
const testing = std.testing;
|
||||||
const alloc = testing.allocator;
|
const alloc = testing.allocator;
|
||||||
@ -88,3 +105,28 @@ test {
|
|||||||
try testing.expect(value.len > 0);
|
try testing.expect(value.len > 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test parseTerminalExec {
|
||||||
|
const testing = std.testing;
|
||||||
|
|
||||||
|
{
|
||||||
|
const actual = parseTerminalExec(&.{ "a", "b", "c" });
|
||||||
|
try testing.expect(actual == null);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const actual = parseTerminalExec(&.{"xdg-terminal-exec"}).?;
|
||||||
|
try testing.expectEqualSlices([*:0]const u8, actual, &.{});
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const actual = parseTerminalExec(&.{ "xdg-terminal-exec", "a", "b", "c" }).?;
|
||||||
|
try testing.expectEqualSlices([*:0]const u8, actual, &.{ "a", "b", "c" });
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const actual = parseTerminalExec(&.{ "xdg-terminal-exec", "-e", "a", "b", "c" }).?;
|
||||||
|
try testing.expectEqualSlices([*:0]const u8, actual, &.{ "a", "b", "c" });
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const actual = parseTerminalExec(&.{ "xdg-terminal-exec", "a", "-e", "b", "c" }).?;
|
||||||
|
try testing.expectEqualSlices([*:0]const u8, actual, &.{ "a", "-e", "b", "c" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user