Files
ghostty/macos/Sources/Helpers/Extensions/String+Extension.swift
Mitchell Hashimoto ef669eeae7 macos: add AppleScript split command
Add a new `split` command to the AppleScript scripting dictionary that
splits a terminal in a given direction (right, left, down, up) and
returns the newly created terminal.

The command is exposed as:
  split terminal <terminal> direction <direction>

Also adds a `fourCharCode` String extension for converting four-character
ASCII strings to their FourCharCode (UInt32) representation.
2026-03-05 20:54:34 -08:00

40 lines
1.3 KiB
Swift

extension String {
func truncate(length: Int, trailing: String = "") -> String {
let maxLength = length - trailing.count
guard maxLength > 0, !self.isEmpty, self.count > length else {
return self
}
return self.prefix(maxLength) + trailing
}
#if canImport(AppKit)
func temporaryFile(_ filename: String = "temp") -> URL {
let url = FileManager.default.temporaryDirectory
.appendingPathComponent(filename)
.appendingPathExtension("txt")
let string = self
try? string.write(to: url, atomically: true, encoding: .utf8)
return url
}
/// Returns the path with the home directory abbreviated as ~.
var abbreviatedPath: String {
let home = FileManager.default.homeDirectoryForCurrentUser.path
if hasPrefix(home) {
return "~" + dropFirst(home.count)
}
return self
}
#endif
/// Converts a four-character ASCII string to its `FourCharCode` (`UInt32`) value.
var fourCharCode: UInt32 {
assert(count <= 4, "FourCharCode string must be at most 4 characters")
var result: UInt32 = 0
for byte in utf8.prefix(4) {
result = (result << 8) | UInt32(byte)
}
return result
}
}