Skip to content

Commit

Permalink
Polish code infrastructure.
Browse files Browse the repository at this point in the history
Adding pre-commit hooks, reformatting code with `swift-format`,
various cleanup.
  • Loading branch information
rsmmr committed Jan 20, 2024
1 parent d1fa14b commit a7486dd
Show file tree
Hide file tree
Showing 16 changed files with 294 additions and 191 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: CI pipeline

on: push

jobs:
build:
runs-on: macos-14

steps:
- uses: actions/checkout@v3

- name: Build code
run: make release

- name: Run check
run: make check
19 changes: 19 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:

- repo: local
hooks:
- id: swift-format
name: Check formatting of Swift source files
entry: swift-format format -i
language: system
types: ["swift"]
stages: ["commit"]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-added-large-files
8 changes: 8 additions & 0 deletions .swift-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"version": 1,
"indentation": {
"spaces": 4
},
"tabWidth" : 4,
"lineLength" : 120,
}
3 changes: 1 addition & 2 deletions .update-changes.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) 2020-2023 by the Zeek Project. See LICENSE for details.

new_commit_msg="Update CHANGES. [skip ci]"
show_authors=1
show_authors=0
new_version_hook=replace_version_in_info_plist
git_msg=git_msg_with_issue

Expand All @@ -17,4 +17,3 @@ function git_msg_with_issue {
function new_version_hook {
replace_version_in_info_plist $@
}

12 changes: 12 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
0.1-3 | 2024-01-20 22:29:20 +0100

* Polish code infrastructure.

* Implement printing.

We support printing only for certain file types, currently: `.gif`,
`.jpg`, `.md`, `.pdf`, `.png`, `.rtf`, `.txt`. For all other formats,
open them in their corresponding applications first and print from
there.

* Remove unneccessary state.

0.1 | 2024-01-07 15:03:19 +0100

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2023, Robin Sommer
Copyright (c) 2024, Robin Sommer

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Expand Down
14 changes: 9 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@

all: adhoc
all: debug

adhoc:
xcodebuild -quiet -target qlview-adhoc
debug:
@xcodebuild -quiet -target qlview-adhoc -configuration Debug

signed:
xcodebuild -quiet -target qlview-signed
release:
@xcodebuild -quiet -target qlview-signed -configuration Release

check:
codesign --verify --verbose build/Release/qlview
codesign --display --verbose=4 build/Release/qlview 2>&1 | grep Signed
31 changes: 27 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
qlview
======
# qlview

Standalone, Quick Look-based document preview application for macOS.
This is a standalone document previewer based on macOS's built-in
Quick Look—imagine a nicer `qlmanage`. It's particularly useful as a
quick command-line viewer for terminal applications, like
[mutt](http://www.mutt.org).

More to come.
![Screenshot](screenshot.png "Screenshot")

## Installation

You currently need to build it yourself. Assuming you have Xcode
installed, either use it to open the project, or run `make release`
from the command line. You will then find an executable in
`build/Release/qlview`.

## Usage

Run it from the command line:

```
# qlview <file>
```

If you execute `qlview` without any arguments, it'll open an empty
window where you can drag a document into.

## Feedback

Feel free to open issues or pull requests.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1
0.1-3
4 changes: 1 addition & 3 deletions qlview/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>org.rsmmr.qlview</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>qlview</string>
<key>CFBundleVersion</key>
<string>0.1</string>
<string>0.1-3</string>
</dict>
</plist>
44 changes: 27 additions & 17 deletions qlview/about.swift
Original file line number Diff line number Diff line change
@@ -1,41 +1,51 @@
// Copyright (c) 2024 by Robin Sommer. See LICENSE for details.

import SwiftUI

extension Bundle {
var bundleName: String {
return (object(forInfoDictionaryKey: "CFBundleName") as? String) ?? "<unknown>"
}

var bundleVersion: String {
return (object(forInfoDictionaryKey: "CFBundleVersion") as? String) ?? "<unknown>"
}

}

struct AboutView: View {
@Binding var showAbout: Bool;
@Binding var showAbout: Bool

var body: some View {
VStack(spacing: 10) {
//Image(nsImage: NSImage(named: "AppIcon")!)

Text(Bundle.main.bundleName)
.font(.system(size: 20, weight: .bold))
.font(.headline)

Text("Version \(Bundle.main.bundleVersion)")
.textSelection(.enabled)

Text("© 2023 Robin Sommer")
.font(.system(size: 10, weight: .thin))
.multilineTextAlignment(.center)
.font(.subheadline)

Text("© 2024 Robin Sommer")
.font(.system(.footnote, weight: .thin))

Spacer()

Link("GitHub", destination: URL(string: "https://github.com/rsmmr/qlview")! )


HStack {
Link("GitHub", destination: URL(string: "https://github.com/rsmmr/qlview")!)
Link(
"License",
destination: URL(
string:
"https://raw.githubusercontent.com/rsmmr/qlview/main/LICENSE?token=GHSAT0AAAAAACLOI6MX53ZVUFTKWHB4O5CCZNMF5WA"
)!)
}
.font(.system(.body, design: .monospaced))

Spacer()

Button("Close", action: { showAbout = false })
Spacer ()
}
.padding(50)
.frame(minWidth: 200)
.padding(25)
}
}
105 changes: 52 additions & 53 deletions qlview/app.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Copyright (c) 2024 by Robin Sommer. See LICENSE for details.

import SwiftUI

extension FocusedValues {
struct DocumentFocusedValues: FocusedValueKey {
typealias Value = Binding<Document?>
}

var document: Binding<Document?>? {
get {
self[DocumentFocusedValues.self]
Expand All @@ -15,29 +17,30 @@ extension FocusedValues {
}
}


@main
struct qlviewApp: App {
@Environment(\.dismiss) private var dismiss
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@FocusedBinding(\.document) var document_binding

var document : Document? {
var document: Document? {
guard let document = document_binding else { return nil }
return document
}

@Environment(\.dismiss) private var dismiss
@NSApplicationDelegateAdaptor(AppDelegate.self) private var appDelegate
@FocusedBinding(\.document) private var document_binding
@State private var showAbout = false

var body: some Scene {
WindowGroup(for: Document.self) { $doc in
ContentView(doc: doc)
.navigationTitle(doc?.url.lastPathComponent ?? "")
.focusedSceneValue(\.document, $doc)
.onKeyPress(keys: [.escape], action: { _ in
NSApplication.shared.keyWindow?.close()
return KeyPress.Result.handled
})
.onKeyPress(
keys: [.escape],
action: { _ in
NSApplication.shared.keyWindow?.close()
return KeyPress.Result.handled
}
)
.ifLet(doc) {
$0.navigationDocument($1.url)
}
Expand All @@ -50,74 +53,70 @@ struct qlviewApp: App {
CommandGroup(replacing: CommandGroupPlacement.appInfo) {
Button("About qlview") { showAbout = true }
}
CommandGroup(after: .newItem, addition: {

Divider()

Button("Open in App") {
guard let document = document else { return }

document.open()
NSApplication.shared.keyWindow?.close()
}
.keyboardShortcut("o", modifiers: .command)

Button("Move To ...") {
guard let document = document else { return }

if document.move() {
CommandGroup(
after: .newItem,
addition: {
Divider()

Button("Open in App") {
guard let document = document else { return }
document.open()
NSApplication.shared.keyWindow?.close()
}

}
.keyboardShortcut("m", modifiers: .command)

Divider()

Button("Print ...") {
guard let document = document else { return }

document.print()
}
.keyboardShortcut("p", modifiers: .command)
.disabled(document == nil || !document!.canPrint())
})
.keyboardShortcut("o", modifiers: .command)

Button("Move To ...") {
guard let document = document else { return }

if document.move() {
NSApplication.shared.keyWindow?.close()
}

}
.keyboardShortcut("m", modifiers: .command)

Divider()

Button("Print ...") {
guard let document = document else { return }
document.print()
}
.keyboardShortcut("p", modifiers: .command)
.disabled(document == nil || !document!.canPrint())
})
}
}
}

class AppDelegate: NSObject, NSApplicationDelegate {
@Environment(\.openWindow) private var openWindow

func applicationDidFinishLaunching(_ aNotification: Notification) {
NSApplication.shared.setActivationPolicy(.regular)
NSApplication.shared.activate(ignoringOtherApps: true)
// NSApp.setActivationPolicy(.accessory) // No menu bar, no dock

let documents = CommandLine.arguments.suffix(from: 1).enumerated().map({ (idx, url) in
return Document(url: URL(fileURLWithPath: url), id: idx)
})

if !documents.isEmpty {
// Close the intial window.
// TODO: Is there a way to prevent this from opening in the 1st place?
// UPDATE: Apparently not!
// Close the intial window. TODO: Is there a way to prevent this
// from opening in the 1st place?
if let window = NSApplication.shared.windows.first {
window.close()
}
}

for doc in documents {
openWindow(value: doc)
}
}

private var aboutBoxWindowController: NSWindowController?

func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
return false
}
return false
}

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
Expand Down
Loading

0 comments on commit a7486dd

Please sign in to comment.