Sha256: e4046d0b929dd5c19c3a03aada14ba489edbfea8e2f8db267ad35b6704abffe3

Contents?: true

Size: 1.24 KB

Versions: 12

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

class Gitlab::Shell
  class History
    DEFAULT_HISTFILESIZE = 200
    DEFAULT_FILE_PATH = File.join(Dir.home, '.gitlab_shell_history')

    def initialize(options = {})
      @file_path = options[:file_path] || DEFAULT_FILE_PATH
      Readline::HISTORY.clear
    end

    def load
      read_from_file { |line| Readline::HISTORY << line.chomp }
    end

    def save
      lines.each { |line| history_file&.puts line }
    end

    def push(line)
      Readline::HISTORY << line
    end
    alias << push

    def lines
      Readline::HISTORY.to_a.last(max_lines)
    end

    private

    def history_file
      @history_file ||= File.open(history_file_path, 'w', 0o600).tap do |file|
        file.sync = true
      end
    rescue Errno::EACCES
      warn 'History not saved; unable to open your history file for writing.'
      @history_file = false
    end

    def history_file_path
      File.expand_path(@file_path)
    end

    def read_from_file(&block)
      path = history_file_path

      File.foreach(path, &block) if File.exist?(path)
    rescue StandardError => e
      warn "History file not loaded: #{e.message}"
    end

    def max_lines
      (ENV['GITLAB_HISTFILESIZE'] || DEFAULT_HISTFILESIZE).to_i
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
gitlab-5.1.0 lib/gitlab/shell_history.rb
gitlab-5.0.0 lib/gitlab/shell_history.rb
gitlab-4.20.1 lib/gitlab/shell_history.rb
gitlab-4.20.0 lib/gitlab/shell_history.rb
fs-gitlab-4.19.3 lib/gitlab/shell_history.rb
fs-gitlab-4.19.2 lib/gitlab/shell_history.rb
fs-gitlab-4.19.1 lib/gitlab/shell_history.rb
gitlab-4.19.0 lib/gitlab/shell_history.rb
fs-gitlab-4.18.2 lib/gitlab/shell_history.rb
fs-gitlab-4.18.1 lib/gitlab/shell_history.rb
gitlab-4.18.0 lib/gitlab/shell_history.rb
gitlab-4.17.0 lib/gitlab/shell_history.rb