Exemplo n.º 1
0
 def test_create(self):
     """Basic creation."""
     f = fmt.new_formatter("monokai", "test", "off")
     self.assertEqual("test", f.file_name)
     self.assertEqual("off", f.is_multipage)
     self.assertFalse(f.dark)
     self.assertFalse(f.colorize)
     self.assertFalse(f.is_raw)
     self.assertFalse(f.is_bw)
     self.assertEqual(None, f.config)
     self.assertEqual("FiveByNineMono", type(f.font_factory).__name__)
     f = fmt.new_formatter("monokai",
                           "test",
                           "off",
                           dark=True,
                           colorize=True,
                           is_raw=True,
                           is_bw=True)
     self.assertEqual("test", f.file_name)
     self.assertEqual("off", f.is_multipage)
     self.assertTrue(f.dark)
     self.assertTrue(f.colorize)
     self.assertTrue(f.is_raw)
     self.assertTrue(f.is_bw)
     self.assertEqual(None, f.config)
     self.assertEqual("FiveByNineMono", type(f.font_factory).__name__)
     self.assertEqual("DefaultSymbolGenerator",
                      f.symbol_generator.__class__.__name__)
Exemplo n.º 2
0
 def test_font(self):
     """Font selection."""
     f = fmt.new_formatter("monokai",
                           "test",
                           "off",
                           font_name="monospace-ascii-2x5")
     self.assertEqual("TwoByFiveMono", type(f.font_factory).__name__)
Exemplo n.º 3
0
 def test_config(self):
     """Config settings."""
     f = fmt.new_formatter("monokai",
                           "test",
                           "off",
                           config=["page_legend=1"])
     self.assertFalse(f.config is None)
Exemplo n.º 4
0
 def test_symbols(self):
     """Test symbol setting."""
     f = fmt.new_formatter("monokai", "test", "off", symbols="abc")
     self.assertEqual("InputStringGenerator",
                      f.symbol_generator.__class__.__name__)
Exemplo n.º 5
0
 def test_map(self):
     """Floss mapping."""
     with self.assertRaises(fmt.FormatterException) as cm:
         fmt.new_formatter("monokai", "test", "off", map_colors=["abc=xyz"])
     self.assertEqual("unable to map: abc=xyz", str(cm.exception))
Exemplo n.º 6
0
def _run(args, default_font):
    """Run pyxstitch."""
    content = None
    file_name = None
    file_ext = os.path.splitext(args.file)
    is_raw = False
    for item in file_ext:
        if item == "." + _RAW:
            is_raw = True
            break
    default_lexer = get_lexer_by_name("text")
    use_lexer = args.lexer
    use_style = args.style
    is_bw = False
    # Shortcut to black and white is to just use a Text lexer
    if args.theme == _B_AND_W:
        if use_lexer is not None:
            log.Log.write("black & white overrides lexer input")
        if use_style != _DEF_STYLE:
            log.Log.write("black & white overrides style")
        use_lexer = "Text"
        use_style = _B_AND_W
        is_bw = True
    is_auto = use_lexer == _AUTODETECT
    if is_auto:
        use_lexer = None
    if not is_raw:
        if use_lexer or args.is_text:
            if use_lexer:
                lexer = get_lexer_by_name(use_lexer)
            else:
                lexer = default_lexer
        else:
            try:
                lexer = get_lexer_for_filename(args.file)
            except Exception as e:
                log.Log.write(e)
                exit(1)
    if os.path.exists(args.file):
        file_name = file_ext[0]
        with open(args.file, 'r') as f:
            content = f.read()
    else:
        if file_ext[1] is not None and len(file_ext[1]) > 1:
            log.Log.write("file not found, pass extension for stdin or a file")
            exit(1)
        file_name = "output"
        content = "".join(sys.stdin.readlines())
    if is_raw:
        _replay(args, file_name, content)
        return
    if is_auto:
        log.Log.write(content)
        try:
            lexer = guess_lexer(content)
            log.Log.write('using {} lexer'.format(lexer.name))
        except Exception as e:  # noqa: F841
            log.Log.write('unable to guess a lexer...defaulting to text')
            lexer = default_lexer
    output_name = args.output
    is_raw = args.is_raw
    if output_name is None:
        output_name = _create_file_name(file_name, args)
    else:
        if args.output.endswith(_RAW) and not is_raw:
            log.Log.write('specify output as {}?'.format(_RAW))
            exit(1)
    preproc = fnt.preprocess(content)
    text = preproc[0]
    rows = preproc[1]
    cols = preproc[2]
    config_file = args.config
    if config_file is None:
        conf = os.path.join(str(Path.home()), ".pyxstitch.config")
        if os.path.exists(conf):
            config_file = conf
    if config_file is not None and not os.path.exists(config_file):
        log.Log.write("unable to find config file: {}".format(config_file))
    formatting = fmt.new_formatter(use_style,
                                   output_name,
                                   args.multipage,
                                   colorize=args.is_dmc,
                                   dark=args.is_dark,
                                   is_raw=is_raw,
                                   is_bw=is_bw,
                                   map_colors=args.map,
                                   font_name=args.font,
                                   rows=rows,
                                   columns=cols,
                                   symbols=args.symbols,
                                   config=args.kv,
                                   config_file=config_file,
                                   horizontal=args.hzoom,
                                   vertical=args.vzoom)
    if args.font == default_font:
        log.Log.write("font: {}".format(formatting.font_factory.display_name))
    log.Log.write("Using lexer: {}".format(lexer.name))
    highlight(text, lexer, formatting)