Nothing Special   »   [go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configurable prompt #115

Merged
merged 2 commits into from
Oct 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions slacker/environment/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from slacker.logger import Logger
from slacker.session import Session

DEFAULT_REPL_PROMPT = "${ro}${w}> "
DEFAULT_READ_ONLY_STR = "(read-only) "

class Config:
__instance = None

Expand All @@ -31,11 +34,11 @@ def get():
Config()
return Config.__instance

def repl_prefix(self):
return self.__repl_prefix
def repl_prompt(self):
return self.__repl_prompt

def set_repl_prefix(self, repl_prefix):
self.__repl_prefix = repl_prefix
def set_repl_prompt(self, repl_prompt):
self.__repl_prompt = repl_prompt

def active_workspace(self):
"""Active workspace name, if defined."""
Expand Down Expand Up @@ -87,32 +90,40 @@ def read_only(self):
def set_read_only(self, enable):
self.__read_only = enable

def read_only_str(self):
return self.__read_only_str

def set_read_only_str(self, str):
self.__read_only_str = str

def file_path(self):
return os.path.expanduser("~/.slacker")

def safe_dict(self):
"""Returns a safe dictionary of current values excluding any tokens."""
return {"repl_prefix": self.repl_prefix(),
return {"repl_prompt": self.repl_prompt(),
"workspaces": self.workspaces(),
"active_workspace": self.active_workspace(),
"log_level": self.log_level(),
"read_only": self.read_only()}
"read_only": self.read_only(),
"read_only_str": self.read_only_str()}

def save(self):
data = {"repl_prefix": self.repl_prefix(),
data = {"repl_prompt": self.repl_prompt(),
"workspaces": self.__workspaces,
"active_workspace": self.active_workspace(),
"log_level": self.log_level(),
"read_only": self.read_only()}
"read_only": self.read_only(),
"read_only_str": self.read_only_str()}
with open(self.file_path(), "w") as fp:
json.dump(data, fp, indent=2)
self.__logger.debug("Saved config to: {}".format(self.file_path()))

def load(self):
with open(self.file_path(), "r") as fp:
data = json.load(fp)
if "repl_prefix" in data:
self.set_repl_prefix(data["repl_prefix"])
if "repl_prompt" in data:
self.set_repl_prompt(data["repl_prompt"])
if "workspaces" in data:
self.__workspaces = data["workspaces"]
if "active_workspace" in data:
Expand All @@ -121,12 +132,15 @@ def load(self):
self.set_log_level(data["log_level"])
if "read_only" in data:
self.set_read_only(data["read_only"])
if "read_only_str" in data:
self.set_read_only_str(data["read_only_str"])
self.__logger.debug("Loaded config from: {}".format(self.file_path()))

def reset(self):
"""Resets all values to default."""
self.set_repl_prefix("> ")
self.set_repl_prompt(DEFAULT_REPL_PROMPT)
self.__workspaces = {} # Workspace name -> API token
self.__active_workspace = None
self.__log_level = logging.INFO
self.__read_only = False
self.__read_only_str = DEFAULT_READ_ONLY_STR
8 changes: 6 additions & 2 deletions slacker/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from argparse import ArgumentParser
from datetime import datetime, timedelta
from string import Template

from prompt_toolkit import prompt
from prompt_toolkit.shortcuts import confirm
Expand All @@ -21,9 +22,12 @@ def readline(completer, history):
""" Set prompt and read input """
try:
config = Config.get()
txt = "{}{}".format(config.active_workspace(), config.repl_prefix())
read_only = ""
if config.read_only():
txt = "(read-only) {}".format(txt)
read_only = config.read_only_str()
tmpl_fill = dict(w=config.active_workspace(), ro=read_only)
s = Template(config.repl_prompt())
txt = s.safe_substitute(tmpl_fill)
return prompt(txt, completer=completer, history=history)

# Handle EOF/^D nicely.
Expand Down