Skip to content

Instantly share code, notes, and snippets.

View yyamasak's full-sized avatar

Yusuke Yamasaki yyamasak

View GitHub Profile
@yyamasak
yyamasak / grep.ps1
Created November 28, 2022 08:29
PowerShell script equivalent to "grep -r -A 1 ABCD *.txt"
Get-ChildItem . -include *.txt -Recurse | Select-String -Context 0,1 ABCD
@yyamasak
yyamasak / mygrab.tcl
Last active September 10, 2021 13:14
Rewrite Tcl/Tk grab command to continuously restore grabbed Tk toplevel window
proc _restore_grab_window {w} {
if {![winfo exists $w] || [grab status $w] eq "none"} {
return
} else {
if {![winfo viewable $w]} {
wm deiconify $w
}
after 2000 [list _restore_grab_window $w]
}
}
console show
package require BLT
namespace import blt::*
set graph [graph .graph]
pack $graph -fill both -expand yes
set gindex "Element A"
@yyamasak
yyamasak / breakable.tcl
Last active March 9, 2020 07:51
syntax sugar for foreach once {1} {puts a; break; puts b}
proc breakable {script} {
uplevel [list foreach __BLOCK__ {1} $script]
}
if {[info exists ::argv0] && $::argv0 eq [info script]} {
set str "line"
puts "enter breakable"
breakable {
puts "1st $str"
break
@yyamasak
yyamasak / unicode_literal_converter.tcl
Created March 6, 2020 06:27
Convert string to unicode literal sequence
package require unicode
proc to_unicode_literal {str} {
set cs [lmap c [unicode::fromstring $str] {format \\u%04x $c}]
join $cs ""
}
if {[namespace exists ::tk]} {
proc copy_to_clipboard {str} {
clipboard clear
@yyamasak
yyamasak / widget_dateentry.tcl.patch
Created February 12, 2020 09:06
tcllib widget::dateentry patch to 0.96
150,152c150,157
< set now [clock seconds]
< set x [clock format $now -format "%d/%m%/%Y"]
< set rawDate [clock scan "$x 00:00:00" -format "%d/%m%/%Y %H:%M:%S"]
---
> if {[info exists $options(-textvariable)]} {
> set x [set $options(-textvariable)]
> set rawDate [clock scan "$x" -format $options(-dateformat)]
> } else {
> set now [clock seconds]
@yyamasak
yyamasak / GetDpiForMonitor.tcl
Created November 28, 2019 08:07
Get Windows DPI scaling factor
package require Ffidl
if {$::tcl_platform(osVersion) >= 6.3} {
namespace eval USER32 {
set MONITOR_DEFAULTTONULL 0x00
set MONITOR_DEFAULTTOPRIMARY 0x01
set MONITOR_DEFAULTTONEAREST 0x02
ffidl::callout MonitorFromWindow {int int} int [ffidl::symbol user32.dll MonitorFromWindow]
@yyamasak
yyamasak / tclsqlite3.patch
Last active December 18, 2018 08:10
A patch file to add extended_errorcode API to tclsqlite3
diff -r C:\src\sqlite-extension\tclsqlite3-org.c C:\src\sqlite-extension\tclsqlite3-mod.c
1871a1872
> "extended_errorcode",
1887c1888,1889
< DB_UPDATE_HOOK, DB_VERSION, DB_WAL_HOOK
---
> DB_UPDATE_HOOK, DB_VERSION, DB_WAL_HOOK,
> DB_EXTENDED_ERRORCODE
2508a2511,2521
> ** $db extended_errorcode
@yyamasak
yyamasak / exterrcode-test.tcl
Last active December 18, 2018 08:03
Runtime loadable extension for sqlite3 to add extended_errorcode() function (but failed. do not use this!)
package require sqlite3
set dbfile ./example.db
set extfile ./exterrcode.dll
file delete $dbfile
sqlite3 db $dbfile
db enable_load_extension 1
db eval [format {SELECT load_extension('%s');} $extfile]
@yyamasak
yyamasak / replace_by_mpexpr.tcl
Created July 5, 2018 05:33
Replace expr command by mpexpr (this works in tclsh but not in wish because of command history hooks)
package require Mpexpr
if {[info commands tclexpr] eq {}} {
rename expr tclexpr
proc expr {args} {uplevel mpexpr $args}
}
# set PI [expr {4*atan(1)}]; #=> 3.14159265358979324