brightpearl-cli 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/core/git.rb CHANGED
@@ -43,6 +43,11 @@ module Brightpearl
43
43
  BRANCH_HAS_STASH = 'branch_has_stash'
44
44
  BRANCH_HAS_UPSTREAM = 'branch_has_upstream'
45
45
 
46
+ SAME_BRANCH_WARNING = 'same_branch_warning'
47
+ SAME_BRANCH_ERROR = 'same_branch_error'
48
+
49
+ RELEASE_BRANCH_REGEX = /release-\d+\.\d+\.\d+-\d{4}/i
50
+
46
51
  def initialize
47
52
 
48
53
  @origin_updated_code = false
@@ -304,6 +309,12 @@ module Brightpearl
304
309
  else
305
310
  brightpearl_code, brightpearl_db = get_remote_branches(sort)
306
311
  end
312
+ show_branches_draw_table(brightpearl_code, brightpearl_db)
313
+ end
314
+
315
+ # Draws the actual table of branches.
316
+ # @return void
317
+ def show_branches_draw_table(brightpearl_code, brightpearl_db)
307
318
  current_code_branch = current_branch_for_repo(Brightpearl::Config.param(Brightpearl::Config::WORKSTATION_PATH_TO_BP_CODE))
308
319
  current_db_branch = current_branch_for_repo(Brightpearl::Config.param(Brightpearl::Config::WORKSTATION_PATH_TO_BP_DB))
309
320
  column_width = [2, 20, 23, 15, 34, 1, 34, 15, 23, 20, 1]
@@ -329,9 +340,6 @@ module Brightpearl
329
340
  db_branch = show_branches_get_db_branch(brightpearl_db, code_branch)
330
341
  if db_branch[:"#{REFNAME}"] != MDASH
331
342
  db_branches_shown << db_branch[:"#{REFNAME}"]
332
- # branch_data = branch_data(code_branch[:"#{REFNAME}"])
333
- else
334
- # branch_data = branch_data(code_branch[:"#{REFNAME}"], CODE)
335
343
  end
336
344
  code_time_adjusted = code_branch[:"#{COMMITTER_DATE}"] - (1 / 24.0)
337
345
  row do
@@ -443,10 +451,10 @@ module Brightpearl
443
451
  # @return void
444
452
  def update_origin(repo_dir = nil)
445
453
  validate_repo_dir(repo_dir)
446
- if @origin_updated_code && repo_dir == Brightpearl::Config.param(Brightpearl::Config::WORKSTATION_PATH_TO_BP_CODE) ||
454
+ unless @origin_updated_code && repo_dir == Brightpearl::Config.param(Brightpearl::Config::WORKSTATION_PATH_TO_BP_CODE) ||
447
455
  @origin_updated_db && repo_dir == Brightpearl::Config.param(Brightpearl::Config::WORKSTATION_PATH_TO_BP_DB) ||
448
456
  @origin_updated_code && @origin_updated_db && repo_dir.nil?
449
- else
457
+
450
458
  Brightpearl::Terminal::output("Updating origin (#{get_repo_shorthand(repo_dir)})")
451
459
  case repo_dir
452
460
  when Brightpearl::Config.param(Brightpearl::Config::WORKSTATION_PATH_TO_BP_CODE)
@@ -511,16 +519,21 @@ module Brightpearl
511
519
 
512
520
  # Checks if you have staged changes in current repo and if so, prompts to stash them.
513
521
  # @return void
514
- def stash_staged_changes
522
+ def check_for_uncommitted_files(automatic = false, force_stash_message = false)
515
523
  repo_loop.each do |repo_dir|
516
524
  results = `cd #{repo_dir} && git status | grep 'Changes '`
517
525
  results = results.split("\n")
518
526
  if results.any?
519
527
  Brightpearl::Terminal::command(['git status'], repo_dir)
520
528
  current_branch = current_branch_for_repo(repo_dir)
521
- if Brightpearl::Terminal::prompt_yes_no("Found uncommitted files on branch #{Brightpearl::Terminal::format_branch(current_branch)}", nil, "Would you like to #{Brightpearl::Terminal::format_action('stash')}\x1B[38;5;89m these changes before continuing?")
529
+ if automatic || Brightpearl::Terminal::prompt_yes_no("Found uncommitted files on branch #{Brightpearl::Terminal::format_branch(current_branch)}", nil, "Would you like to #{Brightpearl::Terminal::format_action('stash')}\x1B[38;5;89m these changes before continuing?")
530
+ Brightpearl::Terminal::output('Stashing working changes', Brightpearl::Terminal::MSG_AUTOMATIC) if automatic
522
531
  Brightpearl::Terminal::command('git stash', repo_dir)
523
532
  else
533
+ if force_stash_message != false
534
+ abort_message = force_stash_message.is_a?(String) ? force_stash_message : nil
535
+ Brightpearl::Terminal::abort(abort_message,["In this particular scenario, you #{Brightpearl::Terminal::format_invalid('cannot continue', true)} unless you stash your changes."], true, false)
536
+ end
524
537
  unless Brightpearl::Terminal::prompt_yes_no('Continue without stashing?', "By selecting #{Brightpearl::Terminal::format_action('Yes')}\x1B[38;5;240m, you're changes will be carried over to the next branch.", nil, false)
525
538
  Brightpearl::Terminal::abort(nil, nil, true, false)
526
539
  end
@@ -531,7 +544,7 @@ module Brightpearl
531
544
 
532
545
  # Checks if you currently have stashed changes for current branch and if so, prompt to un-stash them.
533
546
  # @return void
534
- def check_for_stash
547
+ def check_for_stash(automatic = false)
535
548
  repo_loop.each do |repo_dir|
536
549
  results = check_for_stash_get_stashes(repo_dir)
537
550
  if results.any?
@@ -542,7 +555,8 @@ module Brightpearl
542
555
  stashes_to_skip = 0
543
556
  while results.any?
544
557
  stash_name = results[0].split(/:/).first
545
- if Brightpearl::Terminal::prompt_yes_no("Found the following stash on #{Brightpearl::Terminal::format_branch(current_branch_for_repo(repo_dir))}", "#{results[0]}", "Would you like to #{Brightpearl::Terminal::format_action('apply')}\x1B[38;5;89m this stash now?")
558
+ if automatic || Brightpearl::Terminal::prompt_yes_no("Found the following stash on #{Brightpearl::Terminal::format_branch(current_branch_for_repo(repo_dir))}", "#{results[0]}", "Would you like to #{Brightpearl::Terminal::format_action('apply')}\x1B[38;5;89m this stash now?")
559
+ Brightpearl::Terminal::output('Un-stashing working changes', Brightpearl::Terminal::MSG_AUTOMATIC) if automatic
546
560
  stash_apply_successful = Brightpearl::Terminal::command(["git stash apply #{stash_name}"], repo_dir)
547
561
  if stash_apply_successful[0] == true
548
562
  Brightpearl::Terminal::command("git stash drop #{stash_name}", repo_dir)
@@ -570,7 +584,31 @@ module Brightpearl
570
584
  # @return Array
571
585
  def check_for_stash_get_stashes(repo_dir)
572
586
  results = `cd #{repo_dir} && git stash list | grep 'WIP on #{current_branch_for_repo(repo_dir)}:'`
573
- results = results.split("\n")
587
+ results.split("\n")
588
+ end
589
+
590
+ # Checks if both repos are on same branch and displays either ERROR or WARNING.
591
+ # @return void
592
+ def check_for_same_branch(display = SAME_BRANCH_WARNING)
593
+ branch_code = current_branch_for_repo(Brightpearl::Config.param(Brightpearl::Config::WORKSTATION_PATH_TO_BP_CODE))
594
+ branch_db = current_branch_for_repo(Brightpearl::Config.param(Brightpearl::Config::WORKSTATION_PATH_TO_BP_DB))
595
+ if branch_code != branch_db
596
+ case display
597
+ when SAME_BRANCH_WARNING
598
+ Brightpearl::Terminal::warning("You're on #{Brightpearl::Terminal::format_highlight('2 different branches')}", [
599
+ "#{Brightpearl::Terminal::format_directory(get_repo_shorthand(Brightpearl::Config.param(Brightpearl::Config::WORKSTATION_PATH_TO_BP_CODE)))} is on #{Brightpearl::Terminal::format_branch(branch_code)}",
600
+ "#{Brightpearl::Terminal::format_directory(get_repo_shorthand(Brightpearl::Config.param(Brightpearl::Config::WORKSTATION_PATH_TO_BP_DB)))} is on #{Brightpearl::Terminal::format_branch(branch_db)}"
601
+ ])
602
+ when SAME_BRANCH_ERROR
603
+ else
604
+ Brightpearl::Terminal::error("You're on #{Brightpearl::Terminal::format_highlight('2 different branches')}", [
605
+ "#{Brightpearl::Terminal::format_directory(get_repo_shorthand(Brightpearl::Config.param(Brightpearl::Config::WORKSTATION_PATH_TO_BP_CODE)))} is on #{Brightpearl::Terminal::format_branch(branch_code)}",
606
+ "#{Brightpearl::Terminal::format_directory(get_repo_shorthand(Brightpearl::Config.param(Brightpearl::Config::WORKSTATION_PATH_TO_BP_DB)))} is on #{Brightpearl::Terminal::format_branch(branch_db)}",
607
+ nil,
608
+ 'You cannot run this command unless both of your repos are on the same branch.'
609
+ ])
610
+ end
611
+ end
574
612
  end
575
613
 
576
614
  # Returns an array that include info for BOTH repos. Finds out whether a branch exists and if it's current or not
@@ -614,10 +652,10 @@ module Brightpearl
614
652
  # Check branch exists
615
653
  unless branch_exists(branch_name, repo_dir, Brightpearl::Git::LOCAL)
616
654
  repo_data[:"#{BRANCH_EXISTS_LOCALLY}"] = false
617
- Brightpearl::Terminal::warning("Branch #{Brightpearl::Terminal::format_branch(branch_name)} not found #{Brightpearl::Terminal::format_action('locally')} - (#{get_repo_shorthand(repo_dir)})", nil) if verbose
655
+ Brightpearl::Terminal::output("Branch #{Brightpearl::Terminal::format_branch(branch_name)} not found #{Brightpearl::Terminal::format_action('locally')} in #{Brightpearl::Terminal::format_directory(get_repo_shorthand(repo_dir))}", Brightpearl::Terminal::MSG_WARNING) if verbose
618
656
  Brightpearl::Terminal::output('Checking origin') if verbose
619
657
  unless branch_exists(branch_name, repo_dir, Brightpearl::Git::REMOTE)
620
- Brightpearl::Terminal::warning("Branch #{Brightpearl::Terminal::format_branch(branch_name)} not found #{Brightpearl::Terminal::format_action('remotely')} - (#{get_repo_shorthand(repo_dir)})", nil) if verbose
658
+ Brightpearl::Terminal::output("Branch #{Brightpearl::Terminal::format_branch(branch_name)} not found #{Brightpearl::Terminal::format_action('remotely')} in #{Brightpearl::Terminal::format_directory(get_repo_shorthand(repo_dir))}", Brightpearl::Terminal::MSG_WARNING) if verbose
621
659
  repo_data[:"#{BRANCH_EXISTS}"] = false
622
660
  data << repo_data
623
661
  next
@@ -670,6 +708,7 @@ module Brightpearl
670
708
  # @deprecated
671
709
  def branch_has_upstream(repo_dir, branch_name)
672
710
  validate_repo_dir(repo_dir)
711
+ update_origin(repo_dir)
673
712
  result = `cd #{repo_dir} && git branch -vv | grep 'origin/#{branch_name}'`
674
713
  result == '' ? false : true
675
714
  end
@@ -683,31 +722,8 @@ module Brightpearl
683
722
  result == '' ? false : true
684
723
  end
685
724
 
686
- # Gets upstream info for a branch
687
- # @return hash
688
- def branch_upstream_info(branch_name)
689
- unless @branch_upstream_data
690
- @branch_upstream_data = []
691
- repo_loop.each do |repo_dir|
692
- output = `git for-each-ref --shell --format='
693
- b=%(refname:short)
694
- u=${b}'@{upstream}'
695
- if git rev-parse --verify --quiet "$u" >/dev/null 2>&1; then
696
- test -n "$(git rev-list -n 1 "$u..$b")" &&
697
- echo "$b: has unpushed commits"
698
- else
699
- echo "$b: no upstream configuration" >&2
700
- fi
701
- ' refs/heads | sh`
702
-
703
- @branch_upstream_data << output
704
- end
705
- end
706
- puts @branch_upstream_data
707
- end
708
-
709
725
  # Checks if a branch is up-to-date with its remote
710
- # @return boolea
726
+ # @return boolean
711
727
  def branch_is_up_to_date(repo_dir, branch_name)
712
728
  validate_repo_dir(repo_dir)
713
729
  result_local = `cd #{repo_dir} && git rev-parse #{branch_name}`
@@ -760,12 +776,41 @@ module Brightpearl
760
776
  end
761
777
  end
762
778
 
763
- # Returns short-hand string for repo
779
+ # Returns short-hand string for repo.
764
780
  # @return String
765
781
  def get_repo_shorthand(repo_dir)
766
782
  repo_dir == Brightpearl::Config.param(Brightpearl::Config::WORKSTATION_PATH_TO_BP_CODE) ? 'brightpearl-code' : 'brightpearl-db'
767
783
  end
768
784
 
785
+ # Checks that a branch DOESN'T exist anywhere (local, remote, code and db). If it does, will exit with error.
786
+ # @return void
787
+ def check_branch_does_not_exist_anywhere(branch_name)
788
+ Brightpearl::Terminal::info("Checking that branch #{Brightpearl::Terminal::format_branch(branch_name)} doesn't already exist somewhere")
789
+ if branch_exists_anywhere(branch_name)
790
+ Brightpearl::Terminal::error('Branch already exists', "Cannot continue because a branch named #{Brightpearl::Terminal::format_branch(branch_name)}\x1B[38;5;240m already exists somewhere.", true)
791
+ end
792
+ end
793
+
794
+ # Checks that a branch DOES exist somewhere (local, remote, code or db). If it does not, will exit with error.
795
+ # @return void
796
+ def check_branch_exists_somewhere(branch_name)
797
+ unless branch_exists_anywhere(branch_name)
798
+ Brightpearl::Terminal::error("Branch doesn't exist", "Cannot continue because branch #{Brightpearl::Terminal::format_branch(branch_name)}\x1B[38;5;240m doesn't exist.", true)
799
+ end
800
+ end
801
+
802
+ # Asks if you would like to setup remote tracking. WILL ASSUME that branch doesn't have this (but won't actually check).
803
+ # @return void
804
+ def ask_to_setup_remote_tracking(current_branch, repo_dir)
805
+ tracking_setup_command = "git branch --set-upstream-to=origin/#{current_branch} #{current_branch}"
806
+ if Brightpearl::Terminal::prompt_yes_no('Setup remote tracking?', ["No tracking information found for #{Brightpearl::Terminal::format_branch(current_branch)} on #{Brightpearl::Terminal::format_directory(get_repo_shorthand(repo_dir))}", "By continuing, the script will automatically setup your tracking information with: #{tracking_setup_command}"], Brightpearl::Terminal::MSG_WARNING)
807
+ Brightpearl::Terminal::output("Setting tracking information for this branch to origin/#{current_branch}")
808
+ Brightpearl::Terminal::command(tracking_setup_command, repo_dir)
809
+ else
810
+ Brightpearl::Terminal::warning('No remote tracking setup', "You have not setup remote tracking for branch #{Brightpearl::Terminal::format_branch(current_branch)}", false)
811
+ end
812
+ end
813
+
769
814
  end
770
815
 
771
816
  end
data/lib/core/mysql.rb CHANGED
@@ -4,28 +4,54 @@ module Brightpearl
4
4
 
5
5
  class MySQL
6
6
 
7
- DEFAULT_SCHEMA = 'app'
7
+ DEFAULT_VM_SCHEMA = 'app'
8
+ DEFAULT_EC2_SCHEMA = 'brightpearl'
8
9
 
9
- @database_connection = nil
10
+ @vm_connection = nil
11
+ @ec2_connection = nil
10
12
 
11
13
  @ids_customers = []
12
14
  @ids_shipping = []
13
15
 
14
- def self.get_database_connection(schema = DEFAULT_SCHEMA)
15
- if @database_connection == nil
16
- @database_connection = Mysql.new(
16
+ def self.vm(schema = DEFAULT_VM_SCHEMA)
17
+
18
+ if @vm_connection.nil?
19
+ @vm_connection = Mysql.new(
17
20
  Brightpearl::Config.param(Brightpearl::Config::VM_IP),
18
21
  Brightpearl::Config.param(Brightpearl::Config::VM_MYSQL_USER),
19
22
  Brightpearl::Config.param(Brightpearl::Config::VM_MYSQL_PASSWORD),
20
23
  schema
21
24
  )
22
25
  end
23
- @database_connection
26
+ @vm_connection
27
+
28
+ end
29
+
30
+ def self.ec2(schema = DEFAULT_EC2_SCHEMA)
31
+
32
+ if @ec2_connection.nil?
33
+
34
+ encrypter = Encrypter.new
35
+ host = Brightpearl::Config.param(Brightpearl::Config::EC2_HOST)
36
+ user = Brightpearl::Config.param(Brightpearl::Config::EC2_USER)
37
+ pass = Brightpearl::Config.param(Brightpearl::Config::EC2_PASS)
38
+ if host.nil? || user.nil? || pass.nil? || host == '' || user == '' || pass == ''
39
+ Brightpearl::Terminal::error('EC2 access data not found', ["The command you're trying to run requires access to an EC2 database.", "In order for this to work you will need valid #{Brightpearl::Terminal::format_highlight('access data')}.", "Please speak to #{Brightpearl::Terminal::format_highlight('Albert')} (or team Raptor) for more info."], true)
40
+ end
41
+ @ec2_connection = Mysql.new(
42
+ encrypter.decrypt(host),
43
+ encrypter.decrypt(user),
44
+ encrypter.decrypt(pass),
45
+ schema
46
+ )
47
+ end
48
+ @ec2_connection
49
+
24
50
  end
25
51
 
26
52
  def self.get_random_customer_id
27
53
  unless @ids_customers.any?
28
- customers = Brightpearl::MySQL::get_database_connection.query('SELECT customers_id FROM customers ORDER BY customers_id DESC LIMIT 100')
54
+ customers = Brightpearl::MySQL::vm.query('SELECT customers_id FROM customers ORDER BY customers_id DESC LIMIT 100')
29
55
  customers.each_hash do |row|
30
56
  @ids_customers << row['customers_id']
31
57
  end
@@ -35,7 +61,7 @@ module Brightpearl
35
61
 
36
62
  def self.get_random_shipping_id
37
63
  unless @ids_shipping.any?
38
- ship_methods = Brightpearl::MySQL::get_database_connection.query('SELECT ship_method_id FROM ship_methods ORDER BY ship_method_id DESC LIMIT 100')
64
+ ship_methods = Brightpearl::MySQL::vm.query('SELECT ship_method_id FROM ship_methods ORDER BY ship_method_id DESC LIMIT 100')
39
65
  ship_methods.each_hash do |row|
40
66
  @ids_shipping << row['ship_method_id']
41
67
  end
data/lib/core/terminal.rb CHANGED
@@ -10,6 +10,8 @@ module Brightpearl
10
10
  MSG_INFO = 'info'
11
11
  MSG_WARNING = 'warning'
12
12
  MSG_ERROR = 'error'
13
+ MSG_TODO = 'todo'
14
+ MSG_AUTOMATIC = 'automatic'
13
15
 
14
16
  # Runs a series of commands in the terminal.
15
17
  # @return void
@@ -23,13 +25,13 @@ module Brightpearl
23
25
  end
24
26
  end
25
27
  output = []
26
- if verbose_cd && verbose && commands.any?
27
- puts "\x1B[42m Executing \x1B[0m \x1B[32m\xe2\x86\x92\x1B[0m #{Brightpearl::Terminal::format_command("cd #{location}")}"
28
+ if verbose_cd && verbose && commands.any? && !location.nil?
29
+ puts "\x1B[42m Executing \x1B[0m \x1B[0m\xe2\x86\x92\x1B[0m #{Brightpearl::Terminal::format_command("cd #{location}")}"
28
30
  end
29
31
  if commands.any?
30
32
  commands.each do |command|
31
33
  if verbose
32
- puts "\x1B[42m Executing \x1B[0m \x1B[32m\xe2\x86\x92\x1B[0m #{Brightpearl::Terminal::format_command("#{command}")}"
34
+ puts "\x1B[42m Executing \x1B[0m \x1B[0m\xe2\x86\x92\x1B[0m #{Brightpearl::Terminal::format_command("#{command}")}"
33
35
  end
34
36
  if location.nil?
35
37
  if capture_real_output
@@ -63,13 +65,18 @@ module Brightpearl
63
65
  def self.output(message = 'No message', type = MSG_INFO)
64
66
  case type
65
67
  when MSG_INFO
66
- puts "\x1B[48;5;2m Executing \x1B[0m \x1B[32m\xe2\x86\x92\x1B[0m \x1B[0m#{message}\x1B[0m"
68
+ puts "\x1B[48;5;2m Executing \x1B[0m \x1B[0m\xe2\x86\x92\x1B[0m \x1B[0m#{message}\x1B[0m"
67
69
  when MSG_WARNING
68
- puts "\x1B[48;5;2m Vigilance \x1B[0m \x1B[33m\xe2\x86\x92\x1B[0m \x1B[0m#{message}\x1B[0m"
70
+ puts "\x1B[48;5;202m Warning \x1B[0m \x1B[0m\xe2\x86\x92\x1B[0m \x1B[0m#{message}\x1B[0m"
69
71
  when MSG_ERROR
70
- puts " \x1B[48;5;2m Failure \x1B[0m \x1B[33m\xe2\x86\x92\x1B[0m \x1B[0m#{message}\x1B[0m"
72
+ puts "\x1B[48;5;196m Error \x1B[0m \x1B[0m\xe2\x86\x92\x1B[0m \x1B[0m#{message}\x1B[0m"
73
+ when MSG_TODO
74
+ puts "\x1B[48;5;199m @TODO \x1B[0m \x1B[0m\xe2\x86\x92\x1B[0m \x1B[0m#{message}\x1B[0m"
75
+ when MSG_AUTOMATIC
76
+ puts "\x1B[48;5;96m Automatic \x1B[0m \x1B[0m\xe2\x86\x92\x1B[0m \x1B[0m#{message}\x1B[0m"
71
77
  else
72
- raise RuntimeError "'#{type}' is not a valid type."
78
+ puts "'#{type}' is not a valid Terminal::output() type."
79
+ exit
73
80
  end
74
81
  end
75
82
 
@@ -87,31 +94,15 @@ module Brightpearl
87
94
 
88
95
  # Displays error and exits script by default.
89
96
  # @return void
90
- def self.error(title = nil, message = nil, exit_script = true, preceding_blank_line = true, error_text = 'ERROR')
97
+ def self.error(title = nil, message = nil, exit_script = true, preceding_blank_line = true, error_text = 'Error')
91
98
  if title.nil?
92
99
  title = "It seems you're trying to do something silly."
93
100
  end
94
101
  if preceding_blank_line
95
102
  puts
96
103
  end
97
- puts " \x1B[48;5;196m #{error_text.upcase} \x1B[0m \xe2\x86\x92 #{title}"
98
- puts
99
- unless message.nil?
100
- if message.is_a?(Array)
101
- messages = message
102
- else
103
- messages = message.split("\n")
104
- end
105
- table(:border => false) do
106
- messages.each do |line|
107
- row do
108
- column('', :align => 'left', :width => 4)
109
- column("\x1B[38;5;240m#{line}\x1B[0m", :align => 'left', :width => 180)
110
- end
111
- end
112
- end
113
- puts
114
- end
104
+ puts " \x1B[48;5;196m #{error_text} \x1B[0m \xe2\x86\x92 #{title}"
105
+ parse_messages(message)
115
106
  if exit_script
116
107
  exit
117
108
  end
@@ -126,24 +117,8 @@ module Brightpearl
126
117
  if preceding_blank_line
127
118
  puts
128
119
  end
129
- puts " \x1B[48;5;27m MESSAGE \x1B[0m \xe2\x86\x92 #{title}"
130
- puts
131
- unless message.nil?
132
- if message.is_a?(Array)
133
- messages = message
134
- else
135
- messages = message.split("\n")
136
- end
137
- table(:border => false) do
138
- messages.each do |line|
139
- row do
140
- column('', :align => 'left', :width => 4)
141
- column("\x1B[38;5;240m#{line}\x1B[0m", :align => 'left', :width => 180)
142
- end
143
- end
144
- end
145
- puts
146
- end
120
+ puts " \x1B[48;5;27m Message \x1B[0m \xe2\x86\x92 #{title}"
121
+ parse_messages(message)
147
122
  end
148
123
 
149
124
  # Displays success message.
@@ -155,24 +130,8 @@ module Brightpearl
155
130
  if preceding_blank_line
156
131
  puts
157
132
  end
158
- puts " \x1B[48;5;34m SUCCESS \x1B[0m \xe2\x86\x92 #{title}"
159
- puts
160
- unless message.nil?
161
- if message.is_a?(Array)
162
- messages = message
163
- else
164
- messages = message.split("\n")
165
- end
166
- table(:border => false) do
167
- messages.each do |line|
168
- row do
169
- column('', :align => 'left', :width => 4)
170
- column("\x1B[38;5;240m#{line}\x1B[0m", :align => 'left', :width => 180)
171
- end
172
- end
173
- end
174
- puts
175
- end
133
+ puts " \x1B[48;5;34m Success \x1B[0m \xe2\x86\x92 #{title}"
134
+ parse_messages(message)
176
135
  end
177
136
 
178
137
  # Displays warning message.
@@ -184,24 +143,8 @@ module Brightpearl
184
143
  if preceding_blank_line
185
144
  puts
186
145
  end
187
- puts " \x1B[48;5;202m WARNING \x1B[0m \xe2\x86\x92 #{title}"
188
- puts
189
- unless message.nil?
190
- if message.is_a?(Array)
191
- messages = message
192
- else
193
- messages = message.split("\n")
194
- end
195
- table(:border => false) do
196
- messages.each do |line|
197
- row do
198
- column('', :align => 'left', :width => 4)
199
- column("\x1B[38;5;240m#{line}\x1B[0m", :align => 'left', :width => 180)
200
- end
201
- end
202
- end
203
- puts
204
- end
146
+ puts " \x1B[48;5;202m Warning \x1B[0m \xe2\x86\x92 #{title}"
147
+ parse_messages(message)
205
148
  end
206
149
 
207
150
  # Returns action text in consistent, uniform manner.
@@ -216,7 +159,7 @@ module Brightpearl
216
159
  "\x1B[38;5;40m[#{branch_name}]\x1B[0m"
217
160
  end
218
161
 
219
- # Returns branch name in consistent, uniform manner.
162
+ # Returns command name in consistent, uniform manner.
220
163
  # @return String
221
164
  def self.format_command(command_name)
222
165
  "\x1B[38;5;220m#{command_name}\x1B[0m"
@@ -228,16 +171,26 @@ module Brightpearl
228
171
  "\x1B[38;5;48m#{directory_name}\x1B[0m"
229
172
  end
230
173
 
231
- # Returns branch name in consistent, uniform manner.
174
+ # Returns flag name in consistent, uniform manner.
232
175
  # @return String
233
- def self.format_flag(flag_letter)
234
- "\x1B[38;5;117m-#{flag_letter} flag\x1B[0m"
176
+ def self.format_flag(flag_letter, display_flag_text = true)
177
+ "\x1B[38;5;152m-#{flag_letter}#{display_flag_text ? ' flag' : ''}\x1B[0m"
235
178
  end
236
179
 
237
- # Returns branch name in consistent, uniform manner.
180
+ # Returns action text in consistent, uniform manner.
238
181
  # @return String
239
- def self.format_invalid(invalid_string)
240
- "\x1B[38;5;9m#{invalid_string}\x1B[0m"
182
+ def self.format_highlight(highlighted_text)
183
+ "\x1B[38;5;117m#{highlighted_text}\x1B[0m"
184
+ end
185
+
186
+ # Returns invalid data in consistent, uniform manner.
187
+ # @return String
188
+ def self.format_invalid(invalid_string, capitalize = false)
189
+ if capitalize
190
+ return "\x1B[38;5;196m#{invalid_string.upcase}\x1B[0m"
191
+ else
192
+ return "\x1B[38;5;196m#{invalid_string}\x1B[0m"
193
+ end
241
194
  end
242
195
 
243
196
  # Generates 'filler' string.
@@ -262,24 +215,8 @@ module Brightpearl
262
215
  if preceding_blank_line
263
216
  puts
264
217
  end
265
- puts " \x1B[48;5;56m CONFIRM \x1B[0m \xe2\x86\x92 #{title}"
266
- puts
267
- unless message.nil?
268
- if message.is_a?(Array)
269
- messages = message
270
- else
271
- messages = message.split("\n")
272
- end
273
- table(:border => false) do
274
- messages.each do |line|
275
- row do
276
- column('', :align => 'left', :width => 4)
277
- column("\x1B[38;5;240m#{line}\x1B[0m", :align => 'left', :width => 180)
278
- end
279
- end
280
- end
281
- puts
282
- end
218
+ puts " \x1B[48;5;56m Confirm \x1B[0m \xe2\x86\x92 #{title}"
219
+ parse_messages(message)
283
220
  response = ''
284
221
  until %w[y Y n N x X a A].include? response
285
222
  response = ask(" \x1B[38;5;89m#{confirmation_message} \x1B[0m[y/n]\x1B[90m => \x1B[0m")
@@ -299,34 +236,6 @@ module Brightpearl
299
236
  end
300
237
  end
301
238
 
302
- # Used to display what is about to happen. Prompts for ENTER key before continuing.
303
- # @return void
304
- def self.prompt_enter(text)
305
- puts
306
- if text.is_a?(Array)
307
- text.each do |line|
308
- puts line
309
- end
310
- else
311
- puts text
312
- end
313
- enter_to_continue
314
- end
315
-
316
- # Used to confirm what is about to happen. Prompts for [y/n] key before continuing.
317
- # @return void
318
- def self.prompt_yes(text)
319
- puts
320
- if text.is_a?(Array)
321
- text.each do |line|
322
- puts line
323
- end
324
- else
325
- puts text
326
- end
327
- yes_to_continue
328
- end
329
-
330
239
  # Shows a prompt waiting for user input.
331
240
  # @return string
332
241
  def self.prompt_for_input(text)
@@ -345,42 +254,42 @@ module Brightpearl
345
254
  nil
346
255
  end
347
256
 
348
- # Gives a prompt where ONLY 'Enter' will continue the script, any other key will abort.
349
- # @return void
350
- # @deprecated
351
- def self.enter_to_continue
352
- print "\n\x1B[90mPress \x1B[32mENTER\x1B[90m to continue, any other key to abort\x1B[0m => "
353
- begin
354
- system('stty raw -echo')
355
- response = STDIN.getc
356
- ensure
357
- system('stty -raw echo')
358
- end
359
- if response.chr == "\r"
360
- puts "\n\n"
361
- else
362
- abort("\nScript aborted.\n\n")
363
- end
257
+ # Gets the Terminal width
258
+ # @return Integer
259
+ def self.get_terminal_width
260
+ terminal_size = HighLine::SystemExtensions.terminal_size
261
+ terminal_size[0]
262
+ end
263
+
264
+ # Gets the Terminal height
265
+ # @return Integer
266
+ def self.get_terminal_height
267
+ terminal_size = HighLine::SystemExtensions.terminal_size
268
+ terminal_size[1]
364
269
  end
365
270
 
366
- # Gives a prompt where ONLY 'Y' or 'y' will continue the script, any other key will abort.
271
+ private
272
+
273
+ # Parses messages for various output functions.
367
274
  # @return void
368
- # @deprecated
369
- def self.yes_to_continue
370
- puts "\n"
371
- response = ''
372
- until %w[y n].include? response
373
- response = ask("\x1B[90mWould you like to continue? \x1B[0m[y/n]\x1B[90m => \x1B[0m") { |q| q.limit = 1; q.case = :downcase }
374
- end
375
- case response
376
- when 'y'
377
- puts
378
- return
275
+ def self.parse_messages(message)
276
+ puts
277
+ unless message.nil?
278
+ if message.is_a?(Array)
279
+ messages = message
379
280
  else
380
- puts
381
- output('Aborting script', MSG_ERROR)
382
- puts
383
- exit
281
+ messages = message.split("\n")
282
+ end
283
+ table(:border => false) do
284
+ messages.each do |line|
285
+ line.gsub! "\x1B[0m", "\x1B[38;5;240m" unless line.nil?
286
+ row do
287
+ column('', :align => 'left', :width => 4)
288
+ column("\x1B[38;5;240m#{line}\x1B[0m", :align => 'left', :width => 180)
289
+ end
290
+ end
291
+ end
292
+ puts
384
293
  end
385
294
  end
386
295