Add in config for macro's

Allow for changing macro strings. Also add option in UI to enable/disable macro processing
This commit is contained in:
Duncan Ferguson 2014-01-13 18:44:12 +00:00
parent 803a13d7a5
commit 3fa50f0b49
5 changed files with 150 additions and 51 deletions

View file

@ -2,9 +2,10 @@
- Fixed macros (%u, %s, %h, %n) not doing multiple replacements
- Add in key shortcut for username macro (ALT-u)
- Add in key shortcut for local hostname macro (ALT-l)
- Fixed a bug with 'show history' key shortcut
- Fixed "uninitialised errors in hash element" bug [clusterssh support-requests:#38]
- Fix a bug with 'show history' key shortcut
- Fix "uninitialised errors in hash element" bug [clusterssh support-requests:#38]
- Fixed the default cluster not being opened
- Add in toggle for macros
2013-04-16 Duncan Ferguson <duncan_ferguson@user.sf.net> - v4.02_01
- Refactured file loading code

View file

@ -494,6 +494,24 @@ Default key sequence to retile host windows. See below notes on shortcuts.
Default key sequence to send username to client. See below notes
on shortcuts.
=item macro_servername = %s
=item macro_hostname = %h
=item macro_username = %u
=item macro_newline = %n
=item macro_version = %v
Change the replacement macro used when either using a 'Send' menu item, or when
pasting text into the main console.
=item macros_enabled = yes
Enable or disable macro replacement. Note: this affects pasting into the
main console, items on the 'Send' menu and key_clientname, key_localname, key_servername and key_username.
=item max_addhost_menu_cluster_items = 6
Maximum number of entries in the 'Add Host' menu cluster list before
@ -659,10 +677,25 @@ This (optional) file contains items to populate the send menu. The
default entry could be written as:
<send_menu>
<menu title="Hostname">
<menu title="Use Macros">
<toggle/>
<accelerator>ALT-p</accelerator>
</dmenu>
<menu title="Remote Hostname">
<command>%s</command>
<accelerator>ALT-n</accelerator>
</menu>
<menu title="Local Hostname">
<command>%s</command>
<accelerator>ALT-l</accelerator>
</menu>
<menu title="Username">
<command>%u</command>
<accelerator>ALT-u</accelerator>
</menu>
<menu title="Test Text">
<command>echo "ClusterSSH Version: %v%n</command>
</menu>
</send_menu>
Submenus can also be specified as follows:
@ -689,7 +722,7 @@ B<Caveats:>
=back
The following replacement macros are available:
The following replacement macros are available (note: these can be changed in the configuration file):
=over 4

View file

@ -298,7 +298,7 @@ sub load_keyboard_map() {
# keyboard layout contains the keycode at $modifier level
if (defined(
$keyboardmap{ $keycodetosym{ $keyboard[$i][$modifier]
} }
} }
)
)
{
@ -388,9 +388,6 @@ sub resolve_names(@) {
my ( $self, @servers ) = @_;
logmsg( 2, 'Resolving cluster names: started' );
use Data::Dump qw(dump);
warn dump \@servers;
foreach (@servers) {
my $dirty = $_;
my $username = q{};
@ -533,29 +530,41 @@ SWITCH: {
}
sub send_text($@) {
my $svr = shift;
my $self = shift;
my $svr = shift;
my $text = join( "", @_ );
logmsg( 2, "servers{$svr}{wid}=$servers{$svr}{wid}" );
logmsg( 3, "Sending to '$svr' text:$text:" );
# command macro substitution
if ( $self->config->{macros_enabled} eq 'yes' ) {
# $svr contains a trailing space here, so ensure its stripped off
{
my $servername = $svr;
$servername =~ s/\s+//;
$text =~ s/%s/$servername/xsmg;
}
$text =~ s/%h/hostname()/xsmeg;
# $svr contains a trailing space here, so ensure its stripped off
{
my $macro_servername = $self->config->{macro_servername};
my $servername = $svr;
$servername =~ s/\s+//;
$text =~ s/$macro_servername/$servername/xsmg;
}
$text =~ s/%h/hostname()/xsmeg;
# use connection username, else default to current username
{
my $username = $servers{$svr}{username};
$username ||= getpwuid($UID);
$text =~ s/%u/$username/xsmg;
# use connection username, else default to current username
{
my $macro_username = $self->config->{macro_username};
my $username = $servers{$svr}{username};
$username ||= getpwuid($UID);
$text =~ s/$macro_username/$username/xsmg;
}
{
my $macro_newline = $self->config->{macro_newline};
$text =~ s/$macro_newline/\n/xsmg;
}
{
my $macro_version = $self->config->{macro_version};
$text =~ s/$macro_version/$VERSION/xsmg;
}
}
$text =~ s/%n/\n/xsmg;
foreach my $char ( split( //, $text ) ) {
next if ( !defined($char) );
@ -598,10 +607,11 @@ sub send_text($@) {
}
sub send_text_to_all_servers {
my $self = shift;
my $text = join( '', @_ );
foreach my $svr ( keys(%servers) ) {
send_text( $svr, $text )
$self->send_text( $svr, $text )
if ( $servers{$svr}{active} == 1 );
}
}
@ -1407,7 +1417,7 @@ sub create_windows() {
# now sent it on
foreach my $svr ( keys(%servers) ) {
send_text( $svr, $paste_text )
$self->send_text( $svr, $paste_text )
if ( $servers{$svr}{active} == 1 );
}
}
@ -1616,11 +1626,11 @@ sub key_event {
logmsg( 3, "matched combo" );
if ( $event eq "KeyRelease" ) {
logmsg( 2, "Received hotkey: $hotkey" );
send_text_to_all_servers('%s')
$self->send_text_to_all_servers('%s')
if ( $hotkey eq "key_clientname" );
send_text_to_all_servers('%h')
$self->send_text_to_all_servers('%h')
if ( $hotkey eq "key_localname" );
send_text_to_all_servers('%u')
$self->send_text_to_all_servers('%u')
if ( $hotkey eq "key_username" );
$self->add_host_by_name()
if ( $hotkey eq "key_addhost" );
@ -1760,21 +1770,33 @@ sub populate_send_menu_entries_from_xml {
}
}
else {
my $command = undef;
my $accelerator = undef;
if ( $menu_ref->{command} ) {
$command = sub {
send_text_to_all_servers( $menu_ref->{command}[0] );
};
}
if ( $menu_ref->{accelerator} ) {
$accelerator = $menu_ref->{accelerator};
}
$menu->command(
-label => $menu_ref->{title},
-command => $command,
-accelerator => $accelerator,
);
if ( $menu_ref->{toggle} ) {
$menus{send}->checkbutton(
-label => 'Use Macros',
-variable => \$self->config->{macros_enabled},
-offvalue => 'no',
-onvalue => 'yes',
-accelerator => $accelerator,
);
}
else {
my $command = undef;
if ( $menu_ref->{command} ) {
$command = sub {
$self->send_text_to_all_servers(
$menu_ref->{command}[0] );
};
}
$menu->command(
-label => $menu_ref->{title},
-command => $command,
-accelerator => $accelerator,
);
}
}
}
@ -1788,21 +1810,46 @@ sub populate_send_menu {
if ( !-r $self->config->{send_menu_xml_file} ) {
logmsg( 2, 'Using default send menu' );
$menus{send}->checkbutton(
-label => 'Use Macros',
-variable => \$self->config->{macros_enabled},
-offvalue => 'no',
-onvalue => 'yes',
-accelerator => $self->config->{key_macros_enable},
);
$menus{send}->command(
-label => 'Remote Hostname',
-command => [ \&send_text_to_all_servers, '%s' ],
-label => 'Remote Hostname',
-command => sub {
$self->send_text_to_all_servers(
$self->config->{macro_servername} );
},
-accelerator => $self->config->{key_clientname},
);
$menus{send}->command(
-label => 'Local Hostname',
-command => [ \&send_text_to_all_servers, '%h' ],
-label => 'Local Hostname',
-command => sub {
$self->send_text_to_all_servers(
$self->config->{macro_hostname} );
},
-accelerator => $self->config->{key_localname},
);
$menus{send}->command(
-label => 'Username',
-command => [ \&send_text_to_all_servers, '%u' ],
-label => 'Username',
-command => sub {
$self->send_text_to_all_servers(
$self->config->{macro_username} );
},
-accelerator => $self->config->{key_username},
);
$menus{send}->command(
-label => 'Test Text',
-command => sub {
$self->send_text_to_all_servers( 'echo ClusterSSH Version: '
. $self->config->{macro_version}
. $self->config->{macro_newline} );
},
);
}
else {
logmsg(
@ -1951,11 +1998,6 @@ sub run {
# = $self->resolve_names( @default );
= $self->resolve_names( $self->cluster->get_tag('default') );
warn "blip";
}
else {
warn "blop";
warn scalar $self->cluster->get_tag('default');
}
}

View file

@ -39,6 +39,7 @@ my %default_config = (
key_history => "Alt-h",
key_localname => "Alt-l",
key_retilehosts => "Alt-r",
key_macros_enable => "Alt-p",
key_paste => "Control-v",
key_username => "Alt-u",
mouse_paste => "Button-2",
@ -82,6 +83,13 @@ my %default_config = (
command => q{},
max_host_menu_items => 30,
macros_enabled => 'yes',
macro_servername => '%s',
macro_hostname => '%h',
macro_username => '%u',
macro_newline => '%n',
macro_version => '%v',
max_addhost_menu_cluster_items => 6,
menu_send_autotearoff => 0,
menu_host_autotearoff => 0,

View file

@ -38,6 +38,7 @@ Readonly::Hash my %default_config => {
key_history => "Alt-h",
key_localname => "Alt-l",
key_retilehosts => "Alt-r",
key_macros_enable => "Alt-p",
key_paste => "Control-v",
key_username => "Alt-u",
mouse_paste => "Button-2",
@ -85,6 +86,13 @@ Readonly::Hash my %default_config => {
comms => q{ssh},
max_host_menu_items => 30,
macros_enabled => 'yes',
macro_servername => '%s',
macro_hostname => '%h',
macro_username => '%u',
macro_newline => '%n',
macro_version => '%v',
max_addhost_menu_cluster_items => 6,
menu_send_autotearoff => 0,
menu_host_autotearoff => 0,
@ -306,7 +314,7 @@ isa_ok( $config, "App::ClusterSSH::Config" );
is( $trap->die, undef, 'die message correct' );
is( $trap->stdout, q{}, 'Expecting no STDOUT' );
is( $trap->stderr,
'Moved $HOME/.csshrc to $HOME/.csshrc.DISABLED'
'Moved $HOME/.csshrc to $HOME/.csshrc.DISABLED'
. $/
. 'Created new configuration file within $HOME/.clusterssh/'
. $/,
@ -468,7 +476,7 @@ isa_ok( $config, "App::ClusterSSH::Config" );
isa_ok( $config, "App::ClusterSSH::Config" );
is( $trap->stdout, q{}, 'Expecting no STDOUT' );
is( $trap->stderr,
q{Unable to write default $HOME/.clusterssh/config: Is a directory}
q{Unable to write default $HOME/.clusterssh/config: Is a directory}
. $/
. $/,
'Expecting no STDERR'
@ -495,11 +503,18 @@ key_addhost=Control-Shift-plus
key_clientname=Alt-n
key_history=Alt-h
key_localname=Alt-l
key_macros_enable=Alt-p
key_paste=Control-v
key_quit=Control-q
key_retilehosts=Alt-r
key_username=Alt-u
lang=en
macro_hostname=%h
macro_newline=%n
macro_servername=%s
macro_username=%u
macro_version=%v
macros_enabled=yes
max_addhost_menu_cluster_items=6
max_host_menu_items=30
menu_host_autotearoff=0