PageRenderTime 103ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Exercitus_romanorum.rb

https://bitbucket.org/mfocella/nota_nuc
Ruby | 604 lines | 468 code | 81 blank | 55 comment | 63 complexity | 59f9d4eb7d781cd54f1c7bd0a6d3d97c MD5 | raw file
  1. path = File.expand_path("../../db", __FILE__)
  2. $:.unshift path unless $:.include? path
  3. require 'Quarrier.rb'
  4. module Exercitus_romanorum
  5. include Quarrier
  6. ####
  7. GETTINGSTARTED = "\n Getting started.
  8. This program is meant provide you with a quick and easily accessable place to jot down all those thoughts that pop into your head. The program allows you to create multiple sets of notes and move between them at any time using commands, the arrow buttons, or the Control Right key.
  9. In this program each set of notes is called a slate. A slate has a name and its associated text. When you type something into a slate, it will automatically be saved 3 seconds after the last key was hit. To switch between the command line at the top and the maintext area, you just have to hit the tab key. The previous(or p) and next(or n) commands move you from slate to slate. Typing in the name of a slate will bring you right to it. The ls command lists all of your slates and the v command brings you back to the slates maintext. To learn how to create, delete and rename slates using commands use the help command. The program must have 3 slates at any given time.
  10. To get the most out of this program, you should have it start every time your computer starts and leave it running against the right side of the screen. You want the program to be available all the time so that when that thought, idea or note pops into your head you have a place to put it instantly.
  11. This is an ongoing project and I would love to hear any comments you may have. You can e-mail any ideas, opinions or thoughts here: fakemail@google.com This project is called nota_bene and can be found on bitbucket.org which is where you can can post any bug reports, contribute or fork the project.
  12. https://bitbucket.org/mfocella/nota_nuc"
  13. HELPTEXT = "\n Help
  14. The commands included with this program are:
  15. next #I bring you to the next slate
  16. n #I am a shorter way to say next
  17. previous #I bring you to the previous slate
  18. p #I am the short version of previous
  19. viewslate #I bring the a slates text back into view
  20. v #I am an alias for viewslate
  21. exit #I shut the program down
  22. e #I am an alias for exit
  23. version #I show you the version of this program you are running
  24. lastlog #I list the last part of the log file
  25. history #I show you what the last commands you used were
  26. help(This is me)
  27. about #I give you a little more information about this program
  28. demote #I get rid of admin abilities
  29. super #I give admin abilities
  30. rename #I rename a slate
  31. list #I list your slates
  32. ls #I am an alias for list
  33. delete #I delete stuff
  34. d #I delete the command you are viewing
  35. new #I can be used to add new stuff
  36. The above commands have a variety of different ways to be used. To get more information on any of the commands, type in the command name and then help.
  37. examples
  38. list help
  39. delete help
  40. restore help
  41. To lock the program from others from removing and creating new slates, you can go to the general settings window which is accessed by clicking the button at the bottom right of the program.
  42. "
  43. THANKYOUALL = "\n Thank you matz for the lovely language that is ruby. You had me at hello world. This program also would not exist without those who have created such wonderful ruby libraries including ActiveRecord, Shoes, Logger, Sqlite3 and cucumber. The pickax book, Eloquent Ruby, Practical Object Oriented Programming in Ruby, Build Awesome Command Line Applications in Ruby, and Designing with the Mind in Mind have been invaluable to my learning and have given me the knowledge to create this software you are using. Thank You Bell Labs for the transistor! \n\n Anyone who is reading this and using my program, I hope this program makes your life easier. I am working to make this the best it can be. Thank you for your support. \n\n Mom and dad, you are always doing your best in guiding and helping me. I love you. I would like to thank my coworkers at AMEWAS, who inspired me to create such a program. Steve Atkinson, you are amazing. Your existance has brought me joy. You are a great friend. Andrew, I would not be here today if it were not for you. I owe everything to you. I don't think there exists enough lifetimes to show you what you mean to me. For now, lets push forward.
  44. \n\n\n\nMark Focella"
  45. def is_arg_cmd?(arg)
  46. off_limit_args = [ "--slates", "--hosts", "new", "version", "delete", "rename", "lastlog", "history", "next", "previous", "exit", "demote", "super", "help", "about", "viewslate", "list", "ThankYou" ]
  47. off_limit_args.each do |cmd|
  48. if arg =~ /^#{cmd}\s*$/
  49. return true
  50. else
  51. #return nil
  52. end
  53. end
  54. end
  55. def access?
  56. if get_admin_status == 1
  57. if is_admin? == 0
  58. return false
  59. end
  60. end
  61. end
  62. def is_slate?(command)
  63. slates = []
  64. slates = list_slates
  65. slates.each do |slate|
  66. if command =~ /^#{slate}\s*$/
  67. return true
  68. else
  69. #return nil
  70. next
  71. end
  72. end
  73. end
  74. def slatecount
  75. slates = []
  76. slates = list_slates
  77. number = slates.length
  78. return number
  79. end
  80. def cmd_no_ops?(recievecmd, matchcmd)
  81. if recievecmd =~ /^#{matchcmd}\s*$/
  82. return true
  83. else
  84. return false
  85. end
  86. end
  87. def cmd_option2?(recievecmd, matchcmd, flag1, flag2)
  88. if recievecmd =~ /^#{matchcmd}\s*(#{flag1}|#{flag2})\s*$/
  89. return true
  90. else
  91. return false
  92. end
  93. end
  94. #below method isn't being used
  95. def cmd_flag1?(recievecmd, matchcmd, flag1)
  96. if recievecmd =~ /^#{matchcmd}\s*#{flag1}\s*/
  97. return true
  98. else
  99. nil
  100. end
  101. end
  102. #########################
  103. def cmd_argument?(recievecmd, matchcmd, flag1, flag2)
  104. if recievecmd =~ /^#{matchcmd}\s*(#{flag1}|#{flag2})\s.*\s*$/
  105. return true
  106. else
  107. nil
  108. end
  109. end
  110. def cmd_onlytwo_arguments?(recievecmd, matchcmd)
  111. if recievecmd =~ /^#{matchcmd}\s*\S+\s*\S+\s*$/
  112. return true
  113. else
  114. nil
  115. end
  116. end
  117. def cmd_get_arg1(recievecmd)
  118. arg1 = /^\S*\s*(\S*)\s*\S*\s*$/.match(recievecmd).captures
  119. return arg1
  120. end
  121. def cmd_get_arg2(recievecmd)
  122. arg2 = /^\S*\s*\S*\s*(\S*)\s*$/.match(recievecmd).captures
  123. #put in a check here if argument is equal to one of the commands or already existing slatenames
  124. return arg2
  125. end
  126. def cmd_get_argument(recievecmd, matchcmd)
  127. argument = /^#{matchcmd}\s.+\s(.+)\s*$/.match(recievecmd).captures
  128. #put in a check here if argument is equal to one of the commands or already existing slatenames and if so return error can't be cmd or slatename
  129. return argument
  130. end
  131. def arg_has_special?(arg)
  132. arg.match(/\W+/) { return true }
  133. end
  134. def is_arg_slate?(arg)
  135. slates = []
  136. slates = list_slates
  137. slates.each do |slate|
  138. if arg =~ /^#{slate}$/
  139. return true
  140. else
  141. #return nothing
  142. next
  143. end
  144. end
  145. end
  146. def use_cmd
  147. if using_cmd? == 0
  148. set_cmd_view("1")
  149. else
  150. nil
  151. end
  152. end
  153. def admin_check
  154. if is_admin == 0
  155. return "You must be an admin to use this command"
  156. else
  157. end
  158. end
  159. def get_command(command)
  160. #send the command to a file which will be used by the history command and up and down arrows scrolling through old commands that were run !!here
  161. use_cmd
  162. if command.length >= 400
  163. return "The length of the command must be less than 400 characters long"
  164. else
  165. if command =~ /^about/
  166. parse_about(command)
  167. elsif command =~ /^new/
  168. parse_new(command)
  169. elsif command =~ /^delete/
  170. parse_delete(command)
  171. elsif command =~ /^d\s*$/
  172. parse_delete(command)
  173. elsif command =~ /^ls/
  174. parse_ls(command)
  175. elsif command =~ /^list/
  176. parse_list(command)
  177. elsif command =~ /^ThankYou$/
  178. parse_thankyou(command)
  179. elsif command =~ /^rename/
  180. parse_rename(command)
  181. elsif command =~ /^exit/
  182. parse_exit(command)
  183. elsif command =~ /^e\s*$/
  184. parse_exit(command)
  185. elsif command =~ /^help/
  186. parse_help(command)
  187. elsif command =~ /^h\s*$/
  188. parse_help(command)
  189. elsif command =~ /^demote/
  190. parse_demote(command)
  191. elsif command =~ /^history/
  192. parse_history(command)
  193. elsif command =~ /^version/
  194. parse_version(command)
  195. elsif command =~ /^lastlog/
  196. parse_lastlog(command)
  197. elsif command =~ /^next\s*\S+\s*$/ || command =~ /^n\s*\S+\s*$/
  198. next_help
  199. elsif command =~ /^previous\s*\S+\s*$/ || command =~ /^p\s*\S+\s*$/
  200. previous_help
  201. elsif command =~ /^viewslate\s*\S+\s*$/ || command =~ /^v\s*\S+\s*$/
  202. viewslate_help
  203. elsif command =~ /^super\s*\S+\s*$/
  204. super_help
  205. # elsif command =~ /^print/
  206. # parse_print(command)
  207. else
  208. nocommand(command)
  209. #return "\n#{command}\n\n\n This is not one of the commands or slates. Type help if you would like a list of commands. To learn more about this program type about."
  210. end
  211. end
  212. end
  213. private
  214. def nocommand(command)
  215. return "\n#{command}\n\n\n This is not a valid command or slate. Type help if you would like a list of commands. To learn more about this program type about."
  216. end
  217. def super_help
  218. return "\nsuper\n\n\nThis command asks for the administrator password so that you could become an admin. If you are an admin, you will be able to access all the commands and sectons. This command does not take any arguments.\n\nUsage: super"
  219. end
  220. def next_help
  221. return "\nnext\n\n\nThis command brings you to the next slate in your list of slates. It does not take any arguments. \n\nUsage: next\n\n\n\nAlias: n"
  222. end
  223. def previous_help
  224. return "\nprevious\n\n\nThis command brings you to the previous slate in your list of slates. It does not take any arguments. \n\nUsage: previous \n\n\n\nAlias: p"
  225. end
  226. def viewslate_help
  227. return "\nviewslate\n\n\nThis command removes the command output and lets you view the slatetext. It does not take any arguments. \n\nUsage: viewslate\n\n\n\nAlias: v"
  228. end
  229. def parse_version(command)
  230. match = "version"
  231. if cmd_no_ops?(command, match) == true
  232. ver = "\nYou are running Version 0.0.1 of the Slates program which is called gumshoe."
  233. return ver
  234. else
  235. usage = "\nversion\n\n\nThis command displays the version of this program that is running. It does not take any arguments. \n\nUsage: version"
  236. return usage
  237. end
  238. end
  239. def parse_about(command)
  240. match = "about"
  241. if cmd_no_ops?(command, match) == true
  242. return "\n#{GETTINGSTARTED}"
  243. else
  244. return "\nabout\n\n\nThis command gives you information about Slates. It gives you an overview of the functions and features this program offers. If you are new to Slates, I suggest you start here. \n\nUsage: about"
  245. end
  246. end
  247. def parse_new(command)
  248. flag1 = "-s"
  249. flag2 = "--slate"
  250. match = "new"
  251. if access? == false
  252. return "\nYou must be an administrator to run this command."
  253. else
  254. if cmd_no_ops?(command, match) == true
  255. return "\nThe new command requires an option and an argument."
  256. elsif cmd_argument?(command, match, flag1, flag2) == true
  257. @argument = cmd_get_argument(command, match).join("")
  258. if arg_has_special?("#{@argument}") == true
  259. return "\nThe slatename can't contain special characters"
  260. elsif is_arg_slate?("#{@argument}") == true
  261. return "\nThe slate #{@argument} already exists."
  262. elsif @argument =~ /^(..|.)$/
  263. return "\nSlatename must be more than 2 characters long."
  264. elsif is_arg_cmd?(@argument) == true
  265. return "\nYour slate cannot be the name of a command."
  266. else
  267. new_slate("#{@argument}")
  268. return "\nThe slate #{@argument} has been created."
  269. end
  270. else
  271. return "\nnew\n\n\nThis command allows you to create a new slate. \n\nUsage: new -s <slatename> or new --slate <slatename> \n -s stands for slate. You can only use one flag each time this command is run.\nThis command takes only one argument after the flag which is the name of the slate you want to create."
  272. end
  273. end
  274. end
  275. def parse_delete(command)
  276. flag1 = "-s"
  277. flag2 = "--slate"
  278. match = "delete"
  279. match2 = "d"
  280. if access? == false
  281. return "\nYou must be an administrator to run this command."
  282. else
  283. if cmd_no_ops?(command, match) == true || cmd_no_ops?(command, match2) == true
  284. @numofslates = slatecount
  285. if @numofslates <= 3
  286. return "\nYou cannot delete this slate, because there must be at least 3 slates at all times."
  287. else
  288. viewingslate = get_slatename
  289. previous = previousslate
  290. set_viewing("#{previous}")
  291. remove_slate("#{viewingslate}")
  292. return "\nThe slate #{viewingslate} was deleted."
  293. end
  294. elsif cmd_argument?(command, match, flag1, flag2) == true
  295. @argument = cmd_get_argument(command, match).join("")
  296. if arg_has_special?("#{@argument}") == true
  297. return "\nThere is no slate with the name #{@argument}"
  298. elsif is_arg_slate?("#{@argument}") == true
  299. @numofslates = slatecount
  300. if @numofslates <= 3
  301. return "\nYou cannot delete this slate, because there must be at least 3 slates at all times."
  302. end
  303. if viewing?("#{@argument}") == 1
  304. previous = previousslate
  305. set_viewing("#{previous}")
  306. end
  307. remove_slate("#{@argument}")
  308. return "\nThe slate #{@argument} was deleted."
  309. else
  310. return "\nThere is no slate with the name #{@argument}"
  311. end
  312. else
  313. return "\ndelete\n\n\nThis command allows you to delete a slate. \n\nUsage: delete -s <slatename> or delete --slate <slatename> \n -s stands for slate. You can only use one flag each time this command is run. Delete can only take one argument which is the name of the slate you would like to delete. If you want to delete the slate you are viewing, just type delete or d without any arguments."
  314. end
  315. end
  316. end
  317. def parse_ls(command)
  318. match = "ls"
  319. if cmd_no_ops?(command, match) == true
  320. sap = list_slates.join("\n")
  321. @numofslates = slatecount
  322. far = "\n You have #{@numofslates} slates.\n\n\nYour slates include:\n\n #{sap}"
  323. return far
  324. else
  325. usage = "\nls\n\n\nThis command lists the slates. It is a shortcut for list. It doesn't not take any arguments or flags.\n\nUsage: ls"
  326. return usage
  327. end
  328. end
  329. def parse_list(command)
  330. match = "list"
  331. flag1 = "-s"
  332. flag2 = "--slates"
  333. @numofslates = slatecount
  334. if cmd_no_ops?(command, match) == true
  335. sap = list_slates.join("\n")
  336. hap = list_hosts.join("\n")
  337. slates = "\n You have #{@numofslates} slates.\n\n\nYour slates include: \n\n #{sap}"
  338. return slates
  339. elsif cmd_option2?(command, match, flag1, flag2) == true
  340. sap = list_slates.join("\n")
  341. slates = "\n You have #{@numofslates} slates.\n\n\nYour slates include: \n\n #{sap}"
  342. return slates
  343. else
  344. return "\nlist\n\n\nThis command lists the slates you have. \n\nUsage: list [options]\n\t -s, --slates list only slates\n\t \n\n\n The alias for list is: ls"
  345. end
  346. end
  347. def parse_thankyou(command)
  348. return "\n\n\n\n\n#{THANKYOUALL}"
  349. end
  350. def parse_rename(command)
  351. match = "rename"
  352. if access? == false
  353. return "\nYou must be an administrator to run this command."
  354. else
  355. if cmd_no_ops?(command, match) == true
  356. return "\nThe rename command requires two arguments. The first argument must be an existing slatename and the other argument must be the name you want to change it to."
  357. elsif cmd_onlytwo_arguments?(command, match) == true
  358. oldname = cmd_get_arg1(command).join("")
  359. newname = cmd_get_arg2(command).join("")
  360. if newname =~ /^(..|.)$/
  361. return "\nThe slatename must be more than 2 characters long."
  362. elsif newname =~ /\W/
  363. return "\nYou can only use letters, numbers and underscores in the name of a slate."
  364. elsif oldname =~ /^help$/
  365. return "\nrename\n\n\nThis command renames an already existing slate to some other name. \n\nUsage: rename <oldslatename> <newslatename>\n This command must take in the name of an already existing slate and the name you want to change it to."
  366. elsif oldname =~ /\W/
  367. return "\nYou do not have a slate named: #{oldname}."
  368. elsif is_arg_cmd?(newname) == true
  369. return "\nYour slate cannot be the name of a command."
  370. elsif is_arg_slate?(newname) == true
  371. return "\nYou already have a slate called #{newname}."
  372. elsif is_arg_slate?(oldname) == true
  373. slateid = get_slateid("#{oldname}")
  374. set_slatename("#{slateid}", "#{newname}")
  375. return "\nThe slate #{oldname} has changed its name to #{newname}."
  376. else
  377. return "\nThe first argument #{oldname} is not the name of a slate."
  378. end
  379. else
  380. return "\nrename\n\n\nThis command renames an already existing slate to some other name. \n\nUsage: rename <oldslatename> <newslatename>\n This command must take in the name of an already existing slate and the name you want to change it to."
  381. end
  382. end
  383. end
  384. def parse_exit(command)
  385. match = "exit"
  386. if cmd_no_ops?(command, match) == true
  387. return exit
  388. elsif cmd_no_ops?(command, "e") == true
  389. return exit
  390. else
  391. usage = "\nexit\n\n\nThis command exits the program. A shortcut for this is e.\n\nUsage: exit"
  392. return usage
  393. end
  394. end
  395. def parse_help(command)
  396. match = "help"
  397. if cmd_no_ops?(command, match) == true
  398. return "\n#{HELPTEXT}"
  399. elsif cmd_no_ops?(command, "h") == true
  400. return "\n#{HELPTEXT}"
  401. else
  402. usage = "\nType in help if you need assistance."
  403. return usage
  404. end
  405. end
  406. def parse_print(command)
  407. match = "print"
  408. if cmd_no_ops?(command, match) == true
  409. #slatename = get_slatename
  410. #check that a file with the slatename does not exist already and if it does, change slatename to something else so that can print the slate
  411. #File.open("../../log/#{slatename}", "w+") { |f| f.write(set_maintext_viewing)}
  412. #use cups ruby gem to send a print job to the default printer using the file created above
  413. #file_to_print = Cups::PrintJob.new("file")
  414. #file_to_print.print
  415. #return "You have just sent a print job of the current slatetext to the default printer set up by cups"
  416. #send a message to logger and slate.log that print job send for current slate
  417. #until print == successful do nothing then send log message printed successfully and delete the file created for the print
  418. #use a loop to check the status of the print job with file_to_print.statusand if that value equals the completed status remove the print file and end the loop
  419. #use a ruby fork for above?
  420. #make sure that the job printed and if it did, remove the file that was created with the main text
  421. else
  422. return "The print command does not take any arguments"
  423. end
  424. end
  425. def parse_demote(command)
  426. match = "demote"
  427. if cmd_no_ops?(command, match) == true
  428. set_admin("0")
  429. return "\nYou are no longer an administrator"
  430. else
  431. return "\ndemote\n\n\nThis command takes away administrator abilities. \n\nUsage: demote"
  432. end
  433. end
  434. def parse_history(command)
  435. match = "history"
  436. if cmd_no_ops?(command, match) == true
  437. numolines = File.read(File.expand_path("../../log/history.log", __FILE__)).scan(/\n/).count
  438. #numolines = File.read("../log/history.log").scan(/\n/).count
  439. if numolines == 0
  440. return ""
  441. elsif numolines >= 50
  442. read = IO.readlines(File.expand_path("../../log/history.log", __FILE__))[-50..-1]
  443. return read.join("")
  444. else
  445. numolines *= -1
  446. read = IO.readlines(File.expand_path("../../log/history.log", __FILE__))[numolines..-1]
  447. return read.join("")
  448. end
  449. =begin
  450. file.readlines.each do |line|
  451. count = 0
  452. if count <= 39
  453. lines << line
  454. #linetxt = "#{@period}" + "#{line}\n"
  455. #@period = "#{linetxt}"
  456. count += 1
  457. else
  458. linetxt = lines.join("\n\n\n\n")
  459. return "#{linetxt}"
  460. end
  461. =end
  462. else
  463. return "\nhistory\n\n\nThis command gives you a listing of the last 50 commands that were run. It does not take any arguments. \n\nUsage: history"
  464. end
  465. end
  466. def parse_lastlog(command)
  467. match = "lastlog"
  468. if cmd_no_ops?(command, match) == true
  469. numolines = File.read(File.expand_path("../../log/slate.log", __FILE__)).scan(/\n/).count
  470. if numolines == 0
  471. return ""
  472. elsif numolines >= 100
  473. read = IO.readlines(File.expand_path("../../log/slate.log", __FILE__))[-100..-1]
  474. return read.join("")
  475. else
  476. numolines *= -1
  477. read = IO.readlines(File.expand_path("../../log/slate.log", __FILE__))[numolines..-1]
  478. return read.join("")
  479. end
  480. =begin
  481. lines = []
  482. File.open("../log/slate.log", "r") { |file|
  483. @period = ".START\n\n\n\n"
  484. file.readlines.each do |line|
  485. count = 0
  486. if count <= 39
  487. lines << line
  488. #linetxt = "#{@period}" + "#{line}\n"
  489. #@period = "#{linetxt}"
  490. count += 1
  491. else
  492. linetxt = lines.join("\n\n\n\n")
  493. return "#{linetxt}"
  494. end
  495. end
  496. }
  497. =end
  498. else
  499. return "\nlastlog\n\n\nThis command shows you the last few lines of this programs logfile. It does not take any arguments. \n\nUsage: lastlog"
  500. end
  501. end
  502. end
  503. #Developer : Mark Focella (m.focella@gmail.com)
  504. #Date : February 17, 2014