Skip to content

Commit

Permalink
mvp
Browse files Browse the repository at this point in the history
  • Loading branch information
oil3 committed Oct 15, 2024
1 parent 1cf519e commit 7b1a851
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 107 deletions.
22 changes: 14 additions & 8 deletions CodeColors.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -413,13 +413,15 @@
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_CFBundleDisplayName = "Code Colors";
INFOPLIST_KEY_NSHumanReadableCopyright = "";
INFOPLIST_KEY_CFBundleDisplayName = "Code Colors ";
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.developer-tools";
INFOPLIST_KEY_NSHumanReadableCopyright = "Quet Almahdi Morris";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.0;
MACOSX_DEPLOYMENT_TARGET = 12.2;
MARKETING_VERSION = oct2024;
PRODUCT_BUNDLE_IDENTIFIER = com.oil3.CodeColors;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand All @@ -442,13 +444,15 @@
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_CFBundleDisplayName = "Code Colors";
INFOPLIST_KEY_NSHumanReadableCopyright = "";
INFOPLIST_KEY_CFBundleDisplayName = "Code Colors ";
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.developer-tools";
INFOPLIST_KEY_NSHumanReadableCopyright = "Quet Almahdi Morris";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.0;
MACOSX_DEPLOYMENT_TARGET = 12.2;
MARKETING_VERSION = oct2024;
PRODUCT_BUNDLE_IDENTIFIER = com.oil3.CodeColors;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand All @@ -468,12 +472,13 @@
INFOPLIST_FILE = QuickCodeColorLook/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = QuickCodeColorLook;
INFOPLIST_KEY_LSApplicationCategoryType = "";
INFOPLIST_KEY_NSHumanReadableCopyright = "";
INFOPLIST_KEY_NSHumanReadableCopyright = "Quet Almahdi Morris";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
"@executable_path/../../../../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 12.2;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.oil3.CodeColors.QuickCodeColorLook;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -495,12 +500,13 @@
INFOPLIST_FILE = QuickCodeColorLook/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = QuickCodeColorLook;
INFOPLIST_KEY_LSApplicationCategoryType = "";
INFOPLIST_KEY_NSHumanReadableCopyright = "";
INFOPLIST_KEY_NSHumanReadableCopyright = "Quet Almahdi Morris";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
"@executable_path/../../../../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 12.2;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.oil3.CodeColors.QuickCodeColorLook;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,4 @@
uuid = "60919A6F-13ED-4CF9-8607-2F6AA9EC1D1D"
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "3C42B69D-4F10-4EFA-962E-AE6A10F5F046"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "QuickCodeColorLook/PreviewProvider.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "6"
endingLineNumber = "6"
landmarkName = "PreviewProvider"
landmarkType = "3">
<Locations>
<Location
uuid = "3C42B69D-4F10-4EFA-962E-AE6A10F5F046 - 3c2fcbce1918509b"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "QuickCodeColorLook.PreviewProvider.__allocating_init() -&gt; QuickCodeColorLook.PreviewProvider"
moduleName = "QuickCodeColorLook"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/ssz/Documents/CodeColors/QuickCodeColorLook/PreviewProvider.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "0"
endingLineNumber = "0">
</Location>
<Location
uuid = "3C42B69D-4F10-4EFA-962E-AE6A10F5F046 - 61e72b3e7660e087"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "QuickCodeColorLook.PreviewProvider.init() -&gt; QuickCodeColorLook.PreviewProvider"
moduleName = "QuickCodeColorLook"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/ssz/Documents/CodeColors/%3Ccompiler-generated%3E"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "0"
endingLineNumber = "0">
</Location>
<Location
uuid = "3C42B69D-4F10-4EFA-962E-AE6A10F5F046 - 41f0be72b60e8e96"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "QuickCodeColorLook.PreviewProvider.__deallocating_deinit"
moduleName = "QuickCodeColorLook"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/ssz/Documents/CodeColors/QuickCodeColorLook/PreviewProvider.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "0"
endingLineNumber = "0">
</Location>
</Locations>
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
2 changes: 1 addition & 1 deletion CodeColors/CodeColors.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<true/>
<key>com.apple.security.application-groups</key>
<array>
<string>$(TeamIdentifierPrefix)</string>
<string>com.oil3.codecolors</string>
</array>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
Expand Down
5 changes: 5 additions & 0 deletions CodeColors/CodeColorsApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ struct CodeColorsApp: App {
}
}
}


//
// Copyright Almahdi Morris Quet 2024
//
2 changes: 2 additions & 0 deletions CodeColors/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ struct SettingsView: View {
.frame(width: 400)
}
}


7 changes: 6 additions & 1 deletion CodeColors/SyntaxSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Combine

class SyntaxSettings: ObservableObject {
static let shared = SyntaxSettings()
private let userDefaults = UserDefaults(suiteName: "com.oil3.codecolors")
let userDefaults = UserDefaults(suiteName: "com.oil3.codecolors")

@Published var syntaxColors: [String: Color] = [:]

Expand Down Expand Up @@ -89,3 +89,8 @@ extension Color {
#endif
}
}


//
// Copyright Almahdi Morris Quet 2024
//
31 changes: 25 additions & 6 deletions QuickCodeColorLook/CodeContentLoader.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
// CodeContentLoader.swift

import SwiftUI
import Combine

class CodeContentLoader: ObservableObject {
@Published var attributedLines: [AttributedString] = []
@Published var attributedContent: AttributedString = AttributedString()
@Published var isLoading = false
@Published var totalLines = 0
@Published var fileSize: Int64 = 0

private var shouldCancel = false

func loadFile(at url: URL, maxFileSize: Int64 = 5 * 1024 * 1024) {
func loadFile(at url: URL, maxFileSize: Int64 = 50 * 1024 * 1024) {
self.isLoading = true
self.shouldCancel = false
self.attributedLines = []
self.attributedContent = AttributedString()

// Check file size
if let fileSize = try? url.resourceValues(forKeys: [.fileSizeKey]).fileSize {
Expand All @@ -39,16 +41,30 @@ class CodeContentLoader: ObservableObject {
reader.close()
}

var lineNumber = 1

while let line = reader.nextLine() {
if self.shouldCancel {
break
}

let attributedLine = SyntaxHighlighter.highlightLine(line: line, fileExtension: fileExtension)
// Highlight the line
var attributedLine = SyntaxHighlighter.highlightLine(line: line, fileExtension: fileExtension)

// Prepend line number
// var lineNumberString = AttributedString("\(lineNumber) ")
// lineNumberString.foregroundColor = .gray

lineNumber += 1

// Combine line number and content
// lineNumberString.append(attributedLine)
// Append newline character
attributedLine.append(AttributedString("\n"))

DispatchQueue.main.async {
self.attributedLines.append(attributedLine)
self.totalLines = self.attributedLines.count
self.attributedContent.append(attributedLine)
self.totalLines = lineNumber - 1
}
}

Expand All @@ -62,3 +78,6 @@ class CodeContentLoader: ObservableObject {
shouldCancel = true
}
}
//
// Copyright Almahdi Morris Quet 2024
//
1 change: 1 addition & 0 deletions QuickCodeColorLook/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<array>
<string>public.python-script</string>
<string>public.yaml</string>
<string>public.data</string>
<string>public.xml</string>
<string>public.json</string>
<string>public.swift-source</string>
Expand Down
3 changes: 3 additions & 0 deletions QuickCodeColorLook/LineReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ class LineReader {
close()
}
}
//
// Copyright Almahdi Morris Quet 2024
//
35 changes: 12 additions & 23 deletions QuickCodeColorLook/PreviewProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,24 @@ class PreviewProvider: QLPreviewProvider {
func providePreview(for request: QLFilePreviewRequest, handler: @escaping (QLPreviewReply?, Error?) -> Void) {
}
}
struct ContentView: View {
@ObservedObject var loader: CodeContentLoader
@State private var fontSize: CGFloat = 12.0
// ContentView.swift

var body: some View {
ScrollView {
LazyVStack(alignment: .leading, spacing: 0) {
ForEach(loader.attributedLines.indices, id: \.self) { index in
HStack(alignment: .top, spacing: 5)
{
Text("\(index + 1)")
.font(.system(size: fontSize, weight: .regular, design: .monospaced))
.foregroundColor(.gray)
.frame(width: 40, alignment: .trailing)
.padding(.trailing, 5)
//need to fix so we can select multiple lines, and having line numbers without including them in the selection, while keeping lazy loading
Text(loader.attributedLines[index])
.font(.system(size: fontSize, weight: .regular, design: .monospaced))
}
.textSelection(.enabled)
import SwiftUI

}
}
.padding()
struct ContentView: View {
@ObservedObject var loader: CodeContentLoader
@State private var fontSize: CGFloat = 12.0

var body: some View {
ScrollView([.vertical, .horizontal]) {
Text(loader.attributedContent)
.font(.system(size: fontSize, weight: .regular, design: .monospaced))
.textSelection(.enabled)
.padding()
}
.gesture(MagnificationGesture()
.onChanged { value in
self.fontSize = max(8.0, min(24.0, 12.0 * value))
})
}

}
2 changes: 1 addition & 1 deletion QuickCodeColorLook/QuickCodeColorLook.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<true/>
<key>com.apple.security.application-groups</key>
<array>
<string>$(TeamIdentifierPrefix)</string>
<string>com.oil3.codecolors</string>
</array>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
Expand Down
6 changes: 5 additions & 1 deletion QuickCodeColorLook/SyntaxHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ struct SyntaxHighlighter {
var attributedString = AttributedString()
let lines = content.components(separatedBy: .newlines)
let newline = AttributedString("\n")

let userDefaults = UserDefaults(suiteName: "com.oil3.codecolors")

for line in lines {
var lineAttributedString = AttributedString(line)
applySyntaxHighlighting(to: &lineAttributedString, fileExtension: fileExtension)
Expand Down Expand Up @@ -42,3 +43,6 @@ struct SyntaxHighlighter {
}
}
}
//
// Copyright Almahdi Morris Quet 2024
//
17 changes: 13 additions & 4 deletions QuickCodeColorLook/SyntaxRules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ class SyntaxRules {
("\\b-?\\d+(?:\\.\\d+)?\\b", NSColor.orange),
// Booleans
("\\b(?:True|False|true|false)\\b", NSColor.systemBlue),
// Comments (single line)
("#.*", NSColor.green)
// Comments
("#.*", NSColor.green),
("\"\"\".*?\"\"\"", NSColor.green),

// Python
("\\b(def|class|if|else|elif|return|import|from|as|for|while|try|except|with|lambda|pass|break|continue|yield|assert|async|await)\\b", NSColor.systemPurple),

]

let swiftKeywords = [
Expand All @@ -25,7 +30,9 @@ class SyntaxRules {
"is", "nil", "true", "false", "public", "private", "fileprivate", "internal",
"open", "static", "extension", "protocol", "guard", "defer", "where",
"associatedtype", "inout", "operator", "init", "super", "self", "Type",
"typealias", "Any", "dynamicType"
"typealias", "Any", "dynamicType", "return", "->",
"HStack", "VStack", "ScrollView", "LazyVStack", "LazyHStack", "View", "List", "Grid"

]
let swiftKeywordPattern = "\\b(\(swiftKeywords.joined(separator: "|")))\\b"

Expand All @@ -39,7 +46,9 @@ class SyntaxRules {
// Comments (single line)
("//.*", NSColor.green),
// Comments (multi-line)
("/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/", NSColor.green)
("/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/", NSColor.green),
("\"\"\".*?\"\"\"", NSColor.green),

]

let htmlRules: [(pattern: String, color: NSColor)] = [
Expand Down
Binary file added logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7b1a851

Please sign in to comment.