From 59e3ac9d9d30dce62b2f68643244bea9afb59eca Mon Sep 17 00:00:00 2001 From: Nick White Date: Mon, 21 Oct 2013 18:00:40 +0100 Subject: Initial commit --- tkread | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100755 tkread diff --git a/tkread b/tkread new file mode 100755 index 0000000..8c5d318 --- /dev/null +++ b/tkread @@ -0,0 +1,97 @@ +#!/usr/bin/tclsh +# Usage: tkread [-w] +# -w rewrap lines +# +# TODO: +# - justify text (not simple; see http://wiki.tcl.tk/1774) +# - add a basic search function +# - add function to type 30g to scroll 30% through an article +# - if window width is small, reduce padx space +# - make scrolling using mouse work even when text area isn't hovered over (also note this sort of scrolling doesn't update the title); integrate the MouseWheel event with scroll. this *should* also fix the issue of dragging and then leaving the window, as the strange scrolling is likely caused by related default behaviour + +set fontsize 20 +set colour #333333 +set inverted 0 +set drag 0 + +package require Tk + +proc rewrap {text} { + set wrapped [regsub -all {\n([^\n])} $text { \1}] + set spaced [regsub -all { *} $wrapped { }] + set better [regsub -all {\n *} $spaced "\n"] + set better [regsub -all {\n} $better "\n\n"] + return [regsub -all {\n\n\n} $better "\n"] +} + +proc changeFontSize {change} { + global fontsize + set newsize [expr $fontsize $change] + if {$newsize > 0} { + set fontsize $newsize + .t configure -font "Times $fontsize" -padx [expr $fontsize * 3] -spacing2 [expr [font metrics "Times $fontsize" -linespace] / 4] + } +} + +proc invertColours {} { + global inverted colour + if {$inverted} { + set inverted 0 + .t configure -bg white -fg $colour + . configure -bg #f8f8f5 + } else { + set inverted 1 + .t configure -bg $colour -fg #eeeeee + . configure -bg #222222 + } +} + +proc scroll {dir amount} { + .t yview scroll $dir $amount + set percent [expr [lindex [.t yview] 1] * 100] + set rough [regsub {\..*} $percent {}] + wm title . "tkread $rough%" +} + +proc doMotion {ypos} { + global lasty + if { $lasty != -1 } { + scroll [expr $lasty - $ypos] pixels + } + set lasty $ypos +} + +. configure -bg #f8f8f5 +text .t -font "Times $fontsize" -wrap word -width 64 -padx [expr $fontsize * 3] -bg white -fg $colour -spacing2 [expr [font metrics "Times $fontsize" -linespace] / 4] -relief flat -inactiveselectbackground {} -cursor {} +pack .t -expand yes -fill y +set text [read stdin] +if { $::argc > 0 && [lindex $::argv 0] == "-w" } { + set text [rewrap $text] +} +.t insert end $text +.t configure -state disabled ;# disable cursor + +bind . {scroll -1 unit} +bind . {scroll -1 unit} +bind . {scroll 1 unit} +bind . {scroll 1 unit} +bind . {scroll -1 page} +bind . {scroll -1 page} +bind . {scroll -1 page} +bind . {scroll -1 page} +bind . {scroll -1 page} +bind . {scroll 1 page} +bind . {scroll 1 page} +bind . {scroll 1 page} +bind . {scroll 1 page} +bind . {.t yview moveto 0} +bind . {.t yview moveto 1} +bind . {changeFontSize +5} +bind . {changeFontSize -5} +bind . {changeFontSize +1} +bind . {changeFontSize -1} +bind . {invertColours} +bind . {exit} +bind . {set drag 1; set lasty -1} +bind . {set drag 0} +bind . {if {$drag} { doMotion %y } } -- cgit v1.2.3