~aleteoryx/ntalk

e1e29d9d93f1e5cd8ce74be6f6b074efc8db6d59 — Aleteoryx 2 months ago 4b44b80
various improvements
1 files changed, 80 insertions(+), 14 deletions(-)

M ntalk.tcl
M ntalk.tcl => ntalk.tcl +80 -14
@@ 4,16 4,41 @@ set confdir ~/.config/ntalk
set scriptpath "${confdir}/cscript.tcl"
file mkdir $confdir


proc quit {} {exit 0}
proc restart {} {
	global argv0
	exec [info nameofexecutable] $argv0 &
	exit 0
}


### FONT STUFF ###

font create testingFont -size 100

proc make16 {} {
	foreach font {TkDefaultFont TkFixedFont TkTextFont} {
		font configure testingFont -family [font configure $font -family]
		set fontsize [expr {int(ceil(16.0 / [font metrics testingFont -linespace] * 100))}]
		font configure $font -size $fontsize
	}
}
make16


### UI SETUP ###

frame .foot
entry .foot.input
label .foot.msgs -textvariable lastmsg
label .foot.name

scrollbar .scroll -command {.buffer yview}
text .buffer -width 72 -yscrollcommand {.scroll set} -takefocus 1
text .buffer -width 72 -yscrollcommand {.scroll set}

pack .foot.msgs -side right 
pack .foot.msgs -side right
pack .foot.name -side left
pack .foot.input -side bottom -fill x

pack .foot -side bottom -fill x


@@ 29,24 54,35 @@ if {[catch {package require history}] == 0} {
	}
}

bind . <Control-q> quit
bind . <Control-R> restart


### CONNECTING ###

set user marmalade
set cmds {}
set sok {}
set server "the web"
if [file readable $scriptpath] {
	set fp [open $scriptpath]
	.buffer insert 1.0 [read $fp]
	close $fp
} else {
	.buffer insert 1.0 {# input connection script, then hit C-RET
set sok [socket localhost 44322]
	.buffer insert 1.0 {# input connection script, then hit C-RET. your changes will be saved.
set server localhost
set sok [socket $server 44322]
set user marmalade
}
}

bind .buffer <Control-Return> {
	set cscript [.buffer get 1.0 end]
	eval [.buffer get 1.0 end]
	make16
	if {$sok != {}} { set cscript [.buffer get 1.0 end] }
}
.buffer mark set insert end
focus .buffer
vwait cscript
bind .buffer <Control-Return> {}
.buffer configure -state disabled


@@ 55,9 91,11 @@ set fp [open $scriptpath w]
puts $fp [string trim $cscript]
close $fp

eval $cscript
fconfigure $sok -translation lf; # dammit
set lastmsg 0
set user [string trim $user]
.foot.name configure -text "${user}:"

wm title . "nanochatting on $server"


### NETCODE ###


@@ 65,12 103,15 @@ set lastmsg 0
proc sendl {line} {
	global sok
	regsub "\n" $line " " line
	puts $sok $line
	flush $sok
	if [catch {
		puts $sok $line
		flush $sok
	}] { restart }
}
proc recvl {} {
	global sok
	gets $sok
	if [catch { gets $sok ret }] { restart }
	return $ret
}
proc recvlines {{bd 0}} {
	global lastmsg


@@ 128,6 169,11 @@ proc skip {} {
	recvlines
}

proc quit {} {
	sendl QUIT
	exit 0
}


### ACTUAL CLIENT CODE LMAO ###



@@ 148,14 194,23 @@ proc pollmsgs {} {

### BOOT ###

focus .foot.input
set lastmsg 0
last 16
after 10000 pollmsgs
bind .foot.input <Return> [concat [bind .foot.input <Return>] ";" {
	set line [.foot.input get]
	switch -glob -- $line {
	.foot.input delete 0 end
	switch -glob -- $line [concat $cmds {
		/hist {
			hist
		}
		/quit {
			quit
		}
		/restart {
			restart
		}
		{/last *} {
			last [string range $line 6 end]
		}


@@ 168,10 223,21 @@ bind .foot.input <Return> [concat [bind .foot.input <Return>] ";" {
		{/my *} {
			sendmsg "${user}'s [string range $line 4 end]"
		}
		{/nick *} {
			set user [string trim [string range $line 6 end]]
			.foot.name configure -text "${user}:"
		}
		{/eval *} {
			.foot.input insert 0 [eval [string range $line 6 end]]
		}
		{/exec *} {
			.foot.input insert 0 [exec sh -c [string range $line 6 end]]
		}
		{/calc *} {
			.foot.input insert 0 [expr [string range $line 6 end]]
		}
		default {
			sendmsg "$user: $line"
		}
	}

	.foot.input delete 0 end
	}]
}]