Skip to content

Commit 33e9b60

Browse files
committed
show file size
1 parent 3dff9be commit 33e9b60

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

imageviewer5/AppDelegate.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var totalFilesInFolder = 0 // How many images in the folder that we can show
1818
// Infobar vars
1919
var InfoBar_Name = "(Filename goes here)"
2020
var InfoBar_Format = "(Resolution etc goes here)"
21+
var InfoBar_FileSize = "(Filesize goes here)"
2122
var InfoBar_Misc = "(Misc stuff goes here)"
2223

2324

@@ -243,7 +244,7 @@ func set_new_url(in_url: String) { // This is the main thing. It gets called whe
243244
InfoBar_Format = String(current_image_width) + "x" + String(current_image_height) // TODO show color depth etc too
244245
let TitlebarImageIndex = currentImageIndex + 1 // because 0/x looks weird
245246
InfoBar_Misc = String(TitlebarImageIndex) + "/" + String(totalFilesInFolder)
246-
247+
InfoBar_FileSize = get_human_size(bytes: get_file_size(urlString: current_url))
247248

248249

249250
// Enable menu items since we now should have a image loaded
@@ -287,6 +288,28 @@ func get_filename() -> String { /* picture.jpg */
287288
return fn
288289
}
289290

291+
func get_file_size(urlString: String) -> Int {
292+
let local_url = URL(string: urlString)
293+
do {
294+
let resources = try local_url!.resourceValues(forKeys:[.fileSizeKey])
295+
let fileSize = resources.fileSize!
296+
return fileSize
297+
} catch {
298+
return 0
299+
}
300+
}
301+
302+
func get_human_size(bytes: Int) -> String {
303+
// https://stackoverflow.com/a/42723243
304+
// get_human_size(bytes: get_file_size(urlString: current_url))
305+
let bcf = ByteCountFormatter()
306+
//bcf.allowedUnits = [.useMB, .useKB] // optional: restricts the units to MB and KB only
307+
bcf.countStyle = .file
308+
bcf.isAdaptive = true
309+
let string = bcf.string(fromByteCount: Int64(bytes))
310+
return string
311+
}
312+
290313
func get_folder() -> String { /* /path/to/ */
291314
let NSs = (get_URL_String() as NSString)
292315
let folderpath = NSs.deletingLastPathComponent

imageviewer5/ContentView.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct ContentView: View, DropDelegate {
3939
@State var showInfoBar = UD.bool(forKey: "Show Info Bar")
4040
@State var InfoBar_Text_Name = InfoBar_Name
4141
@State var InfoBar_Text_Format = InfoBar_Format
42+
@State var InfoBar_Text_FileSize = InfoBar_FileSize
4243
@State var InfoBar_Text_Misc = InfoBar_Misc
4344
//@State var bgcolor = Color(.systemBlue)
4445

@@ -56,8 +57,9 @@ struct ContentView: View, DropDelegate {
5657
if self.showInfoBar == true && url_string != "" {
5758
Spacer()
5859
HStack {
59-
Text(self.InfoBar_Text_Name).bold().help(get_full_filepath())
60+
Text(self.InfoBar_Text_Name).bold().help(Text(get_full_filepath()))
6061
Text(self.InfoBar_Text_Format)
62+
Text(self.InfoBar_Text_FileSize)
6163
Text(self.InfoBar_Text_Misc)
6264
}.padding()
6365
}
@@ -72,6 +74,7 @@ struct ContentView: View, DropDelegate {
7274
self.showInfoBar = UD.bool(forKey: "Show Info Bar")
7375
self.InfoBar_Text_Name = InfoBar_Name
7476
self.InfoBar_Text_Format = InfoBar_Format
77+
self.InfoBar_Text_FileSize = InfoBar_FileSize
7578
self.InfoBar_Text_Misc = InfoBar_Misc
7679
}
7780

imageviewer5/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<key>CFBundleShortVersionString</key>
3737
<string>$(MARKETING_VERSION)</string>
3838
<key>CFBundleVersion</key>
39-
<string>439</string>
39+
<string>468</string>
4040
<key>LSApplicationCategoryType</key>
4141
<string>public.app-category.photography</string>
4242
<key>LSMinimumSystemVersion</key>

0 commit comments

Comments
 (0)