@@ -23,33 +23,70 @@ interface Config {
2323 MAX_KEYS : number ;
2424}
2525
26- let DEBUG : boolean = false ;
27- let ISKEYDLOG : boolean = false ;
28-
29-
30- if ( ARGV . includes ( '--help' ) || ARGV . includes ( '-h' ) ) {
31- print ( `Key Visualizer - A simple visualizer for key presses` ) ;
32- print ( `Usage: keyvis [OPTION]` ) ;
33- print ( `` ) ;
34- print ( `Options:` ) ;
35- print ( ` --debug, -d Enable debug mode` ) ;
36- print ( ` --keydlog, -k Enable keyd log` ) ;
37- print ( ` --help, -h Show this help message` ) ;
38- print ( ` --version, -v Show version information` ) ;
39- print ( `` ) ;
40- exit ( 0 ) ;
26+ type STATE = 'INFO' | 'DEBUG' | 'ERROR' ;
27+
28+ interface Option {
29+ alias : string ;
30+ desc : string ;
4131}
4232
43- if ( ARGV . includes ( '--version' ) || ARGV . includes ( '-v' ) ) {
44- print ( `Key Visualizer 0.1.0` ) ;
33+ interface Options {
34+ [ key : string ] : Option ;
35+ }
36+
37+ const OPTIONS : Options = {
38+ '--help' : { alias : '-h' , desc : 'Show this help message' } ,
39+ '--debug' : { alias : '-d' , desc : 'Enable debug mode' } ,
40+ '--keydlog' : { alias : '-l' , desc : 'Enable keyd log' } ,
41+ '--kill' : { alias : '-k' , desc : 'Kill all running keyvis instances' } ,
42+ '--version' : { alias : '-v' , desc : 'Show version information' }
43+ } ;
44+
45+ function showHelp ( ) : void {
46+ print ( '' ) ;
47+ print ( 'Key Visualizer - A simple visualizer for key presses' ) ;
48+ print ( 'Usage: keyvis [OPTION]' ) ;
49+ print ( '' ) ;
50+ print ( 'Options:' ) ;
51+ Object . entries ( OPTIONS ) . forEach ( ( [ flag , { alias, desc } ] ) => {
52+ print ( ` ${ flag . padEnd ( 10 ) } ${ alias . padEnd ( 4 ) } ${ desc } ` ) ;
53+ } ) ;
54+ print ( '' ) ;
4555 exit ( 0 ) ;
4656}
4757
48- if ( ARGV . includes ( '--debug' ) || ARGV . includes ( '-d' ) ) {
49- DEBUG = true ;
58+ function killInstances ( ) : void {
59+ try {
60+ const proc = Gio . Subprocess . new (
61+ [ 'pkill' , '-o' , '-f' , 'gjs.*keyvis' ] ,
62+ Gio . SubprocessFlags . STDOUT_PIPE ,
63+ ) ;
64+ proc . wait ( null ) ;
65+ const status = proc . get_status ( ) ;
66+ status == 0 ? print ( 'Keyvis killed successfully' ) : print ( 'No keyvis instances found' ) ;
67+ exit ( 0 ) ;
68+ } catch ( e ) {
69+ logError ( e instanceof Error ? e : new Error ( String ( e ) ) ) ;
70+ exit ( 1 ) ;
71+ }
72+ }
73+
74+ const hasArg = ( flag : string ) : boolean =>
75+ ARGV . includes ( flag ) || ARGV . includes ( OPTIONS [ flag ] . alias ) ;
76+
77+ if ( hasArg ( '--help' ) ) showHelp ( ) ;
78+
79+ if ( hasArg ( '--version' ) ) {
80+ print ( 'Key Visualizer 0.1.0' ) ;
81+ exit ( 0 ) ;
5082}
51- if ( ARGV . includes ( '--keydlog' ) || ARGV . includes ( '-k' ) ) {
52- ISKEYDLOG = true ;
83+
84+ const DEBUG = hasArg ( '--debug' ) ;
85+ const ISKEYDLOG = hasArg ( '--keydlog' ) ;
86+
87+ if ( hasArg ( '--keydlog' ) && ! hasArg ( '--debug' ) ) {
88+ print ( 'Error: --keydlog must be used with --debug' ) ;
89+ exit ( 1 ) ;
5390}
5491
5592function keydlog ( msg : string ) : void {
@@ -58,8 +95,6 @@ function keydlog(msg: string): void {
5895 }
5996}
6097
61- type STATE = 'INFO' | 'DEBUG' | 'ERROR' ;
62-
6398function debug ( state : STATE , msg : string ) : void {
6499 if ( ! state ) {
65100 state = 'INFO' ;
@@ -75,7 +110,7 @@ function debug(state: STATE, msg: string): void {
75110}
76111
77112const CONFIG : Config = {
78- WINDOW_WIDTH : 500 ,
113+ WINDOW_WIDTH : 400 ,
79114 WINDOW_HEIGHT : 50 ,
80115 MARGIN : 20 ,
81116 CLEAR_TIMEOUT : 1500 ,
@@ -136,7 +171,6 @@ function setupHyprlandRules() {
136171 'windowrulev2 pin,class:(d7om.dev.keyvis)' ,
137172 'windowrulev2 noblur,class:(d7om.dev.keyvis)' ,
138173 'windowrulev2 noshadow,class:(d7om.dev.keyvis)' ,
139- 'windowrulev2 size 200 50,class:(d7om.dev.keyvis)' ,
140174 'windowrulev2 animation slide bottom,class:(d7om.dev.keyvis)' ,
141175 'windowrulev2 nofocus,class:(d7om.dev.keyvis)' ,
142176 'windowrule move 80% 92%,^(d7om.dev.keyvis)$' ,
@@ -276,10 +310,10 @@ const KeyVisualizer = GObject.registerClass(
276310 const css = new Gtk . CssProvider ( ) ;
277311 css . load_from_data ( `
278312 window {
279- opacity: ${ opacity } ;
280- transition: opacity 0.5s;
281- }
282- ` , - 1 ) ;
313+ opacity: ${ opacity } ;
314+ transition: opacity 0.5s;
315+ }
316+ `, - 1 ) ;
283317
284318 const display = Gdk . Display . get_default ( ) ;
285319 if ( ! display ) {
@@ -439,7 +473,6 @@ const KeyVisualizer = GObject.registerClass(
439473
440474 this . fadeTimeout = GLib . timeout_add ( GLib . PRIORITY_DEFAULT , CONFIG . FADE_TIMEOUT , ( ) => {
441475 this . _setWindowOpacity ( 0 ) ;
442- this . _window . hide ( ) ;
443476 this . fadeTimeout = null ;
444477 return GLib . SOURCE_REMOVE ;
445478 } ) ;
@@ -471,6 +504,10 @@ const KeyVisualizer = GObject.registerClass(
471504 }
472505) ;
473506
507+
474508const app = new KeyVisualizer ( ) ;
475- app . run ( [ ] ) ;
509+
510+ hasArg ( '--kill' ) ? killInstances ( ) : app . run ( [ ] )
511+
512+
476513
0 commit comments