This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TehSomeLuigi's profile picture

Minor Edit to the shell program to allow running of .lua files without typing .lua

Started by TehSomeLuigi, 13 April 2013 - 10:48 PM
TehSomeLuigi #1
Posted 14 April 2013 - 12:48 AM
I know this can be done in Lua, because that's exactly what I did.

But my suggestion is, to make a minor edit to the shell program so that program.lua can be ran by simply typing "program".


This would encourage the usage of the .lua extension, so that people with a syntax highlighting text editor can edit the lua files on their PC without having to manually set the filetype everytime.


This can be changed in ComputerCraft/lua/rom/programs/shell

from

local function run( _sCommand, ... )
local sPath = shell.resolveProgram( _sCommand )
if sPath ~= nil then
  tProgramStack[#tProgramStack + 1] = sPath
	 local result = os.run( tEnv, sPath, ... )
  tProgramStack[#tProgramStack] = nil
  return result
	else
	 printError( "No such program" )
	 return false
	end
end

to


local function run( _sCommand, ... )
		local sPath = shell.resolveProgram( _sCommand )
		local sPath2 = shell.resolveProgram( _sCommand..'.lua' )
		if sPath ~= nil then
				tProgramStack[#tProgramStack + 1] = sPath
				local result = os.run( tEnv, sPath, ... )
				tProgramStack[#tProgramStack] = nil
				return result
		elseif sPath2 ~= nil then
				tProgramStack[#tProgramStack + 1] = sPath2
				local result = os.run( tEnv, sPath2, ... )
				tProgramStack[#tProgramStack] = nil
				return result
		else
		printError( "No such program: " .. _sCommand )
		return false
	end
end


I know it's not necessary, but as I said, it'd make people able to use the .lua extension without having to make the user type more, and code editors would better understand these, and maybe Windows can understand what these files are and not "File"…

And of course it still runs the non-.lua programs with highest priority, so it shouldn't break anything.
ScruffyRules #2
Posted 14 April 2013 - 01:25 AM
what if you have testname.lua and testname, have you tested this?
TehSomeLuigi #3
Posted 14 April 2013 - 01:44 AM
Yes, testname has higher priority, if you want testname.lua you have to explicitly type testname.lua in that situation.
JJRcop #4
Posted 14 April 2013 - 04:34 AM
I like it!
Cranium #5
Posted 14 April 2013 - 05:38 AM
well, considering you can already edit your 'edit' program, I don't see this making into the mod. You have the ability to edit everything in the Lua folder in the computercraft zip. Why would we need to do it for you?
MudkipTheEpic #6
Posted 14 April 2013 - 07:18 AM
Well, I'd think the same reason they added ability to put multiple args in one string on shell.run. For convenience.
Bubba #7
Posted 14 April 2013 - 07:36 AM
I like this suggestion. It's always annoyed me having to either rename the program or write an ugly extension each time I want to run it.

And yes, it is a simple thing to do this yourself, but I see no reason not to include it in the mod just because of that (not to mention the fact that it would require custom patching the shell program every time we redownloaded ComputerCraft or created a new computer on a server). If we were to do everything by the philosophy of "you can do this yourself", then nothing would ever be added because technically you can do anything you want with enough time, skill, and knowledge.
Dlcruz129 #8
Posted 14 April 2013 - 08:03 AM
I like this idea. I know that we're not supposed to suggest "enhancements", but there are always exceptions, such as the keys API.
Bubba #9
Posted 14 April 2013 - 08:13 AM
I like this idea. I know that we're not supposed to suggest "enhancements", but there are always exceptions, such as the keys API.

The enhancements clause makes little or no sense to me. Just because you can do it yourself doesn't mean you always should have to. Things designed to make our lives easier should not be a pain to do yourself - they should be included by default. And since this person has gone to the effort of modifying the code themselves, there is really no work on the mod's part either aside from adding it in, and I cannot imagine that it would be too difficult (although who knows - I may be wrong).

I do not mean to sound disrespectful - I appreciate what the mods have done with ComputerCraft. This is simply my opinion on the enhancements clause.
Engineer #10
Posted 14 April 2013 - 10:56 AM
This would be awesome if this is going to be implemented. No more view -> syntax -> ComputerCraft in Sublime Text 2.
Its really usefull, especially if you are using an editor wich does not remember the last opened files.
Espen #11
Posted 14 April 2013 - 11:43 AM
This would be awesome if this is going to be implemented. No more view -> syntax -> ComputerCraft in Sublime Text 2.
Its really usefull, especially if you are using an editor wich does not remember the last opened files.
You're using Sublime and still have need for the mouse? You're doing it wrong. jk, jk … :P/>
Though with keyboard it takes only ~2 seconds (if you'll use Sublimes' most useful function: The command palette!):
[CTRL]+[SHIFT]+P
L U
[ENTER]

Bam, it's in Lua syntax.
To revert to normal text:

[CTRL]+[SHIFT]+P
P L
[ENTER]

I do use full blown IDEs like Eclipse, but I love the sh*t out of Sublime for writing scripts or any kind of extensive text manipulation. ^_^/>
iownall555 #12
Posted 14 April 2013 - 05:50 PM
Perhaps have this so it checks all extensions instead of just the ".lua" extension. Incase people have custom extensions for their program.

Also I support this enhancement.
TehSomeLuigi #13
Posted 15 April 2013 - 04:21 AM
well, considering you can already edit your 'edit' program, I don't see this making into the mod. You have the ability to edit everything in the Lua folder in the computercraft zip. Why would we need to do it for you?

If you read my post, it makes it more accessible to the users. I already did it for myself thanks, but I thought the convenience should be or everyone if you get what I'm saying.






Perhaps have this so it checks all extensions instead of just the ".lua" extension. Incase people have custom extensions for their program.

Also I support this enhancement.

The reason I suggest the .lua extension is because .lua is the widely-known and recognised extensions (by IDEs/editors) for Lua. If I don't add it my editor usually thinks it's written in Matlab. It's usually a pain to set it for multiple files.
theoriginalbit #14
Posted 15 April 2013 - 04:28 AM
This would be awesome if this is going to be implemented. No more view -> syntax -> ComputerCraft in Sublime Text 2.
If you're using Sublime Text 2, and GravityScores plugin, just make sure it is up to date, and that you have the Auto-Lua syntax plugin from NeverCast installed (you just need to change the settings file in the plugin to say "Packages/ComputerCraft/ComputerCraft.tmLanguage" and its done), there is a link to it in Grav's thread and with changes that I also suggested to Grav at one point it now works flawlessly, I never have to manually set the Syntax.
Cloudy #15
Posted 15 April 2013 - 05:17 AM
I don't have any objection to this persay. However, I do think that this would be very un unix-like - which CC is loosely based on.
Left4Cake #16
Posted 15 April 2013 - 07:21 AM
It would really just be a minor convection since a lot of text editors will only color the code if you have .lua as the file excitation .
iownall555 #17
Posted 15 April 2013 - 11:28 AM
The reason I suggest the .lua extension is because .lua is the widely-known and recognised extensions (by IDEs/editors) for Lua. If I don't add it my editor usually thinks it's written in Matlab. It's usually a pain to set it for multiple files.

Of course. I was just expanding on it a bit.

I don't have any objection to this persay. However, I do think that this would be very un unix-like - which CC is loosely based on.

Doesn't Unix support running files this way? Or is that just something that's part of Windows?
Engineer #18
Posted 15 April 2013 - 11:42 AM
It is based on Unix, that will not say it must be similair to Unix. My opinion is: its just a small change, and it is very easy to implement since the OP provides the code for it.

I know Im just some random person saying this, but this really should be implemented, it can be confusing for new programmars.

It wont bother me at all, god bless ST2 and GravityScore and NeverCast for their plugins, but its still a nice feature. But Im thinking for those who are for example editing in np++ or np by itself.

This, again, is an opinion of me and ofcourse can be scrapped anyhow. But please, consider this, talk to dan for his opinion if needed. I know you can decide yourself, but still! :)/>
Shuudoushi #19
Posted 15 April 2013 - 09:52 PM
I think this would be great to add to CC as well, simple, clean and all the work of figuring out how to do it done already.
Nina #20
Posted 15 April 2013 - 10:27 PM
I don't have any objection to this persay. However, I do think that this would be very un unix-like - which CC is loosely based on.

Doesn't Unix support running files this way? Or is that just something that's part of Windows?
No. This is a feature in DOS/Windows as far as I know.
By default, just typing a program's name even if it's in the current directory will run the one in $PATH, for example if I have a directory containing 'test.sh' and 'test', it will actually run the shell's 'test' command. Typing ./test will run 'test' in the current directory. Now if I were to delete 'test' and type ./test I would get an error that test doesn't exist.

%nhp:test> ls
test  test.sh
%nhp:test> test
%nhp:test> which test
test: shell built-in command
%nhp:test> ./test
test
%nhp:test> rm test
%nhp:test> ./test
zsh: ingen sådan fil eller filkatalog: ./test
%nhp:test> ./test.sh
test