How can I get console width?
I'm new here and new to Python language also.
Is there any working way to get console width in Pythonista? I mean in character count in one row, like 40 or 36.
@JonB Yes, I noticed
textStylingAtPosition_InDirection, but also could not get it to work. It appears that OMTextView is a class unique to pythonista that @omz created, I can't find any online resource about it. It seems like a combination of a TextField and a ScrollView. However, it does not have the font() method that a TextField does. Maybe @omz could provide some explanation, another thing that could be tried is comparing all attributes of all views within the console before and after changing the font size.
I think I got the missing piece. After a little testing it seems the default console font is Menlo, 14.
import ui def getConsoleWidth(font=('Menlo',14)): screenWidth = ui.get_screen_size().width charWidth = ui.measure_string('.',font=font).width return int(screenWidth / charWidth-1.5)
I'm not sure why subtract by 1.5 exactly, but I think console margin have something to do with it. Tested on my iphone and ipad mini, both orientations.
To check current font:
app=ObjCClass('UIApplication').sharedApplication() d=app.delegate() cv=d.consoleViewController() font=d.consoleViewController().outputFont() print font
Someone else asked about getting the default font, for resetting.
This can be found in defaults.
defaults = ObjCClass('NSUserDefaults').standardUserDefaults() print str(defaults.stringForKey_('OutputFontName')) print defaults.integerForKey_('OutputFontSize')
See defaults.dictionaryRepresentation() for other defaults.
@JonB, The current font block of code prints
Nonefor me. The default font one is working and printing
'Menlo-Regular', 12.Which is strange because I not changed my font at all, and it's not 12 but 14.
I totally don't understand this, but apparently one needs to trigger an attribute error against the view in order for outputFont to show up... apparently I had done this accidentally while poking around. Indeed the above code didnt work when i restarted, then suddenly worked up again after poking around a bit more. The code below seems to work in a fresh restart. After triggering the error once, all that is needed is to call ouputFont() subsequently.
from objc_util import * app=ObjCClass('UIApplication').sharedApplication() d=app.delegate() cvc=d.consoleViewController() try: cvc.view().thisattributedoesnotexist() except AttributeError: pass font=cvc.outputFont() print font
@Balur , thanks. I will also use your function. I often want to write a full screen divider when debugging. I get lazy though 😱 I set my font in the startup script to Menlo , 22. I have problems seeing smaller. But I tested your function on my ipad pro, and the 1.5 char adjustment works in both orientations.
@JonB Strange, but it's do the job. One little problem for me is how to access the values one by one, not this UICTFont object thing?
getConsoleWidth()is still not perfect when I run on ipad when pythonista editor and console are side by side. Because of
ui.get_screen_size().widthreturns the full screen size, not just the console size. I have to subtrack the editor panel width from the full screen size. Or something like that. Any suggestion?
@JonB I suspect that this has nothing to do with the
AttributeError, but with the fact that you're accessing the
viewproperty while doing the attribute lookup. This has the side effect of loading the view (and typically other setup code) if it isn't loaded already. To be honest, I can't really think of a situation where the console view wouldn't be loaded already, but I can't think of another explanation for this behavior.
@omz indeed, this works for me:
from objc_util import * app=ObjCClass('UIApplication').sharedApplication() d=app.delegate() cvc=d.consoleViewController() cvc.view() font=cvc.outputFont() print font
font.advancementForGlyph_(68).width gives you the actual width of a character.
font.lineHeight() gives the height of a line
font.pointSize() gives the point size, which you dont need except for setting default.
gives the font name. Menlo-Regular or Menlo respectively
The width of the half console can be found somewhere... will need to poke around later
@JonB Thank you, I extended the functions based on your answer. If you find something about 1/3 screen console, please let me know.
# coding: utf-8 import ui import objc_util def getDefaultConsoleFont(): defaults=objc_util.ObjCClass('NSUserDefaults').standardUserDefaults() return (str(defaults.stringForKey_('OutputFontName')),\ defaults.integerForKey_('OutputFontSize')) def getCurrentConsoleFont(): app=objc_util.ObjCClass('UIApplication').sharedApplication() cv=app.delegate().consoleViewController() cv.view() font=cv.outputFont() font_family=str(font.familyNameForCSSFontFamilyValue()) #font_family2=str(font.familyName()) font_size=font.pointSize() char_width=font.advancementForGlyph_(68).width line_height=font.lineHeight() return (font_family, font_size), char_width, line_height def getConsoleCharWidth(): font,charWidth,lineHeight=getCurrentConsoleFont() screenWidth=ui.get_screen_size().width #charWidth=ui.measure_string('.',font=font).width return int(screenWidth / charWidth-1.5) if __name__ == '__main__': ccwidth=getConsoleCharWidth() currentFont,_,_=getCurrentConsoleFont() defaultFont=getDefaultConsoleFont() print('currentConsoleFont='+str(currentFont)) print('defaultConsoleFont='+str(defaultFont)) print('consoleCharWidth = '+str(ccwidth)) print('='*ccwidth)
Sorry for copy paste, it's a bit long.
The objc_util syntax makes it difficult to keep to PEP8 line lengths so just go straight at
It is still shorter than the line above it.
@ccc Edited. Tried my best.
The mini console width is 320 (standard iphone width, i think that is a ux design standard for the split view thingies). I am having trouble finding the most efficient route at finding that view, but the filter_subviews code posted elsewhere on the forums does work to find the OMTextView.