Class: AtcoderTools::CLI

Inherits:
Thor
  • Object
show all
Includes:
Log
Defined in:
lib/atcoder_tools/cli.rb

Constant Summary collapse

Prompt =
TTY::Prompt.new

Constants included from Log

Log::LoggerIns

Instance Method Summary collapse

Methods included from Log

#log_debug, #log_error, #log_info, #log_warn

Instance Method Details

#create(contest_name) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/atcoder_tools/cli.rb', line 80

def create(contest_name)
  contest = Contest.new(contest_name)
  contest.check_validity!
  contest.create!
  
  log_info 'successfully created'
end

#delete(contest_name = nil) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/atcoder_tools/cli.rb', line 143

def delete(contest_name=nil)
  unless contest_name
    contest_name = Prompt.select("Choose your submit contest?") do |submit|
      Contest.list.each_with_index do |contest, i|
        submit.choice contest.name
        if Settings.new.current_contest&.name == contest.name
          submit.default i + 1
        end
      end
    end
  end
  FileUtils.rm_rf(contest_name)
  FileUtils.rm_rf(".atcoder/#{contest_name}")      
  puts 'successfully deleted'
end

#language(lang = '') ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/atcoder_tools/cli.rb', line 54

def language(lang='')
  unless ['ruby', 'c++(gcc)'].include?(lang)
    lang = Prompt.select("choose your default language.", ['ruby', 'c++(gcc)'])
  end

  # コマンドが通っているかテスト
  log_info "Checking path ..."
  res = case lang
        when 'ruby'
          !!system('ruby -v')
        when 'c++(gcc)'
          !!system('c++ -v')
        end

  unless res
    raise "Cannot find path of #{lang}. Is `$ #{lang} -v` working?"
  end

  settings = Settings.new
  settings.language = lang
  settings.save!
  puts
  log_info 'Default language successfully changed!'
end

#loginObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/atcoder_tools/cli.rb', line 18

def 
  # 認証情報を書き込むので、gitignoreを設定する
  unless Dir.exist?('.atcoder')
    Dir.mkdir('.atcoder')
  end
  File.open(".atcoder/.gitignore", mode = "w"){|f|
    f.write("*")  # ファイルに書き込む
  }
  loop do
    prompt = TTY::Prompt.new
    username = prompt.ask("What is your atcoder username?", required: true)
    password = prompt.mask("What is your atcoder password?", required: true)
    puts ""
    log_info 'starting test login.'

    if Atcoder.(username, password)
      settings = Settings.new
      settings.username = username
      settings.password = password
      settings.save!
      log_info 'OK!'
      break
    else
      log_error "Your username or password is invalid! try again!"
    end
  end
end

#logoutObject



47
48
49
50
51
# File 'lib/atcoder_tools/cli.rb', line 47

def logout
  settings = Settings.new
  settings.destroy_credentials!
  settings.save!
end

#startObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/atcoder_tools/cli.rb', line 89

def start
  listener = Listen.to('.', ignore: /.atcoder\/.*/) do |modified, added, removed|
    if modified[0]
      contest_name ,task_name = modified[0].split('/')[-2..-1]
      task_name = task_name.scan(/(.*)\..*/)[0][0]

      contest = Contest.new(contest_name)
      # thor と名前空間がかぶっているため
      task = ::Task.new(contest, task_name)
      # 最後に動かしたcontest, taskを記憶
      settings = Settings.new
      settings.current_contest = contest
      settings.current_task = task
      settings.save!

      task.run
    end
  end
  log_info 'started actoder_tools session.'
  listener.start
  sleep
end

#submitObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/atcoder_tools/cli.rb', line 113

def submit
  prompt = TTY::Prompt.new
  # contest_name = prompt.select("Choose your submit contest?", Contest.list.map(&:name))
  contest_name = Prompt.select("Choose your submit contest?") do |submit|
    Contest.list.each_with_index do |contest, i|
      submit.choice contest.name
      if Settings.new.current_contest&.name == contest.name
        submit.default i + 1
      end
    end
  end

  # contestによっては6種類ない可能性がある。
  task_list = ['a', 'b', 'c', 'd', 'e', 'f']
  task_name = prompt.select("Choose your tesk?") do |submit|
    task_list.each_with_index do |task, i|
      submit.choice task
      if Settings.new.current_task&.name == task
        submit.default i + 1
      end
    end
  end

  contest = Contest.new(contest_name)
  # thor と名前空間がかぶっているため
  task = ::Task.new(contest, task_name)
  Atcoder.submit(task)
end