Program to indent/un-indent text file code (source code) once line numbers are known

These programs are not designed for use by a new programmer: because if used incorrectly they might damage or alter the wrong files. For a programmer that writes a lot of computer code in text, and has a very limited budget, they may be of great help, save a huge amount of time, and a sore hand from pressing the space or delete button for thousands of times.

The files here are ready to run text-based source files for Ruby. However you may need to input some file-names and other additional information in the later programs.  In the introduction below there is some tutorial videos you can check out if you are new to programming. The listing of programs that write to files don’t seem to be popular: possibly because it is very easy to accidentally destroy or damage the wrong files if you don’t know what you are doing; but these programs can improve your productivity and save time if used correctly.

The GNU General Public License just makes sure you can make use of the programs mentioned here–they are available for free (because that is the way the author and programmer who designed these programs wanted it). Donations are welcome, but no facilities have been made to allow for the processing of those donations… so that will have to be a creative adventure 🙂 it is nice to get a chance to work with other programmers from all over the world.

The reason this indent and un-indent computer code has been produced is that often useful code is copied and pasted into programs, but their indentation does not match the program you are developing.

Another common use of these programs is when parts of your program are hidden away in libraries (for ruby these are called modules).  Because programmers often like to name their modules (which then requires further blocks of code to be indented), it means any code that is pasted into the module must be indented properly or in some cases un-indented.

If you are new to programming: indentation makes it easier to see if begin, if, else, and things like end are properly included into the blocks of code. for example every “begin statement” must have a corresponding “end statement” at the end of a block of code.

While the original code here had the correct indentations, the copy and paste to WordPress did not conserve that indentation–the programs here will still work, but it may be harder to change and modify the code if correct indention is not included–as is the case here.

Introduction: Ruby, command prompt window, and you may need to learn, or understand how DOS commands work.

The file listings below can be copied and pasted into your text “program” file. This document is very long–sorry–but that is simply what code looks like (scary and long).

A number of people from The Venus Project (a group that Gharr is a part of) have suggested they wanted to make a computer game–but most of them seem focused on the visual 3D environment and that is good. However producing code can be less exciting–possibly you will make a retro-text based computer game. While that step might not seem to be significant, it is a start to the concept for a game that can be built on, and improved on. You may also play around with files that hold pictures, and procedural generation of artwork to generate some pictures for your game if you are interested in that–the next step to a visual environment or 3D environment would not seem so far off then.

The code provided here is very basic… it is here to make the production of your software easier.

Programing is a very specialized area, and Gharr is not sure how many in the audience is interested in it. While traditional programs came as “.exe” files, and it is assumed that if it is open source#1 or more correctly for the GNU General Public License here the text-source-code#2 is supplied also. Ruby interprets the code as it runs, so if you copy and paste the stuff into a text file with a “.rb” extension you can simply type “ruby program_name.rb” in the directory that the program is in to run it. You will of course have to down load ruby: http://rubyinstaller.org/ (for 64 bit windows 7 environment Gharr used the “Ruby 2.2.1 (x64)” build… but newer versions might be available when you read this article). You also need to use a DOS window (performs text-based (command line) fucntions). The Dos window is a rather uninteresting window that often has a black color: http://windows.microsoft.com/en-au/windows-vista/open-a-command-prompt-window and it should have come as a part of your computer’s software.

If you are new to programming: a text-based source file is a file containing text that in the case of Ruby is interpreted at run time and changed into something that the computer can understand. The files listed here (that are text also) are also called source files. All programs that programmers write tend to start of as text, but some programmers use a fancy thing called IDE that makes the text all sorts of colors–you may see some of those in tutorial videos for programming. There are also programmers that use a graphical environment, or voice commands for example to write programs–but that will not be the case here.

This is a cool way to learn ruby (if you’re a vegetarian–sorry; as he likes to mention cheese, and bacon a lot): http://youtu.be/WJlfVjGt6Hg  😀

Here is a link for all those Vegan Programmers: http://www.vegansociety.com/

You won’t need interactive ruby (irb) for the programs here, you can use the start command prompt window (a window that also has a black color): http://windows.microsoft.com/en-au/windows-vista/open-a-command-prompt-window  or you can use the one that came with your Ruby download.

If you are new to programming, it would be unwise to use the below programs until you feel confident with programming in Ruby and understand how and why the below programs work,  as working with files can be dangerous if you don’t know what you are doing–the wrong files might end up being damaged.

Generate Line Numbers for your computer program (a text source file); Ruby Computer Code–free, no payment required.

This program will produce a text file that you can read and check out what line number your lines of code sits on. This program is not designed for all cases, sometimes your text files can have invisible characters that might be counted as line numbers–so don’t expect the end result to always be what you expect or what you see when you read the text file. Text files should not store any HTML code (that exists in this WordPress document) so make sure you copy and paste this stuff as plain text.

Being creative Gharr (lol) named this program “line_numbers.rb” but you can name it anything you want when you paste it into your own text file. This program has been tested and does work for plain text source files “.txt” that the programmer and author has used.


#********************************************************************
filename = “Source_file.rb”
line_number_filename=”New_file_with_line_numbers.txt”
#********************************************************************

line_number=0
filepos=0

target01 = open(filename, ‘r’)
target02 = open(line_number_filename, ‘w’)

begin

a=target01.gets
if (a!=nil)
line_number+=1
if line_number>=10000
add_zeros=””
elsif line_number>=1000
add_zeros=”0″
elsif line_number>=100
add_zeros=”00″
elsif line_number>=10
add_zeros=”000″
else
add_zeros=”0000″
end
b=”[“+add_zeros+”#{line_number}]: “+a
target02.write(b)
end
end while (a!=nil)

puts “A file has been produced with line numbers for your program.”
puts “Program is ending… bye…”

target01.close
target02.close

This program adds line numbers to a program’s text source code file, and saves it in a differently named file. Make sure that the differently named file does not already exist. This program is free, $0 cost.
Copyright (C) 2015 Gharr (group)

Primary Contact Points for the Gharr (group): https://gharrhome.wordpress.com/ and this is the first and original place where the code was listed in 2015: https://gharrhome.wordpress.com/2015/03/11/program-to-indentun-indent-text-file-code-source-code-once-line-numbers-are-known/

Secondary Contact Point: https://twitter.com/Gharr_home

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see: http://www.gnu.org/licenses/ .

The terms and conditions for the GNU General Public License are explained here: https://www.gnu.org/copyleft/gpl.html

Indent Text Based Computer Code (source file); Ruby Computer Code–free, no payment required.

Once you have produced the line numbers for your text-based source code (contained in a new “.txt” file of course, if you used the above program); you are ready to indent selected parts. You only use the text file with line numbers as a reference, the Source_file.rb here refers to your original text-based source file for your program–and that is the file that you should use here. The resultant indented file is saved into another file name. The resultant file is the same as a text source file and can be run as a program using Ruby. All that has been added is some space characters.

Gharr… named this text source file “indent.rb” but you can name your text file anything you like. this program could also be used as a very primitive text editor that indents text and may have several other uses also–if modified.

This program has been tested and does work for the text source files the programmer and author used.

An example of input for this program might be you want to insert 3 space characters from lines 5 to 10.

When you run the code you would see something like this

What line would you like to start to add space characters to?         5

After what line would you like to stop adding space characters?    10

How many space characters would you like to insert?                      3

The numbers above would have to be input by you.

#********************************************************************
filename = “Source_File.rb”
line_number_filename=”New_file.rb”
#********************************************************************

line_number=0
filepos=0
number_of_spaces=””

target01 = open(filename, ‘r’)
target02 = open(line_number_filename, ‘w’)

begin
puts “The following program takes adds space characters.”
puts “What line would you like to start to add”
print “space characters to? ”
start_line_number=gets.chomp.to_i
puts “After what line would you like to stop adding”
print “space characters?”
end_line_number= gets.chomp.to_i
print “How many space characters would you like to insert?”
nos=gets.chomp.to_i

puts “Start line = #{start_line_number}”
puts “End line = #{end_line_number}”
puts “Number of Spaces = #{nos}”

print “Are you happy with these values (y/n)? ”
answ=gets.chomp.upcase
end while (answ==”N”)

if nos=start_line_number) and (line_number<=end_line_number))
puts “[#{line_number}] putting space in at start of line..”
b=number_of_spaces+a
else
b=a
end
target02.write(b)
end while (a!=nil)

puts “A file has been produced with the extra indentation you wanted.”
puts “Program is ending… bye…”

target01.close
target02.close

This program indents a section of a program’s text source code file, and saves it in a differently named file. Make sure that the differently named file does not already exist. This program is free, $0 cost.
Copyright (C) 2015 Gharr (group)

Primary Contact Points for the Gharr (group): https://gharrhome.wordpress.com/ and this is the first and original place where the code was listed in 2015: : https://gharrhome.wordpress.com/2015/03/11/program-to-indentun-indent-text-file-code-source-code-once-line-numbers-are-known/

Secondary Contact Point: https://twitter.com/Gharr_home

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see: http://www.gnu.org/licenses/ .

The terms and conditions for the GNU General Public License are explained here: https://www.gnu.org/copyleft/gpl.html

Un-Indent Text Based Computer Code (Source File); Ruby Computer Code–free, no payment required.

Warning: This computer code could delete or damage your existing source file computer code; so make sure that the line_number_filename=”New_file.rb” has a different name from your existing source file. If you are very new to programming: you might want to avoid using this program until you understand exactly how and why it works.

This program could delete characters from your original text-based code file if not used correctly;  so make sure you name the new file  has a different name from the source file,  and check out the resultant file to make sure no computer code in the region you un-indented has been deleted. Code could be damaged if un-indent too much (deleting space characters and computer code) or if you un-indent in the wrong place.

Once you have produced the line numbers for your text-based source code (contained in a new “.txt” file of course, if you used the above program that does this function); you are ready to un-indent selected parts. You only use the text file with line numbers as a reference, the Source_file.rb here refers to your original text-based source file for your program–and that is the file that you should use here. The resultant un-indented file is saved into another file name. The resultant file is the same as a text source file and can be run as a program using Ruby. All that this program should do is to delete is some space characters from the text-based source file and write that modified text-based source to a new file.

Gharr named this program “indent_reduce.rb” but you may name it anything you like when you copy and past it into your program.

An example of input for this program as it runs might be you want to un-indent 3 (space characters) from lines 5 to 10.

When you run the code you would see something like this

What line would you like to start the subtract space characters?                          5

After what line would you like to stop subtracting  space characters?                  10

How many space characters would you like to delete from  start of a line?          3

The numbers above would have to be input by you.

#********************************************************************
filename = “Source_File.rb”
line_number_filename=”New_file.rb”
#********************************************************************

line_number=0
filepos=0
number_of_spaces=””

target01 = open(filename, ‘r’)
target02 = open(line_number_filename, ‘w’)

begin
puts “The following program takes out space characters.”
puts “What line would you like to start the subtract”
print “space characters? ”
start_line_number=gets.chomp.to_i
puts “After what line would you like to stop subtracting”
print “space characters?”
end_line_number= gets.chomp.to_i
print “How many space characters would you like to delete from”
print “start of a line?”
nos=gets.chomp.to_i

puts “Start line = #{start_line_number}”
puts “End line = #{end_line_number}”
puts “Number of Spaces to be deleted = #{nos}”

print “Are you happy with these values (y/n)? ”
answ=gets.chomp.upcase
end while (answ==”N”)

if nos=start_line_number) and (line_number<=end_line_number))
puts “[#{line_number}] deleting space at start of line …”
c=a.slice!(0..nos-1)
b=a
else
b=a
end
target02.write(b)
end while (a!=nil)

puts “A file has been produced with the reduced indentation you wanted.”
puts “Program is ending… bye…”

target01.close
target02.close

This program un-indents (deletes space characters) from a program’s text source code file, and saves it in a differently named file. Make sure that the differently named file does not already exist. If used incorrectly, it may delete non-space characters including the computer code–so make sure you understand exactly how and why this program works before using it. New programmers should avoid using this program. This program is free, $0 cost.
Copyright (C) 2015 Gharr (group)

Primary Contact Points for the Gharr (group): https://gharrhome.wordpress.com/ and this is the first and original place where the code was listed in 2015: : https://gharrhome.wordpress.com/2015/03/11/program-to-indentun-indent-text-file-code-source-code-once-line-numbers-are-known/

Secondary Contact Point: https://twitter.com/Gharr_home

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see: http://www.gnu.org/licenses/ .

The terms and conditions for the GNU General Public License are explained here: https://www.gnu.org/copyleft/gpl.html

Larger Programs: game engines, and source code

This is some information for programmers from The Venus Project that might want to work on a Resource Based Economy game. Many have suggested 4X style games could be modified into some style of game that might be useful to model a Resource Based Economy—so the following information may assist such projects.

It can take some time to figure out how 4X programs work. Storage of information, and using that information is not always obvious. Looking at code can give some clues to how you might program a 4X game. It might also give you some ideas of how you might produce a Resource Based Economy game that has no combat. Newer style games also use procedurally generated content; so you may also want to check out that topic.

Reading code from FreeOrion may require medium to advanced skills in programming.

For The Venus Project, this game may be of interest:

“FreeOrion is a free, open source, turn-based space empire and galactic conquest (4X) computer game being designed and built by the FreeOrion project:” http://freeorion.org/index.php/Main_Page

  1. This is some of the source code from FreeOrion: https://github.com/freeorion/freeorion
  2. Master Tree: https://github.com/freeorion/freeorion/tree/master/Empire

It is interesting to note that FreeOrion uses:

Creative Commons and the GNU General Public License.

Because FreeOrion uses Creative Commons and GNU General Public License, it would indicate the things done here are correct. The artwork (above programs have none) would use a Creative Commons license—artwork might be used in a Resource Based Economy game or simulation; even if it is a text-based game as there would be cover art for the game in most cases to help advertise/promote the game; so people will take an initial interest in the game.

According to FreeOrion;

  • Artistic or creative content is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported license.
  • Game content scripts are also licensed under the GNU General Public License

FreeOrion Game-play

Blade Plays FreeOrion Ep. 2 – Making Things Happen [game-play]: https://youtu.be/2zNMDOghH30

End Notes

If you are interested in making a Resource Based Economy computer game or a computer game about some aspect of The Venus Project, leave a comment here or possibly try to contact Gharr on twitter: https://twitter.com/Gharr_home because such projects are of great interest to Gharr.  If Gharr is unavailable, or you want to work on it yourself, then above this section is a heading called: “Larger Programs: game engines, and source code” that may also assist your project; as a lot of people feel that a 4X game could be easily converted into a Resource Based Economy game.

The author and programmer were going to make these programs CC: http://creativecommons.org/ , but the website mentioned that “CC licenses are appropriate for all types of content you want to share publicly, except software and hardware.” So that is why a GNU General Public License was chosen instead. Feel free to leave comments about this also, but Gharr can not give out legal advice, so you will have to read the websites and possibly consult with your lawyer if you really need to understand what options you have. For Gharr, it simply a matter of wanting to share good ideas and work with the community of programmers out there, and possibly get a chance to make this world a better place.

It may seem silly to apply a GNU General Public License to small programs like the ones here, but it is not. This programmer, and the author has found such simple tools are often snapped together into much larger programs, and the code can suddenly become thousands of lines long–and a very complex machine. However to work in a basic environment like Ruby, small additional programs need to be worked on rather than the large program where the errors might be very difficult to find. More expensive IDE’s might be easier to use with large programs, but on a limited budget–as is assumed if one wants to work with programmers from all over the world–programming does not allow for such advanced tracking of errors in large programs, or the components of large programs.

The tiny programs above were integral (and not separate) in the development in some very large (thousands of lines) resources for game programming. A lot of the code and resources used to develop a program is never seen by the end-user–and the above software tools are actually an important part of many programs that have developed by this programmer.

Stripping the above programs out from their environment might make them seem small and unimportant–but that is not the case, they are extremely important and critical to the design process.

End Notes: Reference Points

1) “Why Open Source misses the point of Free Software:” https://www.gnu.org/philosophy/open-source-misses-the-point.html. This is an interesting discussion, and the programmer hopes everyone here is clear that the software here uses the GNU General Public License.

There is obviously a lot of debate about the word “Open Source,”  when it comes to the GNU General Public License and perhaps it was a mistake to use the word in relation to C++ programs.

The GNU General Public License does mention that the source code should be available to programmers:

“When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things.” ~ ref: https://www.gnu.org/copyleft/gpl.html

The contemplation of How C++ “.exe” files are separated from the text file “.cpp” files, is a little more complicated to discuss. Ruby does not have this issue, as mentioned above–unless you convert the “.rb” files into “.exe” files using a special program. Ruby “.rb” files seems to be a wonderful, and clear way to produce software, and to apply the GNU General Public License to such software so you can get software’s source code for free, and modify it if you wish: http://www.gnu.org/licenses/. Also in 2015, anyone can download Ruby for free and run the programs mentioned here–and that means anyone in this world could potentially make use of, or modify the code that is shown here under the GNU General Public License.

While all that is interesting: working with, and collaborating with other programmers all over this world also sounds quite interesting to this programmer. It would be nice if a free Resource Based Economy game were produced by the community of programmers. There are many poor people out there that might love to play such a game and learn more about this world also.

Many people focus on the “resource” word in Resource Based Economy, and propose MineCraft like games. A Resource Based Economy game can be much wider then that, and could involve a creative process, and educational components, as well as the world-community sharing things, and modifying them.

2) The definition of source code can be found here: https://www.gnu.org/copyleft/gpl.html#section1

———☆☆☆———

Last edited in Mar 2015
Gharr is currently in hiatus: “I miss writing all those articles, and sharing all those great things, and ideas on the internet.” 2015

Shortened link to article: Program to indent/un-indent text file code (source code) once line numbers are known [article]: http://wp.me/p10Tww-3d9

—End of Article—

The Material below this point may, or may not be supported by the article, author, or any of those people or organizations that may be mentioned in the above article.
Any material below this point could be: adverts; endorsements; paid to click sites (some may try to trick you into clicking on them by using misleading graphics and/or words); misleading material; material that is for, or against the above article; or other types of material that is not part of the above article.
In single article view: this article does have an associated “Leave a Reply” and “Enter your comment here…” comment box below this point. You should get used to where this comment box is located before you try to leave a comment for this article.

———☆☆☆———

Advertisements
This entry was posted in Creative/artistic person, GNU General Public Licence, learning & study, Programming & Code and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s