Hacking rails.vim to work with Padrino - ruby

I recently cloned rails.vim (vim-rails) hoping to modify it to work with Padrino projects.
Currently I'm trying to get the Rcontroller command to look not only in app/controllers (perfect for rails) but also in any folder in the project that has a sub-folder called 'controllers'. So when I type Rcontroller in command-mode and hit tab, I should be able to tab through admin/controllers/base.rb, admin/controllers/accounts.rb, app/controllers/events.rb etc. This will let users of the plugin to jump to controllers in a 'subapp' of a Padrino application. e.g. PADRINO_ROOT/admin
The current controllerList function seems to handle this autocompletion and here's what I have so far (only slightly modified from the original source)
function! s:controllerList(A,L,P)
let con = padrino#app().relglob("*/controllers/","**/*",".rb")
call map(con,'s:sub(v:val,"_controller$","")')
return s:autocamelize(con,a:A)
endfunction
I added the wildcard before the controllers directory but this gives results like
Rcontroller ers/base
Rcontroller ers/sessions
Rcontroller s/events
for the last one it looks like there is somethings weird going on with string lengths or overlap...
Ideally I'd like to get it to the point where typing Rcontroller admin<TAB> should result in autocompletion to Rcontroller admin/controllers/accounts.rb. Likewise, Rcontroller app<TAB> should result in Rcontroller app/controllers/events.rb
The code for the viewList function has something similar to this and its code is as follows:
function! s:viewList(A,L,P)
let c = s:controller(1)
let top = padrino#app().relglob("app/views/",s:fuzzyglob(a:A))
call filter(top,'v:val !~# "\\~$"')
if c != '' && a:A !~ '/'
let local = padrino#app().relglob("app/views/".c."/","*.*[^~]")
return s:completion_filter(local+top,a:A)
endif
return s:completion_filter(top,a:A)
endfunction
Anyone have any suggestions? Thanks in advance.

You probably want the full path to look like this:
**/controllers/**/*.rb
which globs as "look under any directory for a directory called controllers, then look anywhere under that for a file ending in .rb"
Looking at other usages of "relglob", I can only guess at how it's supposed to work, but my guess is:
first param is "which directory to start looking in"
second param is "how to multiply out the directories from there"
third param is "actual files that will match"
based on this assumption, my guess would be to use:
padrino#app().relglob("app/","**/controllers/**/*",".rb")
Caveat: this is based on my understanding of glob, not of vim or relglob
adjust as per actual usage.
Note: have added "app/" in the assumption that you're unlikely to want to be tabbing through any controllers under vendor/plugin or vendor/gems. This may not be the case, in which case, feel free to change it to "."

Related

Shell help text syntax for repeatable group of arguments

I'm writing a help output for a Bash script. Currently it looks like this:
dl [m|r]… (<file>|<URL> [m|r|<index>]…)…
The meaning that I'm trying to convey (and elsewhere describe with words) is that (after a potential "m" and/or "r") there can be an endless list of sets of arguments. The first argument in each set is always a file or URL and the further arguments can each be "m", "r" or a number. After that, it starts over with a file or URL and so on.
In my special case, I could just write this:
dl [m|r]… (<file>|<URL>) (<file>|<URL>|m|r|<index>)…
This works, because listing a URL and then another URL with nothing in between is allowed, as well as listing an arbitrarily long chain of "m"s (it's just useless to do so) and pretty much any other combination.
But what if that wasn't the case? What if I had for example a command like this:
change (<from> <to>)…
…which would be used e.g. like this:
change from1 to1 from2 to2 from3 to3
Would the bracket syntax be correct here? I just guessed it based on the grouping of (a|b), but I wasn't able to find any documentation that uses this for multiple, non-exclusive arguments that belong together. Is there even a standard for this?

What is a good schema for naming screenshots in selenium webdriver + capyabara

First pass: I name my screenshot "x".
Obviously that minimal setup only allows for 1 screenshot
I want to name the screenshots in a way that makes them unique and also reflect the usage.
I can make the filenname fairly unique with
output_directory = 'screenshots'
time = Time.new
page.save_screenshot("#{output_directory}/#{time}.png")
It's a bit ugly but I get
$ ls screenshots/
'019-04-13 07:07:50 -0400.png''
What would be good format to use that would meet the requirements of both unique and also descriptive. Could I include the scenario description somehow?
How could I end up with something like:
scenario_decsription_2019_04_19-08_55_20
The RSpec test definition methods and hooks (scenario, before, after, etc) all receive an optional parameter which is the test example itself. This allows you to get the description of the test, etc for use in naming your file
scenario "my test" do |example|
...
page.save_screenshot("#{example.full_description}.png")
end
Obviously you could transform the description in any way you want (convert spaces to underscores, etc).
Note: you may also want to look at Capybara.save_path which specifies what directory screenshots are stored in, if you don't want to prepend screenshots/ everywhere.
How about something like this?
scenario "#{scenario = 'user_clicks_the_dropdown'}" do
output_directory = 'screenshots'
time = Time.new
suffix = time.to_s(:db).gsub(/[\:\.\_\s]/,'_')
page.save_screenshot("#{output_directory}/#{scenario}_#{suffix}.png")
end

How to add link to source code in Sphinx

class torch.FloatStorage[source]
byte()
Casts this storage to byte type
char()
Casts this storage to char type
Im trying to get some documentation done, i have managed to to get the format like the one shown above, But im not sure how to give that link of source code which is at the end of that function!
The link takes the person to the file which contains the code,But im not sure how to do it,
This is achieved thanks to one of the builtin sphinx extension.
The one you are looking for in spinx.ext.viewcode. To enable it, add the string 'sphinx.ext.viewcode' to the list extensions in your conf.py file.
In summary, you should see something like that in conf.py
extensions = [
# other extensions that you might already use
# ...
'sphinx.ext.viewcode',
]
I'd recommend looking at the linkcode extension too. Allows you to build a full HTTP link to the code on GitHub or such like. This is sometimes a better option that including the code within the documentation itself. (E.g. may have stronger permission on it than the docs themselves.)
You write a little helper function in your conf.py file, and it does the rest.
What I really like about linkcode is that it creates links for enums, enum values, and data elements, which I could not get to be linked with viewcode.
I extended the link building code to use #:~:text= to cause the linked-to page to scroll to the text. Not perfect, as it will only scroll to the first instance, which may not always be correct, but likely 80~90% of the time it will be.
from urllib.parse import quote
def linkcode_resolve(domain, info):
# print(f"domain={domain}, info={info}")
if domain != 'py':
return None
if not info['module']:
return None
filename = quote(info['module'].replace('.', '/'))
if not filename.startswith("tests"):
filename = "src/" + filename
if "fullname" in info:
anchor = info["fullname"]
anchor = "#:~:text=" + quote(anchor.split(".")[-1])
else:
anchor = ""
# github
result = "https://<github>/<user>/<repo>/blob/master/%s.py%s" % (filename, anchor)
# print(result)
return result

Photoshop script .DS_Store

I'm using Photoshop script. I get files from folders. My problem is that when I get the files and place them in an array the array contains hidden files that are in the folder for example ".DS_Store". I can get around this by using:
if (folders[i] != "~/Downloads/start/.DS_Store"){}
But I would like to use something better as I sometimes look in lots of folders and don't know the "~/Downloads/start/" part.
I tried to use indexOf but Photoshop script does not allow indexOf. Does anybody know of a way to check if ".DS_Store" is in the string "~/Downloads/start/.DS_Store" that works in Photoshop script?
I see this answer but I don't know how to use it to test: Photoshop script to ignore .ds_store
For anyone else looking for a solution to this problem, rather than explicitly trying to skip hidden files like .DS_Store, you can use the Folder Object's getFiles() method and pass an expression to build an array of file types you actually want to open. A simple way to use this method is as follows:
// this expression will match strings that end with .jpg, .tif, or .psd and ignore the case
var fileTypes = new RegExp(/\.(jpg|tif|psd)$/i);
// declare our path
var myFolder = new Folder("~/Downloads/start/");
// create array of files utilizing the expression to filter file types
var myFiles = myFolder.getFiles(fileTypes);
// loop through all the files in our array and do something
for (i = 0; i < myFiles.length; i++) {
var fileToOpen = myFiles[i];
open(fileToOpen);
// do stuff...
}
For anybody looking I used the Polyfill found here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
indexOf() was added to the ECMA-262 standard in the 5th edition; as
such it may not be present in all browsers. You can work around this
by utilizing the following code at the beginning of your scripts. This
will allow you to use indexOf() when there is still no native support.
This algorithm matches the one specified in ECMA-262, 5th edition,
assuming TypeError and Math.abs() have their original values.

how to pass parameters to a Matlab GUI file

i am new to matlab. While working through the Matlab GUI, i faced a problem which is as follows..i want to have 2 figure files, with one figure file calling the other. i know that just by calling the name of the 2nd fig file from the first fig file, we can call the 2nd figure. however, i also wish to send some parameters from one fig file to another.here i need to send the arguments and also obtain these parameters so as to do further processing.i havent been able to find a solution to this problem. i would be glad if someone helps me out with this problem. thanking you in advance
There are three ways I found to do this:
Method 1: Use setappdata and getappdata like so:
setappdata(0,'some_var',value)
some_other_var = getappdata(0,'some_var')
You would use setappdata() in the m-file for fig1 to store whatever data you wanted to pass around, and then call getappdata() in another m-file to retrieve it. The argument 0 to the two functions specifies the MATLAB root workspace, which is accessible by your program everywhere (i.e. it is global). As such, when you close your figures that data will still be available. You may want to use rmappdata to remove them.
Method 2: Use guidata:
Assuming you created your GUI with GUIDE, then you have access to a structure called handles which is passed around everywhere and which you can edit, and so you can do this in a GUI callback:
handles.some_var = some_value
guidata(hObject,handles)
Then you can access handles.some_var elsewhere in some other callback (because handles is automatically passed into it for you) in your other m-file:
some_other_var = get(handles.some_var)
Method 3: Use UserData:
Store the variable you want from your first figure:
set(name_of_fig, 'UserData', some_var)
Then to get it from your other one:
some_other_var = get(name_of_fig, 'UserData')
(Disclaimer: My actual knowledge of MATLAB is not all that great, but it helps to be able to find good resources like this and this, and even this from the official docs. What I've written here may be wrong, so you should definitely consult the docs for more help.)
I would do like this (assuming you're using the GUI builder GUIDE).
Let's say that your figures/m-files are named firstFigure.fig/m and secondFigure.fig/m. In the code of firstFigure, just call secondFigure and pass your parameters as arguments:
someNumber = 1;
someText = 'test';
aMatrix = rand(3);
secondFigure(someNumber, someText, aMatrix);
The arguments will be available to secondFigure as a variable varargin in the callback functions
function varargout = secondFigure(varargin)
and
function secondFigure_OpeningFcn(hObject, eventdata, handles, varargin)
varagin is a cell structure; use cell2mat and char to convert it back:
theNumber = cell2mat(varargin(1));
theText = char(varargin(2));
theTextAgain = cell2mat(varargin(2));
theMatrix = cell2mat(varargin(3));
This may help:
http://www.mathworks.ch/matlabcentral/newsreader/view_thread/171989
The easiest method is to wrap the parameters in a cell array and send them directly to the GUI constructor. A call with two parameters might look like:
figure2({param1, param2})
Then you can unpack the arguments in the opening function (figure2_OpeningFcn) with code like:
handles.par1 = varargin{1}{1};
handles.par2 = varargin{1}{2};
These lines must be placed somewhere before the line that says guidata(hObject, handles);. Then you can access handles.par1 and handles.par2 directly in all the other callbacks.
I assume you are using GUIDE to generate your GUI. You can find figure2_OpeningFcn in figure2.m which will be located in the same directory as figure2.fig.
Note: you can also return values from a figure, returnvalue = my_figure({my_input}). If you'd like instructions on that too, leave a comment and I'll extend my answer.

Resources