Home Download Buy Blog Forum Support

Small script for man page

Small script for man page

Postby Spationaute on Thu Mar 07, 2013 4:43 pm

Hi everyone,

I've done a little python script (duh!) for showing man page with Sublime 3. Although is not the best code, it is simple. The code I post now is working. If you have any suggestions, you're welcome. And if you want to use the code for another just give me a little credit.

Link to gish code.
https://gist.github.com/Spationaute/5120104

Edit: Remove old code to avoid confusion.
Last edited by Spationaute on Sat Mar 09, 2013 5:09 pm, edited 1 time in total.
Spationaute
 
Posts: 9
Joined: Thu Mar 07, 2013 4:36 pm

Re: Small script for man page

Postby Spationaute on Thu Mar 07, 2013 7:15 pm

For download with keymap for linux (I think that script work on mac to, but not on windows).
http://www.stellaire.ca/outils/fast-man.tar.gz

Just do
Code: Select all
tar -C ~/.config/sublime-text-3/Packages/ -xvf fast-man.tar.gz;

to install

To use, select a word to search in the man and press ctrl+. (ctrl+dot).
You can also just press ctrl+. (ctrl+dot) and type the search.
man have the instruction to search all man section and send you the first section found (not good, I must find an idea)

Edit: Update link and instructions.
Last edited by Spationaute on Sat Mar 09, 2013 5:04 pm, edited 1 time in total.
Spationaute
 
Posts: 9
Joined: Thu Mar 07, 2013 4:36 pm

Re: Small script for man page

Postby skuroda on Thu Mar 07, 2013 7:29 pm

I don't know if you thought about this, but have you considered using view.word() on the selection rather than taking the specific region? It may save you some time so you don't have to actually select the word you want to man. If you want to use the selection if it is not empty, perhaps you can use something like this.

Code: Select all
if view.sel()[0].empty():
    word = view.word(view.sel()[0])
else:
    word = view.sel()[0]


Though depending on where the cursor is, that might return spaces (using view.word()), so you would have to do some additional checking. If you trim and find the string is empty, you could open an input panel and request a term to search for. Anyways, just throwing things out there that may or may not be useful to you.
skuroda
 
Posts: 674
Joined: Fri Oct 05, 2012 8:11 am

Re: Small script for man page

Postby Spationaute on Thu Mar 07, 2013 7:37 pm

Thank you,
I'll add trimming as you suggested and try implement the window asking which page is request if no selection is made.

edit:substracting a "the"
Spationaute
 
Posts: 9
Joined: Thu Mar 07, 2013 4:36 pm

Re: Small script for man page

Postby Spationaute on Thu Mar 07, 2013 10:31 pm

I don't understand why my panel is blank when I search from the input panel text and not when I search from the selected text...
Here the new code:
Code: Select all

import sublime, sublime_plugin
from subprocess import *

class man(sublime_plugin.TextCommand):


   def run(self, edit):
      #Set some global to work whit in
      #other functions
      global aedit
      global window
      global view

      #Get the windows and the view
      aedit=edit
      window = self.view.window()
      view = self.view

      #if the selection is empty, ask for the page
      if view.sel()[0].empty():
         window.show_input_panel("Man search :", "", self.input_ret,None,None)
      else:
         #if search is not empty
         word = view.substr(view.sel()[0])
         self.man_search(aedit,word)


   def input_ret(self,text):
      #if an text is set, search it
      if text:
         self.man_search(aedit,text)

   def man_search(self,edit,to_search):
      text = str(to_search)
      #Open the man panel
      man_panel = window.get_output_panel("man")

      #Wait that the panel is ready
      while man_panel.is_loading():
         pass

      #print the search in console
      print("searching in man: {}".format(text))

      #Command to send
      man_command=["man","-P","cat","-7","-a", text]

      #Open the pipe and redirect stdout to the pipe
      pipe = Popen(man_command,stdout=PIPE,stderr=STDOUT)
      (in_pipe,out_pipe) = pipe.communicate()

      #Show the panel
      window.run_command("show_panel",{"panel":"output.man"})

      #insert text in the man panel
      man_panel.insert(edit, 0, in_pipe.decode("utf-8"))
Spationaute
 
Posts: 9
Joined: Thu Mar 07, 2013 4:36 pm

Re: Small script for man page

Postby skuroda on Fri Mar 08, 2013 3:23 am

When I try to bring up a man page from the input panel, I don't get anything. It is giving me an error on the following line
Code: Select all
      print("searching in man: {}".format(text))

I believe you need to specify an index. Is that the same behavior you were seeing?

Side note, have you consider putting this on a gist or something similar. Would make it easier to share and for me to view. If not, that's fine too.
skuroda
 
Posts: 674
Joined: Fri Oct 05, 2012 8:11 am

Re: Small script for man page

Postby Spationaute on Fri Mar 08, 2013 10:05 pm

I've published a gish at https://gist.github.com/Spationaute/5120104
It's the first time a try this site. :)
Spationaute
 
Posts: 9
Joined: Thu Mar 07, 2013 4:36 pm

Re: Small script for man page

Postby Spationaute on Fri Mar 08, 2013 10:18 pm

Mine, don't show any error. It print in the console (and I try print the man, both worked) But it doesn't print in the panel.
Spationaute
 
Posts: 9
Joined: Thu Mar 07, 2013 4:36 pm

Re: Small script for man page

Postby Spationaute on Fri Mar 08, 2013 11:10 pm

I think I understand why. When using the input, the are a new edit context created...
I can write in the output whit run_command insert, but it's really slow.

So,for now, the best, is to print it in console...
Spationaute
 
Posts: 9
Joined: Thu Mar 07, 2013 4:36 pm

Re: Small script for man page

Postby skuroda on Fri Mar 08, 2013 11:27 pm

Hmm, I just realized (through your gist description), I was testing in ST2 not ST3. Yes it's a change in the API. You might want to take a look at http://www.sublimetext.com/docs/3/porting_guide.html. As a work around you can do something like

Code: Select all
class SimpleInsertCommand(sublime_plugin.TextCommand):
    def run(self, edit, content):
        self.view.insert(edit, 0, content)


Then, at https://gist.github.com/Spationaute/512 ... man-py-L56 do something like

Code: Select all
man_panel.run_command("simple_insert", {"content": in_pipe.decode("utf-8")})


Though I don't know how good/bad the performance would be for that. It may in fact be the same, but it might be worth a try.
skuroda
 
Posts: 674
Joined: Fri Oct 05, 2012 8:11 am

Next

Return to Plugin Announcements

Who is online

Users browsing this forum: No registered users and 5 guests