Autore Topic: Higgins: un lanciatore per gambas  (Letto 1329 volte)

Offline koko

  • Gambero
  • **
  • Post: 52
    • Mostra profilo
Re:Higgins: un lanciatore per gambas
« Risposta #15 il: 29 Aprile 2016, 13:56:31 »
Dopo un lungo ed estenuante lavoro...
No, in realtà è stato divertente, credo di aver concluso, a meno di bug dell'ultim'ora.

Comunque, ho fatto molta attenzione a rendere il programma modulare; in altre parole dovrebbe essere semplice aggiungere plugin per estendere i risultati di higgins.
Nel caso qualcuno fosse interessato a scrivenre di nuovi, ecco come fare:

Prima di tutto bisogna scrivere una classe che si chiami:
plugins/plugin_NomeAPiacere.gambas
inoltre il "NomeAPiacere" non deve finire con "_gui" perchè plugins/plugin_NomeAPiacere_gui.gambas è una stringa riservata a eventuali form di configurazione dello stesso plugin.
I plugin sono lanciati in ordine alfabetico, per questo quelli esistenti li ho chiamati (es:) plugin_090_indexer
Eventuali configurazioni interne al plugin potranno essere salvate tramite la classe Settings.
Ma ogni plugin può fare un pò come vuole riguardo il salvataggio delle sue impostazioni.

Questa classe deve rispecchiare bene o male la classe "plugin_template" che ho scritto come una linea guida, eccola:
Codice: [Seleziona]
' Gambas class file

Create Static
Static Public Description As String = "Example plugin"    'A description for this plugin
Static Public Friendly_name As String = "Example plugin"  'A friendly name for this plugin
Static Public configurable As Boolean = False             'Does this plugin provides a configure() method?

Public Sub _free()

End


Public Struct rItemT
  plugin As String            'The plugin name
  Text As String              'Text displayed in the clipboard
  SubText As String           'Text displayed under the main text
  Image_file As String        'Optional image displayed: possible values:
                              '  "mimetype://fullpath/to/filename/to/identify"
                              '     get picture from mime icon
                              '   "desktopfile://fullpath/to/.desktop/file"
                              '     Get the picture from .desktop file
                              '   "resize://fullpath/to/image/file"
                              '     Load the image from disk and resize it automatically
                              '   "/fullpath/to/image/file"
                              '     Load the image from disk
 
  Clipboard_data As String    'The data copied to the clipboard
  Action As String            'String that indicates what to do when activating a result:
                              '"DESKTOPOPEN"
                              '       Open folders and starts .desktop files
                              '"EXEC"
                              '       Executes executables files
                              '"GUESS"
                              '       Choose an action based on filetype
                              '"NULL"
                              '       Does nothing

  Action_p As Variant         'the parameter for Action
 
End Struct


Private textcontrol As Object
Private plugin As String
Private iconsize As Integer
Private minchar As Integer = 3 'The plugin will not operate on searches smaller than that


Public Sub _new(p_plugin As String, p_iconsize As Integer)

  plugin = p_plugin
  iconsize = p_iconsize

End


Public Sub Stop()
 
  'not sure if this is the right way, but this should be called when the plugin has to be unloaded
 
End


Private Sub trigged(query As String) As Boolean
  'this function will decide if the plugin will be search for "query" or not.
  If Len(query) < minchar Then Return False
  'some more controls here (?)
  Return True
End


Public Sub find(textcontrol As Object) As RItemT[]
  'this is the main function, textcontrol is an object that exposes a "text" property.
  'We need to pass an entire object reference instead of a simple string so that
  'we can stop a (slow) plugin as soon as textcontrol.text changes (ie: the user is still typing...)

  Dim anItem As String
  Dim rResults As New RItemT[]
  Dim rResult As RItemT
  Dim current_search As String = textcontrol.text
 
  If Not trigged(textcontrol.text) Then Return
 
  For Each anItem In Dir(User.home).sort()
   
    rResult = create_item(anItem)
    If rResult <> Null Then rResults.Add(rResult)
    'Wait 'if calculations are heavy a wait may be handy.
    If textcontrol.text <> current_search Then Return


  Next 'anItem
  Return rResults

End


Private Function create_item(result As String) As RItemT
  'this function creates an item that will be pushed into the result list.
  Dim return_item As RItemT
  return_item = New RItemT
  return_item.plugin = plugin
  return_item.Action = "EXEC" ' DESKTOPOPEN,EXEC,GUESS,NULL... MORE?
  return_item.Action_p = "/usr/bin/gambas3"
  return_item.Clipboard_data = "Some data copied to the clipboard"
  return_item.image_file = "icon:/" & iconsize & "/bookmark"
  return_item.Text = result
  return_item.SubText = "In: " & User.home
  Return return_item
End

Public Sub configure()
 
  'this starts the configuration window.
  'eg: plugin_template_gui.show()
End

EDIT:
Prossimo passo, permettere di usare plugin scritti in qualsiasi "linguaggio"; l'idea è di lanciarli come come "process" tramite "SHELL" e di comunicare con loro tramite semplici stream.
« Ultima modifica: 29 Aprile 2016, 15:14:03 da koko »

Offline Gianluigi

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 4.151
  • Tonno verde
    • Mostra profilo
Re:Higgins: un lanciatore per gambas
« Risposta #16 il: 01 Maggio 2016, 13:31:41 »
Forse lo hai fatto e io non l'ho visto o capito altrimenti andrebbe spiegato che per far funzionare, almeno in parte, higgins-master occorre che nel sistema sia presente la libreria xbindkeys altrimenti ottieni errore e non funziona.
In realtà anche dopo aver installato così xbindkeys:
Codice: [Seleziona]
~$ xbindkeys
Il programma "xbindkeys" non è attualmente installato. È possibile installarlo digitando:
sudo apt-get install xbindkeys
$ sudo apt-get install xbindkeys
ottengo questo avviso in console:
Codice: [Seleziona]
/home/gian/.config/gambas3/higgins/xbindkeys_higgins: File o directory non esistente
FMain.load_plugin.109: PLUGIN_010_CALCULATOR
FMain.load_plugin.109: PLUGIN_020_APPS
FMain.load_plugin.109: PLUGIN_030_EXECUTABLE
FMain.load_plugin.109: PLUGIN_040_FBOOKMARKS
FMain.load_plugin.109: PLUGIN_050_LS
FMain.load_plugin.109: PLUGIN_090_INDEXER
FMain.load_plugin.109: PLUGIN_TEMPLATE
PLUGIN_090_INDEXER.reindex_timer_Timer.171: -61184844 minutes to go
PLUGIN_090_INDEXER.ReIndex.200: Indexing...
PLUGIN_090_INDEXER.ReIndex.201: sh -c ""
PLUGIN_090_INDEXER.reindex_timer_Timer.171: -61184844 minutes to go
PLUGIN_090_INDEXER.ReIndex.200: Indexing...
PLUGIN_090_INDEXER.ReIndex.201: sh -c ""
PLUGIN_090_INDEXER.indexprocess_kill.213: Finished indexing
PLUGIN_090_INDEXER.indexprocess_kill.213: Finished indexing
PLUGIN_090_INDEXER.reindex_timer_Timer.171: 30 minutes to go

Il lanciatore funziona ma non le shortcut (credo) e l'applicazione non si chiude realmente e se de-commenti il quit da i soliti warning.
Da notare che in realtà higgins/xbindkeys_higgins esiste come puoi vedere dall'immagine allegata.
 :ciao:
nuoto in attesa del bacio di una principessa che mi trasformi in un gambero azzurro

Offline koko

  • Gambero
  • **
  • Post: 52
    • Mostra profilo
Re:Higgins: un lanciatore per gambas
« Risposta #17 il: 02 Maggio 2016, 09:48:27 »
xbindkeys non è, purtroppo, una libreria, ma proprio un programma, ed hai ragione che dovrei segnalarne l'eventuale assenza tramite un messaggio a video.
L'errore che dà in console lo scrive higgins "da design":
Codice: [Seleziona]
Public Sub _new(conf As String, Optional alias As String = "")
  If Not System.Exist("xbindkeys") Then
    Debug "ERROR: xbindkeys not found in system path"
    Quit
  Endif

Ad ogni modo, higgins usa xbindkeys solo per registrare una hotkey (shortcut) che visualizzi/nasconda la sua interfaccia.

Credo di aver capito cosa è successo nel tuo caso.
Al primo avvio, higgins cerca la sua directory di configurazione; se non la trova, assume che quella sia la prima volta che l'utente lo lancia; crea la cartella, e quindi avverte l'utente che va configurata almeno l'hotkey.
Poi vuole far partire xbindkeys per regitrare la hotkey, ma non lo trova ed esce.
L'utente installa xbindkeys e rilancia higgins, che però a quel punto trova la sua cartella (l'aveva creata prima di uscire) e quindi non chiede più di registrare la hotkey.
Quetso lo metto a posto in giornata, ma intanto, avendo installato xbindkeys, puoi impostate la hotkey cliccando col tasto dx sulla tray icon e scegliendo "Options" dal menu, oppure clicchi col tasto sx sulla tray icon e poi clicchi sul pulsante alla destra della barra di ricerca.
nella finestra che si apre, clicca "change default hotkey"; a quel punto i apre xbindkeys che ti chiede di battere una combinazione di tasti; io uso alt-space.
Fatto ciò, alt-space mostrerà e nasconderà il form di higgins.


Riguardo la chiusura, higgins è pensato per rimanere sempre attivo; per chiuderlo si usa la tray icon (tasto dx->quit) oppure tasto dx sul bottone di configurazione -> quit.

Dà un warning di riferimento circolare all'uscita (è solo un warning, non necessariamente un male), ma le risorse vengono liberate tutte correttamente, per cui non c'è bisogno di "Quit".

Scusa la prolissità :D[/code]
« Ultima modifica: 02 Maggio 2016, 09:51:47 da koko »

Offline Gianluigi

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 4.151
  • Tonno verde
    • Mostra profilo
Re:Higgins: un lanciatore per gambas
« Risposta #18 il: 02 Maggio 2016, 11:40:37 »
Purtroppo in questo momento a causa di aggiornamento OS non ho tempo per farti da cavia.
Due cose però te le posso dire:
Ora all'apertura si comporta come dici tu ma la finestra che si apre e che ti dovrebbe dire cosa fare (digitare la combinazione di tasti) è piccola vicino alla trayicon e completamente bianca.
In save_geometry c'è ancora uno stop.
 :ciao:
nuoto in attesa del bacio di una principessa che mi trasformi in un gambero azzurro

Offline koko

  • Gambero
  • **
  • Post: 52
    • Mostra profilo
Re:Higgins: un lanciatore per gambas
« Risposta #19 il: 02 Maggio 2016, 11:45:17 »
purtroppo quella è la finestra di xbindkeys, non ho controllo su di essa.
Al massimo posso ridimensionarla o posizionarla dove voglio...

Offline koko

  • Gambero
  • **
  • Post: 52
    • Mostra profilo
Re:Higgins: un lanciatore per gambas
« Risposta #20 il: 13 Maggio 2016, 13:45:08 »
Sono riuscito finalmente ad implementare i plugin esterni.
Adesso higgins riconoscerà eventuali eseguibili presenti in: ~/.config/gambas3/higgins/extbin
Il bello è che l' "API" con cui il plugin esterno dialoga con higgins si basa su semplici messaggi di testo.
Questo vuol dire che i plugin esterni possono essere scritti in qualsiasi linguaggio.
Un plugin di esempio che ho scritto è il seguente, in bash:
Codice: [Seleziona]
#!/bin/bash
#Simple locate plugin example.
#Put me in ~/.config/gambas3/higgins/extbin to use me

#This plugin will search for files using locate command, which has to be installed.
#This plugin will run only when the trigger is used, so:
#   to search for "myfile.txt"
#   write: l:myfile.txt
#   (trigger is "l:")

#This plugin will not search anything lower than 3 characters (minchar=3)


export IFS=$'\n'
trigger="l:" #Search using the trigger? es: l:myfile
minchar=3    #Don't search on queries smaller than that, trigger does not count.

query="$@"

#Do we use a trigger?
if [ -n "$trigger" ] ; then
    #...Yes, we do, so exit if not triggered
    if [[ $query != $trigger* ]] ; then
        exit
    fi
    #Strip the trigger prefix from the query
    query=$(echo $query| sed "s/^$trigger//")
fi

len=${#query}

#Exit if the query is too small
if [ $len -lt $minchar ] ; then
    exit ;
fi


#Finally, start the search:
for file in $(locate -i "$query") ; do
    echo BEGIN
    # For fields description and possible values, please refer to plugins/plugin_999_template
      echo Text=$(basename $file)
      echo SubText="in " $(dirname $file)"/"
      echo Image_file="mimetype://$file"
      echo Clipboard_data="$file"
      echo Action="GUESS"
      echo Action_p="$file"
    echo END
done

Questo script si può lanciare anche da linea di comando (che poi è quello che higgins fa), così:
Codice: [Seleziona]
# ~/.config/gambas3/higgins/extbin/locate.sh l:ciao 
e l'uscita dello script sarà qualcosa del genere:
Codice: [Seleziona]
BEGIN
Text=ciao.srt
SubText=in  /home/
Image_file=mimetype:///home/ciao.srt
Clipboard_data=/home/ciao.srt
Action=GUESS
Action_p=/home/ciao.srt
END
BEGIN
Text=ciao.ORIGINAL.srt
SubText=in  /home/p2p/.aegisub/autoback/
Image_file=mimetype:///home/p2p/.aegisub/autoback/ciao.ORIGINAL.srt
Clipboard_data=/home/p2p/.aegisub/autoback/ciao.ORIGINAL.srt
Action=GUESS
Action_p=/home/p2p/.aegisub/autoback/ciao.ORIGINAL.srt
END
Higgins parserà l'output del programma e prenderà tutte le informazioni che gli servono per compilare la lista dei risultati.

Più semplice di così!

Altra cosa che ho aggiunto è che ogni plugin può essere abilitato/disabilitato e gli stessi possono essere ordinati in modo da avere prima i risultati che ci interessano maggiormente.
Nella lista dei plugin vengono ovviamente visualizzati anche i plugin esterni.
« Ultima modifica: 13 Maggio 2016, 13:47:16 da koko »