💾 Archived View for gemini.ctrl-c.club › ~nttp › toys › gemview.tcl captured on 2020-09-24 at 00:43:52.

View Raw

More Information

-=-=-=-=-=-=-

#!/usr/bin/env wish

package require Tk 8.5

set version_string "GemView Desktop version 1.1 (1 September 2020)"
set desc_string "Offline viewer for local GemText files"

proc slurp_file fn {
	if {[catch {set f [open $fn]} err]} {
		tk_messageBox -title "Problem opening file" \
			-message $err -detail $fn -icon warning
		return ""
	} elseif {[catch {set text [read $f]} err]} {
		tk_messageBox -title "Problem reading file" \
			-message $err -detail $fn -icon warning
		catch {close $f}
		return ""
	} else {
		catch {close $f}
		return $text
	}
}

proc render_text {content textbox} {
	set mono false
	foreach i [split $content "\n"] {
		if {$mono} {
			if {[string range $i 0 2] == "```"} {
				set mono false
			} else {
				$textbox insert end "$i\n" mono
			}
		} elseif {[string range $i 0 2] == "```"} {
			set mono true
		} elseif {[string range $i 0 2] == "###"} {
			set i [string range $i 3 end]
			set i [string trim $i]
			$textbox insert end "$i\n" h3
		} elseif {[string range $i 0 1] == "##"} {
			set i [string range $i 2 end]
			set i [string trim $i]
			$textbox insert end "$i\n" h2
		} elseif {[string index $i 0] == "#"} {
			set i [string range $i 1 end]
			set i [string trim $i]
			$textbox insert end "$i\n" h1
		} elseif {[string range $i 0 1] == "* "} {
			set i [string range $i 2 end]
			set i [string trim $i]
			$textbox insert end "\u2022 $i\n" litem
		} elseif {[string index $i 0] == ">"} {
			set i [string range $i 1 end]
			set i [string trim $i]
			$textbox insert end "$i\n" quote
		} elseif {[string range $i 0 1] == "=>"} {
			set i [string range $i 2 end]
			set i [string trim $i]
			if {[regexp {(\S+)\s*(.*)} $i link url label]} {
				if {$label == ""} {set label $url}
				$textbox insert end "$label\n" link
			}
		} else {
			$textbox insert end "$i\n"
		}
	}
}

proc open_file fn {
	set content [slurp_file $fn]
	if {$content != ""} {
		.viewer delete 1.0 end
		render_text $content .viewer
		.status configure -text $fn
	}
}

proc do_open {} {
	set ft [list {"GemText files" ".gmi"} {"All files" "*"}]
	set answer [tk_getOpenFile -title "File to view" \
		-parent . -filetypes $ft]
	if {$answer != ""} {
		open_file $answer
	}
}

proc do_about {} {
	global version_string desc_string
	tk_messageBox -title "About GemView Desktop" \
		-message $version_string -detail $desc_string
}

pack [ttk::frame .toolbar] -side top -fill x -padx 2 -pady 2
ttk::label .status -relief sunken -text "GemView Desktop ready"
pack .status -side bottom -fill x -padx 2 -pady 2 -ipadx 2 -ipady 2

text .viewer -width 80 -height 25 -font "Times 14"
pack .viewer -side left -fill both -expand yes -padx 2 -pady 2

ttk::scrollbar .scroll -orient vertical -command {.viewer yview}
pack .scroll -side right -fill y -padx 2 -pady 2

.viewer configure -wrap word -padx 16 -pady 16
.viewer configure -yscrollcommand {.scroll set}

.viewer tag configure quote -lmargin1 32 -rmargin 32
.viewer tag configure litem -lmargin1 16
.viewer tag configure h1 -font "Helvetica 20 bold"
.viewer tag configure h2 -font "Helvetica 18 bold"
.viewer tag configure h3 -font "Helvetica 16 bold"
.viewer tag configure mono -font "Courier 12" -wrap none
.viewer tag configure link -foreground blue -underline yes -wrap none

ttk::button .toolbar.file_open -text "Open" \
	-underline 0 -command do_open
pack .toolbar.file_open -side left -padx 2
ttk::button .toolbar.help_about -text "About" \
	-underline 0 -command do_about
pack .toolbar.help_about -side left -padx 2

wm title . "GemView Desktop"

bind . <Control-o> do_open
bind . <Control-q> exit

set arg1 [lindex $argv 0]

if {$arg1 == "-?" || $arg1 == "--help"} {
	puts $desc_string
	puts "Usage: gemview \[-? | -V | filename]"
	puts "\nOptions:\n"
	puts "-? or --help	show this message and exit"
	puts "-V or --version	show version info and exit"
	puts "\nUse - for a file name to read from standard input."
	puts "Any arguments after the first are ignored."
	exit
} elseif {$arg1 == "-V" || $arg1 == "--version"} {
	puts $version_string
	puts "For usage instructions enter: gemview -?"
	exit
} elseif {$arg1 == "-"} {
	if {[catch {set content [read stdin]} err]} {
		tk_messageBox -title "Problem reading input" \
			-message $err -icon warning
	} else {
		render_text $content .viewer
		.status configure -text "Showing standard input"
	}
} elseif {$arg1 != ""} {
	open_file [file normalize $arg1]
}