2017-10-29 3 views
1

몇 가지 질문/답변/블로그를 성공적으로 검색했습니다. irb history에서 중복 명령을 제거/삭제하는 방법은 무엇입니까?irb 기록에서 중복 명령을 제거하는 방법은 무엇입니까?

이상하게도 필자는 bash를 위해 구성된 동일한 동작을 원합니다. 즉 : 명령을 실행 한 후 정확하게 동일한 명령을 사용하여 기록에서 다른 모든 항목을 삭제합니다.

하지만 irb를 닫을 때 중복을 제거하는 것이 좋습니다.

.irbrc 현재 :

require 'irb/ext/save-history' 
IRB.conf[:SAVE_HISTORY] = 1000 
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb_history" 
IRB.conf[:AUTO_INDENT] = true 

참고 : 루비 2.4.1 (! 이상)

답변

0

이 IRB 콘솔을 닫은 후 중복을 제거합니다. 하지만 Readline (Mac 사용자는 경고)을 사용하는 IRB에서만 작동합니다.

# ~/.irbrc 
require 'irb/ext/save-history' 
IRB.conf[:SAVE_HISTORY] = 1000 
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb_history" 

deduplicate_history = Proc.new do 
    history = Readline::HISTORY.to_a 
    Readline::HISTORY.clear 
    history.reverse!.uniq! 
    history.reverse!.each{|entry| Readline::HISTORY << entry} 
end 

IRB.conf[:AT_EXIT].unshift(deduplicate_history) 

그리고 당신의 IRB는 Readline를 사용하는 경우 즉시 중복을 제거합니다이 원숭이 패치 :

require 'irb/ext/save-history' 
IRB.conf[:SAVE_HISTORY] = 1000 
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb_history" 

class IRB::ReadlineInputMethod 
    alias :default_gets :gets 
    def gets 
     if result = default_gets 
      line = result.chomp 
      history = HISTORY.to_a 
      HISTORY.clear 
      history.each{|entry| HISTORY << entry unless entry == line} 
      HISTORY << line 
     end 
     result 
    end 
end 

을 개선하는 방법에 대한 어떤 제안?

0

AT_EXIT 후크는이를 위해 완벽하게 허용되는 방법입니다. 원숭이 패치가 필요하지 않지만. IRB는 자신의 입력 방법을 만들어이 작업을 수행 할 수있는 기능을 제공합니다.

IRB는 입력을 InputMethod에서 가져옵니다. 기록은 하위 클래스 인 ReadlineInputMethod에 의해 제공됩니다.

InputMethodContext에 첨부되어있다. irb 세션에 conf을 입력하면 현재 컨텍스트에 액세스 할 수 있습니다.

irb은 현재 컨텍스트의 io에 따라 입력을 읽습니다. 예 :

용도 my Bash-like history control class (자세한 내용은 아래 참조). InputMethod 인터페이스에 부합 아무것도

당신은 설정할 수 있습니다 conf.io : MyInputMethod#gets 반환 IRB에 의해 평가 될 것입니다 무엇이든

conf.io = MyInputMethod.new 

. 일반적으로 stdin에서 읽습니다.

당신이 :SCRIPT 설정 옵션을 설정할 수 있습니다 시작할 때 당신의 InputMethod를 사용하는 IRB에게 :

# .irbrc 
IRB.conf[:SCRIPT] = MyInputMethod.new 

IRB는 입력 방법 creating a Context:SCRIPT의 값을 사용합니다. 이 파일을 입력 메소드로 사용하기 위해 파일로 설정할 수 있습니다. 기본적으로 nil이며 stdin이 사용됩니다 (가능한 경우 Readline 경유).

는 중복 ReadlineInputMethod#gets을 무시 무시 입력 방법 만들려면 :

class MyInputMethod < IRB::ReadlineInputMethod 
    def gets 
    line = super # super adds line to HISTORY 
    HISTORY.pop if HISTORY[-1] == HISTORY[-2] 
    line 
    end 
end 

my .irbrc에 정의 InputMethod 한 당신 같은 IRB_HISTCONTROL 또는 IRB_HISTIGNORE을 설정할 수 있습니다을 것 배시 (다소) :

IRB_HISTIGNORE=ignoreboth IRB_HISTCONTROL='\Aq!:foo' irb 

다음 작업을 수행합니다.

  • 엔트리가 공백으로 시작 또는 이력에 추가되지 항목이 이력 q! (a custom method of mine)로 시작하거나 foo 함유
  • 항목을 추가 할 수없는 것 (백투백) 복제