From 7d68ac3619eacb5ef93ec81d9c55835d5aeb5a4c Mon Sep 17 00:00:00 2001 From: George Harker Date: Tue, 14 Nov 2023 15:43:09 -0800 Subject: [PATCH] tab titles are relative to cwd if prefixes match --- Tabs/Sources/Tabs/Tab.swift | 29 ++++++++++++++++++++++++++--- Tabs/Sources/Tabs/TabBar.swift | 5 +++++ VimR/VimR/MainWindow.swift | 1 + 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Tabs/Sources/Tabs/Tab.swift b/Tabs/Sources/Tabs/Tab.swift index 4c140154..14995496 100644 --- a/Tabs/Sources/Tabs/Tab.swift +++ b/Tabs/Sources/Tabs/Tab.swift @@ -22,12 +22,31 @@ final class Tab: NSView { self.adjustToSelectionChange(newValue.isSelected) } didSet { - if self.titleView.stringValue == self.title { return } - self.titleView.stringValue = self.title - self.adjustWidth() + updateTitle(self.title) } } + private func updateTitle(_ title: String) { + let display_title = displayTitle(title) + if self.titleView.stringValue == display_title { return } + self.titleView.stringValue = display_title + self.adjustWidth() + } + + private func displayTitle(_ title: String) -> String { + var display_title = title + if self.tabBar?.cwd != nil { + let cwd = ((self.tabBar?.cwd!)! as NSString).abbreviatingWithTildeInPath + let pref = title.commonPrefix(with: cwd) + if pref == cwd { + let cwd_component_count = (cwd as NSString).pathComponents.count + display_title = NSString.path( + withComponents: Array((title as NSString).pathComponents[cwd_component_count...])) + } + } + return display_title + } + init(withTabRepresentative tabRepresentative: Rep, in tabBar: TabBar) { self.tabBar = tabBar self.tabRepresentative = tabRepresentative @@ -50,6 +69,10 @@ final class Tab: NSView { self.adjustColors(self.isSelected) } + func updateContext() { + updateTitle(self.title) + } + override func mouseUp(with _: NSEvent) { self.tabBar?.select(tab: self) } override func draw(_: NSRect) { diff --git a/Tabs/Sources/Tabs/TabBar.swift b/Tabs/Sources/Tabs/TabBar.swift index b15ea798..0fcb044e 100644 --- a/Tabs/Sources/Tabs/TabBar.swift +++ b/Tabs/Sources/Tabs/TabBar.swift @@ -15,6 +15,11 @@ public final class TabBar: NSView { public typealias TabCallback = (Int, Rep, [Rep]) -> Void public var theme: Theme { self._theme } + public var cwd: String? { + didSet { + self.tabs.forEach { $0.updateContext() } + } + } public var closeHandler: TabCallback? public var selectHandler: TabCallback? diff --git a/VimR/VimR/MainWindow.swift b/VimR/VimR/MainWindow.swift index 6b417eb3..d6268675 100644 --- a/VimR/VimR/MainWindow.swift +++ b/VimR/VimR/MainWindow.swift @@ -344,6 +344,7 @@ final class MainWindow: NSObject, if let cwd = state.cwdToSet { self.neoVimView.cwd = cwd + self.neoVimView.tabBar?.cwd = cwd.path } Completable