<commit_before># -*- coding: utf-8 -*-
require "optparse"
require "yaml"
require "my_help/version"
require 'fileutils'
require "coderay"
require 'colorize'
require 'org2yaml'

module SpecificHelp
  class Command

    def self.run(file,argv=[])
      new(file, argv).execute
    end

    def initialize(file,argv=[])
      @source_file = file
      case File.extname(file)
      when '.yml'
        @help_cont = YAML.load(File.read(file))
      when '.org'
        @help_cont = OrgToYaml(file)
      else
        puts "Not apply on #{file}"
      end
      @help_cont[:head].each{|line| print line.chomp+"\n" } if @help_cont[:head] != nil
      @help_cont[:license].each{|line| print "#{line.chomp}\n" } if @help_cont[:license] != nil
      @argv = argv
    end

    def execute
      if @argv.size==0
        if @source_file.include?('todo')
          @argv << '--all'
        else
          @argv << '--help'
        end
      end
      command_parser = OptionParser.new do |opt|
        opt.on('-v', '--version','show program Version.') { |v|
          opt.version = MyHelp::VERSION
          puts opt.ver
        }
        @help_cont.each_pair{|key,val|
          next if key==:head or key==:license
          opts = val[:opts]
          opt.on(opts[:short],opts[:long],opts[:desc]) {disp_help(key)}
        }

        if ENV['LANG']=='C'
          opt.on('--edit','edit help contents'){edit_help}
          opt.on('--to_hiki','convert to hikidoc format'){to_hiki}
          opt.on('--all','display all helps'){all_help}
          opt.on('--store [item]','store [item] in backfile'){|item| store(item)}
          opt.on('--remove [item]','remove [item] and store in backfile'){|item| remove(item) }
          opt.on('--add [item]','add new [item]'){|item| add(item) }
          opt.on('--backup_list [val]','show last [val] backup list'){|val| backup_list(val)}
        else
          opt.on('--edit','edit help contentsを開く'){edit_help}
          opt.on('--to_hiki','hikiのformatに変更する'){to_hiki}
          opt.on('--all','すべてのhelp画面を表示させる'){all_help}
          opt.on('--store [item]','store [item] でback upをとる'){|item| store(item)}
          opt.on('--remove [item]','remove [item] back upしてるlistを消去する'){|item| remove(item) }
          opt.on('--add [item]','add new [item]で新しいhelpを作る'){|item| add(item) }
          opt.on('--backup_list [val]','back upしているlistを表示させる'){|val| backup_list(val)}
        end

      end
      begin
        command_parser.parse!(@argv)
      rescue=> eval
        p eval
      end
      exit
    end

    def backup_list(val)
      val = val || 10
      print "\n ...showing last #{val} stored items in backup.\n"
      backup=mk_backup_file(@source_file)
      File.open(backup,'r'){|file|
        backup_cont = YAML.load(File.read(backup))
        backup_size = backup_cont.size
        backup_size = backup_size>val.to_i ? val.to_i : backup_size
        backup_keys = backup_cont.keys
        backup_keys.reverse.each_with_index{|item,i|
          break if i>=backup_size
          line = item.to_s.split('_')
          printf("%10s : %8s%8s\n",line[0],line[1],line[2])
        }
      }
    end

    def mk_backup_file(file)
      path = File.dirname(file)
      base = File.basename(file)
      backup= File.join(path,"."+base)
      FileUtils.touch(backup) unless File.exists?(backup)
      return backup
    end

    def store(item)
      if item==nil
        print "spcify --store [item].\n"
        exit
      else
        print "Trying to store #{item}\n"
      end
      backup=mk_backup_file(@source_file)
      unless store_item = @help_cont[item.to_sym] then
        print "No #{item} in this help.  The items are following...\n"
        keys = @help_cont.keys
        keys.each{|key|
          p key
        }
        exit
      end
      p store_name = item+"_"+Time.now.strftime("%Y%m%d_%H%M%S")
      cont = {store_name.to_sym => store_item}
      backup_cont=YAML.load(File.read(backup)) || {}
      backup_cont[store_name.to_sym]=store_item
      File.open(backup,'w'){|file| file.print(YAML.dump(backup_cont))}
    end

    def add(item='new_item')
      print "Trying to add #{item}\n"
      new_item={:opts=>{:short=>'-'+item[0], :long=>'--'+item, :desc=>item},
          :title=>item, :cont=> [item]}
      @help_cont[item.to_sym]=new_item
      File.open(@source_file,'w'){|file| file.print YAML.dump(@help_cont)}
    end

    def remove(item)
      print "Trying to remove #{item}\n"
      store(item)
      @help_cont.delete(item.to_sym)
      File.open(@source_file,'w'){|file| file.print YAML.dump(@help_cont)}
    end

    def edit_help
      p help_file =@source_file
      begin
        p command= "emacs #{help_file}"
        exec command
      rescue => e
        print "\nOption edit is not executable on windows. \n"
        print "Type the following shell command;\n\n"
        print "emacs /home/#{ENV['USER']}/.my_help/#{File.basename(@source_file)}\n\n"
        print "M-x ruby-mode should be good for edit.\n"
      end
    end

    def to_hiki
      @help_cont.each_pair{|key,val|
        if key==:head or key==:license
          hiki_disp(val)
        else
          hiki_help(key)
        end
      }
    end

    def all_help
      @help_cont.each_pair{|key,val|
        if key==:head or key==:license
          val[0]+=":"
          disp(val)
        else
          disp_help(key)
        end
      }
    end

    def hiki_help(key_word)
      items =@help_cont[key_word]
      puts "\n!!"+items[:title]+"\n"
      hiki_disp(items[:cont])
    end

    def hiki_disp(lines)
      lines.each{|line| puts "* #{line}"}  if lines != nil
    end

    def disp_help(key_word)
      print_separater
      items =@help_cont[key_word]
      puts items[:title].magenta
      disp(items[:cont])
      print_separater
    end

    def disp(lines)
      lines.each{|line| puts "* #{line}".blue}
    end

    def print_separater
      print "---\n"
    end
  end
end<commit_msg>update exe dirs<commit_after># -*- coding: utf-8 -*-
require "optparse"
require "yaml"
require "my_help/version"
require 'fileutils'
require "coderay"
require 'colorize'
require 'my_help/org2yml'

module SpecificHelp
  class Command

    def self.run(file,argv=[])
      new(file, argv).execute
    end

    def initialize(file,argv=[])
      @source_file = file
      case File.extname(file)
      when '.yml'
        @help_cont = YAML.load(File.read(file))
      when '.org'
        @help_cont = OrgToYaml.new(file).help_cont
      else
        puts "Not apply on #{file}"
      end
      @help_cont[:head].each{|line| print line.chomp+"\n" } if @help_cont[:head] != nil
      @help_cont[:license].each{|line| print "#{line.chomp}\n" } if @help_cont[:license] != nil
      @argv = argv
    end

    def execute
      if @argv.size==0
        if @source_file.include?('todo')
          @argv << '--all'
        else
          @argv << '--help'
        end
      end
      command_parser = OptionParser.new do |opt|
        opt.on('-v', '--version','show program Version.') { |v|
          opt.version = MyHelp::VERSION
          puts opt.ver
        }
        @help_cont.each_pair{|key,val|
          next if key==:head or key==:license
          opts = val[:opts]
          opt.on(opts[:short],opts[:long],opts[:desc]) {disp_help(key)}
        }

        if ENV['LANG']=='C'
          opt.on('--edit','edit help contents'){edit_help}
          opt.on('--to_hiki','convert to hikidoc format'){to_hiki}
          opt.on('--all','display all helps'){all_help}
          opt.on('--store [item]','store [item] in backfile'){|item| store(item)}
          opt.on('--remove [item]','remove [item] and store in backfile'){|item| remove(item) }
          opt.on('--add [item]','add new [item]'){|item| add(item) }
          opt.on('--backup_list [val]','show last [val] backup list'){|val| backup_list(val)}
        else
          opt.on('--edit','edit help contentsを開く'){edit_help}
          opt.on('--to_hiki','hikiのformatに変更する'){to_hiki}
          opt.on('--all','すべてのhelp画面を表示させる'){all_help}
          opt.on('--store [item]','store [item] でback upをとる'){|item| store(item)}
          opt.on('--remove [item]','remove [item] back upしてるlistを消去する'){|item| remove(item) }
          opt.on('--add [item]','add new [item]で新しいhelpを作る'){|item| add(item) }
          opt.on('--backup_list [val]','back upしているlistを表示させる'){|val| backup_list(val)}
        end

      end
      begin
        command_parser.parse!(@argv)
      rescue=> eval
        p eval
      end
      exit
    end

    def backup_list(val)
      val = val || 10
      print "\n ...showing last #{val} stored items in backup.\n"
      backup=mk_backup_file(@source_file)
      File.open(backup,'r'){|file|
        backup_cont = YAML.load(File.read(backup))
        backup_size = backup_cont.size
        backup_size = backup_size>val.to_i ? val.to_i : backup_size
        backup_keys = backup_cont.keys
        backup_keys.reverse.each_with_index{|item,i|
          break if i>=backup_size
          line = item.to_s.split('_')
          printf("%10s : %8s%8s\n",line[0],line[1],line[2])
        }
      }
    end

    def mk_backup_file(file)
      path = File.dirname(file)
      base = File.basename(file)
      backup= File.join(path,"."+base)
      FileUtils.touch(backup) unless File.exists?(backup)
      return backup
    end

    def store(item)
      if item==nil
        print "spcify --store [item].\n"
        exit
      else
        print "Trying to store #{item}\n"
      end
      backup=mk_backup_file(@source_file)
      unless store_item = @help_cont[item.to_sym] then
        print "No #{item} in this help.  The items are following...\n"
        keys = @help_cont.keys
        keys.each{|key|
          p key
        }
        exit
      end
      p store_name = item+"_"+Time.now.strftime("%Y%m%d_%H%M%S")
      cont = {store_name.to_sym => store_item}
      backup_cont=YAML.load(File.read(backup)) || {}
      backup_cont[store_name.to_sym]=store_item
      File.open(backup,'w'){|file| file.print(YAML.dump(backup_cont))}
    end

    def add(item='new_item')
      print "Trying to add #{item}\n"
      new_item={:opts=>{:short=>'-'+item[0], :long=>'--'+item, :desc=>item},
          :title=>item, :cont=> [item]}
      @help_cont[item.to_sym]=new_item
      File.open(@source_file,'w'){|file| file.print YAML.dump(@help_cont)}
    end

    def remove(item)
      print "Trying to remove #{item}\n"
      store(item)
      @help_cont.delete(item.to_sym)
      File.open(@source_file,'w'){|file| file.print YAML.dump(@help_cont)}
    end

    def edit_help
      p help_file =@source_file
      begin
        p command= "emacs #{help_file}"
        exec command
      rescue => e
        print "\nOption edit is not executable on windows. \n"
        print "Type the following shell command;\n\n"
        print "emacs /home/#{ENV['USER']}/.my_help/#{File.basename(@source_file)}\n\n"
        print "M-x ruby-mode should be good for edit.\n"
      end
    end

    def to_hiki
      @help_cont.each_pair{|key,val|
        if key==:head or key==:license
          hiki_disp(val)
        else
          hiki_help(key)
        end
      }
    end

    def all_help
      @help_cont.each_pair{|key,val|
        if key==:head or key==:license
          val[0]+=":"
          disp(val)
        else
          disp_help(key)
        end
      }
    end

    def hiki_help(key_word)
      items =@help_cont[key_word]
      puts "\n!!"+items[:title]+"\n"
      hiki_disp(items[:cont])
    end

    def hiki_disp(lines)
      lines.each{|line| puts "* #{line}"}  if lines != nil
    end

    def disp_help(key_word)
      print_separater
      items =@help_cont[key_word]
      puts items[:title].magenta
      disp(items[:cont])
      print_separater
    end

    def disp(lines)
      p lines.class
      if lines.kind_of?(Array)
        lines.each{|line| puts "* #{line}".blue}
      else
        print lines
      end
    end

    def print_separater
      print "---\n"
    end
  end
end
{'fraction_non_alphanumeric': 0.15432047329389217, 'fraction_numerical': 0.002333138905091242, 'mean_word_length': 2.264961915125136, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '73298', 'n_tokens_mistral': 4222, 'n_tokens_neox': 4135, 'n_words': 829}