Picocli: how to show header/banner at all times - command-line

Picocli offers the ability to add a nice header in the #Command annotation, for example:
#Command(name = "git-star", header = {
"#|green _ _ _ |#",
"#|green __ _(_) |_ __| |_ __ _ _ _ |#",
"#|green / _` | | _(_-< _/ _` | '_| |#",
"#|green \\__, |_|\\__/__/\\__\\__,_|_| |#",
"#|green |___/ |#"},
description = "Shows GitHub stars for a project",
mixinStandardHelpOptions = true, version = "git-star 0.1")
How do I always show that header/banner when the program is running, without duplicating this banner in two places?
(See also https://github.com/remkop/picocli/issues/517)

There are two aspects to this:
How to get the banner text from the application?
How to render the ANSI colors and styles?
You can get the banner from the usage help message, either with new CommandLine(new App()).getCommandSpec().usageHelpMessage().header() or by injecting a #Spec annotated CommandSpec field in your application.
To render the ANSI styles, use CommandLine.Help.Ansi.AUTO.string(line) for each banner line.
Putting it all together:
#Command(name = "git-star", header = {
"#|green _ _ _ |#",
"#|green __ _(_) |_ __| |_ __ _ _ _ |#",
"#|green / _` | | _(_-< _/ _` | '_| |#",
"#|green \\__, |_|\\__/__/\\__\\__,_|_| |#",
"#|green |___/ |#"},
description = "Shows GitHub stars for a project",
mixinStandardHelpOptions = true, version = "git-star 0.1")
class GitStar implements Runnable {
#Option(names = "-c")
int count;
#Spec CommandSpec spec;
// prints banner every time the command is invoked
public void run() {
String[] banner = spec.usageHelpMessage().header();
// or: String[] banner = new CommandLine(new GitStar())
// .getCommandSpec().usageHelpMessage().header();
for (String line : banner) {
System.out.println(CommandLine.Help.Ansi.AUTO.string(line));
}
// business logic here...
}
public static void main(String[] args) {
CommandLine.run(new GitStar(), args);
}
}

For me in Picocli 4.5.2, works like that:
public void run() {
CommandLine cmd = new CommandLine(new App());
cmd.usage(System.out, Ansi.ON);
// business logic here...
}

Related

TYPOSCRIPT stdWrap.replacement I Want to replace space with underscore _

My typo3 version is 10.4.22
lib.spaces = TEXT
lib.spaces {
current = 1
stdWrap.replacement {
10 {
search =
replace = _
wrap = |
}
}
}
I have a following typoscript. And now my question is, what i have to write in search = ?
so that space is being searched and replace with underscore.
SORRY For my bad english!
replacement.search has stdWrap, so you can use char. The ascii code for space is 32, so that will make it:
lib.spaces = TEXT
lib.spaces {
current = 1
stdWrap.replacement {
10 {
search.char = 32
replace = _
wrap = |
}
}
}

FreeBASIC and GTK Glade how work the glade Button?

a few months ago I started programming with FreeBASIC using the GTK libraries so that the applications are operational on both Windows and Linux without the need to rewrite the code, but being a beginner I only managed to create the mask but I don't know how to handle pressing the button.
Could someone tell me what I need to do to handle the button press?
#INCLUDE ONCE "gtk/gtk.bi"
gtk_init(#__FB_ARGC__, #__FB_ARGV__)
DIM SHARED AS GtkBuilder PTR XML
DIM SHARED AS GObject PTR _
mainWindow, Main_CloseButton
XML = gtk_builder_new()
SCOPE
DIM AS GError PTR meld
VAR GUISTR = SADD( _
"<?xml version=""1.0"" encoding=""UTF-8""?>" _
"<!-- Generated with glade 3.38.2 -->" _
"<interface>" _
"<requires lib=""gtk+"" version=""3.24""/>" _
"<object class=""GtkWindow"" id=""mainWindow"">" _
"<property name=""can-focus"">False</property>" _
"<child>" _
"<object class=""GtkLayout"">" _
"<property name=""visible"">True</property>" _
"<property name=""can-focus"">False</property>" _
"<child>" _
"<object class=""GtkButton"" id=""Main_CloseButton"">" _
"<property name=""label"" translatable=""yes"">Close</property>" _
"<property name=""width-request"">80</property>" _
"<property name=""height-request"">30</property>" _
"<property name=""visible"">True</property>" _
"<property name=""can-focus"">True</property>" _
"<property name=""receives-default"">True</property>" _
"</object>" _
"<packing>" _
"<property name=""x"">350</property>" _
"<property name=""y"">200</property>" _
"</packing>" _
"</child>" _
"</object>" _
"</child>" _
"</object>" _
"</interface>" _
!"\0")
IF 0 = gtk_builder_add_from_string(XML, GUISTR, -1, #meld) THEN
WITH *meld
?"Error (GTK-Builder):"
?*.message
END WITH
g_error_free(meld)
END 2
END IF
END SCOPE
mainWindow = gtk_builder_get_object(XML, "mainWindow")
Main_CloseButton = gtk_builder_get_object(XML, "Main_CloseButton")
gtk_builder_connect_signals(XML, 0)
g_object_unref(XML)
gtk_widget_show_all(GTK_WIDGET(mainWindow))
gtk_main()
First of all you need to add a signal in the XML code that refers to pressing the button.
Ex.
"<property name=""receives-default"">True</property>" _
"<signal name=""clicked"" handler=""on_Main_CloseButton_clicked"" swapped=""no""/>" _
"</object>" _
Create a new file by calling it (on_Main_CloseButton_clicked.bas)
include it before the "gtk_builder_connect_signals(XML, 0)"
Sometimes during the compilation if it is included later it can give some errors (perhaps a compiler bug or the code too messy)
Main_CloseButton = gtk_builder_get_object(XML, "Main_CloseButton")
#INCLUDE "on_Main_CloseButton_clicked.bas"
gtk_builder_connect_signals(XML, 0)
g_object_unref(XML)
gtk_widget_show_all(GTK_WIDGET(mainWindow))
gtk_main()
And inside the new file write a sub that is activated by a handler
on_Main_CloseButton_clicked.bas
SUB on_Main_CloseButton_clicked CDECL ALIAS "on_Main_CloseButton_clicked" ( _
BYVAL widget AS GtkWidget PTR, _
BYVAL user_data AS gpointer) EXPORT ' Standard-Parameterliste
'Place your code here+
end
END SUB

How to convert GTK keyboard event keys to English in any language layouts?

I want to use keyboard shortcuts (like Ctrl+Z/Ctrl+C/Ctrl+V and etc.) in my GTK-3 app. I get different keyboard events for different keyboard layouts.
For example my keyboard have English and Russian layouts and so my Russian key "Я" is at "Z".
There are many languages in world. How I can make app which works with any languages?
Now I translate keycodes only for Russian keyboard this way:
func main_event_listener(event *gdk.Event){
eventObject := &gdk.EventKey{event}
key := eventObject.KeyVal()
state := eventObject.State()
key, state = GTK_TranslateKeyLayoutEnglish(key, state)
if state == gdk.GDK_CONTROL_MASK {
if key == gdk.KEY_z {
//Ctrl+Z
}
if key == gdk.KEY_c {
//Ctrl+C
}
if key == gdk.KEY_v {
//Ctrl+V
}
} else {
if key == gdk.KEY_F5 {
//F5
}
if key == gdk.KEY_Delete {
//Delete
}
}
}
func GTK_TranslateKeyLayoutEnglish(key uint, state uint) (uint, uint) {
key2 := key
state2 := state
if state2 > 8192 { //RUSSIAN Ctrl 8196 == English Ctrl 4
state2 -= 8192
}
switch key {
case gdk.KEY_Cyrillic_ya: //RUSSIAN 'я'
key2 = gdk.KEY_z
case gdk.KEY_Cyrillic_ef: //RUSSIAN 'ф'
key2 = gdk.KEY_a
case gdk.KEY_Cyrillic_che: //RUSSIAN 'ч'
key2 = gdk.KEY_x
case gdk.KEY_Cyrillic_es: //RUSSIAN 'с'
key2 = gdk.KEY_c
case gdk.KEY_Cyrillic_em: //RUSSIAN 'м'
key2 = gdk.KEY_v
//etc
}
return key2, state2
}
I expect better realization of GTK_TranslateKeyLayoutEnglish() function

Switch statement for imported NS_OPTIONS (RawOptionSetType) in Swift?

The switch statement in Swift is so much more expressive. I'm wondering if this might be possible:
Lets look at UIViewAutoresizing for example. It's defined in Objective-C as follows:
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
I can use it in Swift like an enum:
let foo = UIViewAutoresizing([.FlexibleHeight, .FlexibleTopMargin])
Is it possible to use a switch statement instead of multiple if-statements?
if foo & UIViewAutoresizing.FlexibleHeight != nil {
}
if foo & UIViewAutoresizing.FlexibleWidth != nil {
}
if foo & UIViewAutoresizing.FlexibleTopMargin != nil {
}
Something like this pseudo code:
switch foo { // ** THIS IS PSEUDO CODE AND WILL NOT COMPILE **
case & .FlexibleHeight:
println("height")
case & .FlexibleWidth:
println("width")
case & .FlexibleTop:
println("top")
}
I was frustrated enough about this problem that I wrote a Bitmask<T> class that can handle these use cases. The code is up on Github: brynbellomy/SwiftBitmask
It allows you to do stuff like this with any kind of object as your underlying type (here I'm using an enum):
enum MonsterAttributes : IBitmaskRepresentable, IAutoBitmaskable {
case Big, Ugly, Scary
static var autoBitmaskValues : [MonsterAttributes] = [.Big, .Ugly, .Scary,]
var bitmaskValue: UInt16 { return AutoBitmask..autoBitmaskValueFor(self) }
init(bitmaskValue: UInt16) { self = AutoBitmask.autoValueFromBitmask(bitmaskValue) }
}
// various ways to initialize
let option : MonsterAttributes = .Ugly
let bitmaskOfOption = Bitmask(option)
let anotherBitmaskOfOption = |MonsterAttributes.Ugly // same as bitmaskOfOption
let orWithVar = option | .Big // == Bitmask<MonsterAttributes> with a bitmaskValue of 1 | 2
let simpleOr = MonsterAttributes.Big | .Ugly // == Bitmask<MonsterAttributes> with a bitmaskValue of 1 | 2
// getting the raw integral bitmask value
let simpleOrValue = simpleOr.bitmaskValue // == UInt16(1 | 2)
let orValue = (MonsterAttributes.Big | .Ugly).bitmaskValue // == UInt16(1 | 2)
// implements BooleanType
if simpleOr & .Ugly { /* this code will execute */ }
// supports pattern matching operator
if simpleOr ~= .Ugly { /* this code will execute */ }
if simpleOr ~= (.Ugly | .Scary) { /* this code will execute */ }
... and all you have to do is implement a one-property protocol.
I'm really curious if anyone has any feedback on or ideas for the code, so please leave an issue in the queue if you think of anything!
I tried several hours yesterday and today to make this work with switch — no success.
The reason is that in this particular case we need to test against several cases. In Swift we need to use the fallthrough key word. But we are not allowed to fall through to the next case if that next case uses a variable, there for we cannot use the case let where statement, as shown here:
switch foo {
case let x where x & .FlexibleHeight != nil:
println("height")
case let x where x & .FlexibleWidth != nil:
println("width")
case let x where x & .FlexibleTopMargin != nil:
println("top margin")
default:
println("no")
}
This will break out once a case triggered. But
switch foo {
case let x where x & .FlexibleHeight != nil:
println("height")
fallthrough
case let x where x & .FlexibleWidth != nil:
println("width")
fallthrough
case let x where x & .FlexibleTopMargin != nil:
println("top margin")
default:
println("no")
}
does not work for the reason described above.
I'd go with a clear if statement, like
let foo = UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleHeight
let width = foo & UIViewAutoresizing.FlexibleWidth;
let height = foo & UIViewAutoresizing.FlexibleHeight;
if width == .FlexibleWidth {
println("width")
}
if height == .FlexibleHeight {
println("height")
}
or
let foo = UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleHeight
let usesFlexibleWidth = (foo & UIViewAutoresizing.FlexibleWidth) != nil;
let usesFlexibleHeight = (foo & UIViewAutoresizing.FlexibleHeight) != nil;
if usesFlexibleWidth {
println("width")
}
if usesFlexibleHeight {
println("height")
}
You definitely can, although it's a little more convoluted now that RawOptionSetType doesn't implement the BooleanType protocol.
switch foo {
case let x where x & .FlexibleHeight != nil:
println("height")
case let x where x & .FlexibleWidth != nil:
println("width")
case let x where x & .FlexibleTopMargin != nil:
println("top margin")
default:
println("no")
}
Note that this stops on the first condition that matches! So it really is another form of this:
if foo & .FlexibleHeight != nil {
println("height")
} else if foo & .FlexibleWidth != nil {
println("width")
} else if foo & .FlexibleTopMargin != nil {
println("top margin")
}
Another solution that I came up with is this:
let foo = /* .. your value to test .. */
let allCases = [UIViewAutoresizing.FlexibleHeight, UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleTopMargin]
for oneCase in allCases {
switch oneCase & foo {
case UIViewAutoresizing.FlexibleHeight:
println("height")
case UIViewAutoresizing.FlexibleWidth:
println("width")
case UIViewAutoresizing.FlexibleTopMargin:
println("top")
default:
break
}
}

Entity Framework linq query

I need help formatting my link query properly, I am using EF 3.5:
Dim mediators = (From m In entity.Mediators _
Where m.MediatorAvailabilities.Available = "Weekends"
Where (m.isActive = True) _
Order By m.Sequence _
Select New RankingCriteria() With { _
.FirstName = m.FirstName, _
.LastName = m.LastName, _
.CompanyName = m.CompanyName, _
.PhoneHome = m.PhoneHome, _
.PhoneWork = m.PhoneWork, _
.PhoneMobile = m.PhoneMobile, _
.Email = m.Email _
}).ToList()
I have a navigation property in Mediators to MediatorAvailabilities I want to do something like what is in where clause above in order to filter my results. It's not letting me navigate to the appropriate column by doing this: m.MediatorAvailabilities.Available.
How do I do this filter properly?
Thanks, Justin.
You will need to use the Any method. I dont know the proper VB syntax, but it should look something like this:
Where m.MediatorAvailabilities.Any(ma => ma.Available = "Weekends")