feat: print complete default configuration from CLI

This commit is contained in:
2026-10-11 17:03:51 +02:00
parent 41e4a458cf
commit 35181634d6
6 changed files with 36 additions and 1 deletions
+4
View File
@@ -2,6 +2,7 @@ import Foundation
enum PlayerCommand {
case help
case printDefaultConfig
case launch(URL?, newInstance: Bool, noConfig: Bool)
static let usage = """
@@ -9,6 +10,7 @@ enum PlayerCommand {
terebi [--new-instance] [--no-config] --url YOUTUBE_URL
terebi [--new-instance] [--no-config] -- FILE
terebi --help
terebi --print-default-config
Open one local media file or YouTube video and start playback, resuming saved
video progress when available. With no arguments, open or activate the player.
@@ -19,6 +21,7 @@ enum PlayerCommand {
--url URL Open a YouTube video URL.
-- Treat the following argument as a filename, even with a leading '-'.
-h, --help Show this help without launching the player.
--print-default-config Print the complete built-in TOML without launching.
Reuses the running player unless --new-instance is supplied before the media
arguments. The command exits once macOS accepts the launch; playback and
@@ -41,6 +44,7 @@ enum PlayerCommand {
}
if inputs.isEmpty { return .launch(nil, newInstance: newInstance, noConfig: noConfig) }
if inputs == ["--help"] || inputs == ["-h"] { return .help }
if inputs == ["--print-default-config"] { return .printDefaultConfig }
if inputs.first == "--url" {
guard inputs.count == 2 else { throw ArgumentError("--url requires exactly one YouTube URL.") }
return .launch(try YouTubeResolver.validateURL(inputs[1]), newInstance: newInstance, noConfig: noConfig)
+2
View File
@@ -15,6 +15,8 @@ enum TerebiCLI {
switch command {
case .help:
print(PlayerCommand.usage)
case .printDefaultConfig:
print(PlayerConfiguration.defaultTOML, terminator: "")
case .launch(let url, let newInstance, let noConfig):
let configURL = PlayerConfiguration.fileURL(environment: ProcessInfo.processInfo.environment,
homeDirectory: FileManager.default.homeDirectoryForCurrentUser)
@@ -15,6 +15,26 @@ public struct PlayerConfiguration: Equatable, Sendable {
self.keys = keys
}
public static var defaultTOML: String {
let defaults = Self()
let encoder = JSONEncoder()
encoder.outputFormatting = .withoutEscapingSlashes
var lines = [
"automatic_quality = \(defaults.automaticQuality)",
"", "[ui]",
"buffer_info = \(defaults.showBufferInfo)",
"compact_snap_to_edges = \(defaults.snapToEdges)",
"", "[keys]"
]
for (name, action) in PlayerAction.namedActions {
let chords = defaults.keys.shortcuts(for: action).map(\.description)
// JSON string arrays are valid TOML arrays; preserve escaping for printable keys.
let value = String(decoding: try! encoder.encode(chords), as: UTF8.self)
lines.append("\(name) = \(value)")
}
return lines.joined(separator: "\n") + "\n"
}
public static func fileURL(environment: [String: String], homeDirectory: URL) -> URL {
let directory: URL
if let path = environment["XDG_CONFIG_HOME"], path.hasPrefix("/") {