Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 6f58b6fc65
Fetching contributors…

Cannot retrieve contributors at this time

file 98 lines (82 sloc) 2.91 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
from types import NoneType
import sys
import os
import subprocess

tm_support_path = os.environ['TM_SUPPORT_PATH'] + '/lib'
if tm_support_path not in sys.path:
    sys.path.insert(0, tm_support_path)

from tm_helpers import to_plist, from_plist

dialog = os.environ["DIALOG"]
nib_path = os.environ['TM_SUPPORT_PATH'] + '/nibs'

try:
    all
except:
    def all(items):
        for item in items:
            if not item:
                return False
        return True

def item(val):
    if isinstance(val, basestring):
        return {"title": val}
    if isinstance(val, tuple):
        return {"title": val[0]}
    elif val is None:
        return {"separator": 1}

def all_are_instance(it, typ):
    return all([isinstance(i, typ) for i in it])

def menu(options):
    """ Accepts a list and causes TextMate to show an inline menu.
If options is a list of strings, will return the selected index.
If options is a list of (key, value) tuples, will display "key" and
return "value". Note that we don't use dicts, so that key-value options
can be ordered. If you want to use a dict, try dict.items().
In either input case, a list item with value `None` causes tm_dialog to
display a separator for that index.
"""
    hashed_options = False
    if not options:
        return None
    menu = dict(menuItems=[item(thing) for thing in options])
    if all_are_instance(options, (tuple, NoneType)):
        hashed_options = True
    plist = to_plist(menu)
    proc = subprocess.Popen([dialog, '-u'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
    proc.stdin.write(plist)
    output, _ = proc.communicate()
    result = from_plist(output)
    if not 'selectedIndex' in result:
        return None
    index = int(result['selectedIndex'])
    if hashed_options:
        return options[index][1]
    return options[index]

def get_string(**options):
    """Get a string using dialog
Accepts:
- `title` - The string to show in the title bar
- `prompt` - The label for the text field
- `string` - The default value inside the text field
Returns:
- The string entered by the user.
"""
    
    # Set defaults and get options:
    if not options.has_key('title'):
        options['title']='Enter String'
    if not options.has_key('prompt'):
        options['prompt']='String:'
    if not options.has_key('string'):
        options['string']=''
    plist = to_plist(options)
    
    # Run dialog, piping our plist in, and reading the output:
    nib = nib_path + '/RequestString'
    proc = subprocess.Popen([dialog, '-cm', nib],
        stdout=subprocess.PIPE, stdin=subprocess.PIPE)
    proc.stdin.write(plist)
    output, _ = proc.communicate()
    
    # Extract exit value:
    result = from_plist(output)
    if not 'result' in result:
        return None
    else:
        return result['result'].get('returnArgument')
Something went wrong with that request. Please try again.