A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.
Rust archive-managerdesktop-appdual-paneexplorer-alternativefile-browserfile-explorerfile-managerfluent-designgpu-acceleratedgpuimacosmicaproductivityquick-lookrustrust-guitabswindowswindows-11windows-explorer
import AppKit
/// Your own command bar buttons (Windows' quick_commands.rs, toolbar_commands): each runs a
/// program or opens an app, with {folder} and {selection} in its arguments standing for the
/// folder shown and the selected items. One marked "in the menu" is in the right-click menu too.
struct QuickCommand: Codable, Equatable {
var label: String
/// An app (/Applications/X.app), a program's path, or a command found on the PATH.
var program: String
var arguments: String
var inMenu: Bool
var name: String {
if !label.trimmingCharacters(in: .whitespaces).isEmpty { return label }
return ((program as NSString).lastPathComponent as NSString).deletingPathExtension
}
}
@MainActor
enum QuickCommands {
private static let key = "toolbarCommands"
static var all: [QuickCommand] {
get { UserDefaults.standard.data(forKey: key).flatMap { try? JSONDecoder().decode([QuickCommand].self, from: $0) } ?? [] }
set {
if let data = try? JSONEncoder().encode(newValue) { UserDefaults.standard.set(data, forKey: key) }
NotificationCenter.default.post(name: .rfSettingsChanged, object: nil, userInfo: ["key": key])
}
}
/// The words of an argument line, "quoted parts" kept together.
nonisolated static func split(_ line: String) -> [String] {
var out: [String] = [], cur = "", quoted = false, any = false
for c in line {
if c == "\"" { quoted.toggle(); any = true; continue }
if c.isWhitespace && !quoted {
if any || !cur.isEmpty { out.append(cur) }
cur = ""; any = false
continue
}
cur.append(c)
}
if any || !cur.isEmpty { out.append(cur) }
return out
}
/// The arguments with {folder} and {selection} (%sel%) filled in; nil and a reason when one is missing.
nonisolated static func arguments(_ line: String, folder: URL?, selection: [URL]) -> Result<[String], QuickCommandError> {
var out: [String] = []
for w in split(line) {
switch w {
case "{folder}":
guard let folder else { return .failure(.noFolder) }
out.append(folder.path)
case "{selection}", "%sel%":
guard !selection.isEmpty else { return .failure(.noSelection) }
out += selection.map(\.path)
default:
out.append(w)
}
}
return .success(out)
}
static func run(_ c: QuickCommand, folder: URL?, selection: [URL], onError: @escaping (String) -> Void) {
let args: [String]
switch arguments(c.arguments, folder: folder, selection: selection) {
case .success(let a): args = a
case .failure(let e): return onError(e.text)
}
let program = (c.program.trimmingCharacters(in: .whitespaces) as NSString).expandingTildeInPath
if program.hasSuffix(".app") {
let app = URL(fileURLWithPath: program)
let config = NSWorkspace.OpenConfiguration()
// an app given only files opens them; anything else goes to it as arguments
let files = args.map { URL(fileURLWithPath: $0) }
if !args.isEmpty, args.allSatisfy({ FileManager.default.fileExists(atPath: $0) }) {
NSWorkspace.shared.open(files, withApplicationAt: app, configuration: config) { _, e in
if let e { DispatchQueue.main.async { onError(e.localizedDescription) } }
}
} else {
config.arguments = args
NSWorkspace.shared.openApplication(at: app, configuration: config) { _, e in
if let e { DispatchQueue.main.async { onError(e.localizedDescription) } }
}
}
return
}
let p = Process()
if program.contains("/") {
p.executableURL = URL(fileURLWithPath: program)
p.arguments = args
} else {
// a name: found on the PATH, as a shell would
p.executableURL = URL(fileURLWithPath: "/usr/bin/env")
p.arguments = [program] + args
}
if let folder { p.currentDirectoryURL = folder }
p.standardOutput = FileHandle.nullDevice
p.standardError = FileHandle.nullDevice
do { try p.run() } catch { onError(error.localizedDescription) }
}
/// Adds a command, or changes command `index`.
static func edit(_ index: Int?) {
let current = index.flatMap { all.indices.contains($0) ? all[$0] : nil }
let alert = NSAlert()
alert.messageText = current == nil ? String(localized: "New command") : String(localized: "Edit command")
alert.informativeText = String(localized: "In the arguments, {folder} is the folder shown and {selection} the selected items.")
func field(_ value: String, _ placeholder: String) -> NSTextField {
let f = NSTextField(frame: NSRect(x: 0, y: 0, width: 320, height: 24))
f.stringValue = value
f.placeholderString = placeholder
return f
}
let label = field(current?.label ?? "", String(localized: "Button label"))
let program = field(current?.program ?? "", "/Applications/Visual Studio Code.app")
let args = field(current?.arguments ?? "", "{selection}")
let choose = ClosureButton(title: String(localized: "Choose…"), action: {
let panel = NSOpenPanel()
panel.directoryURL = URL(fileURLWithPath: "/Applications")
panel.treatsFilePackagesAsDirectories = false
if panel.runModal() == .OK, let u = panel.url { program.stringValue = u.path }
})
let menu = NSButton(checkboxWithTitle: String(localized: "In the right-click menu too"), target: nil, action: nil)
menu.state = current?.inMenu == true ? .on : .off
let stack = NSStackView(views: [label, NSStackView(views: [program, choose]), args, menu])
stack.orientation = .vertical
stack.alignment = .leading
stack.spacing = 8
stack.frame = NSRect(x: 0, y: 0, width: 400, height: 130)
alert.accessoryView = stack
alert.addButton(withTitle: String(localized: "Save"))
alert.addButton(withTitle: String(localized: "Cancel"))
alert.window.initialFirstResponder = label
guard alert.runModal() == .alertFirstButtonReturn else { return }
let p = program.stringValue.trimmingCharacters(in: .whitespaces)
guard !p.isEmpty else { return }
let c = QuickCommand(label: label.stringValue, program: p, arguments: args.stringValue, inMenu: menu.state == .on)
var list = all
if let index, list.indices.contains(index) { list[index] = c } else { list.append(c) }
all = list
}
}
enum QuickCommandError: Error {
case noFolder, noSelection
var text: String {
switch self {
case .noFolder: String(localized: "Open a folder first.")
case .noSelection: String(localized: "Select files first.")
}
}
}