Sha256: 3c7f59e0ece4bf0fc0b287110fcfa47da8f3a05530bd7a813d8b863c51f83627

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

require "json"

module SublimeTextKit
  # Manages Sublime Text session data.
  class Session
    attr_reader :metadata_dir

    def self.home_path
      ENV.fetch "HOME"
    end

    def self.session_path
      "#{home_path}/Library/Application Support/Sublime Text 3/Local/Session.sublime_session"
    end

    def initialize metadata_directory
      @metadata_dir = File.expand_path metadata_directory
    end

    def workspaces
      Dir["#{metadata_dir}/*.sublime-workspace"]
    end

    def rebuild_recent_workspaces
      session = load_session
      return unless session && session["workspaces"] && session["workspaces"]["recent_workspaces"]

      session["workspaces"]["recent_workspaces"] = workspaces
      save_session session
    end

    private

    def load_session
      File.exist?(self.class.session_path) ? JSON.parse(File.read(self.class.session_path)) : {}
    end

    def save_session json
      File.open(self.class.session_path, "w") { |file| file.write JSON.dump(json) }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sublime_text_kit-10.0.2 lib/sublime_text_kit/session.rb
sublime_text_kit-10.0.1 lib/sublime_text_kit/session.rb