mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-08-29 02:21:43 +00:00
macos: add string extension
This commit is contained in:
18
macos/Sources/Helpers/String+Extension.swift
Normal file
18
macos/Sources/Helpers/String+Extension.swift
Normal file
@@ -0,0 +1,18 @@
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user