git-cola v2.11
[git-cola.git] / cola / models / prefs.py
blob9d81dac81c1e1cdd3652c869b0a008c630163cde
1 from __future__ import division, absolute_import, unicode_literals
3 import sys
5 from .. import core
6 from .. import gitcfg
7 from .. import observable
8 from .. import utils
11 BLAME_VIEWER = 'cola.blameviewer'
12 BOLD_HEADERS = 'cola.boldheaders'
13 CHECKCONFLICTS = 'cola.checkconflicts'
14 COMMENT_CHAR = 'core.commentchar'
15 DIFFCONTEXT = 'gui.diffcontext'
16 DIFFTOOL = 'diff.tool'
17 DISPLAY_UNTRACKED = 'gui.displayuntracked'
18 EDITOR = 'gui.editor'
19 FONTDIFF = 'cola.fontdiff'
20 HISTORY_BROWSER = 'gui.historybrowser'
21 LINEBREAK = 'cola.linebreak'
22 MERGE_DIFFSTAT = 'merge.diffstat'
23 MERGE_KEEPBACKUP = 'merge.keepbackup'
24 MERGE_SUMMARY = 'merge.summary'
25 MERGE_VERBOSITY = 'merge.verbosity'
26 MERGETOOL = 'merge.tool'
27 SAVEWINDOWSETTINGS = 'cola.savewindowsettings'
28 SORT_BOOKMARKS = 'cola.sortbookmarks'
29 TABWIDTH = 'cola.tabwidth'
30 TEXTWIDTH = 'cola.textwidth'
31 USER_EMAIL = 'user.email'
32 USER_NAME = 'user.name'
33 SPELL_CHECK = 'cola.spellcheck'
36 def default_blame_viewer():
37 return 'git gui blame'
40 def blame_viewer():
41 default = default_blame_viewer()
42 return gitcfg.current().get(BLAME_VIEWER, default)
45 def bold_headers():
46 return gitcfg.current().get(BOLD_HEADERS, False)
49 def check_conflicts():
50 return gitcfg.current().get(CHECKCONFLICTS, True)
53 def display_untracked():
54 return gitcfg.current().get(DISPLAY_UNTRACKED, True)
57 def editor():
58 app = gitcfg.current().get(EDITOR, 'gvim')
59 return {'vim': 'gvim -f'}.get(app, app)
62 def comment_char():
63 return gitcfg.current().get(COMMENT_CHAR, '#')
66 def default_history_browser():
67 if utils.is_win32():
68 # On Windows, a sensible default is "python git-cola dag"
69 # which is different than `gitk` below, but is preferred
70 # because we don't have to guess paths.
71 git_cola = sys.argv[0].replace("\\", '/')
72 python = sys.executable.replace("\\", '/')
73 argv = [python, git_cola, 'dag']
74 argv = core.prep_for_subprocess(argv)
75 default = core.list2cmdline(argv)
76 else:
77 # The `gitk` script can be launched as-is on unix
78 default = 'gitk'
79 return default
82 def history_browser():
83 default = default_history_browser()
84 return gitcfg.current().get(HISTORY_BROWSER, default)
87 def linebreak():
88 return gitcfg.current().get(LINEBREAK, True)
90 def spellcheck():
91 return gitcfg.current().get(SPELL_CHECK, False)
93 def sort_bookmarks():
94 return gitcfg.current().get(SORT_BOOKMARKS, True)
97 def tabwidth():
98 return gitcfg.current().get(TABWIDTH, 8)
101 def textwidth():
102 return gitcfg.current().get(TEXTWIDTH, 72)
105 class PreferencesModel(observable.Observable):
106 message_config_updated = 'config_updated'
108 def __init__(self):
109 observable.Observable.__init__(self)
110 self.config = gitcfg.current()
112 def set_config(self, source, config, value):
113 if source == 'repo':
114 self.config.set_repo(config, value)
115 else:
116 self.config.set_user(config, value)
117 message = self.message_config_updated
118 self.notify_observers(message, source, config, value)
120 def get_config(self, source, config):
121 if source == 'repo':
122 return self.config.get_repo(config)
123 else:
124 return self.config.get(config)
127 class SetConfig(object):
129 def __init__(self, model, source, config, value):
130 self.source = source
131 self.config = config
132 self.value = value
133 self.old_value = None
134 self.model = model
136 def is_undoable(self):
137 return True
139 def do(self):
140 self.old_value = self.model.get_config(self.source, self.config)
141 self.model.set_config(self.source, self.config, self.value)
143 def undo(self):
144 if self.old_value is None:
145 return
146 self.model.set_config(self.source, self.config, self.old_value)