Sublime Forum

Recording edited files

#1

At work we use a prehistoric version-control system called “write the name (and path) of any file you edit down in a .txt document”. I was just wondering if it was possible to let sublimetext record the files I edit, so that at then end of the day I could get a list somehow, and just paste that into the .txt document we use.

And I can already hear peoples screaming that we should use git or whatever, and I couldn’t agree more, but it’s not my decision to make.

0 Likes

#2

[code]import sublime_plugin
import time

log_location = “c:/a/EditedFiles.txt”

class EditedFiles(sublime_plugin.EventListener):

def on_post_save(self, v):
date = time.strftime(’%Y-%m-%d %H:%M:%S’, time.gmtime(time.time()))
open(log_location,“a+b”).write((date+"\t"+v.file_name()+"\n").encode(‘utf-8’))[/code]

Save that into a file called “EditedFiles.py” under the package folder.

0 Likes

#3

Great! But is there a post-close event? Cause if it records every time I save the file’s gonna grow like crap. I’m in the habit of hiting Ctrl+S for every statement I write…

0 Likes

#4

Here is the api reference sublimetext.com/docs/2/api_reference.html

I’m not sure what you exactly need.
You may use the following to save a list of unique modified files.

[code]import sublime_plugin

log_location = “c:/a/EditedFiles.txt”

class EditedFiles(sublime_plugin.EventListener):

def on_post_save(self, v):
data = open(log_location,“r+b”).read().split("\n");
data.append(v.file_name().encode(‘utf-8’))
data = list(set(data))
data.sort()
open(log_location,“w+b”).write("\n".join(data));[/code]

0 Likes

#5

Thanks a lot. That looks exactly like what I need :smile:
I really appreciate it :smile:

0 Likes

#6

Just thought I’d post this in case anybody else wanted something of the same (might even throw up a short blog-post at some later point). Anyways; this rocked:

import sublime_plugin
import datetime
import os

log_location = "C:/Users/Aleksander Heintz/Documents/changes/_filename_.txt"
line_feed = "\r\n"

class EditedFiles(sublime_plugin.EventListener):

	def on_post_save(self, v):
		log_file = log_location.replace("_filename_", datetime.datetime.now().strftime("%Y-%m-%d"))
		if not os.path.isfile(log_file):
			open(log_file,"w+") #create if needed
		data = open(log_file,"r+b").read().split(line_feed);
		data.append(v.file_name().encode('utf-8'))
		data = list(set(data))
		data.sort()
		open(log_file,"w+b").write(line_feed.join(data).strip());
0 Likes

#7

Just out of curiosity, how hard would it be to convert this to a sublime-plugin package that could be configurable (in such a way that the user could easily set the log-location)?

0 Likes

#8

Something like this should work to get/set the value in a settings file:

        s = sublime.load_settings("MyPlugin.sublime-settings")
        log_location = s.get("log_location", "C:/Users/Aleksander Heintz/Documents/changes/_filename_.txt")
        s.set("log_location", log_location)
        sublime.save_settings("MyPlugin.sublime-settings")

The second parameter in the get call is the default value if it doesn’t exist in the settings file (or if the settings file doesn’t exist). This will save a file called “MyPlugin.sublime-settings” in the User sub-directory of your Packages directory.

If you want to piggyback the main user settings file, then you can try something like this:

        s = sublime.load_settings("Preferences.sublime-settings")
        log_location = s.get("log_location", "C:/Users/Aleksander Heintz/Documents/changes/_filename_.txt")
        s.set("log_location", log_location)
        sublime.save_settings("Preferences.sublime-settings")
0 Likes

#9

[quote=“jbjornson”]
If you want to piggyback the main user settings file, then you can try something like this:

s = sublime.load_settings("Preferences.sublime-settings") log_location = s.get("log_location", "C:/Users/Aleksander Heintz/Documents/changes/_filename_.txt") s.set("log_location", log_location) sublime.save_settings("Preferences.sublime-settings") [/quote]

In addition to the good explanation from jbjornson, actually if you only need to **read **from main user settings file (which is probably the case here), you can simply use the view.settings():

log_location = self.view.settings().get("log_location", "C:/Users/Aleksander Heintz/Documents/changes/_filename_.txt")
0 Likes

#10

Please don’t mess with application preferences. Use your own settings file.

0 Likes

#11

Thanks :smile:

0 Likes