Forum Archive

Need Full Text search support

wolf71

Pythonista 3 it's greate. And more files add to here.
So Can add full text search function (now just support file name and not include all dirs)? just like editorial.

Webmaster4o

This is Pythonista, you could roll your own. Wouldn't be too hard. I agree that this would be nice as a built-in though, the search function has kind of been left behind as Pythonista had grown. It'd be nice for it to get a much-needed update.

Any thoughts, @omz ?

JonB

stash includes grep which lets you full text search from the command line.

wolf71

@Webmaster4o Yes,Now Using a python script to search full text. but hope as a native pythonista function, just like editorial. it's elegance.

ccc

@wolf71 Put your script in the action menu and use the dialogs module to get the search term from the user and it will be as elegant as a builtin.

Phuket2

Here is my crappy version. With some work it could be ok. I have just been lazy about it. I had the script to the wrench. Works for me when looking for scripts I have written previously. But really, it's crap.
I will get back to it one day. I think the code preview is nice though. As I say, it's not a total loss, just needs a little work.

gist

wolf71
# coding: utf-8

import ui,os,editor

# 目录下所有文件列表
flist = []
# 创建一个数据集,用于向 TableView 提供数据
ds = ui.ListDataSource([])
# 累计文件数量
TotalFiles = 0

# 当搜索框回车后,进行查询
def Search(sender):
    sKey = sender.text.lstrip().encode('utf-8')
    sResult = FileSearch(flist,sKey)
    ResultCnt = len(sResult)
    sender.superview['info'].text = '(%d/%d)' % (ResultCnt,TotalFiles)
    ds.items = sResult

# 在当前查询得到的查询结果里面,再查询    
def Search2nd(sender):
    sKey = sender.superview['sInfo'].text.lstrip().encode('utf-8')
    tfile = []
    for i in ds.items:
        tfile.append('.'+i['title'])
    sResult = FileSearch(tfile,sKey)
    ResultCnt = len(sResult)
    sender.superview['info'].text = '(%d/%d)' % (ResultCnt,TotalFiles)
    ds.items = sResult

def dsSelect(sender):
    selectCnt = sender.selected_row
    sfile = '.'+ds.items[selectCnt]['title']
    # 在编辑器打开新文件
    editor.open_file(sfile,1)

# 构建整个目录文件列表
# 需要依赖一个外部全局变量 flist
def GetFiles(rootDir): 
    for lists in os.listdir(rootDir): 
        path = os.path.join(rootDir, lists) 
        flist.append(path)
        if os.path.isdir(path): GetFiles(path) 

def FileSearch(flist,skey):
    olist = []
    fexttype = ['py','txt','md','sql','js','html','htm','css']
    skey = skey.lower()
    for i in flist:
        if i.lower().find(skey) > -1:
            olist.append({'title':i[1:],'accessory_type':'none'})
        else:
            fext = i.split('.')[-1].lower()
            for j in fexttype:
                if j == fext:
                    with open(i,'r') as fp:
                        for line in fp:
                            if line.lower().find(skey) > -1:
                                olist.append({'title':i[1:],'accessory_type':'detail_button'})
                                break
    return olist


# 加载 UI     
v = ui.load_view()

# 设置对应输入框 Action 事件
v['sInfo'].action = Search

# 将当前目录设置到 Pythonista 系统根目录
doc_path = os.path.expanduser('~/Documents')
os.chdir(doc_path)

# 构建文件列表
GetFiles('.')   
TotalFiles = len(flist)         

ds.action = dsSelect
v['tableview1'].data_source = ds
v['tableview1'].delegate = ds
v.present('popover') #, title_bar_color='white', title_color=0.3)

the form .pyui



[
  {
    "selected" : false,
    "frame" : "{{0, 0}, {410, 615}}",
    "class" : "View",
    "nodes" : [
      {
        "selected" : false,
        "frame" : "{{6, 10}, {231, 28}}",
        "class" : "TextField",
        "nodes" : [

        ],
        "attributes" : {
          "uuid" : "7A865866-A098-4A9D-8340-60FA6C66C016",
          "corner_radius" : 0,
          "frame" : "{{105, 292}, {200, 32}}",
          "border_color" : "RGBA(0.000000,0.000000,1.000000,1.000000)",
          "border_width" : 1,
          "alignment" : "left",
          "autocorrection_type" : "default",
          "font_name" : "<System>",
          "spellchecking_type" : "default",
          "class" : "TextField",
          "name" : "sInfo",
          "font_size" : 17
        }
      },
      {
        "selected" : false,
        "frame" : "{{3, 42}, {401, 567}}",
        "class" : "TableView",
        "nodes" : [

        ],
        "attributes" : {
          "flex" : "WH",
          "data_source_items" : "",
          "data_source_delete_enabled" : false,
          "frame" : "{{105, 208}, {200, 200}}",
          "uuid" : "80A0C61C-7E81-4D08-A044-14F51827780A",
          "data_source_number_of_lines" : 1,
          "class" : "TableView",
          "data_source_font_size" : 16,
          "background_color" : "RGBA(1.0, 1.0, 1.0, 1.0)",
          "name" : "tableview1",
          "row_height" : 32
        }
      },
      {
        "selected" : false,
        "frame" : "{{299, 6}, {105, 32}}",
        "class" : "Label",
        "nodes" : [

        ],
        "attributes" : {
          "font_size" : 18,
          "frame" : "{{130, 292}, {150, 32}}",
          "uuid" : "65227454-EAE5-46F6-BC2B-C83584F78C31",
          "text" : "",
          "alignment" : "left",
          "class" : "Label",
          "name" : "info",
          "font_name" : "<System>"
        }
      },
      {
        "selected" : false,
        "frame" : "{{245, 10}, {46, 28}}",
        "class" : "Button",
        "nodes" : [

        ],
        "attributes" : {
          "border_width" : 1,
          "action" : "Search2nd",
          "frame" : "{{165, 292}, {80, 32}}",
          "title" : "2nd",
          "class" : "Button",
          "font_bold" : true,
          "uuid" : "A874A7F3-61BA-4FF1-9BD9-4C0AF81A117A",
          "corner_radius" : 0,
          "font_size" : 15,
          "name" : "button1"
        }
      }
    ],
    "attributes" : {
      "enabled" : true,
      "background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
      "tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)",
      "border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)",
      "flex" : ""
    }
  }
]

Phuket2

@wolf71 , I tried running your code. But had a few problems. In py3 I got your popup to to show. But when I typed in a word and hit enter, I got an error relating to bytes. So I then ran it under 2.7 and get an error immediately. Just saying. Not sure it's the unicode comments or what going wrong

wolf71

Python 2.7