GUI to justify code and replace spaces with tabs
Jul 3, 2012 17:41:00 GMT 1
Post by bigbass on Jul 3, 2012 17:41:00 GMT 1
GUI to justify code/text and replace spaces with tabs
I wrote the back end command line app
and added a front end GUI
makes fixing files easy when you copy some code from a forum post
original file unedited a new file is created with the same name but with a .2tabs suffix
dependency (bacon app)
compile 2tabs and place it in the path
included in this post the second app code
made box the same size as the forum post area for code so
WYSIWYG
' GUI-2tabs.bac
' Include the files for making a GUI.
INCLUDE "hug.bac"
INIT
SUB entry_demo()
'clear the edit box
TEXT(EDIT_BOX, "")
ORIGINAL_FILE$ = GRAB$(My_entry)
file_start$ = "file:///"
'get rid of drag n drop prefixed file://
IF REGEX(ORIGINAL_FILE$ , file_start$) THEN
ORIGINAL_FILE$ = REPLACE$(ORIGINAL_FILE$ , file_start$, "/")
ELSE
ORIGINAL_FILE$ = GRAB$(My_entry)
ENDIF
'the filtered or unfiltered file gets passed here
JUSTIFY$ = CONCAT$("2tabs ", " ", ORIGINAL_FILE$)
SYSTEM CONCAT$(JUSTIFY$," ")
JUSTIFIED_FILE$ = CONCAT$(ORIGINAL_FILE$,".2tabs")
'--- get the display info using a system call ---'
result_input2$ = EXEC$(CONCAT$("cat", " ", JUSTIFIED_FILE$ ))
TEXT(EDIT_BOX, result_input2$)
END SUB
Mainwin = WINDOW( "Justify and Space 2tabs ", 500, 450 )
My_entry = ENTRY("", 450, 30)
ATTACH( Mainwin, My_entry, 20, 40 )
CALLBACK(My_entry, entry_demo)
My_label = MARK( "Drag file name here to Justify ", 300, 30 )
ATTACH( Mainwin, My_label, -10, 10)
FONT(My_label, "sans 12")
My_label = MARK( "Justified text shown in the box", 300, 30 )
ATTACH( Mainwin, My_label, 0, 90)
FONT(My_label, "sans 12")
' formatting changes if you use another label name
My_label = MARK( "You can select text ", 300, 30 )
ATTACH( Mainwin, My_label, -40 ,110)
FONT(My_label, "sans 12")
EDIT_BOX = EDIT(450, 250)
ATTACH( Mainwin, EDIT_BOX, 20, 140 )
My_btn = BUTTON("2tabs", 80, 25)
ATTACH( Mainwin, My_btn, 290, 415 )
CALLBACK( My_btn, entry_demo)
' Create a button.
My_chk = STOCK("gtk-quit", 80, 25)
ATTACH( Mainwin, My_chk, 405, 415 )
' Make button do something
CALLBACK( My_chk, QUIT)
DISPLAY
'--- ***************************************************** '
'--- PROGRAM: 2tabs.bac '
'--- PURPOSE: indent phpBB code '
'--- AUTHOR: big_bass Joe Arose '
'--- DEPENDS: gcc, bacon, '
'--- PLATFORM: linux '
'--- DATE: Nov-28-2011 replaced with TABS$ 7/14/2012'
'--- NOTES: underscores need to be removed to compile code
'--- LICENSE: GPL version 3 or later '
'--- *****************************************************'
'--- this is only for modifing of code with justified indentation
'--- USUAGE: ./2tabs /path-and-filename" ---'
'--- note the file will get a .2tabs added to the filename ---'
'--- on error print usage ---'
'--- this part was added to for variables to be passed at command line ---'
TRAP LOCAL
SPLIT ARGUMENT$ BY " " TO arg$ SIZE dim
IF LEN(arg$[1]) EQ 0 THEN
PRINT "Copy file to /tmp"
PRINT "USUAGE: ./2tabs /path-and-filename"
END IF
Tab$ = CHR$(9)
FOUR_BLANKS$ = SPC$(4)
'--- copy_this$ is the first argument passed from the command line ---'
'--- path-and-filename is arg$[1] this is a valid bacon argument ---'
'--- give it a string name for clarity ---'
copy_this$ = arg$[1]
'--- another code snippet from GatorDog thanks ---'
IF ISFALSE(FILEEXISTS(copy_this$)) THEN
PRINT NL$, "=>> ", copy_this$, " <<== not found."
PRINT "Please check your directory for assistance ;>)", NL$
END FALSE
END IF
COPY copy_this$ TO CONCAT$(copy_this$,".2tabs")
'--- Rename working file to TMP ext.
RENAME CONCAT$(copy_this$,".2tabs") TO CONCAT$(copy_this$,".2tabsTMP")
OPEN CONCAT$(copy_this$,".2tabs") FOR WRITING AS my_outfile
OPEN CONCAT$(copy_this$,".2tabsTMP") FOR READING AS my_infile
WHILE NOT(ENDFILE(my_infile))
READLN txt$ FROM my_infile
'--- pre step replace TABS$ with four spaces ---'
txt$ = REPLACE$(txt$, Tab$, FOUR_BLANKS$)
'--- check if line of text starts with spaces or TABS$ ---'
IF REGEX(txt$, "^ ") OR REGEX(txt$, "^FOUR_BLANKS$") THEN
PRINT "Yep, ", txt$, "--> starts with spaces !"
PRINT
PRINT
full_count = LEN(txt$)
'--- a chop left is used here to remove only left spaces ---'
chopped_count = LEN(CHOP$(txt$," ",1))
start_space_count = full_count - chopped_count
'--- error checking ---'
'--- blue is ok yellow is error with a limit of 43 spaces ---'
IF start_space_count < 43 THEN
PRINT "you have ";
COLOR FG TO BLUE
PRINT start_space_count," leading space(s)"
COLOR RESET
ELSE
PRINT "you have ";
COLOR FG TO YELLOW
PRINT start_space_count," leading space(s)"
COLOR RESET
END IF
'--- start main ---'
IF start_space_count < 3 THEN
PRINT "will chop off this < 3 spaced indented "
PRINT CHOP$(txt$," ",1)
WRITELN CHOP$(txt$," ",1) TO my_outfile
END IF
IF start_space_count >= 3 AND start_space_count <= 6 THEN
PRINT "will shift you to four so it looks good "
'--- pre step replace TABS$ with four spaces ---'
WRITELN Tab$, CHOP$(txt$," ",1) TO my_outfile
END IF
IF start_space_count >= 7 AND start_space_count <= 10 THEN
PRINT "will shift you to eight so it looks good "
WRITELN Tab$, Tab$ ,CHOP$(txt$," ",1) TO my_outfile
END IF
IF start_space_count >= 11 AND start_space_count <= 14 THEN
PRINT "will shift you to twelve so it looks good "
WRITELN Tab$, Tab$ ,Tab$, CHOP$(txt$," ",1) TO my_outfile
END IF
IF start_space_count >= 15 AND start_space_count <= 18 THEN
PRINT "will shift you to sixteen so it looks good "
WRITELN Tab$, Tab$ ,Tab$ ,Tab$, CHOP$(txt$," ",1) TO my_outfile
END IF
IF start_space_count >= 19 AND start_space_count <= 22 THEN
PRINT "will shift you to twenty so it looks good "
WRITELN Tab$, Tab$, Tab$ ,Tab$ ,Tab$, CHOP$(txt$," ",1) TO my_outfile
END IF
IF start_space_count >= 23 AND start_space_count <= 26 THEN
PRINT "will shift you to twenty four so it looks good "
WRITELN Tab$ ,Tab$, Tab$ ,Tab$ ,Tab$ ,Tab$,CHOP$(txt$," ",1) TO my_outfile
END IF
IF start_space_count >= 27 AND start_space_count <= 30 THEN
PRINT "will shift you to twenty eight so it looks good "
WRITELN Tab$, Tab$ ,Tab$ ,Tab$, Tab$, Tab$ ,Tab$, CHOP$(txt$," ",1) TO my_outfile
END IF
IF start_space_count >= 31 AND start_space_count <= 34 THEN
PRINT "will shift you to thirty two so it looks good "
WRITELN Tab$, Tab$ ,Tab$ ,Tab$ ,Tab$, Tab$ ,Tab$,Tab$, CHOP$(txt$," ",1) TO my_outfile
END IF
IF start_space_count >= 35 AND start_space_count <= 38 THEN
PRINT "will shift you to thirty six so it looks good "
WRITELN Tab$, Tab$ ,Tab$ ,Tab$ ,Tab$ ,Tab$ ,Tab$ ,Tab$, Tab$, CHOP$(txt$," ",1) TO my_outfile
END IF
IF start_space_count >= 39 AND start_space_count <= 42 THEN
PRINT "will shift you to forty so it looks good "
WRITELN Tab$, Tab$ ,Tab$ ,Tab$ ,Tab$ ,Tab$, Tab$ ,Tab$ ,Tab$, Tab$ ,CHOP$(txt$," ",1) TO my_outfile
END IF
'--- if over 43 spaces force all 40 spaces ---'
IF start_space_count > 42 THEN
PRINT "ERROR more than 42 spaces indented you have " ;
COLOR FG TO RED
PRINT start_space_count ," indent limit 40 forced !"
COLOR RESET
WRITELN Tab$ ,Tab$ ,Tab$ ,Tab$ ,Tab$ ,Tab$ ,Tab$, Tab$ ,Tab$ ,Tab$,Tab$, CHOP$(txt$," ",1) TO my_outfile
END IF
ELSE
PRINT "no leading spaces "
PRINT txt$
WRITELN txt$ TO my_outfile
WEND
END IF
CLOSE FILE my_infile
CLOSE FILE my_outfile
DELETE FILE CONCAT$(copy_this$,".2tabsTMP")
PRINT
PRINT
PRINT "The indent conversion is done!"
PRINT copy_this$,".2tabs", " <--- is the modified file "